Browse files

Reformat code

  • Loading branch information...
1 parent 0918675 commit 2790056d93874fbe8b0f22a26d077afc0206ded2 @raymondfeng raymondfeng committed Jan 24, 2014
Showing with 535 additions and 533 deletions.
  1. +33 −33 example/model.js
  2. +320 −320 lib/mongodb.js
  3. +64 −64 test/id.test.js
  4. +6 −6 test/imported.test.js
  5. +7 −5 test/init.js
  6. +87 −87 test/mongodb.test.js
  7. +18 −18 test/objectid.test.js
View
66 example/model.js
@@ -1,51 +1,51 @@
var DataSource = require('loopback-datasource-juggler').DataSource;
-var config = require('rc')('loopback', {dev: {mongodb:{}}}).dev.mongodb;
+var config = require('rc')('loopback', {dev: {mongodb: {}}}).dev.mongodb;
var ds = new DataSource(require('../'), config);
var Customer = ds.createModel('customer', {seq: {type: Number, id: true}, name: String, emails: [String], age: Number});
Customer.create({
- seq: 1,
- name: 'John1',
- emails: ['john@x.com', 'john@y.com'],
- age: 30
-}, function(err, customer) {
- console.log(customer.toObject());
+ seq: 1,
+ name: 'John1',
+ emails: ['john@x.com', 'john@y.com'],
+ age: 30
+}, function (err, customer) {
+ console.log(customer.toObject());
- /*
- customer.updateAttributes({name: 'John'}, function(err, result) {
- console.log(err, result);
- });
+ /*
+ customer.updateAttributes({name: 'John'}, function(err, result) {
+ console.log(err, result);
+ });
- customer.delete(function(err, result) {
- customer.updateAttributes({name: 'JohnX'}, function(err, result) {
- console.log(err, result);
- });
+ customer.delete(function(err, result) {
+ customer.updateAttributes({name: 'JohnX'}, function(err, result) {
+ console.log(err, result);
+ });
- });
- */
+ });
+ */
- Customer.findById(customer.seq, function(err, customer) {
- console.log(customer.toObject());
+ Customer.findById(customer.seq, function (err, customer) {
+ console.log(customer.toObject());
- customer.name = 'John';
- customer.save(function(err, customer) {
- console.log(customer.toObject());
- ds.connector.close();
- });
+ customer.name = 'John';
+ customer.save(function (err, customer) {
+ console.log(customer.toObject());
+ ds.connector.close();
});
+ });
});
/*
-Customer.create({
- id: 2,
- name: 'John2',
- emails: ['john@x.com', 'jhon@y.com'],
- age: 30
-}, function(err, customer) {
- console.log(customer.toObject());
-});
-*/
+ Customer.create({
+ id: 2,
+ name: 'John2',
+ emails: ['john@x.com', 'jhon@y.com'],
+ age: 30
+ }, function(err, customer) {
+ console.log(customer.toObject());
+ });
+ */
View
640 lib/mongodb.js
@@ -11,74 +11,72 @@ var Connector = require('loopback-datasource-juggler').Connector;
* @returns {ObjectID}
*/
function ObjectID(id) {
- if (typeof id !== 'string') {
- return id;
- }
- if(id instanceof mongodb.ObjectID) {
- return id;
- }
- try {
- return new mongodb.ObjectID(id);
- } catch(e) {
- return id;
- }
+ if (typeof id !== 'string') {
+ return id;
+ }
+ if (id instanceof mongodb.ObjectID) {
+ return id;
+ }
+ try {
+ return new mongodb.ObjectID(id);
+ } catch (e) {
+ return id;
+ }
}
/*!
* Generate the mongodb URL from the options
*/
function generateMongoDBURL(options) {
- options.hostname = (options.hostname || options.host || '127.0.0.1');
- options.port = (options.port || 27017);
- options.database = (options.database || options.db || 'test');
- if (options.username && options.password) {
- return "mongodb://" + options.username + ":" + options.password + "@" + options.hostname + ":" + options.port + "/" + options.database;
- } else {
- return "mongodb://" + options.hostname + ":" + options.port + "/" + options.database;
- }
+ options.hostname = (options.hostname || options.host || '127.0.0.1');
+ options.port = (options.port || 27017);
+ options.database = (options.database || options.db || 'test');
+ if (options.username && options.password) {
+ return "mongodb://" + options.username + ":" + options.password + "@" + options.hostname + ":" + options.port + "/" + options.database;
+ } else {
+ return "mongodb://" + options.hostname + ":" + options.port + "/" + options.database;
+ }
}
-
/**
* Initialize the MongoDB connector for the given data source
* @param {DataSource} dataSource The data source instance
* @param {Function} [callback] The callback function
*/
exports.initialize = function initializeDataSource(dataSource, callback) {
- if (!mongodb) {
- return;
- }
+ if (!mongodb) {
+ return;
+ }
- var s = dataSource.settings;
+ var s = dataSource.settings;
- s.safe = (s.safe !== false);
- s.w = s.w || 1;
- s.url = s.url || generateMongoDBURL(s);
- dataSource.connector = new MongoDB(s, dataSource);
- dataSource.ObjectID = mongodb.ObjectID;
+ s.safe = (s.safe !== false);
+ s.w = s.w || 1;
+ s.url = s.url || generateMongoDBURL(s);
+ dataSource.connector = new MongoDB(s, dataSource);
+ dataSource.ObjectID = mongodb.ObjectID;
- if(callback) {
- dataSource.connector.connect(callback);
- }
+ if (callback) {
+ dataSource.connector.connect(callback);
+ }
};
-
/**
* The constructor for MongoDB connector
* @param {Object} settings The settings object
* @param {DataSource} dataSource The data source instance
* @constructor
*/
function MongoDB(settings, dataSource) {
- Connector.call(this, 'mongodb', settings);
+ Connector.call(this, 'mongodb', settings);
- this.debug = settings.debug;
+ this.debug = settings.debug;
- if(this.debug) {
- console.log('MongoDB', settings);
- }
+ if (this.debug) {
+ console.log('MongoDB', settings);
+ }
- this.dataSource = dataSource;
+ this.dataSource = dataSource;
}
@@ -93,26 +91,26 @@ util.inherits(MongoDB, Connector);
* @param {Db} db The mongo DB object
*/
MongoDB.prototype.connect = function (callback) {
- var self = this;
- if (self.db) {
- process.nextTick(function() {
- callback && callback(null, self.db);
- });
- } else {
- mongodb.MongoClient.connect(self.settings.url, self.settings, function (err, db) {
- if(!err) {
- if(self.debug) {
- console.log('MongoDB connection is established: ' + self.settings.url);
- }
- self.db = db;
- } else {
- if(self.debug || !callback) {
- console.error('MongoDB connection is failed: ' + self.settings.url, err);
- }
- }
- callback && callback(err, db);
- });
- }
+ var self = this;
+ if (self.db) {
+ process.nextTick(function () {
+ callback && callback(null, self.db);
+ });
+ } else {
+ mongodb.MongoClient.connect(self.settings.url, self.settings, function (err, db) {
+ if (!err) {
+ if (self.debug) {
+ console.log('MongoDB connection is established: ' + self.settings.url);
+ }
+ self.db = db;
+ } else {
+ if (self.debug || !callback) {
+ console.error('MongoDB connection is failed: ' + self.settings.url, err);
+ }
+ }
+ callback && callback(err, db);
+ });
+ }
};
/**
@@ -123,7 +121,7 @@ MongoDB.prototype.connect = function (callback) {
* @private
*/
MongoDB.prototype.defineForeignKey = function (model, key, cb) {
- cb(null, ObjectID);
+ cb(null, ObjectID);
};
/**
@@ -132,10 +130,10 @@ MongoDB.prototype.defineForeignKey = function (model, key, cb) {
* @returns {*}
*/
MongoDB.prototype.collection = function (name) {
- if(!this.db) {
- throw new Error('MongoDB connection is not established');
- }
- return this.db.collection(name);
+ if (!this.db) {
+ throw new Error('MongoDB connection is not established');
+ }
+ return this.db.collection(name);
};
/**
@@ -145,26 +143,26 @@ MongoDB.prototype.collection = function (name) {
* @param {Function} [callback] The callback function
*/
MongoDB.prototype.create = function (model, data, callback) {
- var self = this;
- if(self.debug) {
- console.log('create', model, data);
- }
- var idValue = self.getIdValue(model, data);
- var idName = self.idName(model);
-
- if (idValue === null) {
- delete data[idName]; // Allow MongoDB to generate the id
- } else {
- var oid = ObjectID(idValue); // Is it an Object ID?
- data._id = oid; // Set it to _id
- delete data[idName];
+ var self = this;
+ if (self.debug) {
+ console.log('create', model, data);
+ }
+ var idValue = self.getIdValue(model, data);
+ var idName = self.idName(model);
+
+ if (idValue === null) {
+ delete data[idName]; // Allow MongoDB to generate the id
+ } else {
+ var oid = ObjectID(idValue); // Is it an Object ID?
+ data._id = oid; // Set it to _id
+ delete data[idName];
+ }
+ this.collection(model).insert(data, {safe: true}, function (err, m) {
+ if (self.debug) {
+ console.log('create.callback', model, err, m);
}
- this.collection(model).insert(data, {safe: true}, function (err, m) {
- if(self.debug) {
- console.log('create.callback', model, err, m);
- }
- callback(err, err ? null : m[0]._id);
- });
+ callback(err, err ? null : m[0]._id);
+ });
};
/**
@@ -174,25 +172,25 @@ MongoDB.prototype.create = function (model, data, callback) {
* @param {Function} [callback] The callback function
*/
MongoDB.prototype.save = function (model, data, callback) {
- var self = this;
- if(self.debug) {
- console.log('save', model, data);
- }
- var idValue = self.getIdValue(model, data);
- var idName = self.idName(model);
+ var self = this;
+ if (self.debug) {
+ console.log('save', model, data);
+ }
+ var idValue = self.getIdValue(model, data);
+ var idName = self.idName(model);
- var oid = ObjectID(idValue);
- delete data[idName];
+ var oid = ObjectID(idValue);
+ delete data[idName];
- this.collection(model).update({_id: oid}, data, {safe: true, upsert: true}, function (err, result) {
- if(!err) {
- self.setIdValue(model, data, idValue);
- }
- if(self.debug) {
- console.log('save.callback', model, err, result);
- }
- callback && callback(err, result);
- });
+ this.collection(model).update({_id: oid}, data, {safe: true, upsert: true}, function (err, result) {
+ if (!err) {
+ self.setIdValue(model, data, idValue);
+ }
+ if (self.debug) {
+ console.log('save.callback', model, err, result);
+ }
+ callback && callback(err, result);
+ });
};
/**
@@ -203,17 +201,17 @@ MongoDB.prototype.save = function (model, data, callback) {
*
*/
MongoDB.prototype.exists = function (model, id, callback) {
- var self = this;
- if(self.debug) {
- console.log('exists', model, id);
- }
- id = ObjectID(id);
- this.collection(model).findOne({_id: id}, function (err, data) {
- if(self.debug) {
- console.log('exists.callback', model, id, err, data);
- }
- callback(err, !!(!err && data));
- });
+ var self = this;
+ if (self.debug) {
+ console.log('exists', model, id);
+ }
+ id = ObjectID(id);
+ this.collection(model).findOne({_id: id}, function (err, data) {
+ if (self.debug) {
+ console.log('exists.callback', model, id, err, data);
+ }
+ callback(err, !!(!err && data));
+ });
};
/**
@@ -223,17 +221,17 @@ MongoDB.prototype.exists = function (model, id, callback) {
* @param {Function} [callback] The callback function
*/
MongoDB.prototype.find = function find(model, id, callback) {
- var self = this;
- if(self.debug) {
- console.log('find', model, id);
- }
- var oid = ObjectID(id);
- this.collection(model).findOne({_id: oid}, function (err, data) {
- if(self.debug) {
- console.log('find.callback', model, id, err, data);
- }
- callback && callback(err, data);
- });
+ var self = this;
+ if (self.debug) {
+ console.log('find', model, id);
+ }
+ var oid = ObjectID(id);
+ this.collection(model).findOne({_id: oid}, function (err, data) {
+ if (self.debug) {
+ console.log('find.callback', model, id, err, data);
+ }
+ callback && callback(err, data);
+ });
};
/**
@@ -244,36 +242,36 @@ MongoDB.prototype.find = function find(model, id, callback) {
* @param {Function} [callback] The callback function
*/
MongoDB.prototype.updateOrCreate = function updateOrCreate(model, data, callback) {
- var self = this;
- if(self.debug) {
- console.log('updateOrCreate', model, data);
- }
-
- var idValue = self.getIdValue(model, data);
-
- if (idValue === null || idValue === undefined) {
- return this.create(data, callback);
- }
- this.find(model, idValue, function (err, inst) {
+ var self = this;
+ if (self.debug) {
+ console.log('updateOrCreate', model, data);
+ }
+
+ var idValue = self.getIdValue(model, data);
+
+ if (idValue === null || idValue === undefined) {
+ return this.create(data, callback);
+ }
+ this.find(model, idValue, function (err, inst) {
+ if (err) {
+ return callback(err);
+ }
+ if (inst) {
+ self.updateAttributes(model, idValue, data, callback);
+ } else {
+ self.create(model, data, function (err, id) {
if (err) {
- return callback(err);
+ return callback(err);
}
- if (inst) {
- self.updateAttributes(model, idValue, data, callback);
+ if (id) {
+ self.setIdValue(model, data, id);
+ callback(null, data);
} else {
- self.create(model, data, function (err, id) {
- if (err) {
- return callback(err);
- }
- if (id) {
- self.setIdValue(model, data, id);
- callback(null, data);
- } else{
- callback(null, null); // wtf?
- }
- });
+ callback(null, null); // wtf?
}
- });
+ });
+ }
+ });
};
/**
@@ -283,17 +281,17 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate(model, data, callback
* @param [callback] The callback function
*/
MongoDB.prototype.destroy = function destroy(model, id, callback) {
- var self = this;
- if(self.debug) {
- console.log('delete', model, id);
- }
- id = ObjectID(id);
- this.collection(model).remove({_id: id}, function(err, result) {
- if(self.debug) {
- console.log('delete.callback', model, id, err, result);
- }
- callback && callback(err, result);
- });
+ var self = this;
+ if (self.debug) {
+ console.log('delete', model, id);
+ }
+ id = ObjectID(id);
+ this.collection(model).remove({_id: id}, function (err, result) {
+ if (self.debug) {
+ console.log('delete.callback', model, id, err, result);
+ }
+ callback && callback(err, result);
+ });
};
/*!
@@ -303,24 +301,24 @@ MongoDB.prototype.destroy = function destroy(model, id, callback) {
* @private
*/
function idIncluded(fields, idName) {
- if(!fields) {
- return true;
- }
- if(Array.isArray(fields)) {
- return fields.indexOf(idName) >= 0;
- }
- if(fields[idName]) {
- // Included
- return true;
- }
- if((idName in fields) && !fields[idName]) {
- // Excluded
- return false;
- }
- for(var f in fields) {
- return !fields[f]; // If the fields has exclusion
- }
+ if (!fields) {
return true;
+ }
+ if (Array.isArray(fields)) {
+ return fields.indexOf(idName) >= 0;
+ }
+ if (fields[idName]) {
+ // Included
+ return true;
+ }
+ if ((idName in fields) && !fields[idName]) {
+ // Excluded
+ return false;
+ }
+ for (var f in fields) {
+ return !fields[f]; // If the fields has exclusion
+ }
+ return true;
}
/**
@@ -331,107 +329,107 @@ function idIncluded(fields, idName) {
* @param {Function} [callback] The callback function
*/
MongoDB.prototype.all = function all(model, filter, callback) {
- var self = this;
- if(self.debug) {
- console.log('all', model, filter);
- }
- if (!filter) {
- filter = {};
- }
- var idName = self.idName(model);
- var query = {};
- if (filter.where) {
- if (filter.where[idName]) {
- var id = filter.where[idName];
- delete filter.where[idName];
- id = ObjectID(id);
- filter.where._id = id;
- }
- Object.keys(filter.where).forEach(function (k) {
- var cond = filter.where[k];
- var spec = false;
- if (cond && cond.constructor.name === 'Object') {
- spec = Object.keys(cond)[0];
- cond = cond[spec];
- }
- if (spec) {
- if (spec === 'between') {
- query[k] = { $gte: cond[0], $lte: cond[1]};
- } else if (spec === 'inq') {
- query[k] = { $in: cond.map(function(x) {
- if ('string' !== typeof x) return x;
- return ObjectID(x);
- })};
- } else {
- query[k] = {};
- query[k]['$' + spec] = cond;
- }
- } else {
- if (cond === null) {
- query[k] = {$type: 10};
- } else {
- query[k] = cond;
- }
- }
- });
- }
- var fields = filter.fields;
- var cursor = null;
- if(fields) {
- cursor = this.collection(model).find(query, fields);
- } else {
- cursor = this.collection(model).find(query);
- }
-
- if (filter.order) {
- var keys = filter.order;
- if (typeof keys === 'string') {
- keys = keys.split(',');
- }
- var args = {};
- for (var index in keys) {
- var m = keys[index].match(/\s+(A|DE)SC$/);
- var key = keys[index];
- key = key.replace(/\s+(A|DE)SC$/, '').trim();
- if (m && m[1] === 'DE') {
- args[key] = -1;
- } else {
- args[key] = 1;
- }
- }
- cursor.sort(args);
- }
- if (filter.limit) {
- cursor.limit(filter.limit);
- }
- if (filter.skip) {
- cursor.skip(filter.skip);
- } else if (filter.offset) {
- cursor.skip(filter.offset);
- }
- cursor.toArray(function (err, data) {
- if(self.debug) {
- console.log('all', model, filter, err, data);
- }
- if (err) {
- return callback(err);
+ var self = this;
+ if (self.debug) {
+ console.log('all', model, filter);
+ }
+ if (!filter) {
+ filter = {};
+ }
+ var idName = self.idName(model);
+ var query = {};
+ if (filter.where) {
+ if (filter.where[idName]) {
+ var id = filter.where[idName];
+ delete filter.where[idName];
+ id = ObjectID(id);
+ filter.where._id = id;
+ }
+ Object.keys(filter.where).forEach(function (k) {
+ var cond = filter.where[k];
+ var spec = false;
+ if (cond && cond.constructor.name === 'Object') {
+ spec = Object.keys(cond)[0];
+ cond = cond[spec];
+ }
+ if (spec) {
+ if (spec === 'between') {
+ query[k] = { $gte: cond[0], $lte: cond[1]};
+ } else if (spec === 'inq') {
+ query[k] = { $in: cond.map(function (x) {
+ if ('string' !== typeof x) return x;
+ return ObjectID(x);
+ })};
+ } else {
+ query[k] = {};
+ query[k]['$' + spec] = cond;
}
- var objs = data.map(function (o) {
- if(idIncluded(fields, self.idName(model))) {
- self.setIdValue(model, o, o._id);
- }
- // Don't pass back _id if the fields is set
- if(fields) {
- delete o._id;
- }
- return o;
- });
- if (filter && filter.include) {
- self._models[model].model.include(objs, filter.include, callback);
+ } else {
+ if (cond === null) {
+ query[k] = {$type: 10};
} else {
- callback(null, objs);
+ query[k] = cond;
}
+ }
+ });
+ }
+ var fields = filter.fields;
+ var cursor = null;
+ if (fields) {
+ cursor = this.collection(model).find(query, fields);
+ } else {
+ cursor = this.collection(model).find(query);
+ }
+
+ if (filter.order) {
+ var keys = filter.order;
+ if (typeof keys === 'string') {
+ keys = keys.split(',');
+ }
+ var args = {};
+ for (var index in keys) {
+ var m = keys[index].match(/\s+(A|DE)SC$/);
+ var key = keys[index];
+ key = key.replace(/\s+(A|DE)SC$/, '').trim();
+ if (m && m[1] === 'DE') {
+ args[key] = -1;
+ } else {
+ args[key] = 1;
+ }
+ }
+ cursor.sort(args);
+ }
+ if (filter.limit) {
+ cursor.limit(filter.limit);
+ }
+ if (filter.skip) {
+ cursor.skip(filter.skip);
+ } else if (filter.offset) {
+ cursor.skip(filter.offset);
+ }
+ cursor.toArray(function (err, data) {
+ if (self.debug) {
+ console.log('all', model, filter, err, data);
+ }
+ if (err) {
+ return callback(err);
+ }
+ var objs = data.map(function (o) {
+ if (idIncluded(fields, self.idName(model))) {
+ self.setIdValue(model, o, o._id);
+ }
+ // Don't pass back _id if the fields is set
+ if (fields) {
+ delete o._id;
+ }
+ return o;
});
+ if (filter && filter.include) {
+ self._models[model].model.include(objs, filter.include, callback);
+ } else {
+ callback(null, objs);
+ }
+ });
};
/**
@@ -441,20 +439,20 @@ MongoDB.prototype.all = function all(model, filter, callback) {
* @param {Function} [callback] The callback function
*/
MongoDB.prototype.destroyAll = function destroyAll(model, where, callback) {
- var self = this;
- if(self.debug) {
- console.log('destroyAll', model, where);
- }
- if(!callback && 'function' === typeof where) {
- callback = where;
- where = undefined;
- }
- this.collection(model).remove(where || {}, function (err, result) {
- if(self.debug) {
- console.log('destroyAll.callback', model, where, err, result);
- }
- callback && callback(err, result);
- });
+ var self = this;
+ if (self.debug) {
+ console.log('destroyAll', model, where);
+ }
+ if (!callback && 'function' === typeof where) {
+ callback = where;
+ where = undefined;
+ }
+ this.collection(model).remove(where || {}, function (err, result) {
+ if (self.debug) {
+ console.log('destroyAll.callback', model, where, err, result);
+ }
+ callback && callback(err, result);
+ });
};
/**
@@ -466,16 +464,16 @@ MongoDB.prototype.destroyAll = function destroyAll(model, where, callback) {
*
*/
MongoDB.prototype.count = function count(model, callback, where) {
- var self = this;
- if(self.debug) {
- console.log('count', model, where);
- }
- this.collection(model).count(where, function (err, count) {
- if(self.debug) {
- console.log('count.callback', model, err, count);
- }
- callback && callback(err, count);
- });
+ var self = this;
+ if (self.debug) {
+ console.log('count', model, where);
+ }
+ this.collection(model).count(where, function (err, count) {
+ if (self.debug) {
+ console.log('count.callback', model, err, count);
+ }
+ callback && callback(err, count);
+ });
};
/**
@@ -485,34 +483,36 @@ MongoDB.prototype.count = function count(model, callback, where) {
* @param {Function} [callback] The callback function
*/
MongoDB.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
- var debug = this.debug;
- if(debug) {
- console.log('updateAttributes', model, id, data);
- }
- var oid = ObjectID(id);
- delete data[this.idName(model)];
-
- this.collection(model).findAndModify({_id: oid}, [['_id','asc']], {$set: data}, {}, function(err, object) {
- if(debug) {
- console.log('updateAttributes.callback', model, id, err, object);
- }
- if(!err && !object) {
- // No result
- err = 'No '+ model +' found for id ' + id;
- }
- cb && cb(err, object);
- });
+ var debug = this.debug;
+ if (debug) {
+ console.log('updateAttributes', model, id, data);
+ }
+ var oid = ObjectID(id);
+ delete data[this.idName(model)];
+
+ this.collection(model).findAndModify({_id: oid}, [
+ ['_id', 'asc']
+ ], {$set: data}, {}, function (err, object) {
+ if (debug) {
+ console.log('updateAttributes.callback', model, id, err, object);
+ }
+ if (!err && !object) {
+ // No result
+ err = 'No ' + model + ' found for id ' + id;
+ }
+ cb && cb(err, object);
+ });
};
/**
* Disconnect from MongoDB
*/
MongoDB.prototype.disconnect = function () {
- if(this.debug) {
- console.log('disconnect');
- }
- if(this.db) {
- this.db.close();
- }
+ if (this.debug) {
+ console.log('disconnect');
+ }
+ if (this.db) {
+ this.db.close();
+ }
};
View
128 test/id.test.js
@@ -2,88 +2,88 @@ require('./init.js');
var ds = getDataSource();
describe('mongodb custom id name', function () {
- var Customer = ds.createModel('customer', {seq: {type: Number, id: true}, name: String, emails: [String], age: Number});
+ var Customer = ds.createModel('customer', {seq: {type: Number, id: true}, name: String, emails: [String], age: Number});
- before(function(done) {
- Customer.deleteAll(done);
- });
+ before(function (done) {
+ Customer.deleteAll(done);
+ });
- it('should allow custom name for the id property for create', function (done) {
- Customer.create({
- seq: 1,
- name: 'John1',
- emails: ['john@x.com', 'john@y.com'],
- age: 30
- }, function (err, customer) {
- customer.seq.should.equal(1);
- done(err, customer);
- });
+ it('should allow custom name for the id property for create', function (done) {
+ Customer.create({
+ seq: 1,
+ name: 'John1',
+ emails: ['john@x.com', 'john@y.com'],
+ age: 30
+ }, function (err, customer) {
+ customer.seq.should.equal(1);
+ done(err, customer);
});
+ });
- it('should allow custom name for the id property for findById', function (done) {
- Customer.findById(1, function (err, customer) {
- customer.seq.should.equal(1);
- done(err, customer);
- });
+ it('should allow custom name for the id property for findById', function (done) {
+ Customer.findById(1, function (err, customer) {
+ customer.seq.should.equal(1);
+ done(err, customer);
});
+ });
- it('should generate id value for create', function (done) {
- Customer.create({
- name: 'John1',
- emails: ['john@x.com', 'john@y.com'],
- age: 30
- }, function (err, customer) {
- // console.log(customer);
- customer.should.have.property('seq');
- Customer.findById(customer.seq, function (err, customer1) {
- customer1.seq.should.equal(customer.seq);
- done(err, customer);
- });
- });
+ it('should generate id value for create', function (done) {
+ Customer.create({
+ name: 'John1',
+ emails: ['john@x.com', 'john@y.com'],
+ age: 30
+ }, function (err, customer) {
+ // console.log(customer);
+ customer.should.have.property('seq');
+ Customer.findById(customer.seq, function (err, customer1) {
+ customer1.seq.should.equal(customer.seq);
+ done(err, customer);
+ });
});
+ });
});
describe('mongodb default id name', function () {
- var Customer1 = ds.createModel('customer1', {name: String, emails: [String], age: Number});
+ var Customer1 = ds.createModel('customer1', {name: String, emails: [String], age: Number});
- before(function(done) {
- Customer1.deleteAll(done);
- });
+ before(function (done) {
+ Customer1.deleteAll(done);
+ });
- it('should allow value for the id property for create', function (done) {
- Customer1.create({
- id: 1,
- name: 'John1',
- emails: ['john@x.com', 'john@y.com'],
- age: 30
- }, function (err, customer) {
- customer.id.should.equal(1);
- done(err, customer);
- });
+ it('should allow value for the id property for create', function (done) {
+ Customer1.create({
+ id: 1,
+ name: 'John1',
+ emails: ['john@x.com', 'john@y.com'],
+ age: 30
+ }, function (err, customer) {
+ customer.id.should.equal(1);
+ done(err, customer);
});
+ });
- it('should allow value the id property for findById', function (done) {
- Customer1.findById(1, function (err, customer) {
- customer.id.should.equal(1);
- done(err, customer);
- });
+ it('should allow value the id property for findById', function (done) {
+ Customer1.findById(1, function (err, customer) {
+ customer.id.should.equal(1);
+ done(err, customer);
});
+ });
- it('should generate id value for create', function (done) {
- Customer1.create({
- name: 'John1',
- emails: ['john@x.com', 'john@y.com'],
- age: 30
- }, function (err, customer) {
- // console.log(customer);
- customer.should.have.property('id');
- Customer1.findById(customer.id, function (err, customer1) {
- customer1.id.should.equal(customer.id);
- done(err, customer);
- });
- });
+ it('should generate id value for create', function (done) {
+ Customer1.create({
+ name: 'John1',
+ emails: ['john@x.com', 'john@y.com'],
+ age: 30
+ }, function (err, customer) {
+ // console.log(customer);
+ customer.should.have.property('id');
+ Customer1.findById(customer.id, function (err, customer1) {
+ customer1.id.should.equal(customer.id);
+ done(err, customer);
+ });
});
+ });
});
View
12 test/imported.test.js
@@ -1,10 +1,10 @@
-describe('mongodb imported features', function() {
+describe('mongodb imported features', function () {
- before(function() {
- require('./init.js');
- });
+ before(function () {
+ require('./init.js');
+ });
- require('loopback-datasource-juggler/test/common.batch.js');
- // require('loopback-datasource-juggler/test/include.test.js');
+ require('loopback-datasource-juggler/test/common.batch.js');
+ // require('loopback-datasource-juggler/test/include.test.js');
});
View
12 test/init.js
@@ -2,11 +2,13 @@ module.exports = require('should');
var DataSource = require('loopback-datasource-juggler').DataSource;
-var config = require('rc')('loopback', {test: {mongodb:{}}}).test.mongodb;
+var config = require('rc')('loopback', {test: {mongodb: {}}}).test.mongodb;
-global.getDataSource = global.getSchema = function() {
- var db = new DataSource(require('../'), config);
- db.log = function (a) { console.log(a); };
+global.getDataSource = global.getSchema = function () {
+ var db = new DataSource(require('../'), config);
+ db.log = function (a) {
+ console.log(a);
+ };
- return db;
+ return db;
};
View
174 test/mongodb.test.js
@@ -3,112 +3,112 @@ var should = require('./init.js');
var User, Post, PostWithStringId, db;
-describe('mongodb', function(){
+describe('mongodb', function () {
- before(function() {
- db = getDataSource();
+ before(function () {
+ db = getDataSource();
- User = db.define('User', {
- name: { type: String, index: true },
- email: { type: String, index: true },
- age: Number,
- });
-
- Post = db.define('Post', {
- title: { type: String, length: 255, index: true },
- content: { type: String }
- });
-
- PostWithStringId = db.define('PostWithStringId', {
- id: {type: String, id: true},
- title: { type: String, length: 255, index: true },
- content: { type: String }
- });
+ User = db.define('User', {
+ name: { type: String, index: true },
+ email: { type: String, index: true },
+ age: Number,
+ });
- User.hasMany(Post);
- Post.belongsTo(User);
+ Post = db.define('Post', {
+ title: { type: String, length: 255, index: true },
+ content: { type: String }
});
- beforeEach(function(done) {
- User.destroyAll(function() {
- Post.destroyAll(function() {
- done();
- });
- });
+ PostWithStringId = db.define('PostWithStringId', {
+ id: {type: String, id: true},
+ title: { type: String, length: 255, index: true },
+ content: { type: String }
});
- it('hasMany should support additional conditions', function (done) {
- User.create(function (e, u) {
- u.posts.create({}, function (e, p) {
- u.posts({where: {_id: p.id}}, function (err, posts) {
- should.not.exist(err);
- posts.should.have.lengthOf(1);
+ User.hasMany(Post);
+ Post.belongsTo(User);
+ });
- done();
- });
- });
- });
+ beforeEach(function (done) {
+ User.destroyAll(function () {
+ Post.destroyAll(function () {
+ done();
+ });
});
+ });
- it('should allow to find by id string', function (done) {
- Post.create(function (err, post) {
- Post.find(post.id.toString(), function (err, post) {
- should.not.exist(err);
- should.exist(post);
+ it('hasMany should support additional conditions', function (done) {
+ User.create(function (e, u) {
+ u.posts.create({}, function (e, p) {
+ u.posts({where: {_id: p.id}}, function (err, posts) {
+ should.not.exist(err);
+ posts.should.have.lengthOf(1);
- done();
- });
+ done();
});
+ });
});
+ });
- it('find should return an object with an id, which is instanceof ObjectId', function (done) {
- Post.create(function (err, post) {
- Post.findById(post.id, function (err, post) {
- should.not.exist(err);
- post.id.should.be.an.instanceOf(db.ObjectID);
- post._id.should.be.an.instanceOf(db.ObjectID);
+ it('should allow to find by id string', function (done) {
+ Post.create(function (err, post) {
+ Post.find(post.id.toString(), function (err, post) {
+ should.not.exist(err);
+ should.exist(post);
- done();
- });
-
- });
+ done();
+ });
});
+ });
- it('all should return object with an id, which is instanceof ObjectID', function (done) {
- var post = new Post({title: 'a', content: 'AAA'})
- post.save(function (err, post) {
- Post.all({where: {title: 'a'}}, function (err, posts) {
- should.not.exist(err);
- posts.should.have.lengthOf(1);
- post = posts[0];
- post.should.have.property('title', 'a');
- post.should.have.property('content', 'AAA');
- post.id.should.be.an.instanceOf(db.ObjectID);
- post._id.should.be.an.instanceOf(db.ObjectID);
-
- done();
- });
+ it('find should return an object with an id, which is instanceof ObjectId', function (done) {
+ Post.create(function (err, post) {
+ Post.findById(post.id, function (err, post) {
+ should.not.exist(err);
+ post.id.should.be.an.instanceOf(db.ObjectID);
+ post._id.should.be.an.instanceOf(db.ObjectID);
+
+ done();
+ });
- });
});
+ });
+
+ it('all should return object with an id, which is instanceof ObjectID', function (done) {
+ var post = new Post({title: 'a', content: 'AAA'})
+ post.save(function (err, post) {
+ Post.all({where: {title: 'a'}}, function (err, posts) {
+ should.not.exist(err);
+ posts.should.have.lengthOf(1);
+ post = posts[0];
+ post.should.have.property('title', 'a');
+ post.should.have.property('content', 'AAA');
+ post.id.should.be.an.instanceOf(db.ObjectID);
+ post._id.should.be.an.instanceOf(db.ObjectID);
+
+ done();
+ });
- it('all should return honor filter.fields', function (done) {
- var post = new Post({title: 'b', content: 'BBB'})
- post.save(function (err, post) {
- Post.all({fields: ['title'], where: {title: 'b'}}, function (err, posts) {
- should.not.exist(err);
- posts.should.have.lengthOf(1);
- post = posts[0];
- post.should.have.property('title', 'b');
- post.should.not.have.property('content');
- done();
- });
+ });
+ });
+
+ it('all should return honor filter.fields', function (done) {
+ var post = new Post({title: 'b', content: 'BBB'})
+ post.save(function (err, post) {
+ Post.all({fields: ['title'], where: {title: 'b'}}, function (err, posts) {
+ should.not.exist(err);
+ posts.should.have.lengthOf(1);
+ post = posts[0];
+ post.should.have.property('title', 'b');
+ post.should.not.have.property('content');
+ done();
+ });
- });
});
+ });
- it('create should convert id from string to ObjectID if format matches',
- function(done) {
+ it('create should convert id from string to ObjectID if format matches',
+ function (done) {
var oid = new db.ObjectID().toString();
PostWithStringId.create({id: oid, title: 'c', content: 'CCC'}, function (err, post) {
PostWithStringId.findById(oid, function (err, post) {
@@ -119,10 +119,10 @@ describe('mongodb', function(){
});
});
- after(function(done){
- User.destroyAll(function(){
- Post.destroyAll(done);
- });
+ after(function (done) {
+ User.destroyAll(function () {
+ Post.destroyAll(done);
});
+ });
});
View
36 test/objectid.test.js
@@ -2,27 +2,27 @@ require('./init.js');
var db, Book, Chapter;
-describe('ObjectID', function() {
+describe('ObjectID', function () {
- before(function() {
- db = getDataSource();
- Book = db.define('Book');
- Chapter = db.define('Chapter');
- Book.hasMany('chapters');
- Chapter.belongsTo('book');
- });
-
- it('should cast foreign keys as ObjectID', function(done) {
+ before(function () {
+ db = getDataSource();
+ Book = db.define('Book');
+ Chapter = db.define('Chapter');
+ Book.hasMany('chapters');
+ Chapter.belongsTo('book');
+ });
- Chapter.beforeCreate = function(next, data) {
- data.bookId.should.be.an.instanceOf(db.ObjectID);
- this.bookId.should.be.an.instanceOf(db.ObjectID);
- next();
- };
+ it('should cast foreign keys as ObjectID', function (done) {
- Book.create(function(err, book) {
- Chapter.create({bookId: book.id.toString()}, done);
- });
+ Chapter.beforeCreate = function (next, data) {
+ data.bookId.should.be.an.instanceOf(db.ObjectID);
+ this.bookId.should.be.an.instanceOf(db.ObjectID);
+ next();
+ };
+ Book.create(function (err, book) {
+ Chapter.create({bookId: book.id.toString()}, done);
});
+
+ });
});

0 comments on commit 2790056

Please sign in to comment.