Opery is the best ORM abstraction tool to create database modules for your projects.
- 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
npm install --save opery
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
})
})
options
(Object)orm
(Object) ORM configuration objectNOTE:
Only Sequelize is support currentlyadapter
(Adapter) Opery adapater objectmodelsDir
(String) Models directoryservicesDir
(String) Services directory
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.
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.
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 can be created by anyone. this is a list of services created by the community
Adpapters can be created by anyone. this is a list of adapters created by the community
Currently Hooks are proposal and internal use.
Do something before run opery.
opery.beforeRun(() => {
// do something
})
Do something after run opery.
opery.afterRun(() => {
// do something
})
Register one or several services globally (for all models).
opery.base(CustomService)
opery.run(options).then(db => {
const result = await db.services.SomeModel.customServiceMethod()
})
Register one or several services for specific models.
opery.service(auth)
opery.run(options).then(db => {
const result = await db.services.Auth.authMethod()
})
Initialize opery module to work with database layer.
options
(Object)orm
(Object) ORM configuration objectNOTE:
Only Sequelize is support currentlyadapter
(Adapter) Opery adapater objectmodelsDir
(String) Models directoryservicesDir
(String) Services directory
opery.run(options).then(db => {
// do something with db module
})
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
-
Guillermo López @glopezep
-
Jorge Cuevas @jcuevas012
MIT © Guillermo Lopez