Mongoose Model Builder
Have you ever wished that there is a way to write Mongoose schemas like a config object, instead of calling the mongoose.schema
methods? This Schema Builder is the short script that I wrote for my own project.
How to use?
To Build A Mongoose Schema
say we want to build a new mongoose schema:
var mongoose = ;var map = ; // require lodash 4.0.0<5.0.0. For <4.0.0 you need `require('lodash/collection/map');var subSchemaConfig = resourcePrototype: path: type: String content: type: String url: type: String __options__: _id: false autoIndexId: false ; var subSchema = ; // if this is in a test, then we can expect: should;should;should;should;shouldnot;
To Build A Mongoose Model
var mongoose = ;var map = ; // require lodash 4.0.0<5.0.0. For <4.0.0 you need `require('lodash/collection/map'); var schemaConfig = HashStore: key: String v: Number resources: subSchemaresourcePrototype// include a sub schema __index__: key: -1 __options__: unique: true __options__: minimize: false __ensureIndexes__: true ; Models = ; // and now you can use the model: var hashItem = key: 'randome-string' v: 0 // resources
To Develop and Test
run
npm test
This calls mocha ./ModelBuilder.spec.js
which executes the mocha spec for the model builder.