Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

medolino/mongoose-schema-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

121 Commits

Repository files navigation

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.

Cli usage example

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"
 }
 }
 }
}
*/

About

Parse mongoose schema to JSON format

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

AltStyle によって変換されたページ (->オリジナル) /