1.0.6 • Published 3 years ago

@mdreal/sequelize-orm-controller v1.0.6

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Sequelize ORM Dynamic Controller

Automatically parses models in folder. And give easy usage interface to user. If you want you can get as middleware too.

Parse

Default directory is `${root}/models` but you can change directory with .setCurrentDirectory or setCwd methods;

Import

// ES6
import { ORM, ORMiddleware } from "sequelize-orm-controller"

// or CommonJS, ES5 or lower
const { ORM, ORMiddleware } = require("sequelize-orm-controller")

Usage

As App

const orm = new ORM(/* Sequelize Constructor */);

orm.build()
  .then((models) => {
    console.log(models)
  })

As Middleware

const app = require("express")()

app.use(ORMiddleware({
  key: /* Optional. key in request object, by default: `orm` */,
  mwd: /* Optional. models working directory, by default: `{root}/models` */,
  /* Sequelize Constructor Options */
}))

app.get("*", (req, res) => {
  const user = req[/* key in options object */]
    .user().findOne({ id: 4 })
})

Naming

Method name is name defined in model. sequelize.define("ModelName", ModelObject)

But first letter are lower. For Example: your model name is User but method is user() or model name is OAuth2Token and method name is oAuth2Token. And each method returns to you class which contains under methods

  • create
  • update
  • delete
  • findOne
  • findAll
  • findByPK
  • findOrBuild
  • findOrCreate
  • findCreateFind
  • findAndCountAll

Model Format

models save on `{root}/models` or any other custom folder.

Model must structure below.

module.exports = (sequelize, DataTypes) => {
  const ModelName = sequelize.define("ModelName", {
   /* Here your all model fields */
  });

  ModelName.associate = (allModels) => {
    /* Here all your model associations */
  };

  return ModelName;
};

Support

If do you have any other idea or anything i wait you in issues tab...

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago