0.4.0 • Published 6 years ago

opery v0.4.0

Weekly downloads
50
License
MIT
Repository
github
Last release
6 years ago

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

0.4.0

6 years ago

0.3.21

6 years ago

0.3.20

6 years ago

0.3.19

6 years ago

0.3.18

6 years ago

0.3.17

6 years ago

0.3.16

6 years ago

0.3.15

6 years ago

0.3.14

6 years ago

0.3.13

6 years ago

0.3.12

6 years ago

0.3.11

6 years ago

0.3.10

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.0

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.0

6 years ago

0.1.12

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago