0.8.16 • Published 2 years ago

moleculer-sequelize v0.8.16

Weekly downloads
204
License
MIT
Repository
gitlab
Last release
2 years ago

Moleculer Sequelize

Build Status

Moleculer logo

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:

ParamTypedefaultdescription
idstring, number or array-Identifier of row
whereobject or string-search query
fieldsstring or array-fields returned in response
excludestring 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:

ParamTypedefaultdescription
limitnumber-limit of items in response
sortstring-model field to sort list
searchstring or array-applies "LIKE" operation in field
whereobject or string-search query
fieldsstring or array-table fields returned in response
excludestring 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:

ParamTypedefaultdescription
pagenumber1selected page
pageSizenumber10number of item per page
sortstring-model field to sort list
searchstring or array-applies "LIKE" operation in field
whereobject or string-search query
fieldsstring or array-table fields returned in response
excludestring 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:

ParamTypedefaultdescription
idnumberXIdentifier 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:

ParamTypedefaultdescription
idnumberrequiredIdentifier of row
...entityobjectrequiredelements 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:

ParamTypedefaultdescription
idnumberrequiredIdentifier 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"
  }]
}
0.8.12

2 years ago

0.8.14

2 years ago

0.8.13

2 years ago

0.8.16

2 years ago

0.8.15

2 years ago

0.8.9

2 years ago

0.8.8

2 years ago

0.8.7

2 years ago

0.8.11

2 years ago

0.8.10

2 years ago

0.8.6

2 years ago

0.8.5

3 years ago

0.8.4

3 years ago

0.7.6

3 years ago

0.7.5

3 years ago

0.7.7

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.7.4

3 years ago

0.7.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.18

3 years ago

0.6.17

3 years ago

0.6.19

3 years ago

0.6.16

3 years ago

0.6.14

3 years ago

0.6.15

3 years ago

0.6.12

3 years ago

0.6.11

3 years ago

0.6.13

3 years ago

0.6.10

3 years ago

0.6.9

3 years ago

0.6.8

3 years ago

0.6.7

3 years ago

0.6.6

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.10

3 years ago

0.5.11

3 years ago

0.5.9

3 years ago

0.5.12

3 years ago

0.5.8

3 years ago

0.5.7

3 years ago

0.5.6

3 years ago

0.5.5

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.0

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.4.0

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.3

3 years ago

0.3.0

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.7

3 years ago

0.1.4

3 years ago

0.2.2

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago