speck-sequelize-repository
v1.1.0-alpha
Published
Repositories using Speck
Downloads
38
Readme
Speck Sequelize Repository - Create repositories handle Sequelize models with Speck.
This package let you use Speck with repositories to handle Sequelize models.
Installing
$ npm install speck-sequelize-repository
Examples
Sample Repository
const { Repository } = require('speck-sequelize-repository')
const { MySequelizeModel } = require('./models')
const MyModelMap = require('./modelMaps/MyModelMap')
const MyModelRepository = {
customQuery ({ someField }) {
return this.findOneByCriterias({ someField })
}
}
module.exports = Repository.for(MySequelizeModel, MyModelMap, MyModelRepository)
Sample Mapper
Creates a mapper like in object-mapper:
const { Mapper } = require('speck-sequelize-repository')
const map = {
toEntity: {
'someField': 'someField'
},
toDatabase: {
'someField': 'someField'
}
}
module.exports = new Mapper(Object, map)
Auto Mapper
Creates a Mapper without specifying manually the map fields:
const { AutoMapper } = require('speck-sequelize-repository')
const mapper = new AutoMapper(someSpeckEntity)
The map fields will be automatically created from a speck entity. Note: entity relationships will have opinionated naming, example: entity.relationship
will expect on database a relationship
table with relationshipId
field.
You can also override some of the automatically generated fields naming, or exclude entity properties from map on AutoMapper constructor:
const mapper = new AutoMapper(someSpeckEntity, {
override: {
entityField: 'otherFieldName',
entityRelationshipField: 'otherTableName.otherFieldName'
},
exclude: ['someEntityProperty']
)