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 🙏

© 2024 – Pkg Stats / Ryan Hefner

swagelize

v1.5.0

Published

Generate Sequelize model definitions and DAO from a Swagger 2.0 schema

Downloads

14

Readme

ORIGINALY FORKED FROM : https://github.com/kingsquare/swagger-sequelize

this project is an open source project developed by Yoop Digital company: http://yoop.digital

Generate Sequelize model definitions from a Swagger 2.0 schema Tasks the lib does:

  • Generate sequelize structure folders and files with definitions and associations
    • Generate a folder models with index.js containing Sequelize configuration and initialisation
    • Generate all models from swagger definitions node
    • Generate associations within sequelize model files
      • One to Many associations,
      • Many to many associations,
      • One to one associations.
  • Generate a dao folder with all methods described in swagger path node including parameters and sequelize glu code initialization
    • Note that sometimes, due to lack of precision in paths node, swagelize can't dtermine sequelize method or model, so you will have to determine what to write here yourself, according to project needs

Restrictions : this libray has been developed for MariaDB project needs, so I only tested it for MariaDB sequelization in a nodejs project

Other restriction: Swagger spec is not suitable for Model definitions, the lack of cardinality info doesn't allow us to perform proper definition. I had to process some new fields in swagger spec, that are not covered by swagger spec to enable the cardinality feature and thus, proper generation of sequelize models. Here after, what you need to do in order to specify cardinality:

  • First of all, you must specify Primary keys in a special model attribute [x-primary-key], it must be an array !!:
"Pet": {
      "type": "object",
      ...
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        ...
      },
      "x-primary-key": ["id"],
      ...
    },
  • For Object arrays, you can specify a "throughTable" attribute, and that would means it's a N-N associations: Example :
"Pet": {
      "type": "object",
      "required": [
        "name",
        "photoUrls"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "tags": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Tag"
          },
          "throughTable": "PetTag"
        },
      },
      "x-primary-key": ["id"],
    },
  • Same thing goes for $ref objects : here is a [Pet]N-----1..0[Category] association ( nullable: false would make it [Pet]N-----1[Category] )
"Pet": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "category": {
          "$ref": "#/definitions/Category",
          "sourceCardinality": "N",
          "nullable": true
        },
        ...
      },
      "x-primary-key": ["id"],
    },

Note the "sourceCardinality" : "N" attribute, allowing to specify the N part ine the relationship. if you decide to put "sourceCardinality" : "1", then you're describing a [Pet]1-----1[Category]. For sourceCardinality value, Only 1 and N are supported here

Here is a [Pet]N-----N[Category] association:

"Pet": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "category": {
          "$ref": "#/definitions/Category",
          "throughTable": "PetCategory",
          "nullable": true
        },
        ...
      },
      "x-primary-key": ["id"],
    },

That will generate a table PetCategory with 2 fields id_pet, id_category as composed primary key, each being FK to respective tables. . Note that, if the association table must have other fields, you don't have to specify the FKs. Moreover, you need to specify that this is a throughTable, so that references won't be handle for this table.

"PetCategory": {
      "type": "object",
      "throughTable": true,
      "properties": {
        "pet": {
          "$ref": "#/definitions/Pet"
        },
        "tag": {
          "$ref": "#/definitions/Category"
        },
        "dateX": {
          "type": "string",
          "format": "date-time"
        }
      }
    }

====

Prequisites:

Currently, the project simply maps Swagger-datatypes to their Sequelize counterpart.

Sample usage:

var swagelize = require('swagelize');
var fs = require('fs');
var Sequelize = require('sequelize');

var sequelize = new Sequelize('<your uri>');
var swaggerSpec = JSON.parse(fs.readFileSync('<your swagger.json>', 'utf-8'));

swagelize.generateAll(swaggerSpec);

This project is assuming you have configured sequelize through a config.js contained in a config folder as specified on sequelize docs example of config.js

module.exports = {
    "local": {
        "username": "root",
        "password": "admin",
        "database": "TEST_SWAGUELIZE",
        "host": "127.0.0.1",
        "dialect": "mysql",
        "port": "3306"
    }
};

In case you want to read from a swagger.yaml rather than from a swagger.json, you could replace the JSON-import

var swaggerSpec = JSON.parse(fs.readFileSync('<your swagger.sjon>', 'utf-8'));

with a YAML-import

var yaml = require('js-yaml');
var swaggerSpec = yaml.safeLoad(fs.readFileSync('<your swagger.yaml>', 'utf8'));

To be consistent, one should "officially" add js-yaml to the project:

npm install --save js-yaml

Full generation of project

You can combined this project with swagger-codegen project (https://github.com/swagger-api/swagger-codegen), in order to have Full layer service generated from the swagger spec. Use this jar as follow:

  • Add a script to package.Json :
"generate-api": "cd <CONTAINING FOLDER .jar and swagger spec> && java -jar swagger-codegen-cli.jar generate -i swagger.json -l nodejs-server -o ../",
  • Then run your script : npm run generate-api
  • Result would be a full nodejs project, with API service layer generated