mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Updated mongoose
This commit is contained in:
120
node_modules/mongoose/examples/schema/schema.js
generated
vendored
Normal file
120
node_modules/mongoose/examples/schema/schema.js
generated
vendored
Normal file
@ -0,0 +1,120 @@
|
||||
|
||||
/**
|
||||
* 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);
|
27
node_modules/mongoose/examples/schema/storing-schemas-as-json/index.js
generated
vendored
Normal file
27
node_modules/mongoose/examples/schema/storing-schemas-as-json/index.js
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
// modules
|
||||
var mongoose = require('../../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// parse json
|
||||
var raw = require('./schema.json');
|
||||
|
||||
// create a schema
|
||||
var timeSignatureSchema = Schema(raw);
|
||||
|
||||
// compile the model
|
||||
var TimeSignature = mongoose.model('TimeSignatures', timeSignatureSchema);
|
||||
|
||||
// create a TimeSignature document
|
||||
var threeFour = new TimeSignature({
|
||||
count: 3,
|
||||
unit: 4,
|
||||
description: "3/4",
|
||||
additive: false,
|
||||
created: new Date,
|
||||
links: ["http://en.wikipedia.org/wiki/Time_signature"],
|
||||
user_id: "518d31a0ef32bbfa853a9814"
|
||||
});
|
||||
|
||||
// print its description
|
||||
console.log(threeFour);
|
9
node_modules/mongoose/examples/schema/storing-schemas-as-json/schema.json
generated
vendored
Normal file
9
node_modules/mongoose/examples/schema/storing-schemas-as-json/schema.json
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"count": "number",
|
||||
"unit": "number",
|
||||
"description": "string",
|
||||
"links": ["string"],
|
||||
"created": "date",
|
||||
"additive": "boolean",
|
||||
"user_id": "ObjectId"
|
||||
}
|
Reference in New Issue
Block a user