npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

opery

v0.4.0

Published

Opery is the best ORM abstraction tool to create database modules for your projects.

Downloads

16

Readme

opery

Opery is the best ORM abstraction tool to create database modules for your projects.

Features

  • Makes easy the use of repository pattern in this way code doesn't break whenever you want change the ORM or or refactoring some method to communicate with the database.
  • Based on adapters pattern.
  • Allows to change orm easily

Installation

npm install --save opery

Usage

const opery = require('opery')
const sequelizeAdapter = require('opery-sequelize-adapter')

const options = {
  orm: { // necessary configuration for setup ORM (NOTE: in this case sequelize).
    database: 'test',
    username: 'my_user',
    password: 'secret',
    host: 'localhost',
    dialect: 'mysql',
  },
  adapter: sequelizeAdapter,
  modelsDir: '/path/to/models',
  servicesDir: '/path/to/services',
}

opery.run(options).then(db => {
  db.services.SomeModel.create(data, (err, created) => {
    if (err) {
      // do something with err
    }
    // do something with created user
  })

  // Or using promises
  
  db.services.SomeModel.create(data).then(created => {
    // do something with created user
  }).catch(err => {
    // do something with err
  })
})

Configuration

  • options (Object)
    • orm(Object) ORM configuration object NOTE: Only Sequelize is support currently
    • adapter (Adapter) Opery adapater object
    • modelsDir (String) Models directory
    • servicesDir (String) Services directory

Services

What is a service?

Services are logical abstraction for managing workloads.

For example, if you're working in an API REST and you have to call a user by username, it would be simpler to contain that logic somewhere and just call the method to obtain that user by username.

How to create a service?

Create a opery service is easy, Services are simply a function that return an Javascript Literal Object.

IMPORTANT: Service function name is really important. this name will depend if the service works or not. this name must be equal to the name of the model with which we are going to work since service function receives by parameter the model that is equal to the name of service function

Example:

In this example we will work with Sequelize.

imagine that we already have a project set up like this in example list, and we have created our user model.

Our next step will be to create a service to work with that model.

Let's go to the code.


// this is our user service
function user (Model) {
  return {
    async getUserByEmail (email, options) {
      const user = await Model.findOne({ where: { email }, ...options })
    },
    async getUserByUsername () {
      //...
    }
  }
}

We already created our user service. Our next step is to use the service. we'll see that in the section on how to use a service.

How to use a service?

There are two ways to use Opery Services.

The First is setting service globally so that it affects all models.

we do this using the base method.

opery.base(service: Function. [...service: Function]) -> void

Services list

Services can be created by anyone. this is a list of services created by the community

Adapters

What is an adapter?

How to create an adapter?

Adapter list

Adpapters can be created by anyone. this is a list of adapters created by the community

Hooks

Currently Hooks are proposal and internal use.

opery

beforeRun(callback: Function) -> Promise

Do something before run opery.


opery.beforeRun(() => {
  // do something
})

afterRun(callback: Function) -> Promise

Do something after run opery.


opery.afterRun(() => {
  // do something
})

API

opery

base(service: Function. [...service: Function]) -> void

Register one or several services globally (for all models).


opery.base(CustomService)

opery.run(options).then(db => {
  const result = await db.services.SomeModel.customServiceMethod()
})

service(service: Function. [...service: Function]) -> void

Register one or several services for specific models.


opery.service(auth)

opery.run(options).then(db => {
  const result = await db.services.Auth.authMethod()
})

run(options: Object) -> db: Object

Initialize opery module to work with database layer.

  • options (Object)
    • orm(Object) ORM configuration object NOTE: Only Sequelize is support currently
    • adapter (Adapter) Opery adapater object
    • modelsDir (String) Models directory
    • servicesDir (String) Services directory

opery.run(options).then(db => {
  // do something with db module
})

Contributing

Fork this repository to your own GitHub account and then clone it to your local device

As always, you can run the AVA and ESLint tests using: npm test

Authors

License

MIT © Guillermo Lopez