npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

sequelize-json-schema

v2.1.1

Published

Use your Sequelize models in JSON Schemas or Swagger

Readme

sequelize-json-schema

NPM Version CircleCI

Generate JSON Schema structures from Sequelize instances, models, and model attributes.

Schemas may be generated at three levels of granularity:

| | | |---|---| | getSequelizeSchema() | Generate a full description of your database (all models, attributes, and associations) | | getModelSchema() | Generate the schema definitions entry for a specific model (all attributes) | | getAttributeSchema() | Generate the properties entry for a specific attribute |

See API documentation below for details and examples.

Installation

npm install sequelize-json-schema

Migrating v1 → v2

The version 1 API of this module is available as getModelSchema(), with the following changes:

  • private option has been removed. Use exclude instead.
  • alwaysRequired option has been removed. Schemas should be manually amended if needed using schema.required.push(...Object.keys(schema.properties)).
  • allowNull option has been removed. (Schema reflects the allowNull property of individual attributes).

API

Note: Examples below assume the following [fairly standard] setup code for Sequelize:

// Import this module
const sjs = require('sequelize-json-schema');

// Import Sequelize thingz
const Sequelize = require('Sequelize');
const {DataTypes} = Sequelize;

// Create a sequelize instance
const sequelize = new Sequelize('database', 'username', 'password', {dialect: 'sqlite'});

getSequelizeSchema(sequelize[, options])

| | | |---|---| | sequelize | Sequelize A Sequelize instance | | options.useRefs | Default for useRefs model option | | options.attributes | Default for attributes model option | | options.exclude | Default for exclude model option | | options.modelOptions | Model-specific options | | (returns) | Object JSON Schema object |

Example

Schema for simple one-model schema:

const Person = sequelize.define('Person', {name: DataTypes.STRING});

console.log(sjs.getSequelizeSchema(sequelize));

⇒ {
⇒   '$schema': 'http://json-schema.org/draft-07/schema#',
⇒   type: 'object',
⇒   definitions: {
⇒     Person: {
⇒       type: 'object',
⇒       properties: {
⇒         id: { type: 'integer', format: 'int32' },
⇒         name: { type: [ 'string', 'null' ], maxLength: 255 },
⇒         createdAt: { type: 'string', format: 'date-time' },
⇒         updatedAt: { type: 'string', format: 'date-time' }
⇒       },
⇒       required: [ 'id', 'createdAt', 'updatedAt' ]
⇒     }
⇒   }
⇒ }

... continuing on, use options to exclude a few properties:

const options = {exclude: ['id', 'createdAt', 'updatedAt']};

console.log(sjs.getSequelizeSchema(sequelize, options));

⇒ {
⇒   '$schema': 'http://json-schema.org/draft-07/schema#',
⇒   type: 'object',
⇒   definitions: {
⇒     Person: {
⇒       type: 'object',
⇒       properties: { name: { type: [ 'string', 'null' ], maxLength: 255 } }
⇒     }
⇒   }
⇒ }

... continuing on, add another model and some associations:

const Address = sequelize.define('Address', {
  street: DataTypes.STRING('tiny'),
  city: DataTypes.STRING,
  state: {type: DataTypes.STRING(2)},
  zipcode: DataTypes.NUMBER,
});

Person.hasOne(Address);
Address.hasMany(Person);

console.log(sjs.getSequelizeSchema(sequelize, options));

⇒ {
⇒   '$schema': 'http://json-schema.org/draft-07/schema#',
⇒   type: 'object',
⇒   definitions: {
⇒     Person: {
⇒       type: 'object',
⇒       properties: {
⇒         name: { type: [ 'string', 'null' ], maxLength: 255 },
⇒         Address: { '$ref': '#/definitions/Address' }
⇒       }
⇒     },
⇒     Address: {
⇒       type: 'object',
⇒       properties: {
⇒         street: { type: [ 'string', 'null' ], maxLength: 255 },
⇒         city: { type: [ 'string', 'null' ], maxLength: 255 },
⇒         state: { type: [ 'string', 'null' ], maxLength: 2 },
⇒         zipcode: { type: [ 'number', 'null' ] },
⇒         People: { type: 'array', items: { '$ref': '#/definitions/Person' } }
⇒       }
⇒     }
⇒   }
⇒ }

... continuing (customizing with options and modelOptions):

console.log(sjs.getSequelizeSchema(sequelize, {
  exclude: ['createdAt', 'updatedAt'],
  modelOptions: {
    Person: {exclude: ['id']},
    Address: {attributes: ['id']},
  }
}));

⇒ {
⇒   '$schema': 'http://json-schema.org/draft-07/schema#',
⇒   type: 'object',
⇒   definitions: {
⇒     Person: {
⇒       type: 'object',
⇒       properties: {
⇒         name: { type: [ 'string', 'null' ], maxLength: 255 },
⇒         Address: { '$ref': '#/definitions/Address' }
⇒       }
⇒     },
⇒     Address: {
⇒       type: 'object',
⇒       properties: { id: { type: 'integer', format: 'int32' } },
⇒       required: [ 'id' ]
⇒     }
⇒   }
⇒ }

getModelSchema(model[, options])

| | | |---|---| | model | Sequelize.Model | Sequelize model instance | | options | Object | | options.useRefs | Boolean = true Determines how associations are described in the schema. If true, model.associations are described as $refs to the appropriate entry in the schema definitions. If false, assiciations are described as plain attributes | | options.attributes | Array Attributes to include in the schema | | options.exclude | Array Attributes to exclude from the schema | | (return) | Object JSON Schema definition for the model|

Example

... continuing getSequelizeSchema() example, above:

console.log(sjs.getModelSchema(Person));

⇒ {
⇒   type: 'object',
⇒   properties: {
⇒     id: { type: 'integer', format: 'int32' },
⇒     name: { type: [ 'string', 'null' ], maxLength: 255 },
⇒     createdAt: { type: 'string', format: 'date-time' },
⇒     updatedAt: { type: 'string', format: 'date-time' },
⇒     Address: { '$ref': '#/definitions/Address' }
⇒   },
⇒   required: [ 'id', 'createdAt', 'updatedAt' ]
⇒ }

... continuing (useRefs = false to treat associations as plain attributes):

console.log(sjs.getModelSchema(Person, {useRefs: false}));

⇒ {
⇒   type: 'object',
⇒   properties: {
⇒     id: { type: 'integer', format: 'int32' },
⇒     name: { type: [ 'string', 'null' ], maxLength: 255 },
⇒     createdAt: { type: 'string', format: 'date-time' },
⇒     updatedAt: { type: 'string', format: 'date-time' },
⇒     AddressId: { type: [ 'integer', 'null' ], format: 'int32' }
⇒   },
⇒   required: [ 'id', 'createdAt', 'updatedAt' ]
⇒ }

getAttributeSchema(attribute)

| | | |---|---| | attribute | Sequelize.Model attribute | | | (returns) | Object JSON Schema property for the attribute|

Example

... continuing getModelSchema() example, above:

console.log(sjs.getAttributeSchema(Person.rawAttributes.name));

⇒ { type: [ 'string', 'null' ], maxLength: 255 }

Markdown generated from README_js.md by RunMD Logo