November's Paddleball Marathon
Join us to discuss the challenges, solutions and best practices for in-house JavaScript code sharing. Tuesday, 12/17 at 10am PT/1pm ET.Sign up here »

mongoose_schema-json

1.0.3 • Public • Published

mongoose_schema-json

Convert mongoose schema object to JSON and vice versa.

Installation

npm install mongoose_schema-json

Usage

For example when you want to store mongoose schema in mongo database you need to convert it into JSON string.

Example of conversion

// mongoose schema in object notation
 
var schemaObj = {
    name: {type: String, lowercase: true},
    age: Number,
    born: Date,
    isActive: Boolean,
    fans: [Schema.Types.Mixed],
    company_id: Schema.Types.ObjectId
}
// mongoose schema in JSON notation
 
var jsonStr = {
    "name": {"type": "String", "lowercase": true},
    "age": "Number",
    "born": "Date",
    "isActive": "Boolean",
    "fans": ["Schema.Types.Mixed"],
    "company_id": "Schema.Types.ObjectId"
}

Methods

  • schema2json()
  • json2schema()
var convert = require('mongoose_schema-json');
 
var jsonStr = convert.schema2json(schemaObj);
var schemaObj = convert.json2schema(jsonStr);

Testing

Test results will be visible in console. NodeJS must be installed !

$ node test schema2json schema1

$ node test json2schema json1

Install

npm i mongoose_schema-json

DownloadsWeekly Downloads

13

Version

1.0.3

License

MIT

Last publish

Collaborators

  • avatar