2.1.1 • Published 3 years ago

sequelize-mg v2.1.1

Weekly downloads
97
License
MIT
Repository
github
Last release
3 years ago

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

nameformatdescription
dirstringSpecify 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
flagBeginstringThe starting point for marking the replaceable area
flagEndstringUsed 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.
lfstringNewline, default is '\n'
sequelizeTextstringThe corresponding text of the sequelize object, only for replaceable areas
fileHeadstringFile header, the part before the replaceable area, only valid when creating a new model file
fileTailstringEnd of file, the part after the replaceable area, only valid when creating a new model file
fileOptionsanyOptions when reading and writing files, defaults to 'utf8'
rewritebooleanForce the entire file to be regenerated, the default is false
notice(name, table, flag)=>nullIf 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

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

2.1.1

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago