This is an exporter to convert MySQL Workbench Models (*.mwb) to Nodejs Sequelize Schema.
- PHP 7.2+
- Composer to install the dependencies
php composer.phar require --dev mysql-workbench-schema-exporter/node-exporter
This will install the exporter and also require mysql-workbench-schema-exporter.
You then can invoke the CLI script using vendor/bin/mysql-workbench-schema-export
.
Additionally to the common options of mysql-workbench-schema-exporter these options are supported:
Currently, no special options can be configured for Sequelize Model.
-
useSemicolons
Whether or not to add semicolons to ends of lines (standard Eslint compliance).
Default is
true
. -
generateAssociationMethod
Generate an association method to define associations between models.
You may instantiate your model this way :
const Sequelize = require('sequelize'); const sequelize = new Sequelize({...}); const MyModel1 = sequelize.import('./path/to/MyModel'); const MyModel2 = sequelize.import('./path/to/MyMode2'); ... MyModel1.associate(); MyModel2.associate(); ...
Default is
false
. -
generateForeignKeysFields
Whether or not to generate foreign keys fields.
You could want to delegate it to association method (one could experiment relations creation order problems when foreign key field is present in definition).
Default is
true
. -
useTimestamps
Sets the models
timestamps
property.Default is
false
. -
injectExtendFunction
Injects an
extend
functions to models in order to provide extra definitions whitout modifying generated model files (and thus, being able to regenerate models).Example :
User.js
const { DataTypes, Model } = require('sequelize') class User extends Model { } module.exports = (sequelize, extend) => { User.init(extend({ ... lastName: { type: DataTypes.STRING(200), field: 'name', allowNull: false, defaultValue: '' }, firstName: { ... }, ... }), { ... }) User.associate = () => { ... } return User }
extensions/User.js
const deepmerge = require('deepmerge') const { DataTypes } = require('sequelize') module.exports = sequelize => function (baseDefinition) { return deepmerge(baseDefinition, { lastName: { get() { const rawValue = this.getDataValue('lastName'); return rawValue ? rawValue.toUpperCase() : null; } }, fullName: { type: DataTypes.VIRTUAL(DataTypes.STRING, ['firstName', 'lastName']), get() { return `${this.firstName} ${this.lastName}` }, set(value) { throw new Error('Do not try to set the `fullName` value!') } } }, { clone: false }) }
Initialization can be achieved like this :
const Sequelize = require('sequelize'); const sequelize = new Sequelize({...}); const extendUser = require('./path/to/extensions/User')(sequelize); const User = require('./path/to/User')(sequelize, extendUser); ...
See documentation for mysql-workbench-schema-exporter
const Sequelize = require('sequelize');
const sequelize = new Sequelize({...});
const MyModel = sequelize.import('./path/to/MyModel');
// do something with MyModel
MyModel.findOne({...}).then((res) => {...});
const Sequelize = require('sequelize');
const sequelize = new Sequelize({...});
const MyModel = require('./path/to/MyModel')(sequelize);
// do something with MyModel
MyModel.findOne({...}).then((res) => {...});