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

swaggering-mongoose

v0.4.1

Published

Generate mongoose schemas and models from Swagger documents

Downloads

14

Readme

swaggering-mongoose

Generate mongoose schemas and models from Swagger documents

Installation

npm install swaggering-mongoose

Usage

Basic usage

Pass a swagger document with definitions to the compile() method and then dynamically access the underlying mongoose models.

const swaggeringMongoose = require('swaggering-mongoose');

const swagger = fs.readFileSync('./petstore.json');
const { models: { Pet } } = swaggeringMongoose.compile(swagger);
const myPet = new Pet({
    id: 123,
    name: 'Fluffy'
    });
myPet.save();

Basic support for OpenApi 3.0.0 has been introduced. See the components/schemas in petstore3.json

Advanced usage

The compile() method returns both the generated schemas and models from a Swagger document. Internally, the process is composed by three steps:

  1. getDefinitions(spec): returns a definition set from a Swagger file (from the definitions or the components/schemas)
  2. getSchemas(definitions): returns a set of mongoose schemas from a definitions set
  3. getModels(schemas): creates and returns a set of mongoose models from a schemas set

These functions are exported along with the compile(), to be used to hook up the intermediate results for advanced usages. See below:

const swaggeringMongoose = require('swaggering-mongoose');

const swagger = fs.readFileSync('./petstore.json');

const definitions = swaggeringMongoose.getDefinitions(swagger);
// you can augment/override definitions here

const schemas = swaggeringMongoose.getSchemas(definitions);

// you can augment schemas here, e.g.
schemas.Pet.set('autoIndex', true);
// or
schemas.Pet.set('toJSON', { transform: (doc, pojo) => {
  pojo._id = pojo._id.toString() // the _id object is now a string in the POJO doc
} })

const { Pet } = swaggeringMongoose.getModels(schemas);
const myPet = new Pet({
    id: 123,
    name: 'Fluffy'
    });
myPet.save();

Objects Relationships

swaggering-mongoose supports relationships between definitions in a Swagger document. e.g.

"definitions" : {
    "Book" : {
        "type": "object",
        "properties" : {
            "name" : {
                "type": "string"
            },
            "category" : {
                "type": "array",
                "items": {
                    "$ref" : "#/definitions/Category"
                }
            }
        }
    },

    "Category" : {
        "type" : "object",
        "properties" : {
            "name": {
                "type": "string",
                "default": "cat1"
            }
        }
    }
}

Mongoose extension and override

While the Swagger specification tries to accommodate most use cases, additional details can be added to extend the documentation with specific mongoose options, such as indexes, external references or fields selection.

These extras can be defined as JSON objects in a x-swaggering-mongoose property, e.g.

"definitions" : {
    "User" : {
        "type": "object",
        "properties" : {
            "name" : {
                "type": "string",
                "x-swaggering-mongoose": {
                    "index": {
                        "unique" : true
                   }
                }
            },
            "role" : {
                "type": "string",
                "x-swaggering-mongoose": {
                    "type": "ObjectId",
                    "ref": "Role"
                }
            },
            "password" : {
                "type": "password",
                "x-swaggering-mongoose": {
                    "select": false
                }
            }
        }
    },

Additionally, (thanks @ChrisdeWolf) specific mongoose schema options can be applied using the same x-swaggering-mongoose property, e.g.

"schemas": {
    "Owner": {
        "type": "object",
        "properties": {
            "name": {
                "type": "string"
            }
        },
        "x-swaggering-mongoose": {
            "timestamps": true, 
            "versionKey": "_custom"
        }
    },
}

Limitations

swaggering-mongoose supports the following attributes out-of-the-box: integer, long, float, double, string, password, boolean, date, dateTime, date-time, object, array (including nested schemas).

Mongoose doesn't support required property for nested object (plain object, not reference). Similarly, swaggering-mongoose silently ignores the property even if explicitly defined with an override.