diff options
author | 2017-08-07 11:39:39 +0800 | |
---|---|---|
committer | 2017-08-07 11:39:39 +0800 | |
commit | 21d72c4a80fe2937d0c4ddd20624b27adbcd989b (patch) | |
tree | e5013ee12f74f8452e01cbff16e7b0158bc456cb /common/src/main/webapp/usageguide/appserver/node_modules/mongoose/examples/schema/schema.js | |
parent | f533e73e2ae32e010b16abdcf7985abaf31ab843 (diff) |
Upload the ESR GUI seed code
Issue-ID: AAI-68
Change-Id: Ia50ce0570c2fabecd77199d4e8454f56fe587c4e
Signed-off-by: lizi00164331 <li.zi30@zte.com.cn>
Diffstat (limited to 'common/src/main/webapp/usageguide/appserver/node_modules/mongoose/examples/schema/schema.js')
-rw-r--r-- | common/src/main/webapp/usageguide/appserver/node_modules/mongoose/examples/schema/schema.js | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/examples/schema/schema.js b/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/examples/schema/schema.js new file mode 100644 index 0000000..5bc99ae --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/mongoose/examples/schema/schema.js @@ -0,0 +1,119 @@ +/** + * Module dependencies. + */ + +var mongoose = require('../../lib'), + Schema = mongoose.Schema; + +/** + * Schema definition + */ + +// recursive embedded-document schema + +var Comment = new Schema(); + +Comment.add({ + title: { + type: String, + index: true + }, + date: Date, + body: String, + comments: [Comment] +}); + +var BlogPost = new Schema({ + title: { + type: String, + index: true + }, + slug: { + type: String, + lowercase: true, + trim: true + }, + date: Date, + buf: Buffer, + comments: [Comment], + creator: Schema.ObjectId +}); + +var Person = new Schema({ + name: { + first: String, + last: String + }, + email: { + type: String, + required: true, + index: { + unique: true, + sparse: true + } + }, + alive: Boolean +}); + +/** + * Accessing a specific schema type by key + */ + +BlogPost.path('date') +.default(function() { + return new Date(); +}) +.set(function(v) { + return v === 'now' ? new Date() : v; +}); + +/** + * Pre hook. + */ + +BlogPost.pre('save', function(next, done) { + /* global emailAuthor */ + emailAuthor(done); // some async function + next(); +}); + +/** + * Methods + */ + +BlogPost.methods.findCreator = function(callback) { + return this.db.model('Person').findById(this.creator, callback); +}; + +BlogPost.statics.findByTitle = function(title, callback) { + return this.find({title: title}, callback); +}; + +BlogPost.methods.expressiveQuery = function(creator, date, callback) { + return this.find('creator', creator).where('date').gte(date).run(callback); +}; + +/** + * Plugins + */ + +function slugGenerator(options) { + options = options || {}; + var key = options.key || 'title'; + + return function slugGenerator(schema) { + schema.path(key).set(function(v) { + this.slug = v.toLowerCase().replace(/[^a-z0-9]/g, '').replace(/-+/g, ''); + return v; + }); + }; +} + +BlogPost.plugin(slugGenerator()); + +/** + * Define model. + */ + +mongoose.model('BlogPost', BlogPost); +mongoose.model('Person', Person); |