0.2.7 • Published 3 months ago

mongoose-schema-parser v0.2.7

Weekly downloads
26
License
ISC
Repository
github
Last release
3 months ago

Mongoose Schema Parser

Build Status Coverage Status Known Vulnerabilities JavaScript Style Guide npm

Mongoose Schema Parser tool parses Mongoose schemas defined in project and returns data in JSON format.

How It Works

Script uses mongoose instance to parse registered schemas. You can use it from CLI or inside your application code.

Install

Install globally if you want to use it from cli

npm i -g mongoose-schema-parser

or locally if you want to use it from your code

npm i mongoose-schema-parser --save

Usage

Cli usage

If you install package globally, you can run it from anywhere in your system. Script returns file with parsed schema in JSON format.

mongoose-schema-parser -c /path/to/project -p "**/*.model.js" -o output-schema.json

Script options:

  -c, --cwd      Current working directory                    [string] [default: "/your/project/dir"]
  -p, --pattern  Search file pattern                  [string] [required] [default: "**/*.model.js"]
  -o, --output   Output file path                                                [string] [required]
  --help, -h     Show help                                                                 [boolean]

Output example:

{
  "Category": {
    "schema": {
      "_id": {
        "type": "ObjectId"
      },
      "name": {
        "type": "String",
        "details": {
          "required": true,
          "maxlength": 150
        }
      },
      "enabled": {
        "type": "Boolean",
        "details": {
          "default": true
        }
      }
    }
  },
  "Item": {
    "schema": {
      "_id": {
        "type": "ObjectId"
      },
      "name": {
        "type": "String"
      },
      "category": {
        "type": "ObjectId",
        "details": {
          "ref": "Category"
        }
      },
      "sizes": {
        "type": "Schema",
        "schema": {
          "size": {
            "type": "Number"
          }
        }
      },
      "created": {
        "type": "Date",
        "details": {}
      }
    }
  }
}

Code usage

This example shows, how to use parseSchemaFromMongoose function to parse schema in your application. Function returns schema as JS object.

const mongoose = require('mongoose')
const { parseSchemaFromMongoose } = require('mongoose-schema-parser')

// content of ./models/example-01.model
/* 
const ExampleSchema = new mongoose.Schema({
  name: 'string'
})
mongoose.model('ExampleModel', ExampleSchema)
*/
require('./models/example-01.model')

const exportedSchema = parseSchemaFromMongoose(mongoose)

console.log(JSON.stringify(exportedSchema, null, 2))
/* outputs:
{
  "ExampleModel": {
    "schema": {
      "name": {
        "type": "String"
      },
      "_id": {
        "type": "ObjectId"
      }
    }
  }
}
*/
0.2.7

3 months ago

0.2.6

9 months ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

3 years ago

0.2.0

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago