JSPM

  • Created
  • Published
  • Downloads 5867
  • Score
    100M100P100Q149341F
  • License MIT

Convert Sequelize models into various JSON Schema variants (using the Strategy Pattern)

Package Exports

  • @alt3/sequelize-to-json-schemas

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@alt3/sequelize-to-json-schemas) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

NPM Version Build Status Greenkeeper Enabled NPM Total Downloads Code Coverage Code Climate maintainability Conventional Commits Contributor Covenant

sequelize-to-json-schemas

Convert Sequelize models into various JSON Schema variants (using the Strategy Pattern).

Supported Schemas

More welcome, inspiration found here

Installation

npm install @alt3/sequelize-to-json-schemas --save

Usage

const { JsonSchemaManager, JsonSchema7Strategy, OpenApi3Strategy } = require('@alt3/sequelize-to-json-schemas');
const schemaManager = new JsonSchemaManager();

// now generate a JSON Schema Draft-07 model schema
let schema = schemaManager.generate(userModel, new JsonSchema7Strategy());

// and/or the OpenAPI 3.0 equivalent
schema = schemaManager.generate(userModel, new OpenApi3Strategy());

Main Goals

  • understandable code, highly maintainable
  • support for Sequelize versions 4, 5 and beyond
  • valid schemas (enforced by the ajv and Swagger Parser validators)
  • JsonSchemaManager for single (rock solid) core functionality shared between all strategies
  • StrategyInterface for simplified implementation of new schema variants

Configuration Options

To configure global options use the JsonSchemaManager initialization:

const schemaManager = new JsonSchemaManager({
  baseUri: '/',
  absolutePaths: true,
  disableComments: true,
});

To configure (per) model options use the generate() method:

const userSchema = schemaManager.generate(userModel, strategy, {
  title: 'MyUser',
  description: 'My Description',
  exclude: ['someAttribute'],
  include: ['someAttribute'],
  associations: true,
  excludeAssociations: ['someAssociation'],
  includeAssociations: ['someAssociation'],
});

The following Sequelize attribute options are automatically converted into schema properties:

module.exports = sequelize => {
  const Model = sequelize.define('user', {
    userName: {
      type: DataTypes.STRING,
      allowNull: true,
      defaultValue: 'Default Value',
      associate: {},
    },
  });

  return Model;
};

To configure additional schema properties add a jsonSchema property with one or more of the following options to your Sequelize attribute:

module.exports = sequelize => {
  const Model = sequelize.define('user', {
    userName: {
      type: DataTypes.STRING,
      jsonSchema: {
        description: 'My attribute description',
        comment: 'My attribute comment',
        examples: ['My example 1', 'My example 2'],
        readOnly: true, // OR writeOnly: true
      },
    },
  });

  return Model;
};

License

This project is released under MIT LICENSE.

Contributing

Please refer to the guidelines for contributing.