Sequelize model generate to json schema.
-
install
npm install sequelize-model-to-json-schema --save
-
transform
-
demo
const s2json = require('sequelize-model-to-json-schema') const Project = sequelize.define('Project', { title: { type: Sequelize.STRING, comment: 'title' }, bigint: { type: Sequelize.BIGINT, comment: 'test bigint' }, number: { type: Sequelize.DOUBLE, comment: 'test double' }, number_decimal: { type: Sequelize.DECIMAL, comment: 'test decimal' }, uuid: { type: Sequelize.UUID, comment: 'test uuid' }, description: { type: Sequelize.TEXT, comment: 'description' } }) const transformModel = s2json.transform(Project) <!-- { type: 'object', properties: { id: { type: 'integer', format: 'int32', description: undefined }, title: { type: 'string', maxLength: 255, description: 'title' }, bigint: { type: 'integer', format: 'int32', description: 'test bigint' }, number: { type: 'number', format: 'double', description: 'test double' }, number_decimal: { type: 'number', description: 'test decimal' }, uuid: { type: 'string', format: 'uuid', description: 'test uuid' }, description: { type: 'string', description: 'description' }, createdAt: { type: 'string', format: 'date-time', description: undefined }, updatedAt: { type: 'string', format: 'date-time', description: undefined } } } -->
-
exclude
const s2json = require('sequelize-model-to-json-schema') const Project = sequelize.define('Project', { title: { type: Sequelize.STRING, comment: 'title' }, description: { type: Sequelize.TEXT, comment: 'description' } }) const transformModel = s2json.transform(Project, { exclude: ['title'] }) <!-- { type: 'object', properties: { id: { type: 'integer', format: 'int32', description: undefined }, description: { type: 'string', description: 'description' }, createdAt: { type: 'string', format: 'date-time', description: undefined }, updatedAt: { type: 'string', format: 'date-time', description: undefined } } } -->
-
-
convert
-
string
const convertStr = { test: { type: Sequelize.STRING, comment: 'test field' } } const convertJson = s2json.convert(convertStr) <!-- { type: 'object', properties: { test: { type: 'string', description: 'test field' } } } -->
-
object
const convertObj = { test: { type: Sequelize.JSON(), keys: { test: { type: Sequelize.STRING, comment: 'test field' } } } } const convertJson = s2json.convert(convertObj) <!-- { type: 'object', properties: { test: { type: 'object', properties: { test: { type: 'string', description: 'test field' } } } } } -->
-
array
const convertArr = { test: { type: Sequelize.ARRAY(), items: { test: { type: Sequelize.STRING, comment: 'test field' } } } } const convertJson = s2json.convert(convertArr) <!-- { type: 'object', properties: { test: { type: 'array', items: { type: 'object', properties: { test: { type: 'string', description: 'test field' } } } } } } -->
-