mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Added node-modules
This commit is contained in:
42
node_modules/mongoose/examples/README.md
generated
vendored
Normal file
42
node_modules/mongoose/examples/README.md
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
|
||||
This directory contains runnable sample mongoose programs.
|
||||
|
||||
To run:
|
||||
|
||||
- first install [Node.js](http://nodejs.org/)
|
||||
- from the command line, execute: `node example.js`, replacing "example.js" with the name of a program.
|
||||
|
||||
|
||||
Goal is to show:
|
||||
|
||||
- global schemas
|
||||
- GeoJSON schemas / use (with crs)
|
||||
- text search
|
||||
- storing schemas as json
|
||||
- lean querires
|
||||
- statics
|
||||
- methods and statics on subdocs
|
||||
- custom types
|
||||
- querybuilder
|
||||
- promises
|
||||
- express + mongoose
|
||||
- accessing driver collection, db
|
||||
- connecting to replica sets
|
||||
- connecting to sharded clusters
|
||||
- enabling a fail fast mode
|
||||
- on the fly schemas
|
||||
- storing files
|
||||
- map reduce
|
||||
- aggregation
|
||||
- advanced hooks
|
||||
- using $elemMatch to return a subset of an array
|
||||
- query casting
|
||||
- upserts
|
||||
- pagination
|
||||
- express + mongoose session handling
|
||||
- group by (use aggregation)
|
||||
- authentication
|
||||
- schema migration techniques
|
||||
- converting documents to plain objects (show transforms)
|
||||
- how to $unset
|
||||
|
70
node_modules/mongoose/examples/doc-methods.js
generated
vendored
Normal file
70
node_modules/mongoose/examples/doc-methods.js
generated
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Schema
|
||||
*/
|
||||
|
||||
var CharacterSchema = Schema({
|
||||
name: { type: String, required: true }
|
||||
, health: { type: Number, min: 0, max: 100 }
|
||||
})
|
||||
|
||||
/**
|
||||
* Methods
|
||||
*/
|
||||
|
||||
CharacterSchema.methods.attack = function () {
|
||||
console.log('%s is attacking', this.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Character model
|
||||
*/
|
||||
|
||||
var Character = mongoose.model('Character', CharacterSchema);
|
||||
|
||||
/**
|
||||
* Connect to the database on localhost with
|
||||
* the default port (27017)
|
||||
*/
|
||||
|
||||
var dbname = 'mongoose-example-doc-methods-' + ((Math.random()*10000)|0);
|
||||
var uri = 'mongodb://localhost/' + dbname;
|
||||
|
||||
console.log('connecting to %s', uri);
|
||||
|
||||
mongoose.connect(uri, function (err) {
|
||||
// if we failed to connect, abort
|
||||
if (err) throw err;
|
||||
|
||||
// we connected ok
|
||||
example();
|
||||
})
|
||||
|
||||
/**
|
||||
* Use case
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Character.create({ name: 'Link', health: 100 }, function (err, link) {
|
||||
if (err) return done(err);
|
||||
console.log('found', link);
|
||||
link.attack(); // 'Link is attacking'
|
||||
done();
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up
|
||||
*/
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
mongoose.connection.db.dropDatabase(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
}
|
42
node_modules/mongoose/examples/hooks.js
generated
vendored
Normal file
42
node_modules/mongoose/examples/hooks.js
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
mongoose.connect('mongodb://localhost/example-hooks');
|
||||
|
||||
var schema = Schema({ name: String });
|
||||
schema.post('save', function () {
|
||||
console.log('post save hook', arguments);
|
||||
})
|
||||
|
||||
schema.pre('save', function (next) {
|
||||
console.log('pre save');
|
||||
next();
|
||||
})
|
||||
|
||||
var M = mongoose.model('Hooks', schema);
|
||||
|
||||
var doc = new M({ name: 'hooooks' });
|
||||
doc.save(function (err) {
|
||||
console.log('save callback');
|
||||
mongoose.disconnect();
|
||||
})
|
||||
|
||||
|
||||
////
|
||||
|
||||
Model.on('init', cb);
|
||||
Model.on('remove', cb);
|
||||
Model.on('update', cb);
|
||||
// Model.update() and doc.save()
|
||||
Model.on('insert', cb);
|
||||
Model.on('save', cb);
|
||||
|
||||
var promise = Model.save(doc, options, callback);
|
||||
|
||||
//
|
||||
|
||||
var schema = new Schema(..);
|
||||
schema.pre('save', function (next, done) {
|
||||
|
||||
})
|
135
node_modules/mongoose/examples/population-across-three-collections.js
generated
vendored
Normal file
135
node_modules/mongoose/examples/population-across-three-collections.js
generated
vendored
Normal file
@ -0,0 +1,135 @@
|
||||
|
||||
var assert = require('assert')
|
||||
var mongoose = require('../');
|
||||
var Schema = mongoose.Schema;
|
||||
var ObjectId = mongoose.Types.ObjectId;
|
||||
|
||||
/**
|
||||
* Connect to the db
|
||||
*/
|
||||
|
||||
var dbname = 'testing_populateAdInfinitum_' + require('../lib/utils').random()
|
||||
mongoose.connect('localhost', dbname);
|
||||
mongoose.connection.on('error', function() {
|
||||
console.error('connection error', arguments);
|
||||
});
|
||||
|
||||
/**
|
||||
* Schemas
|
||||
*/
|
||||
|
||||
var user = new Schema({
|
||||
name: String,
|
||||
friends: [{
|
||||
type: Schema.ObjectId,
|
||||
ref: 'User'
|
||||
}]
|
||||
});
|
||||
var User = mongoose.model('User', user);
|
||||
|
||||
var blogpost = Schema({
|
||||
title: String,
|
||||
tags: [String],
|
||||
author: {
|
||||
type: Schema.ObjectId,
|
||||
ref: 'User'
|
||||
}
|
||||
})
|
||||
var BlogPost = mongoose.model('BlogPost', blogpost);
|
||||
|
||||
/**
|
||||
* example
|
||||
*/
|
||||
|
||||
mongoose.connection.on('open', function() {
|
||||
|
||||
/**
|
||||
* Generate data
|
||||
*/
|
||||
|
||||
var userIds = [new ObjectId, new ObjectId, new ObjectId, new ObjectId];
|
||||
var users = [];
|
||||
|
||||
users.push({
|
||||
_id: userIds[0],
|
||||
name: 'mary',
|
||||
friends: [userIds[1], userIds[2], userIds[3]]
|
||||
});
|
||||
users.push({
|
||||
_id: userIds[1],
|
||||
name: 'bob',
|
||||
friends: [userIds[0], userIds[2], userIds[3]]
|
||||
});
|
||||
users.push({
|
||||
_id: userIds[2],
|
||||
name: 'joe',
|
||||
friends: [userIds[0], userIds[1], userIds[3]]
|
||||
});
|
||||
users.push({
|
||||
_id: userIds[3],
|
||||
name: 'sally',
|
||||
friends: [userIds[0], userIds[1], userIds[2]]
|
||||
});
|
||||
|
||||
User.create(users, function(err, docs) {
|
||||
assert.ifError(err);
|
||||
|
||||
var blogposts = [];
|
||||
blogposts.push({
|
||||
title: 'blog 1',
|
||||
tags: ['fun', 'cool'],
|
||||
author: userIds[3]
|
||||
})
|
||||
blogposts.push({
|
||||
title: 'blog 2',
|
||||
tags: ['cool'],
|
||||
author: userIds[1]
|
||||
})
|
||||
blogposts.push({
|
||||
title: 'blog 3',
|
||||
tags: ['fun', 'odd'],
|
||||
author: userIds[2]
|
||||
})
|
||||
|
||||
BlogPost.create(blogposts, function(err, docs) {
|
||||
assert.ifError(err);
|
||||
|
||||
/**
|
||||
* Population
|
||||
*/
|
||||
|
||||
BlogPost
|
||||
.find({ tags: 'fun' })
|
||||
.lean()
|
||||
.populate('author')
|
||||
.exec(function(err, docs) {
|
||||
assert.ifError(err);
|
||||
|
||||
/**
|
||||
* Populate the populated documents
|
||||
*/
|
||||
|
||||
var opts = {
|
||||
path: 'author.friends',
|
||||
select: 'name',
|
||||
options: { limit: 2 }
|
||||
}
|
||||
|
||||
BlogPost.populate(docs, opts, function(err, docs) {
|
||||
assert.ifError(err);
|
||||
console.log('populated');
|
||||
var s = require('util').inspect(docs, { depth: null })
|
||||
console.log(s);
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
function done(err) {
|
||||
if (err) console.error(err.stack);
|
||||
mongoose.connection.db.dropDatabase(function() {
|
||||
mongoose.connection.close();
|
||||
});
|
||||
}
|
95
node_modules/mongoose/examples/population-basic.js
generated
vendored
Normal file
95
node_modules/mongoose/examples/population-basic.js
generated
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var Game = mongoose.model('Game', gameSchema);
|
||||
|
||||
/**
|
||||
* Connect to the console database on localhost with
|
||||
* the default port (27017)
|
||||
*/
|
||||
|
||||
mongoose.connect('mongodb://localhost/console', function (err) {
|
||||
// if we failed to connect, abort
|
||||
if (err) throw err;
|
||||
|
||||
// we connected ok
|
||||
createData();
|
||||
})
|
||||
|
||||
/**
|
||||
* Data generation
|
||||
*/
|
||||
|
||||
function createData () {
|
||||
Console.create({
|
||||
name: 'Nintendo 64'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'September 29, 1996'
|
||||
}, function (err, nintendo64) {
|
||||
if (err) return done(err);
|
||||
|
||||
Game.create({
|
||||
name: 'Legend of Zelda: Ocarina of Time'
|
||||
, developer: 'Nintendo'
|
||||
, released: new Date('November 21, 1998')
|
||||
, consoles: [nintendo64]
|
||||
}, function (err) {
|
||||
if (err) return done(err);
|
||||
example();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Population
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Game
|
||||
.findOne({ name: /^Legend of Zelda/ })
|
||||
.populate('consoles')
|
||||
.exec(function (err, ocinara) {
|
||||
if (err) return done(err);
|
||||
|
||||
console.log(
|
||||
'"%s" was released for the %s on %s'
|
||||
, ocinara.name
|
||||
, ocinara.consoles[0].name
|
||||
, ocinara.released.toLocaleDateString());
|
||||
|
||||
done();
|
||||
})
|
||||
}
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
Console.remove(function () {
|
||||
Game.remove(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
})
|
||||
}
|
101
node_modules/mongoose/examples/population-of-existing-doc.js
generated
vendored
Normal file
101
node_modules/mongoose/examples/population-of-existing-doc.js
generated
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var Game = mongoose.model('Game', gameSchema);
|
||||
|
||||
/**
|
||||
* Connect to the console database on localhost with
|
||||
* the default port (27017)
|
||||
*/
|
||||
|
||||
mongoose.connect('mongodb://localhost/console', function (err) {
|
||||
// if we failed to connect, abort
|
||||
if (err) throw err;
|
||||
|
||||
// we connected ok
|
||||
createData();
|
||||
})
|
||||
|
||||
/**
|
||||
* Data generation
|
||||
*/
|
||||
|
||||
function createData () {
|
||||
Console.create({
|
||||
name: 'Nintendo 64'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'September 29, 1996'
|
||||
}, function (err, nintendo64) {
|
||||
if (err) return done(err);
|
||||
|
||||
Game.create({
|
||||
name: 'Legend of Zelda: Ocarina of Time'
|
||||
, developer: 'Nintendo'
|
||||
, released: new Date('November 21, 1998')
|
||||
, consoles: [nintendo64]
|
||||
}, function (err) {
|
||||
if (err) return done(err);
|
||||
example();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Population
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Game
|
||||
.findOne({ name: /^Legend of Zelda/ })
|
||||
.exec(function (err, ocinara) {
|
||||
if (err) return done(err);
|
||||
|
||||
console.log('"%s" console _id: %s', ocinara.name, ocinara.consoles[0]);
|
||||
|
||||
// population of existing document
|
||||
ocinara.populate('consoles', function (err) {
|
||||
if (err) return done(err);
|
||||
|
||||
console.log(
|
||||
'"%s" was released for the %s on %s'
|
||||
, ocinara.name
|
||||
, ocinara.consoles[0].name
|
||||
, ocinara.released.toLocaleDateString());
|
||||
|
||||
done();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
Console.remove(function () {
|
||||
Game.remove(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
})
|
||||
}
|
112
node_modules/mongoose/examples/population-of-multiple-existing-docs.js
generated
vendored
Normal file
112
node_modules/mongoose/examples/population-of-multiple-existing-docs.js
generated
vendored
Normal file
@ -0,0 +1,112 @@
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var Game = mongoose.model('Game', gameSchema);
|
||||
|
||||
/**
|
||||
* Connect to the console database on localhost with
|
||||
* the default port (27017)
|
||||
*/
|
||||
|
||||
mongoose.connect('mongodb://localhost/console', function (err) {
|
||||
// if we failed to connect, abort
|
||||
if (err) throw err;
|
||||
|
||||
// we connected ok
|
||||
createData();
|
||||
})
|
||||
|
||||
/**
|
||||
* Data generation
|
||||
*/
|
||||
|
||||
function createData () {
|
||||
Console.create({
|
||||
name: 'Nintendo 64'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'September 29, 1996'
|
||||
}, {
|
||||
name: 'Super Nintendo'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'August 23, 1991'
|
||||
}, function (err, nintendo64, superNintendo) {
|
||||
if (err) return done(err);
|
||||
|
||||
Game.create({
|
||||
name: 'Legend of Zelda: Ocarina of Time'
|
||||
, developer: 'Nintendo'
|
||||
, released: new Date('November 21, 1998')
|
||||
, consoles: [nintendo64]
|
||||
}, {
|
||||
name: 'Mario Kart'
|
||||
, developer: 'Nintendo'
|
||||
, released: 'September 1, 1992'
|
||||
, consoles: [superNintendo]
|
||||
}, function (err) {
|
||||
if (err) return done(err);
|
||||
example();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Population
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Game
|
||||
.find({})
|
||||
.exec(function (err, games) {
|
||||
if (err) return done(err);
|
||||
|
||||
console.log('found %d games', games.length);
|
||||
|
||||
var options = { path: 'consoles', select: 'name released -_id' };
|
||||
Game.populate(games, options, function (err, games) {
|
||||
if (err) return done(err);
|
||||
|
||||
games.forEach(function (game) {
|
||||
console.log(
|
||||
'"%s" was released for the %s on %s'
|
||||
, game.name
|
||||
, game.consoles[0].name
|
||||
, game.released.toLocaleDateString());
|
||||
})
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
Console.remove(function () {
|
||||
Game.remove(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
})
|
||||
}
|
124
node_modules/mongoose/examples/population-options.js
generated
vendored
Normal file
124
node_modules/mongoose/examples/population-options.js
generated
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var Game = mongoose.model('Game', gameSchema);
|
||||
|
||||
/**
|
||||
* Connect to the console database on localhost with
|
||||
* the default port (27017)
|
||||
*/
|
||||
|
||||
mongoose.connect('mongodb://localhost/console', function (err) {
|
||||
// if we failed to connect, abort
|
||||
if (err) throw err;
|
||||
|
||||
// we connected ok
|
||||
createData();
|
||||
})
|
||||
|
||||
/**
|
||||
* Data generation
|
||||
*/
|
||||
|
||||
function createData () {
|
||||
Console.create({
|
||||
name: 'Nintendo 64'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'September 29, 1996'
|
||||
}, {
|
||||
name: 'Super Nintendo'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'August 23, 1991'
|
||||
}, {
|
||||
name: 'XBOX 360'
|
||||
, manufacturer: 'Microsoft'
|
||||
, released: 'November 22, 2005'
|
||||
}, function (err, nintendo64, superNintendo, xbox360) {
|
||||
if (err) return done(err);
|
||||
|
||||
Game.create({
|
||||
name: 'Legend of Zelda: Ocarina of Time'
|
||||
, developer: 'Nintendo'
|
||||
, released: new Date('November 21, 1998')
|
||||
, consoles: [nintendo64]
|
||||
}, {
|
||||
name: 'Mario Kart'
|
||||
, developer: 'Nintendo'
|
||||
, released: 'September 1, 1992'
|
||||
, consoles: [superNintendo]
|
||||
}, {
|
||||
name: 'Perfect Dark Zero'
|
||||
, developer: 'Rare'
|
||||
, released: 'November 17, 2005'
|
||||
, consoles: [xbox360]
|
||||
}, function (err) {
|
||||
if (err) return done(err);
|
||||
example();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Population
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Game
|
||||
.find({})
|
||||
.populate({
|
||||
path: 'consoles'
|
||||
, match: { manufacturer: 'Nintendo' }
|
||||
, select: 'name'
|
||||
, options: { comment: 'population' }
|
||||
})
|
||||
.exec(function (err, games) {
|
||||
if (err) return done(err);
|
||||
|
||||
games.forEach(function (game) {
|
||||
console.log(
|
||||
'"%s" was released for the %s on %s'
|
||||
, game.name
|
||||
, game.consoles.length ? game.consoles[0].name : '??'
|
||||
, game.released.toLocaleDateString());
|
||||
})
|
||||
|
||||
return done();
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up
|
||||
*/
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
Console.remove(function () {
|
||||
Game.remove(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
})
|
||||
}
|
96
node_modules/mongoose/examples/population-plain-objects.js
generated
vendored
Normal file
96
node_modules/mongoose/examples/population-plain-objects.js
generated
vendored
Normal file
@ -0,0 +1,96 @@
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var Game = mongoose.model('Game', gameSchema);
|
||||
|
||||
/**
|
||||
* Connect to the console database on localhost with
|
||||
* the default port (27017)
|
||||
*/
|
||||
|
||||
mongoose.connect('mongodb://localhost/console', function (err) {
|
||||
// if we failed to connect, abort
|
||||
if (err) throw err;
|
||||
|
||||
// we connected ok
|
||||
createData();
|
||||
})
|
||||
|
||||
/**
|
||||
* Data generation
|
||||
*/
|
||||
|
||||
function createData () {
|
||||
Console.create({
|
||||
name: 'Nintendo 64'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'September 29, 1996'
|
||||
}, function (err, nintendo64) {
|
||||
if (err) return done(err);
|
||||
|
||||
Game.create({
|
||||
name: 'Legend of Zelda: Ocarina of Time'
|
||||
, developer: 'Nintendo'
|
||||
, released: new Date('November 21, 1998')
|
||||
, consoles: [nintendo64]
|
||||
}, function (err) {
|
||||
if (err) return done(err);
|
||||
example();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Population
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Game
|
||||
.findOne({ name: /^Legend of Zelda/ })
|
||||
.populate('consoles')
|
||||
.lean() // just return plain objects, not documents wrapped by mongoose
|
||||
.exec(function (err, ocinara) {
|
||||
if (err) return done(err);
|
||||
|
||||
console.log(
|
||||
'"%s" was released for the %s on %s'
|
||||
, ocinara.name
|
||||
, ocinara.consoles[0].name
|
||||
, ocinara.released.toLocaleDateString());
|
||||
|
||||
done();
|
||||
})
|
||||
}
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
Console.remove(function () {
|
||||
Game.remove(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
})
|
||||
}
|
102
node_modules/mongoose/examples/schema.js
generated
vendored
Normal file
102
node_modules/mongoose/examples/schema.js
generated
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
, 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){
|
||||
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);
|
Reference in New Issue
Block a user