# swagger-sequelize

> Generate Sequelize model definitions from a Swagger 2.0 schema

Latest version **0.1.5** (published 2019-10-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install swagger-sequelize
pnpm add swagger-sequelize
yarn add swagger-sequelize
bun add swagger-sequelize
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2019-10-21 |
| First published | 2015-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 0 |
| Unpacked size | 40.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Kingsquare BV - Tim de Koning |
| Maintainers | gekkie, reggino |

## Links

- npm: https://www.npmjs.com/package/swagger-sequelize
- Repository: https://github.com/kingsquare/swagger-sequelize
- npm.io page: https://npm.io/package/swagger-sequelize

## Recent versions

- 0.1.5 (latest) — 2019-10-21
- 0.1.4 — 2019-10-01
- 0.1.3 — 2018-12-19
- 0.1.2 — 2018-01-21
- 0.1.1 — 2017-05-24
- 0.1.0 — 2016-10-12
- 0.0.8 — 2016-09-23
- 0.0.7 — 2016-09-14
- 0.0.6 — 2016-09-13
- 0.0.5 — 2016-01-13
- 0.0.4 — 2015-12-07
- 0.0.3 — 2015-12-02
- 0.0.2 — 2015-11-30
- 0.0.1 — 2015-11-26

## README

Generate Sequelize model definitions from a Swagger 2.0 schema
====

Prequisites: 

- Create a description of your REST service in a JSON format (see [http://swagger.io/](Swagger.io)) 
- Create your app and install (see [http://docs.sequelizejs.com/en/latest/](Sequelizejs.com))

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

Sample usage:

```js
var swaggerSequelize = require('swagger-sequelize');
var fs = require('fs');
var Sequelize = require('sequelize');

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

var MyModel =  sequelize.define('MyModel', swaggerSequelize.generate(swaggerSpec.definitions.MyModel));

// ... do stuff with MyModel e.g. to setup your tables:

MyModel.sync({force: true})

```

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

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

with a YAML-import
```js
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
```

## Primary key

To make your primary key work in Sequelize one may need to mark `"x-primary-key": true` in the model definition in `swagger.json`:

```JSON
"definitions": {
    "Document": {
        "properties": {
            "id": {
                "type": "integer",
                "format": "int32",
                "description": "Unique Identifier representing a document",
                "x-primary-key": true
            },
```

And in `swagger.yaml`, it would be:

```YAML
definitions:
  # Model definition
  Document:
    properties:
      id:
        type: integer
        format: int32
        description: Unique Identifier representing a document
        x-primary-key: true
```

## Additional parametrization

In the same way as with `x-primary-key`, you can parameterize the attributes `x-autoincrement`, `x-unique` and `x-allow-null`

## Default value for UUID fields

It is possible to set default values for fields with `uuid` format
##### JSON
```JSON
"definitions": {
    "Document": {
        "properties": {
            "id": {
                "type": "string",
                "format": "uuid",
                "default": "Sequelize.UUIDV4"
            },
```
##### YAML
```YAML
definitions:
  # Model definition
  Document:
    properties:
      id:
        type: string
        format: uuid
        default: Sequelize.UUIDV4
```

---
_Source: https://npm.io/package/swagger-sequelize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
