Giter VIP home page Giter VIP logo

egg-sequelize-wrapper's Introduction

egg-sequelize-wrapper

Install

$ npm i egg-sequelize-wrapper --save

Usage

// {app_root}/config/plugin.js
exports.sequelizeWrapper = {
  enable: true,
  package: 'egg-sequelize-wrapper',
};

Example

  • find
// {app_root}/app/service/account.js

const accouts = await this.app.model.Account.$findAndCount({
  where: {
    username_like: 'dev',
    'role.description_or_like': '超级',
    'role.description_or_ne': '2018',
    'role.description_or_and_eq': '上班'
  },
  attributes: [ 'id', 'username', 'password', 'createdAt', 'updatedAt' ],
  order: [[ 'createdAt', 'DESC' ]],
  limit: 10,
  offset: 0,
  include: [
    {
      model: model.Role,
      attributes: [ 'id', 'description' ],
      where: {
        status_in: '1,0',
        createdAt_gte: '2018-01-01',
      },
      required: false,
      through: {
        attributes: [],
        where: { status: 1 },
      },
    },
  ],
}, {
  json: true,
  fields: [
    { origin: 'createdAt', type: 'date' },
    { origin: 'password', type: 'empty' },
    {
      origin: 'roles.id',
      field: 'roles',
    },
  ],
  fat: true,
});

it separate the opts into query options and return options the most import change of query options is where, u can use _(between|like|ne...) / _or_(like|ne..) / _or_and_(like) to simplify the query conditions other attributes which set in sequelize.model.find can also be used in query opts, the detail info u can check lib/components/where.js

in the return options, there are three parameters: json | fields | fat json = true means the returns had been processed by JSON.stringify if json = true, then u can specify every props of the instance, set the rule and return the handled prop, origin is necessary, e.x: companies.staffs.name field is the name of the handled prop, it also support nest set. the detail info u can check lib/components/transform.js

// qeury result
{
    name: 'company name',
    staffs: [
        {
            name: '小a',
            age: 22,
        },
        {
            name: '小b',
            age: 33,
        }
    ],
    header: {
        name: '小c',
        age: '1966-10-10 00:00:00+8:00',
        role: {
            title: 'CEO'
        }
    }
}

const fields = [
    { origin: 'staffs.name', field: 'staffNames' },
    { origin: 'staffs.age', field: 'doubleStaffName', filter: x => x > 30, translate: x => x * 2},
    { origin: 'header.role.title', field: 'headerTitle'},
    { origin: 'header.age', type: 'date' }
]

// if fat = false

return = { 
   staffNames: [ '小a', '小b' ],
   doubleStaffName: [ 66 ],
   headerTitle: 'CEO',
   header: { age: '1966-10-10' } 
}

// else

return = 
{ name: 'company name',
  staffs: [ { name: '小a', age: 22 }, { name: '小b', age: 33 } ],
  header: { name: '小c', age: '1966-10-10', role: { title: 'CEO' } },
  staffNames: [ '小a', '小b' ],
  doubleStaffName: [ 66 ],
  headerTitle: 'CEO' }
  • create

it also separate args and return options the args is same like sequelize.model.create, but if the args is array, the function automatically call bulkCreate the return options support relative create

await models.account.$create(
    [
        {
            name: 'a',
            age: 22,
            role: {
                name: 'student'
            }
        }
    ],
    {
        include: [
            {
                model: models.role
            }
        ]
    }
)
  • update

it receive updateOpts and queryOpts, updateOpts can be object or array, queryOpts support include update be careful, include need precondition: id & transaction

await models.account.$update(
    {
        name: 'update a',
        role: {
            name: 'senior student'
        }
    },
    {
        where: {name: 'a'},
        include: [{model: models.role}],
        transaction: t
    }
)

Questions & Suggestions

Please open an issue here.

License

MIT

egg-sequelize-wrapper's People

Contributors

1effortlessness avatar

Stargazers

crixusshen avatar momo avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.