sequelize-mg

2.1.1 • Public • Published

sequelize-mg

node version NPM version npm download

This package provides the function of generating the sequelize model file according to the table, supports partial update files and custom generated file formats, and some codes refer to sequelize-auto.

中文说明

Prerequisites

  • node >=8

Install

npm i sequelize-mg

Usage

The following is an example of creating a mysql correspondence table using sequelize-auto. Please confirm that the ./models/ directory already exists.

'use strict';

const AutoSequelize = require('sequelize-auto');
const sequelizeGen = require('./');

const auto = new AutoSequelize('database', 'yourname', 'yourpass', {
  dialect: 'mysql',
  directory: false, // we don't use sequelize-auto to generate model files
  define: {
    timestamps: false,
    freezeTableName: true,
  },
});
auto.run().then(data => {
  const tables = {};
  for (const tableName in data.tables) {
    const table = data.tables[tableName];
    for (const fieldName in table) {
      const field = table[fieldName];
      field.isSerialKey = field.foreignKey;
    }
    tables[tableName] = { columns: table, comment: 'sample' };
  }

  sequelizeGen(tables, { dialect: 'mysql' });
});

You can also get tables in other ways, and generate your own model files with your own defined info like this.

const sequelizeGen = require('sequelize-mg');

const { tables, info, config} = yourDatabaseReader(params);
sequelizeGen(tables, info, config); // Note: The default v2t function requires info.dialect to be present and is a string

Config

name format description
dir string Specify the storage path of the model file. The default value is './models'.
gfn (table)=>'' GenerateFileName,Generate a file name based on the table name. By default, the table name is the file name.
gt (table, fields, comment, info, config)=>'' GenerateTable,Generate text for the replaceable area, where fields are already processed text
f2t (table, field, obj, info, config)=>'' FieldToText,Generate field text for the table
t2t (table, field, obj, info, config)=>'' TypeToText,Generate type text and return, the result is stored to obj.typeText
v2t (table, field, obj, info, config)=>'' defaultValueToText,Generate defaultValue text and return, return the result to obj.defaultValText
flagBegin string The starting point for marking the replaceable area
flagEnd string Used to mark the end point of the replaceable area. When updating the model file, only the part between the start point and the end point will be replaced.
lf string Newline, default is '\n'
sequelizeText string The corresponding text of the sequelize object, only for replaceable areas
fileHead string File header, the part before the replaceable area, only valid when creating a new model file
fileTail string End of file, the part after the replaceable area, only valid when creating a new model file
fileOptions any Options when reading and writing files, defaults to 'utf8'
rewrite boolean Force the entire file to be regenerated, the default is false
notice (name, table, flag)=>null If you configure this parameter, call this method after generating the model

Default configuration

The external incoming configuration will be merged with the default configuration, you can view the default configuration in [here] (./lib/default)

Run tests

npm i sequelize-auto
npm i mysql
npm run test

Author

985ch

License

Copyright © 2019 985ch.
This project is MIT licensed.
This README was translate by google

Package Sidebar

Install

npm i sequelize-mg

Weekly Downloads

11

Version

2.1.1

License

MIT

Unpacked Size

23 kB

Total Files

8

Last publish

Collaborators

  • 985ch