moleculer-sequelize v0.8.16
Moleculer Sequelize
Table of contents
Features |
---|
1. Introduction |
2. Configuration |
3. Service-Model |
4. Actions |
⠀4.1 get |
⠀4.2 find |
⠀4.3 list |
⠀4.4 create |
⠀4.5 update |
⠀4.6 remove |
⠀4.7 insert |
⠀4.8 removeMany |
⠀4.9 updateMany |
5. Using ctx |
6. Relationships |
About
Sequelize plugin for Moleculer.js Framework
If you like to use Sequelize and use models in your backend services, but you like to have relationships between models, this is a plugin for you. Simplifying and make default action for microservices, actions with more default parameters and callback functions.
Configuration
Create Simple Mixin to use with every Service-Models.
const MoleculerSequelize = require("moleculer-sequelize");
const SequelizeMixin = {
mixins: [MoleculerSequelize],
settings: {
sequelize: new Sequelize( "database_development", "root", "123456",{
host: "127.0.0.1",
dialect: "mysql",
}),
},
};
module.exports = SequelizeMixin;
Service-Model definition
const Sequelize = require("sequelize");
const SequelizeMixin = require("./sequelize.mixin");
module.exports = {
name: "users",
mixins: [SequelizeMixin],
model: {
name: "users",
define: {
id: {
primaryKey: true,
autoIncrement: true,
type: Sequelize.INTEGER
},
name: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.STRING,
},
association:{},
options:{},
},
};
#4 Actions
This lib generate some defaults actions for CRUD operations. Remember, this lib works based on mixin, so, if you overwrite default handler in action, some lib features will stop to work, if you wanna use this lib use the callbacks functions
##4.1 get Used to call one single element from table
Params:
Param | Type | default | description |
---|---|---|---|
id | string, number or array | - | Identifier of row |
where | object or string | - | search query |
fields | string or array | - | fields returned in response |
exclude | string or array | - | table fields to exclude in response |
Callbacks:
queryHandler(ctx, params) formatResult(ctx, result)
4.2 find
The difference between find and list is find don´t have pagination!
Params:
Param | Type | default | description |
---|---|---|---|
limit | number | - | limit of items in response |
sort | string | - | model field to sort list |
search | string or array | - | applies "LIKE" operation in field |
where | object or string | - | search query |
fields | string or array | - | table fields returned in response |
exclude | string or array | - | table fields to exclude in response |
Callbacks:
queryHandler(ctx, params) formatResult(ctx, result)
List
Used to get a paginated list from a table
Params:
Param | Type | default | description |
---|---|---|---|
page | number | 1 | selected page |
pageSize | number | 10 | number of item per page |
sort | string | - | model field to sort list |
search | string or array | - | applies "LIKE" operation in field |
where | object or string | - | search query |
fields | string or array | - | table fields returned in response |
exclude | string or array | - | table fields to exclude in response |
Callbacks:
queryHandler(ctx, params) formatResult(ctx, result)
create
Used to create a new row in table
Params:
Params in create actions is the entity you will create
Callbacks:
beforeCreate(ctx) afterCreate(ctx, model) formatResult(ctx, result)
update
Used to create a update a row based in id primary key in table
Params:
Param | Type | default | description |
---|---|---|---|
id | number | X | Identifier of row |
Params in create actions is the entity you will create
Callbacks:
beforeUpdate(ctx) afterUpdate(ctx, model) formatResult(ctx, result)
insert
Used To create multiple rows in table
Params:
Param | Type | default | description |
---|---|---|---|
id | number | required | Identifier of row |
...entity | object | required | elements in model that will be updated |
Params in update actions is the id and rest is the model attributes
Callbacks:
beforeCreate(ctx) afterCreate(ctx, model) formatResult(ctx, result)
remove
Used To remove a row in table
Params:
Param | Type | default | description |
---|---|---|---|
id | number | required | Identifier of row |
Params in update actions is the id and rest is the model attributes
Callbacks:
queryHandler(ctx, params) beforeRemove(ctx) afterRemove(ctx, model) formatResult(ctx, result)
Callbacks:
####queryHandler(ctx, params) Useful to modify and param query or add some validation on action, this callback receive two params, ctx(the context of action) and normalized query for this action, to change default query just modify "params" variable or return new query object.
formatResult(ctx, result)
Useful to modify the result of action, this lib works in entity based crud, but sometimes we need to return extra fields or change some name field names. This callback receive two params, ctx(the context of action) and default result for this action, to change default result just modify "result" variable or return new result object.
beforeCreate(ctx)
Useful to validate data before create data in database. This callback receive one param, ctx(the context of action).
afterCreate(ctx)
Useful to do some action after data created in database. This callback receive one param, ctx(the context of action).
beforeUpdate(ctx, model)
Useful to modify the result of action, this lib works in entity based crud, but sometimes we need to return extra fields or change some name field names. This callback receive two params, ctx(the context of action) and default result for this action, to change default result just modify "result" variable or return new result object.
afterUpdate(ctx, model)
Useful to modify the result of action, this lib works in entity based crud, but sometimes we need to return extra fields or change some name field names. This callback receive two params, ctx(the context of action) and default result for this action, to change default result just modify "result" variable or return new result object.
beforeRemove(ctx, model)
Useful to modify the result of action, this lib works in entity based crud, but sometimes we need to return extra fields or change some name field names. This callback receive two params, ctx(the context of action) and default result for this action, to change default result just modify "result" variable or return new result object.
afterRemove(ctx, model)
Useful to modify the result of action, this lib works in entity based crud, but sometimes we need to return extra fields or change some name field names. This callback receive two params, ctx(the context of action) and default result for this action, to change default result just modify "result" variable or return new result object.
Using ctx
You can pass the ctx with params for your function
exemple:
action:{
create :{
afterCreate(ctx){
if(ctx.meta.user.type === "ADMIN"){
ctx.params.type = ctx.meta.user.type;
} else{
throw new MoleculerClientError("ACCESS_DENIED", 401)
}
}
}
}
Relationships
Its possible to create Relationships between tables, just adding association key with name of other model and relationType(hasOne, hasMany, belongsTo, belongsToMany)
const Sequelize = require("sequelize");
const SequelizeMixin = require("./sequelize.mixin");
module.exports = {
name: "companies",
mixins: [SequelizeMixin],
model: {
name: "companies",
define: {
id: {
primaryKey: true,
autoIncrement: true,
type: Sequelize.INTEGER
},
name: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.STRING,
},
},
association:[
{
model:"users",
relationship: "hasMany",
}
]
}
const Sequelize = require("sequelize");
const SequelizeMixin = require("./sequelize.mixin");
module.exports = {
name: "users",
mixins: [SequelizeMixin],
model: {
name: "users",
define: {
id: {
primaryKey: true,
autoIncrement: true,
type: Sequelize.INTEGER
},
name: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.STRING,
companyId: Sequelize.INTEGER,
},
association:[
{
model:"users",
relationship: "belongsTo",
foreignkey: "companyId",
}
],
}
}
And to integrate this to CRUD actions just add 'includes' in service-model action
const Sequelize = require("sequelize");
const SequelizeMixin = require("./sequelize.mixin");
module.exports = {
name: "users",
mixins: [SequelizeMixin],
actions:{
findOne:{
includes: ['items']
}
}
}
and Will return
{
"name": "foo",
"email": "foo@bar.com",
"password": "123456",
"items": [{
"name": "bar"
}]
}
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago