Backport ESLint from master
This commit is contained in:
parent
ee6c0ad461
commit
65d6d6a508
|
@ -0,0 +1,2 @@
|
||||||
|
coverage
|
||||||
|
support
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"extends": "loopback",
|
||||||
|
"rules": {
|
||||||
|
"max-len": ["error", 110, 4, {
|
||||||
|
"ignoreComments": true,
|
||||||
|
"ignoreUrls": true,
|
||||||
|
"ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)"
|
||||||
|
}],
|
||||||
|
// NOTE(bajtos) we should eventuall remove this override
|
||||||
|
// and fix all of those 100+ violations
|
||||||
|
"one-var": "off"
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,6 @@ var User, Post, Passport, City, Street, Building;
|
||||||
var nbSchemaRequests = 0;
|
var nbSchemaRequests = 0;
|
||||||
|
|
||||||
setup(function() {
|
setup(function() {
|
||||||
|
|
||||||
Passport.find({ include: 'owner' }, function(err, passports) {
|
Passport.find({ include: 'owner' }, function(err, passports) {
|
||||||
console.log('passports.owner', passports);
|
console.log('passports.owner', passports);
|
||||||
});
|
});
|
||||||
|
@ -31,7 +30,6 @@ setup(function() {
|
||||||
User.find({ include: ['posts', 'passports'] }, function(err, users) {
|
User.find({ include: ['posts', 'passports'] }, function(err, users) {
|
||||||
console.log('users.passports && users.posts', users);
|
console.log('users.passports && users.posts', users);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function setup(done) {
|
function setup(done) {
|
||||||
|
@ -108,7 +106,6 @@ function setup(done) {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,6 @@ function loadSchemasSync(schemaFile, dataSource) {
|
||||||
var schemas = JSON.parse(fs.readFileSync(schemaFile));
|
var schemas = JSON.parse(fs.readFileSync(schemaFile));
|
||||||
|
|
||||||
return dataSource.buildModels(schemas);
|
return dataSource.buildModels(schemas);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var models = loadSchemasSync(path.join(__dirname, 'jdb-schemas.json'));
|
var models = loadSchemasSync(path.join(__dirname, 'jdb-schemas.json'));
|
||||||
|
|
|
@ -29,7 +29,14 @@ var User = modelBuilder.define('User', {
|
||||||
friends: [String],
|
friends: [String],
|
||||||
});
|
});
|
||||||
|
|
||||||
var user = new User({ name: 'Joe', age: 20, address: { street: '123 Main St', 'city': 'San Jose', state: 'CA' },
|
var user = new User({
|
||||||
|
name: 'Joe',
|
||||||
|
age: 20,
|
||||||
|
address: {
|
||||||
|
street: '123 Main St',
|
||||||
|
city: 'San Jose',
|
||||||
|
state: 'CA',
|
||||||
|
},
|
||||||
emails: [
|
emails: [
|
||||||
{ label: 'work', email: 'xyz@sample.com' },
|
{ label: 'work', email: 'xyz@sample.com' },
|
||||||
],
|
],
|
||||||
|
|
|
@ -33,7 +33,6 @@ Customer.create({ name: 'John' }, function(err, customer) {
|
||||||
});
|
});
|
||||||
|
|
||||||
Order.create({ orderDate: new Date(), items: ['Phone'] }, function(err, order) {
|
Order.create({ orderDate: new Date(), items: ['Phone'] }, function(err, order) {
|
||||||
|
|
||||||
order.customer.create({ name: 'Smith' }, function(err, customer2) {
|
order.customer.create({ name: 'Smith' }, function(err, customer2) {
|
||||||
console.log(order, customer2);
|
console.log(order, customer2);
|
||||||
order.save(function(err, order) {
|
order.save(function(err, order) {
|
||||||
|
@ -90,26 +89,41 @@ Physician.create({ name: 'Dr John' }, function(err, physician1) {
|
||||||
Physician.create({ name: 'Dr Smith' }, function(err, physician2) {
|
Physician.create({ name: 'Dr Smith' }, function(err, physician2) {
|
||||||
Patient.create({ name: 'Mary' }, function(err, patient1) {
|
Patient.create({ name: 'Mary' }, function(err, patient1) {
|
||||||
Patient.create({ name: 'Ben' }, function(err, patient2) {
|
Patient.create({ name: 'Ben' }, function(err, patient2) {
|
||||||
Appointment.create({ appointmentDate: new Date(), physicianId: physician1.id, patientId: patient1.id },
|
Appointment.create({
|
||||||
function(err, appt1) {
|
appointmentDate: new Date(),
|
||||||
Appointment.create({ appointmentDate: new Date(), physicianId: physician1.id, patientId: patient2.id },
|
physicianId: physician1.id,
|
||||||
function(err, appt2) {
|
patientId: patient1.id,
|
||||||
physician1.patients(console.log);
|
}, function(err, appt1) {
|
||||||
physician1.patients({ where: { name: 'Mary' }}, console.log);
|
Appointment.create({
|
||||||
patient1.physicians(console.log);
|
appointmentDate: new Date(),
|
||||||
|
physicianId: physician1.id,
|
||||||
|
patientId: patient2.id,
|
||||||
|
}, function(err, appt2) {
|
||||||
|
physician1.patients(console.log);
|
||||||
|
physician1.patients({ where: { name: 'Mary' }}, console.log);
|
||||||
|
patient1.physicians(console.log);
|
||||||
|
|
||||||
// Build an appointment?
|
// Build an appointment?
|
||||||
var patient3 = patient1.physicians.build({ name: 'Dr X' });
|
var patient3 = patient1.physicians.build({ name: 'Dr X' });
|
||||||
console.log('Physician 3: ', patient3, patient3.constructor.modelName);
|
console.log(
|
||||||
|
'Physician 3: ',
|
||||||
|
patient3,
|
||||||
|
patient3.constructor.modelName
|
||||||
|
);
|
||||||
|
|
||||||
// Create a physician?
|
// Create a physician?
|
||||||
patient1.physicians.create({ name: 'Dr X' }, function(err, patient4) {
|
patient1.physicians.create(
|
||||||
console.log('Physician 4: ', patient4, patient4.constructor.modelName);
|
{ name: 'Dr X' },
|
||||||
});
|
function(err, patient4) {
|
||||||
|
console.log(
|
||||||
|
'Physician 4: ',
|
||||||
});
|
patient4,
|
||||||
|
patient4.constructor.modelName
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -146,7 +160,6 @@ Assembly.create({ name: 'car' }, function(err, assembly) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
37
lib/dao.js
37
lib/dao.js
|
@ -67,9 +67,9 @@ function convertSubsetOfPropertiesByType(inst, data) {
|
||||||
for (var key in data) {
|
for (var key in data) {
|
||||||
// Convert the properties by type
|
// Convert the properties by type
|
||||||
typedData[key] = inst[key];
|
typedData[key] = inst[key];
|
||||||
if (typeof typedData[key] === 'object'
|
if (typeof typedData[key] === 'object' &&
|
||||||
&& typedData[key] !== null
|
typedData[key] !== null &&
|
||||||
&& typeof typedData[key].toObject === 'function') {
|
typeof typedData[key].toObject === 'function') {
|
||||||
typedData[key] = typedData[key].toObject();
|
typedData[key] = typedData[key].toObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -907,7 +907,6 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
|
||||||
}
|
}
|
||||||
if (!err) Model.emit('changed', obj);
|
if (!err) Model.emit('changed', obj);
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (cb.promise) {
|
if (cb.promise) {
|
||||||
cb(err, [obj, created]);
|
cb(err, [obj, created]);
|
||||||
|
@ -924,7 +923,7 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
|
||||||
where: query.where,
|
where: query.where,
|
||||||
data: data,
|
data: data,
|
||||||
isNewInstance: true,
|
isNewInstance: true,
|
||||||
currentInstance : currentInstance,
|
currentInstance: currentInstance,
|
||||||
hookState: hookState,
|
hookState: hookState,
|
||||||
options: options,
|
options: options,
|
||||||
};
|
};
|
||||||
|
@ -1748,8 +1747,7 @@ DataAccessObject.find = function find(query, options, cb) {
|
||||||
|
|
||||||
cb(err, results);
|
cb(err, results);
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
cb(err, data || []);
|
cb(err, data || []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1834,7 +1832,9 @@ DataAccessObject.findOne = function findOne(query, options, cb) {
|
||||||
* @param {Object) [options] Options
|
* @param {Object) [options] Options
|
||||||
* @param {Function} [cb] Callback called with (err, info)
|
* @param {Function} [cb] Callback called with (err, info)
|
||||||
*/
|
*/
|
||||||
DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyAll = function destroyAll(where, options, cb) {
|
DataAccessObject.remove =
|
||||||
|
DataAccessObject.deleteAll =
|
||||||
|
DataAccessObject.destroyAll = function destroyAll(where, options, cb) {
|
||||||
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
|
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
|
||||||
if (connectionPromise) {
|
if (connectionPromise) {
|
||||||
return connectionPromise;
|
return connectionPromise;
|
||||||
|
@ -1927,7 +1927,6 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
|
||||||
} else {
|
} else {
|
||||||
connector.destroyAll(Model.modelName, where, done);
|
connector.destroyAll(Model.modelName, where, done);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function done(err, info) {
|
function done(err, info) {
|
||||||
|
@ -1968,8 +1967,14 @@ function whereIsEmpty(where) {
|
||||||
// [FIXME] rfeng: This is a hack to set up 'deleteById' first so that
|
// [FIXME] rfeng: This is a hack to set up 'deleteById' first so that
|
||||||
// 'deleteById' will be used as the name for strong-remoting to keep it backward
|
// 'deleteById' will be used as the name for strong-remoting to keep it backward
|
||||||
// compatible for angular SDK
|
// compatible for angular SDK
|
||||||
DataAccessObject.removeById = DataAccessObject.destroyById = DataAccessObject.deleteById = function deleteById(id, options, cb) {
|
DataAccessObject.removeById =
|
||||||
var connectionPromise = stillConnecting(this.getDataSource(), this, arguments);
|
DataAccessObject.destroyById =
|
||||||
|
DataAccessObject.deleteById = function deleteById(id, options, cb) {
|
||||||
|
var connectionPromise = stillConnecting(
|
||||||
|
this.getDataSource(),
|
||||||
|
this,
|
||||||
|
arguments
|
||||||
|
);
|
||||||
if (connectionPromise) {
|
if (connectionPromise) {
|
||||||
return connectionPromise;
|
return connectionPromise;
|
||||||
}
|
}
|
||||||
|
@ -2246,7 +2251,6 @@ DataAccessObject.prototype.save = function(options, cb) {
|
||||||
connector.save(modelName, inst.constructor._forDB(data), saveCallback);
|
connector.save(modelName, inst.constructor._forDB(data), saveCallback);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}, data, cb);
|
}, data, cb);
|
||||||
}, data, cb);
|
}, data, cb);
|
||||||
}
|
}
|
||||||
|
@ -2291,7 +2295,6 @@ DataAccessObject.updateAll = function(where, data, options, cb) {
|
||||||
data = where;
|
data = where;
|
||||||
where = {};
|
where = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (cb === undefined) {
|
} else if (cb === undefined) {
|
||||||
// One of:
|
// One of:
|
||||||
// updateAll(where, data, options) -> Promise
|
// updateAll(where, data, options) -> Promise
|
||||||
|
@ -2714,7 +2717,7 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
|
||||||
Model: Model,
|
Model: Model,
|
||||||
hookState: hookState,
|
hookState: hookState,
|
||||||
data: context.data,
|
data: context.data,
|
||||||
isNewInstance:false,
|
isNewInstance: false,
|
||||||
options: options,
|
options: options,
|
||||||
};
|
};
|
||||||
Model.notifyObserversOf('loaded', ctx, function(err) {
|
Model.notifyObserversOf('loaded', ctx, function(err) {
|
||||||
|
@ -2742,7 +2745,7 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
|
||||||
Model: Model,
|
Model: Model,
|
||||||
where: byIdQuery(Model, id).where,
|
where: byIdQuery(Model, id).where,
|
||||||
data: context.data,
|
data: context.data,
|
||||||
isNewInstance:false,
|
isNewInstance: false,
|
||||||
currentInstance: inst,
|
currentInstance: inst,
|
||||||
hookState: hookState,
|
hookState: hookState,
|
||||||
options: options,
|
options: options,
|
||||||
|
@ -2807,8 +2810,8 @@ function(data, options, cb) {
|
||||||
if (isPKMissing(Model, cb))
|
if (isPKMissing(Model, cb))
|
||||||
return cb.promise;
|
return cb.promise;
|
||||||
|
|
||||||
var allowExtendedOperators = connector.settings
|
var allowExtendedOperators = connector.settings &&
|
||||||
&& connector.settings.allowExtendedOperators;
|
connector.settings.allowExtendedOperators;
|
||||||
|
|
||||||
var strict = this.__strict;
|
var strict = this.__strict;
|
||||||
var model = Model.modelName;
|
var model = Model.modelName;
|
||||||
|
|
|
@ -156,7 +156,6 @@ function DataSource(name, settings, modelBuilder) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(DataSource, EventEmitter);
|
util.inherits(DataSource, EventEmitter);
|
||||||
|
@ -314,22 +313,22 @@ DataSource.prototype.setup = function(name, settings) {
|
||||||
|
|
||||||
if (connector) {
|
if (connector) {
|
||||||
var postInit = function postInit(err, result) {
|
var postInit = function postInit(err, result) {
|
||||||
|
|
||||||
this._setupConnector();
|
this._setupConnector();
|
||||||
// we have an connector now?
|
// we have an connector now?
|
||||||
if (!this.connector) {
|
if (!this.connector) {
|
||||||
throw new Error('Connector is not defined correctly: it should create `connector` member of dataSource');
|
throw new Error('Connector is not defined correctly: it should ' +
|
||||||
|
'create `connector` member of dataSource');
|
||||||
}
|
}
|
||||||
this.connected = !err; // Connected now
|
this.connected = !err; // Connected now
|
||||||
if (this.connected) {
|
if (this.connected) {
|
||||||
this.emit('connected');
|
this.emit('connected');
|
||||||
} else {
|
} else {
|
||||||
// The connection fails, let's report it and hope it will be recovered in the next call
|
// The connection fails, let's report it and hope it will be recovered in the next call
|
||||||
console.error('Connection fails: ', err, '\nIt will be retried for the next request.');
|
console.error('Connection fails: ', err, '\nIt will be retried for ' +
|
||||||
|
'the next request.');
|
||||||
this.emit('error', err);
|
this.emit('error', err);
|
||||||
this.connecting = false;
|
this.connecting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -434,7 +433,6 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
|
||||||
modelClass[relation.type].call(modelClass, name, params);
|
modelClass[relation.type].call(modelClass, name, params);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
if (throughModel && !isModelDataSourceAttached(throughModel)) {
|
if (throughModel && !isModelDataSourceAttached(throughModel)) {
|
||||||
// Set up a listener to the through model
|
// Set up a listener to the through model
|
||||||
|
@ -480,8 +478,8 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
|
||||||
throughModel = isModelClass(r.through) ? r.through : self.getModel(r.through, true);
|
throughModel = isModelClass(r.through) ? r.through : self.getModel(r.through, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((targetModel && !isModelDataSourceAttached(targetModel))
|
if ((targetModel && !isModelDataSourceAttached(targetModel)) ||
|
||||||
|| (throughModel && !isModelDataSourceAttached(throughModel))) {
|
(throughModel && !isModelDataSourceAttached(throughModel))) {
|
||||||
// Create a listener to defer the relation set up
|
// Create a listener to defer the relation set up
|
||||||
createListener(rn, r, targetModel, throughModel);
|
createListener(rn, r, targetModel, throughModel);
|
||||||
} else {
|
} else {
|
||||||
|
@ -542,7 +540,6 @@ DataSource.prototype.setupDataAccess = function(modelClass, settings) {
|
||||||
// define scopes from LDL (options.relations)
|
// define scopes from LDL (options.relations)
|
||||||
var scopes = settings.scopes || {};
|
var scopes = settings.scopes || {};
|
||||||
this.defineScopes(modelClass, scopes);
|
this.defineScopes(modelClass, scopes);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -594,7 +591,9 @@ DataSource.prototype.setupDataAccess = function(modelClass, settings) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
DataSource.prototype.createModel = DataSource.prototype.define = function defineClass(className, properties, settings) {
|
DataSource.prototype.createModel =
|
||||||
|
DataSource.prototype.define = function defineClass(className, properties,
|
||||||
|
settings) {
|
||||||
var args = slice.call(arguments);
|
var args = slice.call(arguments);
|
||||||
|
|
||||||
if (!className) {
|
if (!className) {
|
||||||
|
@ -752,7 +751,6 @@ DataSource.prototype.attach = function(modelClass) {
|
||||||
this.setupDataAccess(modelClass, modelClass.settings);
|
this.setupDataAccess(modelClass, modelClass.settings);
|
||||||
modelClass.emit('dataSourceAttached', modelClass);
|
modelClass.emit('dataSourceAttached', modelClass);
|
||||||
return modelClass;
|
return modelClass;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1311,7 +1309,6 @@ DataSource.prototype.discoverSchemas = function(modelName, options, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel(tasks, function(err, results) {
|
async.parallel(tasks, function(err, results) {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
cb(err);
|
cb(err);
|
||||||
return cb.promise;
|
return cb.promise;
|
||||||
|
@ -1353,8 +1350,8 @@ DataSource.prototype.discoverSchemas = function(modelName, options, cb) {
|
||||||
var propName = nameMapper('column', item.columnName);
|
var propName = nameMapper('column', item.columnName);
|
||||||
schema.properties[propName] = {
|
schema.properties[propName] = {
|
||||||
type: item.type,
|
type: item.type,
|
||||||
required: (item.nullable === 'N' || item.nullable === 'NO'
|
required: (item.nullable === 'N' || item.nullable === 'NO' ||
|
||||||
|| item.nullable === 0 || item.nullable === false),
|
item.nullable === 0 || item.nullable === false),
|
||||||
length: item.dataLength,
|
length: item.dataLength,
|
||||||
precision: item.dataPrecision,
|
precision: item.dataPrecision,
|
||||||
scale: item.dataScale,
|
scale: item.dataScale,
|
||||||
|
@ -1589,7 +1586,6 @@ DataSource.prototype.discoverSchemasSync = function(modelName, options) {
|
||||||
self.discoverSchemasSync(otherTables[t].tableName, newOptions);
|
self.discoverSchemasSync(otherTables[t].tableName, newOptions);
|
||||||
}
|
}
|
||||||
return options.visited;
|
return options.visited;
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1671,7 +1667,6 @@ DataSource.prototype.discoverAndBuildModelsSync = function(modelName, options) {
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.buildModelFromInstance = function(name, json, options) {
|
DataSource.prototype.buildModelFromInstance = function(name, json, options) {
|
||||||
|
|
||||||
// Introspect the JSON document to generate a schema
|
// Introspect the JSON document to generate a schema
|
||||||
var schema = ModelBuilder.introspect(json);
|
var schema = ModelBuilder.introspect(json);
|
||||||
|
|
||||||
|
@ -1859,7 +1854,6 @@ DataSource.prototype.defineForeignKey = function defineForeignKey(className, key
|
||||||
// Add the foreign key property to the data source _models
|
// Add the foreign key property to the data source _models
|
||||||
this.defineProperty(className, key, fkDef);
|
this.defineProperty(className, key, fkDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1905,10 +1899,13 @@ DataSource.prototype.copyModel = function copyModel(Master) {
|
||||||
hiddenProperty(Slave, 'relations', Master.relations);
|
hiddenProperty(Slave, 'relations', Master.relations);
|
||||||
|
|
||||||
if (!(className in dataSource.modelBuilder.models)) {
|
if (!(className in dataSource.modelBuilder.models)) {
|
||||||
|
|
||||||
// store class in model pool
|
// store class in model pool
|
||||||
dataSource.modelBuilder.models[className] = Slave;
|
dataSource.modelBuilder.models[className] = Slave;
|
||||||
dataSource.modelBuilder.definitions[className] = new ModelDefinition(dataSource.modelBuilder, md.name, md.properties, md.settings);
|
dataSource.modelBuilder.definitions[className] = new ModelDefinition(
|
||||||
|
dataSource.modelBuilder,
|
||||||
|
md.name,
|
||||||
|
md.properties,
|
||||||
|
md.settings);
|
||||||
|
|
||||||
if ((!dataSource.isTransaction) && dataSource.connector && dataSource.connector.define) {
|
if ((!dataSource.isTransaction) && dataSource.connector && dataSource.connector.define) {
|
||||||
dataSource.connector.define({
|
dataSource.connector.define({
|
||||||
|
@ -1917,7 +1914,6 @@ DataSource.prototype.copyModel = function copyModel(Master) {
|
||||||
settings: md.settings,
|
settings: md.settings,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Slave;
|
return Slave;
|
||||||
|
|
11
lib/geo.js
11
lib/geo.js
|
@ -138,11 +138,17 @@ function GeoPoint(data) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(Array.isArray(data) || typeof data === 'object' || typeof data === 'string', 'must provide valid geo-coordinates array [lat, lng] or object or a "lat, lng" string');
|
assert(
|
||||||
|
Array.isArray(data) ||
|
||||||
|
typeof data === 'object' ||
|
||||||
|
typeof data === 'string',
|
||||||
|
'must provide valid geo-coordinates array [lat, lng] or object or a ' +
|
||||||
|
'"lat, lng" string');
|
||||||
|
|
||||||
if (typeof data === 'string') {
|
if (typeof data === 'string') {
|
||||||
data = data.split(/,\s*/);
|
data = data.split(/,\s*/);
|
||||||
assert(data.length === 2, 'must provide a string "lat,lng" creating a GeoPoint with a string');
|
assert(data.length === 2, 'must provide a string "lat,lng" creating a ' +
|
||||||
|
'GeoPoint with a string');
|
||||||
}
|
}
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
data = {
|
data = {
|
||||||
|
@ -259,7 +265,6 @@ var EARTH_RADIUS = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function geoDistance(x1, y1, x2, y2, options) {
|
function geoDistance(x1, y1, x2, y2, options) {
|
||||||
|
|
||||||
var type = (options && options.type) || 'miles';
|
var type = (options && options.type) || 'miles';
|
||||||
|
|
||||||
// Convert to radians
|
// Convert to radians
|
||||||
|
|
|
@ -36,10 +36,10 @@ Hookable.afterDestroy = null;
|
||||||
// TODO: Evaluate https://github.com/bnoguchi/hooks-js/
|
// TODO: Evaluate https://github.com/bnoguchi/hooks-js/
|
||||||
Hookable.prototype.trigger = function trigger(actionName, work, data, callback) {
|
Hookable.prototype.trigger = function trigger(actionName, work, data, callback) {
|
||||||
var capitalizedName = capitalize(actionName);
|
var capitalizedName = capitalize(actionName);
|
||||||
var beforeHook = this.constructor['before' + capitalizedName]
|
var beforeHook = this.constructor['before' + capitalizedName] ||
|
||||||
|| this.constructor['pre' + capitalizedName];
|
this.constructor['pre' + capitalizedName];
|
||||||
var afterHook = this.constructor['after' + capitalizedName]
|
var afterHook = this.constructor['after' + capitalizedName] ||
|
||||||
|| this.constructor['post' + capitalizedName];
|
this.constructor['post' + capitalizedName];
|
||||||
if (actionName === 'validate') {
|
if (actionName === 'validate') {
|
||||||
beforeHook = beforeHook || this.constructor.beforeValidation;
|
beforeHook = beforeHook || this.constructor.beforeValidation;
|
||||||
afterHook = afterHook || this.constructor.afterValidation;
|
afterHook = afterHook || this.constructor.afterValidation;
|
||||||
|
|
|
@ -198,16 +198,15 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
subInclude = null;
|
subInclude = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
relationName = include;
|
relationName = include;
|
||||||
subInclude = null;
|
subInclude = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var relation = relations[relationName];
|
var relation = relations[relationName];
|
||||||
if (!relation) {
|
if (!relation) {
|
||||||
cb(new Error('Relation "' + relationName + '" is not defined for '
|
cb(new Error('Relation "' + relationName + '" is not defined for ' +
|
||||||
+ self.modelName + ' model'));
|
self.modelName + ' model'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var polymorphic = relation.polymorphic;
|
var polymorphic = relation.polymorphic;
|
||||||
|
@ -251,8 +250,7 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
var fields = filter.fields;
|
var fields = filter.fields;
|
||||||
if (Array.isArray(fields) && fields.indexOf(relation.keyTo) === -1) {
|
if (Array.isArray(fields) && fields.indexOf(relation.keyTo) === -1) {
|
||||||
fields.push(relation.keyTo);
|
fields.push(relation.keyTo);
|
||||||
}
|
} else if (isPlainObject(fields) && !fields[relation.keyTo]) {
|
||||||
else if (isPlainObject(fields) && !fields[relation.keyTo]) {
|
|
||||||
fields[relation.keyTo] = true;
|
fields[relation.keyTo] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,8 +279,7 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
}
|
}
|
||||||
//assuming all other relations with multiple=true as hasMany
|
//assuming all other relations with multiple=true as hasMany
|
||||||
return includeHasMany(cb);
|
return includeHasMany(cb);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (polymorphic) {
|
if (polymorphic) {
|
||||||
if (relation.type === 'hasOne') {
|
if (relation.type === 'hasOne') {
|
||||||
return includePolymorphicHasOne(cb);
|
return includePolymorphicHasOne(cb);
|
||||||
|
@ -373,8 +370,7 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
//make sure that the modelToIdName is included if fields are specified
|
//make sure that the modelToIdName is included if fields are specified
|
||||||
if (Array.isArray(fields) && fields.indexOf(modelToIdName) === -1) {
|
if (Array.isArray(fields) && fields.indexOf(modelToIdName) === -1) {
|
||||||
fields.push(modelToIdName);
|
fields.push(modelToIdName);
|
||||||
}
|
} else if (isPlainObject(fields) && !fields[modelToIdName]) {
|
||||||
else if (isPlainObject(fields) && !fields[modelToIdName]) {
|
|
||||||
fields[modelToIdName] = true;
|
fields[modelToIdName] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -486,7 +482,6 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
obj.__cachedRelations[relationName].push(target);
|
obj.__cachedRelations[relationName].push(target);
|
||||||
processTargetObj(obj, next);
|
processTargetObj(obj, next);
|
||||||
}, next);
|
}, next);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -848,7 +843,6 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
function processTargetObj(obj, callback) {
|
function processTargetObj(obj, callback) {
|
||||||
|
|
||||||
var isInst = obj instanceof self;
|
var isInst = obj instanceof self;
|
||||||
|
|
||||||
// Calling the relation method on the instance
|
// Calling the relation method on the instance
|
||||||
|
@ -923,7 +917,6 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
defineCachedRelations(obj);
|
defineCachedRelations(obj);
|
||||||
obj.__cachedRelations[relationName] = result;
|
obj.__cachedRelations[relationName] = result;
|
||||||
|
|
||||||
|
@ -931,7 +924,6 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,7 @@
|
||||||
// License text available at https://opensource.org/licenses/MIT
|
// License text available at https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
module.exports = function getIntrospector(ModelBuilder) {
|
module.exports = function getIntrospector(ModelBuilder) {
|
||||||
|
|
||||||
function introspectType(value) {
|
function introspectType(value) {
|
||||||
|
|
||||||
// Unknown type, using Any
|
// Unknown type, using Any
|
||||||
if (value === null || value === undefined) {
|
if (value === null || value === undefined) {
|
||||||
return ModelBuilder.Any;
|
return ModelBuilder.Any;
|
||||||
|
|
|
@ -20,8 +20,8 @@ exports.inherits = function(newClass, baseClass, options) {
|
||||||
|
|
||||||
if (options.staticProperties) {
|
if (options.staticProperties) {
|
||||||
Object.keys(baseClass).forEach(function(classProp) {
|
Object.keys(baseClass).forEach(function(classProp) {
|
||||||
if (classProp !== 'super_' && (!newClass.hasOwnProperty(classProp)
|
if (classProp !== 'super_' && (!newClass.hasOwnProperty(classProp) ||
|
||||||
|| options.override)) {
|
options.override)) {
|
||||||
var pd = Object.getOwnPropertyDescriptor(baseClass, classProp);
|
var pd = Object.getOwnPropertyDescriptor(baseClass, classProp);
|
||||||
Object.defineProperty(newClass, classProp, pd);
|
Object.defineProperty(newClass, classProp, pd);
|
||||||
}
|
}
|
||||||
|
|
|
@ -526,7 +526,6 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
||||||
ModelClass.emit('defined', ModelClass);
|
ModelClass.emit('defined', ModelClass);
|
||||||
|
|
||||||
return ModelClass;
|
return ModelClass;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// DataType for Date
|
// DataType for Date
|
||||||
|
@ -633,7 +632,6 @@ ModelBuilder.prototype.copyModel = function copyModel(Master) {
|
||||||
hiddenProperty(Slave, 'relations', Master.relations);
|
hiddenProperty(Slave, 'relations', Master.relations);
|
||||||
|
|
||||||
if (!(className in modelBuilder.models)) {
|
if (!(className in modelBuilder.models)) {
|
||||||
|
|
||||||
// store class in model pool
|
// store class in model pool
|
||||||
modelBuilder.models[className] = Slave;
|
modelBuilder.models[className] = Slave;
|
||||||
modelBuilder.definitions[className] = {
|
modelBuilder.definitions[className] = {
|
||||||
|
@ -686,8 +684,7 @@ ModelBuilder.prototype.resolveType = function(type) {
|
||||||
var itemType = this.resolveType(type[0]);
|
var itemType = this.resolveType(type[0]);
|
||||||
if (typeof itemType === 'function') {
|
if (typeof itemType === 'function') {
|
||||||
return [itemType];
|
return [itemType];
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return itemType; // Not resolved, return the type string
|
return itemType; // Not resolved, return the type string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -782,13 +779,9 @@ ModelBuilder.prototype.buildModels = function(schemas, createModel) {
|
||||||
* @returns {}
|
* @returns {}
|
||||||
*/
|
*/
|
||||||
ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) {
|
ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) {
|
||||||
|
|
||||||
// Introspect the JSON document to generate a schema
|
// Introspect the JSON document to generate a schema
|
||||||
var schema = introspect(json);
|
var schema = introspect(json);
|
||||||
|
|
||||||
// Create a model for the generated schema
|
// Create a model for the generated schema
|
||||||
return this.define(name, schema, options);
|
return this.define(name, schema, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
19
lib/model.js
19
lib/model.js
|
@ -212,7 +212,11 @@ ModelBaseClass.prototype._initProperties = function(data, options) {
|
||||||
if (!~fields.indexOf(ctor.relations[p].keyTo)) {
|
if (!~fields.indexOf(ctor.relations[p].keyTo)) {
|
||||||
fields.push(ctor.relations[p].keyTo);
|
fields.push(ctor.relations[p].keyTo);
|
||||||
}
|
}
|
||||||
self.__data[p] = new modelTo(propVal, { fields: fields, applySetters: false, persisted: options.persisted });
|
self.__data[p] = new modelTo(propVal, {
|
||||||
|
fields: fields,
|
||||||
|
applySetters: false,
|
||||||
|
persisted: options.persisted,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -308,7 +312,6 @@ ModelBaseClass.prototype._initProperties = function(data, options) {
|
||||||
|
|
||||||
// Handle complex types (JSON/Object)
|
// Handle complex types (JSON/Object)
|
||||||
if (!BASE_TYPES[type.name]) {
|
if (!BASE_TYPES[type.name]) {
|
||||||
|
|
||||||
if (typeof self.__data[p] !== 'object' && self.__data[p]) {
|
if (typeof self.__data[p] !== 'object' && self.__data[p]) {
|
||||||
try {
|
try {
|
||||||
self.__data[p] = JSON.parse(self.__data[p] + '');
|
self.__data[p] = JSON.parse(self.__data[p] + '');
|
||||||
|
@ -318,15 +321,15 @@ ModelBaseClass.prototype._initProperties = function(data, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type.prototype instanceof ModelBaseClass) {
|
if (type.prototype instanceof ModelBaseClass) {
|
||||||
if (!(self.__data[p] instanceof type)
|
if (!(self.__data[p] instanceof type) &&
|
||||||
&& typeof self.__data[p] === 'object'
|
typeof self.__data[p] === 'object' &&
|
||||||
&& self.__data[p] !== null) {
|
self.__data[p] !== null) {
|
||||||
self.__data[p] = new type(self.__data[p]);
|
self.__data[p] = new type(self.__data[p]);
|
||||||
}
|
}
|
||||||
} else if (type.name === 'Array' || Array.isArray(type)) {
|
} else if (type.name === 'Array' || Array.isArray(type)) {
|
||||||
if (!(self.__data[p] instanceof List)
|
if (!(self.__data[p] instanceof List) &&
|
||||||
&& self.__data[p] !== undefined
|
self.__data[p] !== undefined &&
|
||||||
&& self.__data[p] !== null) {
|
self.__data[p] !== null) {
|
||||||
self.__data[p] = List(self.__data[p], type, self);
|
self.__data[p] = List(self.__data[p], type, self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -203,8 +203,8 @@ RelationDefinition.prototype.defineMethod = function(name, fn) {
|
||||||
RelationDefinition.prototype.applyScope = function(modelInstance, filter) {
|
RelationDefinition.prototype.applyScope = function(modelInstance, filter) {
|
||||||
filter = filter || {};
|
filter = filter || {};
|
||||||
filter.where = filter.where || {};
|
filter.where = filter.where || {};
|
||||||
if ((this.type !== 'belongsTo' || this.type === 'hasOne')
|
if ((this.type !== 'belongsTo' || this.type === 'hasOne') &&
|
||||||
&& typeof this.polymorphic === 'object') { // polymorphic
|
typeof this.polymorphic === 'object') { // polymorphic
|
||||||
var discriminator = this.polymorphic.discriminator;
|
var discriminator = this.polymorphic.discriminator;
|
||||||
if (this.polymorphic.invert) {
|
if (this.polymorphic.invert) {
|
||||||
filter.where[discriminator] = this.modelTo.modelName;
|
filter.where[discriminator] = this.modelTo.modelName;
|
||||||
|
@ -255,8 +255,8 @@ RelationDefinition.prototype.applyProperties = function(modelInstance, obj) {
|
||||||
target[key] = source[k];
|
target[key] = source[k];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((this.type !== 'belongsTo' || this.type === 'hasOne')
|
if ((this.type !== 'belongsTo' || this.type === 'hasOne') &&
|
||||||
&& typeof this.polymorphic === 'object') { // polymorphic
|
typeof this.polymorphic === 'object') { // polymorphic
|
||||||
var discriminator = this.polymorphic.discriminator;
|
var discriminator = this.polymorphic.discriminator;
|
||||||
if (this.polymorphic.invert) {
|
if (this.polymorphic.invert) {
|
||||||
target[discriminator] = this.modelTo.modelName;
|
target[discriminator] = this.modelTo.modelName;
|
||||||
|
@ -792,9 +792,9 @@ HasMany.prototype.findById = function(fkId, options, cb) {
|
||||||
if (inst[fk] != null && idEquals(inst[fk], modelInstance[pk])) {
|
if (inst[fk] != null && idEquals(inst[fk], modelInstance[pk])) {
|
||||||
cb(null, inst);
|
cb(null, inst);
|
||||||
} else {
|
} else {
|
||||||
err = new Error('Key mismatch: ' + modelFrom.modelName + '.' + pk
|
err = new Error('Key mismatch: ' + modelFrom.modelName + '.' + pk + ': ' +
|
||||||
+ ': ' + modelInstance[pk]
|
modelInstance[pk] + ', ' + modelTo.modelName + '.' + fk + ': ' +
|
||||||
+ ', ' + modelTo.modelName + '.' + fk + ': ' + inst[fk]);
|
inst[fk]);
|
||||||
err.statusCode = 400;
|
err.statusCode = 400;
|
||||||
cb(err);
|
cb(err);
|
||||||
}
|
}
|
||||||
|
@ -929,9 +929,9 @@ HasManyThrough.prototype.findById = function(fkId, options, cb) {
|
||||||
self.exists(fkId, options, function(err, exists) {
|
self.exists(fkId, options, function(err, exists) {
|
||||||
if (err || !exists) {
|
if (err || !exists) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
err = new Error('No relation found in ' + modelThrough.modelName
|
err = new Error('No relation found in ' + modelThrough.modelName +
|
||||||
+ ' for (' + self.definition.modelFrom.modelName + '.' + modelInstance[pk]
|
' for (' + self.definition.modelFrom.modelName + '.' +
|
||||||
+ ',' + modelTo.modelName + '.' + fkId + ')');
|
modelInstance[pk] + ',' + modelTo.modelName + '.' + fkId + ')');
|
||||||
err.statusCode = 404;
|
err.statusCode = 404;
|
||||||
}
|
}
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
@ -973,9 +973,9 @@ HasManyThrough.prototype.destroyById = function(fkId, options, cb) {
|
||||||
self.exists(fkId, options, function(err, exists) {
|
self.exists(fkId, options, function(err, exists) {
|
||||||
if (err || !exists) {
|
if (err || !exists) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
err = new Error('No record found in ' + modelThrough.modelName
|
err = new Error('No record found in ' + modelThrough.modelName +
|
||||||
+ ' for (' + self.definition.modelFrom.modelName + '.' + modelInstance[pk]
|
' for (' + self.definition.modelFrom.modelName + '.' +
|
||||||
+ ' ,' + modelTo.modelName + '.' + fkId + ')');
|
modelInstance[pk] + ' ,' + modelTo.modelName + '.' + fkId + ')');
|
||||||
err.statusCode = 404;
|
err.statusCode = 404;
|
||||||
}
|
}
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
@ -1022,7 +1022,7 @@ HasManyThrough.prototype.create = function create(data, options, cb) {
|
||||||
var fk2 = keys[1];
|
var fk2 = keys[1];
|
||||||
|
|
||||||
function createRelation(to, next) {
|
function createRelation(to, next) {
|
||||||
var d = {}, q = {}, filter = { where:q };
|
var d = {}, q = {}, filter = { where: q };
|
||||||
d[fk1] = q[fk1] = modelInstance[definition.keyFrom];
|
d[fk1] = q[fk1] = modelInstance[definition.keyFrom];
|
||||||
d[fk2] = q[fk2] = to[pk2];
|
d[fk2] = q[fk2] = to[pk2];
|
||||||
definition.applyProperties(modelInstance, d);
|
definition.applyProperties(modelInstance, d);
|
||||||
|
@ -1353,8 +1353,7 @@ BelongsTo.prototype.update = function(targetModelData, options, cb) {
|
||||||
if (inst instanceof ModelBaseClass) {
|
if (inst instanceof ModelBaseClass) {
|
||||||
inst.updateAttributes(targetModelData, options, cb);
|
inst.updateAttributes(targetModelData, options, cb);
|
||||||
} else {
|
} else {
|
||||||
cb(new Error('BelongsTo relation ' + definition.name
|
cb(new Error('BelongsTo relation ' + definition.name + ' is empty'));
|
||||||
+ ' is empty'));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return cb.promise;
|
return cb.promise;
|
||||||
|
@ -1379,8 +1378,7 @@ BelongsTo.prototype.destroy = function(options, cb) {
|
||||||
cb && cb(err, targetModel);
|
cb && cb(err, targetModel);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
cb(new Error('BelongsTo relation ' + definition.name
|
cb(new Error('BelongsTo relation ' + definition.name + ' is empty'));
|
||||||
+ ' is empty'));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return cb.promise;
|
return cb.promise;
|
||||||
|
@ -1446,7 +1444,6 @@ BelongsTo.prototype.related = function(condOrRefresh, options, cb) {
|
||||||
|
|
||||||
self.resetCache(newValue);
|
self.resetCache(newValue);
|
||||||
} else if (typeof cb === 'function') { // acts as async getter
|
} else if (typeof cb === 'function') { // acts as async getter
|
||||||
|
|
||||||
if (discriminator) {
|
if (discriminator) {
|
||||||
var modelToName = modelInstance[discriminator];
|
var modelToName = modelInstance[discriminator];
|
||||||
if (typeof modelToName !== 'string') {
|
if (typeof modelToName !== 'string') {
|
||||||
|
@ -1463,8 +1460,7 @@ BelongsTo.prototype.related = function(condOrRefresh, options, cb) {
|
||||||
var query = { where: {}};
|
var query = { where: {}};
|
||||||
query.where[pk] = modelInstance[fk];
|
query.where[pk] = modelInstance[fk];
|
||||||
|
|
||||||
if (query.where[pk] === undefined
|
if (query.where[pk] === undefined || query.where[pk] === null) {
|
||||||
|| query.where[pk] === null) {
|
|
||||||
// Foreign key is undefined
|
// Foreign key is undefined
|
||||||
return process.nextTick(cb);
|
return process.nextTick(cb);
|
||||||
}
|
}
|
||||||
|
@ -1485,14 +1481,15 @@ BelongsTo.prototype.related = function(condOrRefresh, options, cb) {
|
||||||
return cb(null, null);
|
return cb(null, null);
|
||||||
}
|
}
|
||||||
// Check if the foreign key matches the primary key
|
// Check if the foreign key matches the primary key
|
||||||
if (inst[pk] != null && modelInstance[fk] != null
|
if (inst[pk] != null && modelInstance[fk] != null &&
|
||||||
&& inst[pk].toString() === modelInstance[fk].toString()) {
|
inst[pk].toString() === modelInstance[fk].toString()) {
|
||||||
self.resetCache(inst);
|
self.resetCache(inst);
|
||||||
cb(null, inst);
|
cb(null, inst);
|
||||||
} else {
|
} else {
|
||||||
err = new Error('Key mismatch: ' + self.definition.modelFrom.modelName + '.' + fk
|
err = new Error(
|
||||||
+ ': ' + modelInstance[fk]
|
'Key mismatch: ' + self.definition.modelFrom.modelName + '.' + fk +
|
||||||
+ ', ' + modelTo.modelName + '.' + pk + ': ' + inst[pk]);
|
': ' + modelInstance[fk] +
|
||||||
|
', ' + modelTo.modelName + '.' + pk + ': ' + inst[pk]);
|
||||||
err.statusCode = 400;
|
err.statusCode = 400;
|
||||||
cb(err);
|
cb(err);
|
||||||
}
|
}
|
||||||
|
@ -1718,8 +1715,9 @@ HasOne.prototype.create = function(targetModelData, options, cb) {
|
||||||
self.resetCache(targetModel);
|
self.resetCache(targetModel);
|
||||||
cb && cb(err, targetModel);
|
cb && cb(err, targetModel);
|
||||||
} else {
|
} else {
|
||||||
cb && cb(new Error('HasOne relation cannot create more than one instance of '
|
cb && cb(new Error(
|
||||||
+ modelTo.modelName));
|
'HasOne relation cannot create more than one instance of ' +
|
||||||
|
modelTo.modelName));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return cb.promise;
|
return cb.promise;
|
||||||
|
@ -1739,8 +1737,7 @@ HasOne.prototype.update = function(targetModelData, options, cb) {
|
||||||
delete targetModelData[fk];
|
delete targetModelData[fk];
|
||||||
targetModel.updateAttributes(targetModelData, cb);
|
targetModel.updateAttributes(targetModelData, cb);
|
||||||
} else {
|
} else {
|
||||||
cb(new Error('HasOne relation ' + definition.name
|
cb(new Error('HasOne relation ' + definition.name + ' is empty'));
|
||||||
+ ' is empty'));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return cb.promise;
|
return cb.promise;
|
||||||
|
@ -1758,8 +1755,7 @@ HasOne.prototype.destroy = function(options, cb) {
|
||||||
if (targetModel instanceof ModelBaseClass) {
|
if (targetModel instanceof ModelBaseClass) {
|
||||||
targetModel.destroy(options, cb);
|
targetModel.destroy(options, cb);
|
||||||
} else {
|
} else {
|
||||||
cb(new Error('HasOne relation ' + definition.name
|
cb(new Error('HasOne relation ' + definition.name + ' is empty'));
|
||||||
+ ' is empty'));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return cb.promise;
|
return cb.promise;
|
||||||
|
@ -1891,14 +1887,16 @@ HasOne.prototype.related = function(condOrRefresh, options, cb) {
|
||||||
return cb(null, null);
|
return cb(null, null);
|
||||||
}
|
}
|
||||||
// Check if the foreign key matches the primary key
|
// Check if the foreign key matches the primary key
|
||||||
if (inst[fk] != null && modelInstance[pk] != null
|
if (inst[fk] != null && modelInstance[pk] != null &&
|
||||||
&& inst[fk].toString() === modelInstance[pk].toString()) {
|
inst[fk].toString() === modelInstance[pk].toString()) {
|
||||||
self.resetCache(inst);
|
self.resetCache(inst);
|
||||||
cb(null, inst);
|
cb(null, inst);
|
||||||
} else {
|
} else {
|
||||||
err = new Error('Key mismatch: ' + self.definition.modelFrom.modelName + '.' + pk
|
err = new Error(
|
||||||
+ ': ' + modelInstance[pk]
|
'Key mismatch: ' + self.definition.modelFrom.modelName + '.' + pk +
|
||||||
+ ', ' + modelTo.modelName + '.' + fk + ': ' + inst[fk]);
|
': ' + modelInstance[pk] +
|
||||||
|
', ' + modelTo.modelName + '.' + fk + ': ' + inst[fk]
|
||||||
|
);
|
||||||
err.statusCode = 400;
|
err.statusCode = 400;
|
||||||
cb(err);
|
cb(err);
|
||||||
}
|
}
|
||||||
|
@ -3127,9 +3125,9 @@ ReferencesMany.prototype.findById = function(fkId, options, cb) {
|
||||||
if (utils.findIndexOf(ids, inst[pk], idEquals) > -1) {
|
if (utils.findIndexOf(ids, inst[pk], idEquals) > -1) {
|
||||||
cb(null, inst);
|
cb(null, inst);
|
||||||
} else {
|
} else {
|
||||||
err = new Error('Key mismatch: ' + modelFrom.modelName + '.' + fk
|
err = new Error('Key mismatch: ' + modelFrom.modelName + '.' + fk +
|
||||||
+ ': ' + modelInstance[fk]
|
': ' + modelInstance[fk] +
|
||||||
+ ', ' + modelTo.modelName + '.' + pk + ': ' + inst[pk]);
|
', ' + modelTo.modelName + '.' + pk + ': ' + inst[pk]);
|
||||||
err.statusCode = 400;
|
err.statusCode = 400;
|
||||||
cb(err);
|
cb(err);
|
||||||
}
|
}
|
||||||
|
|
14
lib/scope.js
14
lib/scope.js
|
@ -3,6 +3,8 @@
|
||||||
// This file is licensed under the MIT License.
|
// This file is licensed under the MIT License.
|
||||||
// License text available at https://opensource.org/licenses/MIT
|
// License text available at https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
/*eslint-disable camelcase*/
|
||||||
|
|
||||||
var i8n = require('inflection');
|
var i8n = require('inflection');
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var defineCachedRelations = utils.defineCachedRelations;
|
var defineCachedRelations = utils.defineCachedRelations;
|
||||||
|
@ -80,8 +82,8 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
|
||||||
}
|
}
|
||||||
cb = cb || utils.createPromiseCallback();
|
cb = cb || utils.createPromiseCallback();
|
||||||
|
|
||||||
if (!self.__cachedRelations || self.__cachedRelations[name] === undefined
|
if (!self.__cachedRelations || self.__cachedRelations[name] === undefined ||
|
||||||
|| actualRefresh) {
|
actualRefresh) {
|
||||||
// It either doesn't hit the cache or refresh is required
|
// It either doesn't hit the cache or refresh is required
|
||||||
var params = mergeQuery(actualCond, scopeParams, { nestedInclude: true });
|
var params = mergeQuery(actualCond, scopeParams, { nestedInclude: true });
|
||||||
var targetModel = this.targetModel(receiver);
|
var targetModel = this.targetModel(receiver);
|
||||||
|
@ -181,8 +183,8 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
||||||
return self.__cachedRelations[name];
|
return self.__cachedRelations[name];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (typeof condOrRefresh === 'function'
|
if (typeof condOrRefresh === 'function' &&
|
||||||
&& options === undefined && cb === undefined) {
|
options === undefined && cb === undefined) {
|
||||||
// customer.orders(cb)
|
// customer.orders(cb)
|
||||||
cb = condOrRefresh;
|
cb = condOrRefresh;
|
||||||
options = {};
|
options = {};
|
||||||
|
@ -226,8 +228,8 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
f.getAsync = function(condOrRefresh, options, cb) {
|
f.getAsync = function(condOrRefresh, options, cb) {
|
||||||
if (typeof condOrRefresh === 'function'
|
if (typeof condOrRefresh === 'function' &&
|
||||||
&& options === undefined && cb === undefined) {
|
options === undefined && cb === undefined) {
|
||||||
// customer.orders.getAsync(cb)
|
// customer.orders.getAsync(cb)
|
||||||
cb = condOrRefresh;
|
cb = condOrRefresh;
|
||||||
options = {};
|
options = {};
|
||||||
|
|
|
@ -39,7 +39,6 @@ Types.Any.prototype.toObject = Types.Any.prototype.toJSON = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = function(modelTypes) {
|
module.exports = function(modelTypes) {
|
||||||
|
|
||||||
var GeoPoint = require('./geo').GeoPoint;
|
var GeoPoint = require('./geo').GeoPoint;
|
||||||
|
|
||||||
for (var t in Types) {
|
for (var t in Types) {
|
||||||
|
|
27
lib/utils.js
27
lib/utils.js
|
@ -29,9 +29,9 @@ function safeRequire(module) {
|
||||||
try {
|
try {
|
||||||
return require(module);
|
return require(module);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Run "npm install loopback-datasource-juggler ' + module
|
console.log('Run "npm install loopback-datasource-juggler ' + module +
|
||||||
+ '" command to use loopback-datasource-juggler using ' + module
|
'" command to use loopback-datasource-juggler using ' + module +
|
||||||
+ ' database engine');
|
' database engine');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,9 +55,8 @@ function setScopeValuesFromWhere(data, where, targetModel) {
|
||||||
var prop = targetModel.definition.properties[i];
|
var prop = targetModel.definition.properties[i];
|
||||||
if (prop) {
|
if (prop) {
|
||||||
var val = where[i];
|
var val = where[i];
|
||||||
if (typeof val !== 'object' || val instanceof prop.type
|
if (typeof val !== 'object' || val instanceof prop.type ||
|
||||||
|| prop.type.name === 'ObjectID') // MongoDB key
|
prop.type.name === 'ObjectID') { // MongoDB key
|
||||||
{
|
|
||||||
// Only pick the {propertyName: propertyValue}
|
// Only pick the {propertyName: propertyValue}
|
||||||
data[i] = where[i];
|
data[i] = where[i];
|
||||||
}
|
}
|
||||||
|
@ -136,8 +135,7 @@ function convertToArray(include) {
|
||||||
var obj = {};
|
var obj = {};
|
||||||
obj[includeEntry] = true;
|
obj[includeEntry] = true;
|
||||||
normalized.push(obj);
|
normalized.push(obj);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
normalized.push(includeEntry);
|
normalized.push(includeEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -181,8 +179,7 @@ function mergeQuery(base, update, spec) {
|
||||||
var saved = base.include;
|
var saved = base.include;
|
||||||
base.include = {};
|
base.include = {};
|
||||||
base.include[update.include] = saved;
|
base.include[update.include] = saved;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
//default behaviour of inclusion merge - merge inclusions at the same
|
//default behaviour of inclusion merge - merge inclusions at the same
|
||||||
//level. - https://github.com/strongloop/loopback-datasource-juggler/pull/569#issuecomment-95310874
|
//level. - https://github.com/strongloop/loopback-datasource-juggler/pull/569#issuecomment-95310874
|
||||||
base.include = mergeIncludes(base.include, update.include);
|
base.include = mergeIncludes(base.include, update.include);
|
||||||
|
@ -323,8 +320,8 @@ function removeUndefined(query, handleUndefined) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.isArray(x) && (typeof x === 'object' && x !== null
|
if (!Array.isArray(x) && (typeof x === 'object' && x !== null &&
|
||||||
&& x.constructor !== Object)) {
|
x.constructor !== Object)) {
|
||||||
// This object is not a plain object
|
// This object is not a plain object
|
||||||
this.update(x, true); // Stop navigating into this object
|
this.update(x, true); // Stop navigating into this object
|
||||||
return x;
|
return x;
|
||||||
|
@ -441,12 +438,10 @@ function defineCachedRelations(obj) {
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
function isPlainObject(obj) {
|
function isPlainObject(obj) {
|
||||||
return (typeof obj === 'object') && (obj !== null)
|
return (typeof obj === 'object') && (obj !== null) &&
|
||||||
&& (obj.constructor === Object);
|
(obj.constructor === Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function sortObjectsByIds(idName, ids, objects, strict) {
|
function sortObjectsByIds(idName, ids, objects, strict) {
|
||||||
ids = ids.map(function(id) {
|
ids = ids.map(function(id) {
|
||||||
return (typeof id === 'object') ? String(id) : id;
|
return (typeof id === 'object') ? String(id) : id;
|
||||||
|
|
|
@ -356,7 +356,8 @@ function validateUniqueness(attr, conf, err, done) {
|
||||||
err();
|
err();
|
||||||
} else if (found.length === 1 && idName === attr && isNewRecord) {
|
} else if (found.length === 1 && idName === attr && isNewRecord) {
|
||||||
err();
|
err();
|
||||||
} else if (found.length === 1 && (!this.id || !found[0].id || found[0].id.toString() != this.id.toString())) {
|
} else if (found.length === 1 && (!this.id || !found[0].id ||
|
||||||
|
found[0].id.toString() != this.id.toString())) {
|
||||||
err();
|
err();
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
|
@ -494,7 +495,6 @@ Validatable.prototype.isValid = function(callback, data) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}, data, callback);
|
}, data, callback);
|
||||||
|
|
||||||
if (async) {
|
if (async) {
|
||||||
|
@ -504,7 +504,6 @@ Validatable.prototype.isValid = function(callback, data) {
|
||||||
} else {
|
} else {
|
||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function cleanErrors(inst) {
|
function cleanErrors(inst) {
|
||||||
|
@ -522,8 +521,8 @@ function validationFailed(inst, attr, conf, cb) {
|
||||||
|
|
||||||
// here we should check skip validation conditions (if, unless)
|
// here we should check skip validation conditions (if, unless)
|
||||||
// that can be specified in conf
|
// that can be specified in conf
|
||||||
if (skipValidation(inst, conf, 'if')
|
if (skipValidation(inst, conf, 'if') ||
|
||||||
|| skipValidation(inst, conf, 'unless')) {
|
skipValidation(inst, conf, 'unless')) {
|
||||||
if (cb) cb(false);
|
if (cb) cb(false);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,7 @@
|
||||||
"async": "~1.0.0",
|
"async": "~1.0.0",
|
||||||
"debug": "^2.1.1",
|
"debug": "^2.1.1",
|
||||||
"depd": "^1.0.0",
|
"depd": "^1.0.0",
|
||||||
|
"eslint": "^2.9.0",
|
||||||
"inflection": "^1.6.0",
|
"inflection": "^1.6.0",
|
||||||
"loopback-connector": "^2.1.0",
|
"loopback-connector": "^2.1.0",
|
||||||
"node-uuid": "^1.4.2",
|
"node-uuid": "^1.4.2",
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
// This file is licensed under the MIT License.
|
// This file is licensed under the MIT License.
|
||||||
// License text available at https://opensource.org/licenses/MIT
|
// License text available at https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
/* eslint-disable camelcase */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Describe context objects of operation hooks in comprehensive HTML table.
|
* Describe context objects of operation hooks in comprehensive HTML table.
|
||||||
* Usage:
|
* Usage:
|
||||||
|
|
|
@ -9,7 +9,6 @@ var async = require('async');
|
||||||
var db, User;
|
var db, User;
|
||||||
|
|
||||||
describe('basic-querying', function() {
|
describe('basic-querying', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
User = db.define('User', {
|
User = db.define('User', {
|
||||||
|
@ -23,7 +22,6 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
db.automigrate(done);
|
db.automigrate(done);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ping', function() {
|
describe('ping', function() {
|
||||||
|
@ -36,7 +34,6 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findById', function() {
|
describe('findById', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
User.destroyAll(done);
|
User.destroyAll(done);
|
||||||
});
|
});
|
||||||
|
@ -61,7 +58,6 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findByIds', function() {
|
describe('findByIds', function() {
|
||||||
|
@ -122,11 +118,9 @@ describe('basic-querying', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('find', function() {
|
describe('find', function() {
|
||||||
|
|
||||||
before(seed);
|
before(seed);
|
||||||
|
|
||||||
it('should query collection', function(done) {
|
it('should query collection', function(done) {
|
||||||
|
@ -304,7 +298,7 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support number "gte" that is satisfied', function(done) {
|
it('should support number "gte" that is satisfied', function(done) {
|
||||||
User.find({ order: 'seq', where: { order: { 'gte': 3 },
|
User.find({ order: 'seq', where: { order: { 'gte': 3 },
|
||||||
}}, function(err, users) {
|
}}, function(err, users) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
users.should.have.property('length', 4);
|
users.should.have.property('length', 4);
|
||||||
|
@ -361,7 +355,7 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support string "gte" that is satisfied by null value', function(done) {
|
it('should support string "gte" that is satisfied by null value', function(done) {
|
||||||
User.find({ order: 'seq', where: { name: { 'gte': null },
|
User.find({ order: 'seq', where: { name: { 'gte': null },
|
||||||
}}, function(err, users) {
|
}}, function(err, users) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
users.should.have.property('length', 0);
|
users.should.have.property('length', 0);
|
||||||
|
@ -370,7 +364,7 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support string "gte" that is satisfied', function(done) {
|
it('should support string "gte" that is satisfied', function(done) {
|
||||||
User.find({ order: 'seq', where: { name: { 'gte': 'Paul McCartney' },
|
User.find({ order: 'seq', where: { name: { 'gte': 'Paul McCartney' },
|
||||||
}}, function(err, users) {
|
}}, function(err, users) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
users.should.have.property('length', 4);
|
users.should.have.property('length', 4);
|
||||||
|
@ -409,7 +403,7 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support boolean "gte" that is satisfied', function(done) {
|
it('should support boolean "gte" that is satisfied', function(done) {
|
||||||
User.find({ order: 'seq', where: { vip: { 'gte': true },
|
User.find({ order: 'seq', where: { vip: { 'gte': true },
|
||||||
}}, function(err, users) {
|
}}, function(err, users) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
users.should.have.property('length', 3);
|
users.should.have.property('length', 3);
|
||||||
|
@ -451,12 +445,10 @@ describe('basic-querying', function() {
|
||||||
var remaining = 0;
|
var remaining = 0;
|
||||||
|
|
||||||
function sample(fields) {
|
function sample(fields) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
expect: function(arr) {
|
expect: function(arr) {
|
||||||
remaining++;
|
remaining++;
|
||||||
User.find({ fields: fields }, function(err, users) {
|
User.find({ fields: fields }, function(err, users) {
|
||||||
|
|
||||||
remaining--;
|
remaining--;
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
|
||||||
|
@ -493,11 +485,9 @@ describe('basic-querying', function() {
|
||||||
sample(['id']).expect(['id']);
|
sample(['id']).expect(['id']);
|
||||||
sample(['email']).expect(['email']);
|
sample(['email']).expect(['email']);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('count', function() {
|
describe('count', function() {
|
||||||
|
|
||||||
before(seed);
|
before(seed);
|
||||||
|
|
||||||
it('should query total count', function(done) {
|
it('should query total count', function(done) {
|
||||||
|
@ -520,7 +510,6 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findOne', function() {
|
describe('findOne', function() {
|
||||||
|
|
||||||
before(seed);
|
before(seed);
|
||||||
|
|
||||||
it('should find first record (default sort by id)', function(done) {
|
it('should find first record (default sort by id)', function(done) {
|
||||||
|
@ -576,11 +565,9 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('exists', function() {
|
describe('exists', function() {
|
||||||
|
|
||||||
before(seed);
|
before(seed);
|
||||||
|
|
||||||
it('should check whether record exist', function(done) {
|
it('should check whether record exist', function(done) {
|
||||||
|
@ -744,10 +731,10 @@ describe.skip('queries', function() {
|
||||||
var aliases = ['deleteById', 'destroyById', 'removeById'];
|
var aliases = ['deleteById', 'destroyById', 'removeById'];
|
||||||
async.each(aliases, function(alias, cb) {
|
async.each(aliases, function(alias, cb) {
|
||||||
Todo[alias](1, function(err) {
|
Todo[alias](1, function(err) {
|
||||||
should.exist(err);
|
should.exist(err);
|
||||||
err.message.should.equal(expectedErrMsg);
|
err.message.should.equal(expectedErrMsg);
|
||||||
cb();
|
cb();
|
||||||
});
|
});
|
||||||
}, done);
|
}, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ function skip(name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function testSchema(exportCasesHere, dataSource) {
|
module.exports = function testSchema(exportCasesHere, dataSource) {
|
||||||
|
|
||||||
batch = exportCasesHere;
|
batch = exportCasesHere;
|
||||||
schemaName = dataSource.name;
|
schemaName = dataSource.name;
|
||||||
if (dataSource.name.match(/^\/.*\/test\/\.\.$/)) {
|
if (dataSource.name.match(/^\/.*\/test\/\.\.$/)) {
|
||||||
|
@ -52,7 +51,6 @@ module.exports = function testSchema(exportCasesHere, dataSource) {
|
||||||
// dataSource.disconnect();
|
// dataSource.disconnect();
|
||||||
console.log('Test done in %dms\n', Date.now() - start);
|
console.log('Test done in %dms\n', Date.now() - start);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(module.exports, 'it', {
|
Object.defineProperty(module.exports, 'it', {
|
||||||
|
@ -96,7 +94,6 @@ function testOrm(dataSource) {
|
||||||
var Post, User, Passport, Log, Dog;
|
var Post, User, Passport, Log, Dog;
|
||||||
|
|
||||||
it('should define class', function(test) {
|
it('should define class', function(test) {
|
||||||
|
|
||||||
User = dataSource.define('User', {
|
User = dataSource.define('User', {
|
||||||
name: { type: String, index: true },
|
name: { type: String, index: true },
|
||||||
email: { type: String, index: true },
|
email: { type: String, index: true },
|
||||||
|
@ -196,7 +193,6 @@ function testOrm(dataSource) {
|
||||||
test.done();
|
test.done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should initialize object properly', function(test) {
|
it('should initialize object properly', function(test) {
|
||||||
|
@ -317,7 +313,8 @@ function testOrm(dataSource) {
|
||||||
post.destroy(function() {
|
post.destroy(function() {
|
||||||
Post.exists(post.id, function(err, exists) {
|
Post.exists(post.id, function(err, exists) {
|
||||||
if (err) console.log(err);
|
if (err) console.log(err);
|
||||||
test.ok(!exists, 'Hey! ORM told me that object exists, but it looks like it doesn\'t. Something went wrong...');
|
test.ok(!exists, 'Hey! ORM told me that object exists, but it ' +
|
||||||
|
'looks like it doesn\'t. Something went wrong...');
|
||||||
Post.findById(post.id, function(err, obj) {
|
Post.findById(post.id, function(err, obj) {
|
||||||
test.equal(obj, null, 'Param obj should be null');
|
test.equal(obj, null, 'Param obj should be null');
|
||||||
test.done();
|
test.done();
|
||||||
|
@ -443,7 +440,6 @@ function testOrm(dataSource) {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should navigate variations of belongsTo regardless of column name', function(test) {
|
it('should navigate variations of belongsTo regardless of column name', function(test) {
|
||||||
|
|
||||||
Dog.create({ name: 'theDog' }, function(err, obj) {
|
Dog.create({ name: 'theDog' }, function(err, obj) {
|
||||||
test.ok(obj instanceof Dog);
|
test.ok(obj instanceof Dog);
|
||||||
Log.create({ name: 'theLog', ownerId: obj.id }, function(err, obj) {
|
Log.create({ name: 'theLog', ownerId: obj.id }, function(err, obj) {
|
||||||
|
@ -468,7 +464,6 @@ function testOrm(dataSource) {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('hasMany should support additional conditions', function(test) {
|
it('hasMany should support additional conditions', function(test) {
|
||||||
|
|
||||||
User.create(function(e, u) {
|
User.create(function(e, u) {
|
||||||
u.posts.create({}, function(e, p) {
|
u.posts.create({}, function(e, p) {
|
||||||
u.posts({ where: { id: p.id }}, function(e, posts) {
|
u.posts({ where: { id: p.id }}, function(e, posts) {
|
||||||
|
@ -477,9 +472,9 @@ function testOrm(dataSource) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*eslint-disable*/
|
||||||
it('hasMany should be cached', function(test) {
|
it('hasMany should be cached', function(test) {
|
||||||
//User.create(function (e, u) {
|
//User.create(function (e, u) {
|
||||||
// u.posts.create({}, function (e, p) {
|
// u.posts.create({}, function (e, p) {
|
||||||
|
@ -494,7 +489,6 @@ function testOrm(dataSource) {
|
||||||
User.findById(post.userId, function(err, user) {
|
User.findById(post.userId, function(err, user) {
|
||||||
User.create(function(err, voidUser) {
|
User.create(function(err, voidUser) {
|
||||||
Post.create({ userId: user.id }, function() {
|
Post.create({ userId: user.id }, function() {
|
||||||
|
|
||||||
// There can't be any concurrency because we are counting requests
|
// There can't be any concurrency because we are counting requests
|
||||||
// We are first testing cases when user has posts
|
// We are first testing cases when user has posts
|
||||||
user.posts(function(err, data) {
|
user.posts(function(err, data) {
|
||||||
|
@ -531,14 +525,11 @@ function testOrm(dataSource) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -546,8 +537,8 @@ function testOrm(dataSource) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
/*eslint-enable*/
|
||||||
|
|
||||||
// it('should handle hasOne relationship', function (test) {
|
// it('should handle hasOne relationship', function (test) {
|
||||||
// User.create(function (err, u) {
|
// User.create(function (err, u) {
|
||||||
|
@ -736,7 +727,6 @@ function testOrm(dataSource) {
|
||||||
function numerically(a, b) {
|
function numerically(a, b) {
|
||||||
return a - b;
|
return a - b;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// if (
|
// if (
|
||||||
|
@ -1139,5 +1129,4 @@ function testOrm(dataSource) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ var async = require('async');
|
||||||
var db, User, options, filter;
|
var db, User, options, filter;
|
||||||
|
|
||||||
describe('crud-with-options', function() {
|
describe('crud-with-options', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
User = db.define('User', {
|
User = db.define('User', {
|
||||||
|
@ -25,11 +24,9 @@ describe('crud-with-options', function() {
|
||||||
filter = { fields: ['name', 'id'] };
|
filter = { fields: ['name', 'id'] };
|
||||||
|
|
||||||
db.automigrate(['User'], done);
|
db.automigrate(['User'], done);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findById', function() {
|
describe('findById', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
User.destroyAll(done);
|
User.destroyAll(done);
|
||||||
});
|
});
|
||||||
|
@ -186,11 +183,9 @@ describe('crud-with-options', function() {
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findByIds', function() {
|
describe('findByIds', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
var people = [
|
var people = [
|
||||||
{ id: 1, name: 'a', vip: true },
|
{ id: 1, name: 'a', vip: true },
|
||||||
|
@ -231,11 +226,9 @@ describe('crud-with-options', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('find', function() {
|
describe('find', function() {
|
||||||
|
|
||||||
before(seed);
|
before(seed);
|
||||||
|
|
||||||
it('should allow find(cb)', function(done) {
|
it('should allow find(cb)', function(done) {
|
||||||
|
@ -303,11 +296,9 @@ describe('crud-with-options', function() {
|
||||||
User.find({ limit: 3 }, {}, 'invalid cb');
|
User.find({ limit: 3 }, {}, 'invalid cb');
|
||||||
}).should.throw('The cb argument must be a function');
|
}).should.throw('The cb argument must be a function');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('count', function() {
|
describe('count', function() {
|
||||||
|
|
||||||
before(seed);
|
before(seed);
|
||||||
|
|
||||||
it('should allow count(cb)', function(done) {
|
it('should allow count(cb)', function(done) {
|
||||||
|
@ -336,11 +327,9 @@ describe('crud-with-options', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findOne', function() {
|
describe('findOne', function() {
|
||||||
|
|
||||||
before(seed);
|
before(seed);
|
||||||
|
|
||||||
it('should allow findOne(cb)', function(done) {
|
it('should allow findOne(cb)', function(done) {
|
||||||
|
@ -383,11 +372,9 @@ describe('crud-with-options', function() {
|
||||||
done();
|
done();
|
||||||
}, undefined);
|
}, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('exists', function() {
|
describe('exists', function() {
|
||||||
|
|
||||||
before(seed);
|
before(seed);
|
||||||
|
|
||||||
it('should allow exists(id, cb)', function(done) {
|
it('should allow exists(id, cb)', function(done) {
|
||||||
|
@ -410,11 +397,9 @@ describe('crud-with-options', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('save', function() {
|
describe('save', function() {
|
||||||
|
|
||||||
it('should allow save(options, cb)', function(done) {
|
it('should allow save(options, cb)', function(done) {
|
||||||
var options = { foo: 'bar' };
|
var options = { foo: 'bar' };
|
||||||
var opts;
|
var opts;
|
||||||
|
@ -431,11 +416,9 @@ describe('crud-with-options', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('destroyAll with options', function() {
|
describe('destroyAll with options', function() {
|
||||||
|
|
||||||
beforeEach(seed);
|
beforeEach(seed);
|
||||||
|
|
||||||
it('should allow destroyAll(where, options, cb)', function(done) {
|
it('should allow destroyAll(where, options, cb)', function(done) {
|
||||||
|
@ -482,11 +465,9 @@ describe('crud-with-options', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('updateAll ', function() {
|
describe('updateAll ', function() {
|
||||||
|
|
||||||
beforeEach(seed);
|
beforeEach(seed);
|
||||||
|
|
||||||
it('should allow updateAll(where, data, cb)', function(done) {
|
it('should allow updateAll(where, data, cb)', function(done) {
|
||||||
|
@ -533,9 +514,7 @@ describe('crud-with-options', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function seed(done) {
|
function seed(done) {
|
||||||
|
|
|
@ -9,7 +9,6 @@ var should = require('./init.js');
|
||||||
var db, Model;
|
var db, Model;
|
||||||
|
|
||||||
describe('datatypes', function() {
|
describe('datatypes', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
Nested = db.define('Nested', {});
|
Nested = db.define('Nested', {});
|
||||||
|
@ -98,7 +97,6 @@ describe('datatypes', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect data types when updating attributes', function(done) {
|
it('should respect data types when updating attributes', function(done) {
|
||||||
|
@ -137,7 +135,6 @@ describe('datatypes', function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function testDataInDB(done) {
|
function testDataInDB(done) {
|
||||||
|
|
||||||
// verify that the value stored in the db is still an object
|
// verify that the value stored in the db is still an object
|
||||||
function cb(err, data) {
|
function cb(err, data) {
|
||||||
should.exist(data);
|
should.exist(data);
|
||||||
|
|
|
@ -51,7 +51,6 @@ var setupProducts = function(ids, done) {
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('default scope', function() {
|
describe('default scope', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
|
|
||||||
|
@ -132,7 +131,6 @@ describe('default scope', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('manipulation', function() {
|
describe('manipulation', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -140,7 +138,7 @@ describe('default scope', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a scoped instance', function() {
|
it('should return a scoped instance', function() {
|
||||||
var p = new Tool({ name: 'Product A', kind:'ignored' });
|
var p = new Tool({ name: 'Product A', kind: 'ignored' });
|
||||||
p.name.should.equal('Product A');
|
p.name.should.equal('Product A');
|
||||||
p.kind.should.equal('Tool');
|
p.kind.should.equal('Tool');
|
||||||
p.setAttributes({ kind: 'ignored' });
|
p.setAttributes({ kind: 'ignored' });
|
||||||
|
@ -209,11 +207,9 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findById', function() {
|
describe('findById', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -244,11 +240,9 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('find', function() {
|
describe('find', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -318,11 +312,9 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('exists', function() {
|
describe('exists', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -368,11 +360,9 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('count', function() {
|
describe('count', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -418,11 +408,9 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('removeById', function() {
|
describe('removeById', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
function isDeleted(id, done) {
|
function isDeleted(id, done) {
|
||||||
|
@ -478,11 +466,9 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('update', function() {
|
describe('update', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -525,11 +511,9 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('remove', function() {
|
describe('remove', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -599,11 +583,9 @@ describe('default scope', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('scopes', function() {
|
describe('scopes', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -639,17 +621,15 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('scope function', function() {
|
describe('scope function', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db.automigrate(done);
|
db.automigrate(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a scoped instance - widget', function(done) {
|
it('should create a scoped instance - widget', function(done) {
|
||||||
Widget.create({ name: 'Product', kind:'ignored' }, function(err, p) {
|
Widget.create({ name: 'Product', kind: 'ignored' }, function(err, p) {
|
||||||
p.name.should.equal('Product');
|
p.name.should.equal('Product');
|
||||||
p.kind.should.equal('Widget');
|
p.kind.should.equal('Widget');
|
||||||
done();
|
done();
|
||||||
|
@ -657,7 +637,7 @@ describe('default scope', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a scoped instance - thing', function(done) {
|
it('should create a scoped instance - thing', function(done) {
|
||||||
Thing.create({ name: 'Product', kind:'ignored' }, function(err, p) {
|
Thing.create({ name: 'Product', kind: 'ignored' }, function(err, p) {
|
||||||
p.name.should.equal('Product');
|
p.name.should.equal('Product');
|
||||||
p.kind.should.equal('Thing');
|
p.kind.should.equal('Thing');
|
||||||
done();
|
done();
|
||||||
|
@ -691,11 +671,9 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('relations', function() {
|
describe('relations', function() {
|
||||||
|
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -814,11 +792,9 @@ describe('default scope', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with include option', function() {
|
describe('with include option', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db.automigrate(done);
|
db.automigrate(done);
|
||||||
});
|
});
|
||||||
|
@ -840,7 +816,5 @@ describe('default scope', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -65,7 +65,7 @@ describe('defaults', function() {
|
||||||
|
|
||||||
it('should preserve defaults in upsert update', function(done) {
|
it('should preserve defaults in upsert update', function(done) {
|
||||||
Server.findOne({}, function(err, server) {
|
Server.findOne({}, function(err, server) {
|
||||||
Server.upsert({ id:server.id, port: 1337 }, function(err, s) {
|
Server.upsert({ id: server.id, port: 1337 }, function(err, s) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
(Number(1337)).should.equal(s.port);
|
(Number(1337)).should.equal(s.port);
|
||||||
server.createdAt.should.eql(s.createdAt);
|
server.createdAt.should.eql(s.createdAt);
|
||||||
|
@ -73,5 +73,4 @@ describe('defaults', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -596,7 +596,8 @@ describe('discoverExportedForeignKeys', function() {
|
||||||
ds.discoverExportedForeignKeys('INVENTORY', options);
|
ds.discoverExportedForeignKeys('INVENTORY', options);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should discover foreign key definitions using `discoverExportedForeignKeys` - promise variant', function(done) {
|
it('should discover foreign key definitions using ' +
|
||||||
|
'`discoverExportedForeignKeys` - promise variant', function(done) {
|
||||||
ds.discoverExportedForeignKeys('INVENTORY', {})
|
ds.discoverExportedForeignKeys('INVENTORY', {})
|
||||||
.then(function(modelForeignKeys) {
|
.then(function(modelForeignKeys) {
|
||||||
modelForeignKeys.should.be.eql(exportedForeignKeys);
|
modelForeignKeys.should.be.eql(exportedForeignKeys);
|
||||||
|
|
|
@ -12,9 +12,7 @@ var GeoPoint = require('../lib/geo').GeoPoint;
|
||||||
var DELTA = 0.0000001;
|
var DELTA = 0.0000001;
|
||||||
|
|
||||||
describe('GeoPoint', function() {
|
describe('GeoPoint', function() {
|
||||||
|
|
||||||
describe('constructor', function() {
|
describe('constructor', function() {
|
||||||
|
|
||||||
it('should support a valid array', function() {
|
it('should support a valid array', function() {
|
||||||
var point = new GeoPoint([-34, 150]);
|
var point = new GeoPoint([-34, 150]);
|
||||||
|
|
||||||
|
@ -78,33 +76,26 @@ describe('GeoPoint', function() {
|
||||||
};
|
};
|
||||||
fn.should.throw();
|
fn.should.throw();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('toString()', function() {
|
describe('toString()', function() {
|
||||||
|
|
||||||
it('should return a string in the form "lat,lng"', function() {
|
it('should return a string in the form "lat,lng"', function() {
|
||||||
|
|
||||||
var point = new GeoPoint({ lat: -34, lng: 150 });
|
var point = new GeoPoint({ lat: -34, lng: 150 });
|
||||||
point.toString().should.equal('-34,150');
|
point.toString().should.equal('-34,150');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('distance calculation between two points', function() {
|
describe('distance calculation between two points', function() {
|
||||||
|
|
||||||
var here = new GeoPoint({ lat: 40.77492964101182, lng: -73.90950187151662 });
|
var here = new GeoPoint({ lat: 40.77492964101182, lng: -73.90950187151662 });
|
||||||
var there = new GeoPoint({ lat: 40.7753227, lng: -73.909217 });
|
var there = new GeoPoint({ lat: 40.7753227, lng: -73.909217 });
|
||||||
|
|
||||||
it('should return value in miles by default', function() {
|
it('should return value in miles by default', function() {
|
||||||
|
|
||||||
var distance = GeoPoint.distanceBetween(here, there);
|
var distance = GeoPoint.distanceBetween(here, there);
|
||||||
distance.should.be.a.Number;
|
distance.should.be.a.Number;
|
||||||
distance.should.be.approximately(0.03097916611592679, DELTA);
|
distance.should.be.approximately(0.03097916611592679, DELTA);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return value using specified unit', function() {
|
it('should return value using specified unit', function() {
|
||||||
|
|
||||||
/* Supported units:
|
/* Supported units:
|
||||||
* - `radians`
|
* - `radians`
|
||||||
* - `kilometers`
|
* - `kilometers`
|
||||||
|
@ -138,7 +129,5 @@ describe('GeoPoint', function() {
|
||||||
distance.should.be.a.Number;
|
distance.should.be.a.Number;
|
||||||
distance.should.be.approximately(0.0004483676593058972, DELTA);
|
distance.should.be.approximately(0.0004483676593058972, DELTA);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -14,7 +14,6 @@ var j = require('../'),
|
||||||
db, User;
|
db, User;
|
||||||
|
|
||||||
describe('hooks', function() {
|
describe('hooks', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
|
|
||||||
|
@ -29,7 +28,6 @@ describe('hooks', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('initialize', function() {
|
describe('initialize', function() {
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
User.afterInitialize = null;
|
User.afterInitialize = null;
|
||||||
});
|
});
|
||||||
|
@ -54,11 +52,9 @@ describe('hooks', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('create', function() {
|
describe('create', function() {
|
||||||
|
|
||||||
afterEach(removeHooks('Create'));
|
afterEach(removeHooks('Create'));
|
||||||
|
|
||||||
it('should be triggered on create', function(done) {
|
it('should be triggered on create', function(done) {
|
||||||
|
@ -209,7 +205,6 @@ describe('hooks', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('update', function() {
|
describe('update', function() {
|
||||||
|
@ -275,7 +270,6 @@ describe('hooks', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('destroy', function() {
|
describe('destroy', function() {
|
||||||
|
|
||||||
afterEach(removeHooks('Destroy'));
|
afterEach(removeHooks('Destroy'));
|
||||||
|
|
||||||
it('should be triggered on destroy', function(done) {
|
it('should be triggered on destroy', function(done) {
|
||||||
|
@ -308,7 +302,6 @@ describe('hooks', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('lifecycle', function() {
|
describe('lifecycle', function() {
|
||||||
|
@ -435,7 +428,6 @@ describe('hooks', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ var DataSource = require('../').DataSource;
|
||||||
var db, User, Profile, AccessToken, Post, Passport, City, Street, Building, Assembly, Part;
|
var db, User, Profile, AccessToken, Post, Passport, City, Street, Building, Assembly, Part;
|
||||||
|
|
||||||
describe('include', function() {
|
describe('include', function() {
|
||||||
|
|
||||||
before(setup);
|
before(setup);
|
||||||
|
|
||||||
it('should fetch belongsTo relation', function(done) {
|
it('should fetch belongsTo relation', function(done) {
|
||||||
|
@ -61,7 +60,6 @@ describe('include', function() {
|
||||||
|
|
||||||
it('should fetch Passport - Owner - Posts', function(done) {
|
it('should fetch Passport - Owner - Posts', function(done) {
|
||||||
Passport.find({ include: { owner: 'posts' }}, function(err, passports) {
|
Passport.find({ include: { owner: 'posts' }}, function(err, passports) {
|
||||||
|
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(passports);
|
should.exist(passports);
|
||||||
passports.length.should.be.ok;
|
passports.length.should.be.ok;
|
||||||
|
@ -156,7 +154,7 @@ describe('include', function() {
|
||||||
|
|
||||||
it('should fetch Passports with include scope on Posts', function(done) {
|
it('should fetch Passports with include scope on Posts', function(done) {
|
||||||
Passport.find({
|
Passport.find({
|
||||||
include: { owner: { relation: 'posts', scope:{
|
include: { owner: { relation: 'posts', scope: {
|
||||||
fields: ['title'], include: ['author'],
|
fields: ['title'], include: ['author'],
|
||||||
order: 'title DESC',
|
order: 'title DESC',
|
||||||
}}},
|
}}},
|
||||||
|
@ -211,7 +209,7 @@ describe('include', function() {
|
||||||
|
|
||||||
it('should fetch Users with include scope on Posts - belongsTo', function(done) {
|
it('should fetch Users with include scope on Posts - belongsTo', function(done) {
|
||||||
Post.find({
|
Post.find({
|
||||||
include: { relation: 'author', scope:{ fields: ['name'] }},
|
include: { relation: 'author', scope: { fields: ['name'] }},
|
||||||
}, function(err, posts) {
|
}, function(err, posts) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(posts);
|
should.exist(posts);
|
||||||
|
@ -228,9 +226,12 @@ describe('include', function() {
|
||||||
|
|
||||||
it('should fetch Users with include scope on Posts - hasMany', function(done) {
|
it('should fetch Users with include scope on Posts - hasMany', function(done) {
|
||||||
User.find({
|
User.find({
|
||||||
include: { relation: 'posts', scope:{
|
include: {
|
||||||
order: 'title DESC',
|
relation: 'posts',
|
||||||
}},
|
scope: {
|
||||||
|
order: 'title DESC',
|
||||||
|
},
|
||||||
|
},
|
||||||
}, function(err, users) {
|
}, function(err, users) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(users);
|
should.exist(users);
|
||||||
|
@ -258,9 +259,12 @@ describe('include', function() {
|
||||||
|
|
||||||
it('should fetch Users with include scope on Passports - hasMany', function(done) {
|
it('should fetch Users with include scope on Passports - hasMany', function(done) {
|
||||||
User.find({
|
User.find({
|
||||||
include: { relation: 'passports', scope:{
|
include: {
|
||||||
where: { number: '2' },
|
relation: 'passports',
|
||||||
}},
|
scope: {
|
||||||
|
where: { number: '2' },
|
||||||
|
},
|
||||||
|
},
|
||||||
}, function(err, users) {
|
}, function(err, users) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(users);
|
should.exist(users);
|
||||||
|
@ -373,13 +377,11 @@ describe('include', function() {
|
||||||
|
|
||||||
// Create a part
|
// Create a part
|
||||||
assembly.parts.create({ partNumber: 'door' }, function(err, part4) {
|
assembly.parts.create({ partNumber: 'door' }, function(err, part4) {
|
||||||
|
|
||||||
Assembly.find({ include: 'parts' }, function(err, assemblies) {
|
Assembly.find({ include: 'parts' }, function(err, assemblies) {
|
||||||
assemblies.length.should.equal(1);
|
assemblies.length.should.equal(1);
|
||||||
assemblies[0].parts().length.should.equal(2);
|
assemblies[0].parts().length.should.equal(2);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -401,8 +403,7 @@ describe('include', function() {
|
||||||
if (profile) {
|
if (profile) {
|
||||||
profile.should.be.an.instanceOf(Profile);
|
profile.should.be.an.instanceOf(Profile);
|
||||||
usersWithProfile++;
|
usersWithProfile++;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
(profile === null).should.be.true;
|
(profile === null).should.be.true;
|
||||||
}
|
}
|
||||||
// The __cachedRelations should be removed from json output
|
// The __cachedRelations should be removed from json output
|
||||||
|
@ -744,7 +745,6 @@ function setup(done) {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -826,7 +826,6 @@ describe('Model instance with included relation .toJSON()', function() {
|
||||||
function(err) {
|
function(err) {
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function createChallengers(callback) {
|
function createChallengers(callback) {
|
||||||
|
@ -850,7 +849,6 @@ describe('Model instance with included relation .toJSON()', function() {
|
||||||
it('should recursively serialize objects', function(done) {
|
it('should recursively serialize objects', function(done) {
|
||||||
var filter = { include: { gameParticipations: 'results' }};
|
var filter = { include: { gameParticipations: 'results' }};
|
||||||
ChallengerModel.find(filter, function(err, challengers) {
|
ChallengerModel.find(filter, function(err, challengers) {
|
||||||
|
|
||||||
var levelOneInclusion = challengers[0].toJSON().gameParticipations[0];
|
var levelOneInclusion = challengers[0].toJSON().gameParticipations[0];
|
||||||
assert(levelOneInclusion.__data === undefined, '.__data of a level 1 inclusion is undefined.');
|
assert(levelOneInclusion.__data === undefined, '.__data of a level 1 inclusion is undefined.');
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
// This file is licensed under the MIT License.
|
// This file is licensed under the MIT License.
|
||||||
// License text available at https://opensource.org/licenses/MIT
|
// License text available at https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
/* eslint-disable camelcase */
|
||||||
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var should = require('should');
|
var should = require('should');
|
||||||
|
|
||||||
|
@ -53,7 +55,7 @@ describe('include_util', function() {
|
||||||
var objs = [
|
var objs = [
|
||||||
{ id: 11, letter: 'A' },
|
{ id: 11, letter: 'A' },
|
||||||
{ id: 22, letter: 'B' },
|
{ id: 22, letter: 'B' },
|
||||||
];
|
];
|
||||||
var result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'id');
|
var result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'id');
|
||||||
result.exist(11).should.be.true;
|
result.exist(11).should.be.true;
|
||||||
result.exist(22).should.be.true;
|
result.exist(22).should.be.true;
|
||||||
|
@ -65,7 +67,7 @@ describe('include_util', function() {
|
||||||
{ fk_id: 22, letter: 'B' },
|
{ fk_id: 22, letter: 'B' },
|
||||||
{ fk_id: 33, letter: 'C' },
|
{ fk_id: 33, letter: 'C' },
|
||||||
{ fk_id: 11, letter: 'HA!' },
|
{ fk_id: 11, letter: 'HA!' },
|
||||||
];
|
];
|
||||||
|
|
||||||
var result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'fk_id');
|
var result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'fk_id');
|
||||||
result.get(11)[0]['letter'].should.equal('A');
|
result.get(11)[0]['letter'].should.equal('A');
|
||||||
|
|
|
@ -30,7 +30,6 @@ var json = {
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('Introspection of model definitions from JSON', function() {
|
describe('Introspection of model definitions from JSON', function() {
|
||||||
|
|
||||||
it('should handle simple types', function() {
|
it('should handle simple types', function() {
|
||||||
assert.equal(introspectType('123'), 'string');
|
assert.equal(introspectType('123'), 'string');
|
||||||
assert.equal(introspectType(true), 'boolean');
|
assert.equal(introspectType(true), 'boolean');
|
||||||
|
@ -129,6 +128,5 @@ describe('Introspection of model definitions from JSON', function() {
|
||||||
assert.deepEqual(obj, copy);
|
assert.deepEqual(obj, copy);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ var ModelBuilder = jdb.ModelBuilder;
|
||||||
var DataSource = jdb.DataSource;
|
var DataSource = jdb.DataSource;
|
||||||
|
|
||||||
describe('ModelBuilder define model', function() {
|
describe('ModelBuilder define model', function() {
|
||||||
|
|
||||||
it('should be able to define plain models', function(done) {
|
it('should be able to define plain models', function(done) {
|
||||||
var modelBuilder = new ModelBuilder();
|
var modelBuilder = new ModelBuilder();
|
||||||
|
|
||||||
|
@ -260,7 +259,6 @@ describe('ModelBuilder define model', function() {
|
||||||
follow.should.have.property('id');
|
follow.should.have.property('id');
|
||||||
assert.deepEqual(follow.id, { followerId: 1, followeeId: 2 });
|
assert.deepEqual(follow.id, { followerId: 1, followeeId: 2 });
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('DataSource ping', function() {
|
describe('DataSource ping', function() {
|
||||||
|
@ -398,7 +396,6 @@ describe('DataSource define model', function() {
|
||||||
Color.all(function(err, colors) {
|
Color.all(function(err, colors) {
|
||||||
colors.should.have.lengthOf(3);
|
colors.should.have.lengthOf(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should emit events during attach', function() {
|
it('should emit events during attach', function() {
|
||||||
|
@ -435,7 +432,6 @@ describe('DataSource define model', function() {
|
||||||
var User = ds.define('User', { name: String, bio: String }, { strict: true });
|
var User = ds.define('User', { name: String, bio: String }, { strict: true });
|
||||||
|
|
||||||
User.create({ name: 'Joe', age: 20 }, function(err, user) {
|
User.create({ name: 'Joe', age: 20 }, function(err, user) {
|
||||||
|
|
||||||
User.modelName.should.equal('User');
|
User.modelName.should.equal('User');
|
||||||
user.should.be.type('object');
|
user.should.be.type('object');
|
||||||
assert(user.name === 'Joe');
|
assert(user.name === 'Joe');
|
||||||
|
@ -479,7 +475,6 @@ describe('DataSource define model', function() {
|
||||||
User.modelName.should.equal('User');
|
User.modelName.should.equal('User');
|
||||||
|
|
||||||
User.create({ name: 'Joe', age: 20 }, function(err, user) {
|
User.create({ name: 'Joe', age: 20 }, function(err, user) {
|
||||||
|
|
||||||
user.should.be.type('object').and.have.property('name', 'Joe');
|
user.should.be.type('object').and.have.property('name', 'Joe');
|
||||||
user.should.have.property('name', 'Joe');
|
user.should.have.property('name', 'Joe');
|
||||||
user.should.have.property('age', 20);
|
user.should.have.property('age', 20);
|
||||||
|
@ -501,7 +496,6 @@ describe('DataSource define model', function() {
|
||||||
var User = ds.define('User', {});
|
var User = ds.define('User', {});
|
||||||
|
|
||||||
User.create({ name: 'Joe', age: 20 }, function(err, user) {
|
User.create({ name: 'Joe', age: 20 }, function(err, user) {
|
||||||
|
|
||||||
User.modelName.should.equal('User');
|
User.modelName.should.equal('User');
|
||||||
user.should.be.type('object').and.have.property('name', 'Joe');
|
user.should.be.type('object').and.have.property('name', 'Joe');
|
||||||
user.should.have.property('name', 'Joe');
|
user.should.have.property('name', 'Joe');
|
||||||
|
@ -576,7 +570,6 @@ describe('DataSource define model', function() {
|
||||||
user.toObject().should.not.have.property('age');
|
user.toObject().should.not.have.property('age');
|
||||||
user.toObject(true).should.not.have.property('age');
|
user.toObject(true).should.not.have.property('age');
|
||||||
user.toObject(false).should.have.property('age', 20);
|
user.toObject(false).should.have.property('age', 20);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should update the instance with unknown properties', function(done) {
|
it('should update the instance with unknown properties', function(done) {
|
||||||
|
@ -601,7 +594,6 @@ describe('DataSource define model', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -656,7 +648,6 @@ describe('DataSource define model', function() {
|
||||||
});
|
});
|
||||||
User.http.path.should.equal('/accounts');
|
User.http.path.should.equal('/accounts');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Load models with base', function() {
|
describe('Load models with base', function() {
|
||||||
|
@ -791,7 +782,6 @@ describe('Models attached to a dataSource', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -816,7 +806,6 @@ describe('Models attached to a dataSource', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -838,7 +827,6 @@ describe('Models attached to a dataSource', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('save should update the instance with the same id', function(done) {
|
it('save should update the instance with the same id', function(done) {
|
||||||
|
@ -859,7 +847,6 @@ describe('Models attached to a dataSource', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -881,7 +868,6 @@ describe('Models attached to a dataSource', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -903,7 +889,6 @@ describe('Models attached to a dataSource', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -940,7 +925,6 @@ describe('DataSource connector types', function() {
|
||||||
result = ds.supportTypes(['rdbms']);
|
result = ds.supportTypes(['rdbms']);
|
||||||
assert(!result);
|
assert(!result);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('DataSource constructor', function() {
|
describe('DataSource constructor', function() {
|
||||||
|
@ -1012,7 +996,11 @@ describe('Load models with relations', function() {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
var Post = ds.define('Post', { userId: Number, content: String });
|
var Post = ds.define('Post', { userId: Number, content: String });
|
||||||
var User = ds.define('User', { name: String }, { relations: { posts: { type: 'hasMany', model: 'Post' }}});
|
var User = ds.define(
|
||||||
|
'User',
|
||||||
|
{ name: String },
|
||||||
|
{ relations: { posts: { type: 'hasMany', model: 'Post' }}}
|
||||||
|
);
|
||||||
|
|
||||||
assert(User.relations['posts']);
|
assert(User.relations['posts']);
|
||||||
done();
|
done();
|
||||||
|
@ -1022,7 +1010,11 @@ describe('Load models with relations', function() {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
var User = ds.define('User', { name: String });
|
var User = ds.define('User', { name: String });
|
||||||
var Post = ds.define('Post', { userId: Number, content: String }, { relations: { user: { type: 'belongsTo', model: 'User' }}});
|
var Post = ds.define(
|
||||||
|
'Post',
|
||||||
|
{ userId: Number, content: String },
|
||||||
|
{ relations: { user: { type: 'belongsTo', model: 'User' }}}
|
||||||
|
);
|
||||||
|
|
||||||
assert(Post.relations['user']);
|
assert(Post.relations['user']);
|
||||||
done();
|
done();
|
||||||
|
@ -1032,7 +1024,11 @@ describe('Load models with relations', function() {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
var Post = ds.define('Post', { userId: Number, content: String });
|
var Post = ds.define('Post', { userId: Number, content: String });
|
||||||
var User = ds.define('User', { name: String }, { relations: { posts: { type: 'referencesMany', model: 'Post' }}});
|
var User = ds.define(
|
||||||
|
'User',
|
||||||
|
{ name: String },
|
||||||
|
{ relations: { posts: { type: 'referencesMany', model: 'Post' }}}
|
||||||
|
);
|
||||||
|
|
||||||
assert(User.relations['posts']);
|
assert(User.relations['posts']);
|
||||||
done();
|
done();
|
||||||
|
@ -1042,7 +1038,11 @@ describe('Load models with relations', function() {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
var Post = ds.define('Post', { userId: Number, content: String });
|
var Post = ds.define('Post', { userId: Number, content: String });
|
||||||
var User = ds.define('User', { name: String }, { relations: { posts: { type: 'embedsMany', model: 'Post' }}});
|
var User = ds.define(
|
||||||
|
'User',
|
||||||
|
{ name: String },
|
||||||
|
{ relations: { posts: { type: 'embedsMany', model: 'Post' }}}
|
||||||
|
);
|
||||||
|
|
||||||
assert(User.relations['posts']);
|
assert(User.relations['posts']);
|
||||||
done();
|
done();
|
||||||
|
@ -1095,8 +1095,15 @@ describe('Load models with relations', function() {
|
||||||
it('should set up foreign key with the correct type', function(done) {
|
it('should set up foreign key with the correct type', function(done) {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
var User = ds.define('User', { name: String, id: { type: String, id: true }});
|
var User = ds.define(
|
||||||
var Post = ds.define('Post', { content: String }, { relations: { user: { type: 'belongsTo', model: 'User' }}});
|
'User',
|
||||||
|
{ name: String, id: { type: String, id: true }}
|
||||||
|
);
|
||||||
|
var Post = ds.define(
|
||||||
|
'Post',
|
||||||
|
{ content: String },
|
||||||
|
{ relations: { user: { type: 'belongsTo', model: 'User' }}}
|
||||||
|
);
|
||||||
|
|
||||||
var fk = Post.definition.properties['userId'];
|
var fk = Post.definition.properties['userId'];
|
||||||
assert(fk, 'The foreign key should be added');
|
assert(fk, 'The foreign key should be added');
|
||||||
|
@ -1108,14 +1115,37 @@ describe('Load models with relations', function() {
|
||||||
it('should set up hasMany and belongsTo relations', function(done) {
|
it('should set up hasMany and belongsTo relations', function(done) {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
var User = ds.define('User', { name: String }, { relations: { posts: { type: 'hasMany', model: 'Post' }, accounts: { type: 'hasMany', model: 'Account' }}});
|
var User = ds.define(
|
||||||
|
'User',
|
||||||
|
{ name: String },
|
||||||
|
{
|
||||||
|
relations: {
|
||||||
|
posts: {
|
||||||
|
type: 'hasMany',
|
||||||
|
model: 'Post',
|
||||||
|
},
|
||||||
|
accounts: {
|
||||||
|
type: 'hasMany',
|
||||||
|
model: 'Account',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
assert(!User.relations['posts']);
|
assert(!User.relations['posts']);
|
||||||
assert(!User.relations['accounts']);
|
assert(!User.relations['accounts']);
|
||||||
|
|
||||||
var Post = ds.define('Post', { userId: Number, content: String }, { relations: { user: { type: 'belongsTo', model: 'User' }}});
|
var Post = ds.define(
|
||||||
|
'Post',
|
||||||
|
{ userId: Number, content: String },
|
||||||
|
{ relations: { user: { type: 'belongsTo', model: 'User' }}}
|
||||||
|
);
|
||||||
|
|
||||||
var Account = ds.define('Account', { userId: Number, type: String }, { relations: { user: { type: 'belongsTo', model: 'User' }}});
|
var Account = ds.define(
|
||||||
|
'Account',
|
||||||
|
{ userId: Number, type: String },
|
||||||
|
{ relations: { user: { type: 'belongsTo', model: 'User' }}}
|
||||||
|
);
|
||||||
|
|
||||||
assert(Post.relations['user']);
|
assert(Post.relations['user']);
|
||||||
assert.deepEqual(Post.relations['user'].toJSON(), {
|
assert.deepEqual(Post.relations['user'].toJSON(), {
|
||||||
|
@ -1157,11 +1187,14 @@ describe('Load models with relations', function() {
|
||||||
var Post = ds.define('Post', { userId: Number, content: String });
|
var Post = ds.define('Post', { userId: Number, content: String });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var User = ds.define('User', { name: String }, { relations: { posts: { model: 'Post' }}});
|
var User = ds.define(
|
||||||
|
'User',
|
||||||
|
{ name: String },
|
||||||
|
{ relations: { posts: { model: 'Post' }}}
|
||||||
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw if the relation type is invalid', function(done) {
|
it('should throw if the relation type is invalid', function(done) {
|
||||||
|
@ -1170,11 +1203,14 @@ describe('Load models with relations', function() {
|
||||||
var Post = ds.define('Post', { userId: Number, content: String });
|
var Post = ds.define('Post', { userId: Number, content: String });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var User = ds.define('User', { name: String }, { relations: { posts: { type: 'hasXYZ', model: 'Post' }}});
|
var User = ds.define(
|
||||||
|
'User',
|
||||||
|
{ name: String },
|
||||||
|
{ relations: { posts: { type: 'hasXYZ', model: 'Post' }}}
|
||||||
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle hasMany through', function(done) {
|
it('should handle hasMany through', function(done) {
|
||||||
|
@ -1190,11 +1226,21 @@ describe('Load models with relations', function() {
|
||||||
assert(!Physician.relations['patients']); // Appointment hasn't been resolved yet
|
assert(!Physician.relations['patients']); // Appointment hasn't been resolved yet
|
||||||
assert(!Patient.relations['physicians']); // Appointment hasn't been resolved yet
|
assert(!Patient.relations['physicians']); // Appointment hasn't been resolved yet
|
||||||
|
|
||||||
var Appointment = ds.createModel('Appointment', {
|
var Appointment = ds.createModel('Appointment',
|
||||||
physicianId: Number,
|
{ physicianId: Number, patientId: Number, appointmentDate: Date },
|
||||||
patientId: Number,
|
{
|
||||||
appointmentDate: Date,
|
relations: {
|
||||||
}, { relations: { patient: { type: 'belongsTo', model: 'Patient' }, physician: { type: 'belongsTo', model: 'Physician' }}});
|
patient: {
|
||||||
|
type: 'belongsTo',
|
||||||
|
model: 'Patient',
|
||||||
|
},
|
||||||
|
physician: {
|
||||||
|
type: 'belongsTo',
|
||||||
|
model: 'Physician',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
assert(Physician.relations['patients']);
|
assert(Physician.relations['patients']);
|
||||||
assert(Patient.relations['physicians']);
|
assert(Patient.relations['physicians']);
|
||||||
|
@ -1203,19 +1249,47 @@ describe('Load models with relations', function() {
|
||||||
|
|
||||||
it('should handle hasMany through options', function(done) {
|
it('should handle hasMany through options', function(done) {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
var Physician = ds.createModel('Physician', {
|
var Physician = ds.createModel('Physician',
|
||||||
name: String,
|
{ name: String },
|
||||||
}, { relations: { patients: { model: 'Patient', type: 'hasMany', foreignKey: 'leftId', through: 'Appointment' }}});
|
{
|
||||||
|
relations: {
|
||||||
var Patient = ds.createModel('Patient', {
|
patients: {
|
||||||
name: String,
|
model: 'Patient',
|
||||||
}, { relations: { physicians: { model: 'Physician', type: 'hasMany', foreignKey: 'rightId', through: 'Appointment' }}});
|
type: 'hasMany',
|
||||||
|
foreignKey: 'leftId',
|
||||||
var Appointment = ds.createModel('Appointment', {
|
through: 'Appointment',
|
||||||
physicianId: Number,
|
},
|
||||||
patientId: Number,
|
},
|
||||||
appointmentDate: Date,
|
}
|
||||||
}, { relations: { patient: { type: 'belongsTo', model: 'Patient' }, physician: { type: 'belongsTo', model: 'Physician' }}});
|
);
|
||||||
|
var Patient = ds.createModel('Patient',
|
||||||
|
{ name: String },
|
||||||
|
{
|
||||||
|
relations: {
|
||||||
|
physicians: {
|
||||||
|
model: 'Physician',
|
||||||
|
type: 'hasMany',
|
||||||
|
foreignKey: 'rightId',
|
||||||
|
through: 'Appointment',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
var Appointment = ds.createModel('Appointment',
|
||||||
|
{ physicianId: Number, patientId: Number, appointmentDate: Date },
|
||||||
|
{
|
||||||
|
relations: {
|
||||||
|
patient: {
|
||||||
|
type: 'belongsTo',
|
||||||
|
model: 'Patient',
|
||||||
|
},
|
||||||
|
physician: {
|
||||||
|
type: 'belongsTo',
|
||||||
|
model: 'Physician',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
assert(Physician.relations['patients'].keyTo === 'leftId');
|
assert(Physician.relations['patients'].keyTo === 'leftId');
|
||||||
assert(Patient.relations['physicians'].keyTo === 'rightId');
|
assert(Patient.relations['physicians'].keyTo === 'rightId');
|
||||||
|
@ -1227,7 +1301,11 @@ describe('Load models with relations', function() {
|
||||||
var modelBuilder = new ModelBuilder();
|
var modelBuilder = new ModelBuilder();
|
||||||
|
|
||||||
var Post = modelBuilder.define('Post', { userId: Number, content: String });
|
var Post = modelBuilder.define('Post', { userId: Number, content: String });
|
||||||
var User = modelBuilder.define('User', { name: String }, { relations: { posts: { type: 'hasMany', model: 'Post' }}});
|
var User = modelBuilder.define(
|
||||||
|
'User',
|
||||||
|
{ name: String },
|
||||||
|
{ relations: { posts: { type: 'hasMany', model: 'Post' }}}
|
||||||
|
);
|
||||||
|
|
||||||
assert(!User.relations['posts']);
|
assert(!User.relations['posts']);
|
||||||
Post.attachTo(ds);
|
Post.attachTo(ds);
|
||||||
|
@ -1235,7 +1313,6 @@ describe('Load models with relations', function() {
|
||||||
assert(User.relations['posts']);
|
assert(User.relations['posts']);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model with scopes', function() {
|
describe('Model with scopes', function() {
|
||||||
|
@ -1355,7 +1432,6 @@ describe('DataAccessObject', function() {
|
||||||
|
|
||||||
where = model._coerce({ vip: '' });
|
where = model._coerce({ vip: '' });
|
||||||
assert.deepEqual(where, { vip: false });
|
assert.deepEqual(where, { vip: false });
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to coerce where clause with and operators', function() {
|
it('should be able to coerce where clause with and operators', function() {
|
||||||
|
@ -1547,7 +1623,6 @@ describe('DataAccessObject', function() {
|
||||||
ds.settings.test = 'willNotGet';
|
ds.settings.test = 'willNotGet';
|
||||||
assert.notEqual(model._getSetting('test'), ds.settings.test, 'Should not get datasource setting');
|
assert.notEqual(model._getSetting('test'), ds.settings.test, 'Should not get datasource setting');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Load models from json', function() {
|
describe('Load models from json', function() {
|
||||||
|
@ -1575,7 +1650,6 @@ describe('Load models from json', function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
it('should be able to define models from json', function() {
|
it('should be able to define models from json', function() {
|
||||||
|
|
||||||
var models = loadSchemasSync(path.join(__dirname, 'test1-schemas.json'));
|
var models = loadSchemasSync(path.join(__dirname, 'test1-schemas.json'));
|
||||||
|
|
||||||
models.should.have.property('AnonymousModel_0');
|
models.should.have.property('AnonymousModel_0');
|
||||||
|
@ -1828,5 +1902,4 @@ describe('ModelBuilder options.models', function() {
|
||||||
var M1 = builder.define('M1');
|
var M1 = builder.define('M1');
|
||||||
assert.equal(M2.M1, M1, 'M1 should be injected to M2');
|
assert.equal(M2.M1, M1, 'M1 should be injected to M2');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -13,7 +13,6 @@ var ValidationError = require('..').ValidationError;
|
||||||
var UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
var UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||||
|
|
||||||
describe('manipulation', function() {
|
describe('manipulation', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
|
|
||||||
|
@ -27,7 +26,6 @@ describe('manipulation', function() {
|
||||||
}, { forceId: true, strict: true });
|
}, { forceId: true, strict: true });
|
||||||
|
|
||||||
db.automigrate(['Person'], done);
|
db.automigrate(['Person'], done);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// A simplified implementation of LoopBack's User model
|
// A simplified implementation of LoopBack's User model
|
||||||
|
@ -56,7 +54,6 @@ describe('manipulation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('create', function() {
|
describe('create', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
Person.destroyAll(done);
|
Person.destroyAll(done);
|
||||||
});
|
});
|
||||||
|
@ -112,7 +109,6 @@ describe('manipulation', function() {
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(done);
|
.catch(done);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return instance of object', function(done) {
|
it('should return instance of object', function(done) {
|
||||||
|
@ -328,7 +324,6 @@ describe('manipulation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('save', function() {
|
describe('save', function() {
|
||||||
|
|
||||||
it('should save new object', function(done) {
|
it('should save new object', function(done) {
|
||||||
var p = new Person;
|
var p = new Person;
|
||||||
p.save(function(err) {
|
p.save(function(err) {
|
||||||
|
@ -530,7 +525,7 @@ describe('manipulation', function() {
|
||||||
// Using {foo: 'bar'} only causes dependent test failures due to the
|
// Using {foo: 'bar'} only causes dependent test failures due to the
|
||||||
// stripping of object properties when in strict mode (ie. {foo: 'bar'}
|
// stripping of object properties when in strict mode (ie. {foo: 'bar'}
|
||||||
// changes to '{}' and breaks other tests
|
// changes to '{}' and breaks other tests
|
||||||
person.updateAttributes({ name: 'John', foo:'bar' },
|
person.updateAttributes({ name: 'John', foo: 'bar' },
|
||||||
function(err, p) {
|
function(err, p) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
should.not.exist(p.foo);
|
should.not.exist(p.foo);
|
||||||
|
@ -545,7 +540,7 @@ describe('manipulation', function() {
|
||||||
it('should throw error on unknown attributes when strict: throw', function(done) {
|
it('should throw error on unknown attributes when strict: throw', function(done) {
|
||||||
Person.definition.settings.strict = 'throw';
|
Person.definition.settings.strict = 'throw';
|
||||||
Person.findById(person.id, function(err, p) {
|
Person.findById(person.id, function(err, p) {
|
||||||
p.updateAttributes({ foo:'bar' },
|
p.updateAttributes({ foo: 'bar' },
|
||||||
function(err, p) {
|
function(err, p) {
|
||||||
should.exist(err);
|
should.exist(err);
|
||||||
err.name.should.equal('Error');
|
err.name.should.equal('Error');
|
||||||
|
@ -563,7 +558,7 @@ describe('manipulation', function() {
|
||||||
it('should throw error on unknown attributes when strict: throw', function(done) {
|
it('should throw error on unknown attributes when strict: throw', function(done) {
|
||||||
Person.definition.settings.strict = 'validate';
|
Person.definition.settings.strict = 'validate';
|
||||||
Person.findById(person.id, function(err, p) {
|
Person.findById(person.id, function(err, p) {
|
||||||
p.updateAttributes({ foo:'bar' },
|
p.updateAttributes({ foo: 'bar' },
|
||||||
function(err, p) {
|
function(err, p) {
|
||||||
should.exist(err);
|
should.exist(err);
|
||||||
err.name.should.equal('ValidationError');
|
err.name.should.equal('ValidationError');
|
||||||
|
@ -1029,11 +1024,11 @@ describe('manipulation', function() {
|
||||||
Post.findById(postInstance.id, function(err, p) {
|
Post.findById(postInstance.id, function(err, p) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
p.replaceAttributes({ title: 'b' }, function(err, p) {
|
p.replaceAttributes({ title: 'b' }, function(err, p) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
p.should.not.have.property('content', undefined);
|
p.should.not.have.property('content', undefined);
|
||||||
p.title.should.equal('b');
|
p.title.should.equal('b');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1041,11 +1036,11 @@ describe('manipulation', function() {
|
||||||
Post.findById(postInstance.id, function(err, p) {
|
Post.findById(postInstance.id, function(err, p) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
p.replaceAttributes({ title: 'b' }, { validate: false }, function(err, p) {
|
p.replaceAttributes({ title: 'b' }, { validate: false }, function(err, p) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
p.should.not.have.property('content', undefined);
|
p.should.not.have.property('content', undefined);
|
||||||
p.title.should.equal('b');
|
p.title.should.equal('b');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1117,7 +1112,6 @@ describe('manipulation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('destroy', function() {
|
describe('destroy', function() {
|
||||||
|
|
||||||
it('should destroy record', function(done) {
|
it('should destroy record', function(done) {
|
||||||
Person.create(function(err, p) {
|
Person.create(function(err, p) {
|
||||||
p.destroy(function(err) {
|
p.destroy(function(err) {
|
||||||
|
@ -1175,7 +1169,6 @@ describe('manipulation', function() {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(done);
|
.catch(done);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: implement destroy with filtered set
|
// TODO: implement destroy with filtered set
|
||||||
|
@ -1209,10 +1202,10 @@ describe('manipulation', function() {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
data.should.have.length(0);
|
data.should.have.length(0);
|
||||||
Person.find({ where: { name: 'Jane' }}, function(err, data) {
|
Person.find({ where: { name: 'Jane' }}, function(err, data) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
data.should.have.length(1);
|
data.should.have.length(1);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1514,7 +1507,6 @@ describe('manipulation', function() {
|
||||||
|
|
||||||
p1 = new Person({ name: 'John', married: undefined });
|
p1 = new Person({ name: 'John', married: undefined });
|
||||||
p1.should.have.property('married', undefined);
|
p1.should.have.property('married', undefined);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should coerce boolean types properly', function() {
|
it('should coerce boolean types properly', function() {
|
||||||
|
@ -1612,10 +1604,10 @@ describe('manipulation', function() {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
people.should.be.empty;
|
people.should.be.empty;
|
||||||
Person.find({ where: { name: 'Harry Hoe' }}, function(err, people) {
|
Person.find({ where: { name: 'Harry Hoe' }}, function(err, people) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
people.should.have.length(5);
|
people.should.have.length(5);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -135,7 +135,6 @@ describe('Memory connector', function() {
|
||||||
assert.equal(users.length, 2);
|
assert.equal(users.length, 2);
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -281,7 +280,7 @@ describe('Memory connector', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should successfully extract 2 users using implied and', function(done) {
|
it('should successfully extract 2 users using implied and', function(done) {
|
||||||
User.find({ where: { role:'lead', vip:true }}, function(err, users) {
|
User.find({ where: { role: 'lead', vip: true }}, function(err, users) {
|
||||||
should(users.length).be.equal(2);
|
should(users.length).be.equal(2);
|
||||||
should(users[0].name).be.equal('John Lennon');
|
should(users[0].name).be.equal('John Lennon');
|
||||||
should(users[1].name).be.equal('Paul McCartney');
|
should(users[1].name).be.equal('Paul McCartney');
|
||||||
|
@ -289,8 +288,14 @@ describe('Memory connector', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should successfully extract 2 users using implied and & and', function(done) {
|
it('should successfully extract 2 users using implied and & and',
|
||||||
User.find({ where: { name: 'John Lennon', and: [{ role:'lead' }, { vip:true }] }}, function(err, users) {
|
function(done) {
|
||||||
|
User.find({
|
||||||
|
where: {
|
||||||
|
name: 'John Lennon',
|
||||||
|
and: [{ role: 'lead' }, { vip: true }],
|
||||||
|
},
|
||||||
|
}, function(err, users) {
|
||||||
should(users.length).be.equal(1);
|
should(users.length).be.equal(1);
|
||||||
should(users[0].name).be.equal('John Lennon');
|
should(users[0].name).be.equal('John Lennon');
|
||||||
done();
|
done();
|
||||||
|
@ -488,8 +493,8 @@ describe('Memory connector', function() {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
users.length.should.be.equal(2);
|
users.length.should.be.equal(2);
|
||||||
for (var i = 0; i < users.length; i++) {
|
for (var i = 0; i < users.length; i++) {
|
||||||
users[i].address.state.should.be.eql('CA');
|
users[i].address.state.should.be.eql('CA');
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -574,7 +579,6 @@ describe('Memory connector', function() {
|
||||||
},
|
},
|
||||||
], done);
|
], done);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use collection setting', function(done) {
|
it('should use collection setting', function(done) {
|
||||||
|
@ -696,7 +700,6 @@ describe('Memory connector', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findOrCreate', function() {
|
describe('findOrCreate', function() {
|
||||||
|
@ -941,7 +944,6 @@ describe('Memory connector with options', function() {
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Memory connector with observers', function() {
|
describe('Memory connector with observers', function() {
|
||||||
|
|
|
@ -15,7 +15,6 @@ var modelBuilder = new ModelBuilder();
|
||||||
var mixins = modelBuilder.mixins;
|
var mixins = modelBuilder.mixins;
|
||||||
|
|
||||||
function timestamps(Model, options) {
|
function timestamps(Model, options) {
|
||||||
|
|
||||||
Model.defineProperty('createdAt', { type: Date });
|
Model.defineProperty('createdAt', { type: Date });
|
||||||
Model.defineProperty('updatedAt', { type: Date });
|
Model.defineProperty('updatedAt', { type: Date });
|
||||||
|
|
||||||
|
@ -46,7 +45,6 @@ function timestamps(Model, options) {
|
||||||
mixins.define('TimeStamp', timestamps);
|
mixins.define('TimeStamp', timestamps);
|
||||||
|
|
||||||
describe('Model class', function() {
|
describe('Model class', function() {
|
||||||
|
|
||||||
it('should define mixins', function() {
|
it('should define mixins', function() {
|
||||||
mixins.define('Example', function(Model, options) {
|
mixins.define('Example', function(Model, options) {
|
||||||
Model.prototype.example = function() {
|
Model.prototype.example = function() {
|
||||||
|
@ -111,7 +109,6 @@ describe('Model class', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#mixin()', function() {
|
describe('#mixin()', function() {
|
||||||
|
|
||||||
var Person, Author, Address;
|
var Person, Author, Address;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
@ -139,7 +136,5 @@ describe('Model class', function() {
|
||||||
Author.mixin(Address);
|
Author.mixin(Address);
|
||||||
Author._mixins.should.containEql(Address);
|
Author._mixins.should.containEql(Address);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -49,7 +49,6 @@ describe('ModelDefinition class', function() {
|
||||||
assert.deepEqual(User.toJSON(), json);
|
assert.deepEqual(User.toJSON(), json);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to define additional properties', function(done) {
|
it('should be able to define additional properties', function(done) {
|
||||||
|
@ -80,7 +79,6 @@ describe('ModelDefinition class', function() {
|
||||||
assert.deepEqual(json.properties.id, { type: 'Number', id: true });
|
assert.deepEqual(json.properties.id, { type: 'Number', id: true });
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to define nesting models', function(done) {
|
it('should be able to define nesting models', function(done) {
|
||||||
|
@ -122,7 +120,6 @@ describe('ModelDefinition class', function() {
|
||||||
state: { type: 'String' }});
|
state: { type: 'String' }});
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to define referencing models', function(done) {
|
it('should be able to define referencing models', function(done) {
|
||||||
|
@ -163,7 +160,6 @@ describe('ModelDefinition class', function() {
|
||||||
assert.equal(json.properties.address.type, 'Address');
|
assert.equal(json.properties.address.type, 'Address');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to define referencing models by name', function(done) {
|
it('should be able to define referencing models by name', function(done) {
|
||||||
|
@ -204,7 +200,6 @@ describe('ModelDefinition class', function() {
|
||||||
assert.equal(json.properties.address.type, 'Address');
|
assert.equal(json.properties.address.type, 'Address');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should report correct id names', function(done) {
|
it('should report correct id names', function(done) {
|
||||||
|
|
|
@ -16,7 +16,6 @@ var INVALID_DATA = { name: null };
|
||||||
var VALID_DATA = { name: INITIAL_NAME };
|
var VALID_DATA = { name: INITIAL_NAME };
|
||||||
|
|
||||||
describe('optional-validation', function() {
|
describe('optional-validation', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
ModelWithForceId = db.createModel(
|
ModelWithForceId = db.createModel(
|
||||||
|
@ -137,7 +136,6 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('no model setting', function() {
|
describe('no model setting', function() {
|
||||||
|
|
||||||
describe('method create', function() {
|
describe('method create', function() {
|
||||||
it('should throw on create with validate:true with invalid data', function(done) {
|
it('should throw on create with validate:true with invalid data', function(done) {
|
||||||
User.create(INVALID_DATA, { validate: true }, expectValidationError(done));
|
User.create(INVALID_DATA, { validate: true }, expectValidationError(done));
|
||||||
|
@ -169,8 +167,14 @@ describe('optional-validation', function() {
|
||||||
User.findOrCreate(getNewWhere(), INVALID_DATA, { validate: true }, expectValidationError(done));
|
User.findOrCreate(getNewWhere(), INVALID_DATA, { validate: true }, expectValidationError(done));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on findOrCreate with validate:false with invalid data', function(done) {
|
it('should NOT throw on findOrCreate with validate:false with invalid ' +
|
||||||
User.findOrCreate(getNewWhere(), INVALID_DATA, { validate: false }, expectCreateSuccess(INVALID_DATA, done));
|
'data', function(done) {
|
||||||
|
User.findOrCreate(
|
||||||
|
getNewWhere(),
|
||||||
|
INVALID_DATA,
|
||||||
|
{ validate: false },
|
||||||
|
expectCreateSuccess(INVALID_DATA, done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on findOrCreate with validate:true with valid data', function(done) {
|
it('should NOT throw on findOrCreate with validate:true with valid data', function(done) {
|
||||||
|
@ -191,12 +195,22 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('method updateOrCreate on existing data', function() {
|
describe('method updateOrCreate on existing data', function() {
|
||||||
it('should throw on updateOrCreate(id) with validate:true with invalid data', function(done) {
|
it('should throw on updateOrCreate(id) with validate:true with invalid ' +
|
||||||
callUpdateOrCreateWithExistingUserId(null, { validate: true }, expectValidationError(done));
|
'data', function(done) {
|
||||||
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
null,
|
||||||
|
{ validate: true },
|
||||||
|
expectValidationError(done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on updateOrCreate(id) with validate:false with invalid data', function(done) {
|
it('should NOT throw on updateOrCreate(id) with validate:false with ' +
|
||||||
callUpdateOrCreateWithExistingUserId(null, { validate: false }, expectChangeSuccess(INVALID_DATA, done));
|
'invalid data', function(done) {
|
||||||
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
null,
|
||||||
|
{ validate: false },
|
||||||
|
expectChangeSuccess(INVALID_DATA, done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on updateOrCreate(id) with validate:true with valid data', function(done) {
|
it('should NOT throw on updateOrCreate(id) with validate:true with valid data', function(done) {
|
||||||
|
@ -292,11 +306,9 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('model setting: automaticValidation: false', function() {
|
describe('model setting: automaticValidation: false', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
User.settings.automaticValidation = false;
|
User.settings.automaticValidation = false;
|
||||||
done();
|
done();
|
||||||
|
@ -334,7 +346,12 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on findOrCreate with validate:false with invalid data', function(done) {
|
it('should NOT throw on findOrCreate with validate:false with invalid data', function(done) {
|
||||||
User.findOrCreate(getNewWhere(), INVALID_DATA, { validate: false }, expectCreateSuccess(INVALID_DATA, done));
|
User.findOrCreate(
|
||||||
|
getNewWhere(),
|
||||||
|
INVALID_DATA,
|
||||||
|
{ validate: false },
|
||||||
|
expectCreateSuccess(INVALID_DATA, done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on findOrCreate with validate:true with valid data', function(done) {
|
it('should NOT throw on findOrCreate with validate:true with valid data', function(done) {
|
||||||
|
@ -346,7 +363,11 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on findOrCreate with invalid data', function(done) {
|
it('should NOT throw on findOrCreate with invalid data', function(done) {
|
||||||
User.findOrCreate(getNewWhere(), INVALID_DATA, expectCreateSuccess(INVALID_DATA, done));
|
User.findOrCreate(
|
||||||
|
getNewWhere(),
|
||||||
|
INVALID_DATA,
|
||||||
|
expectCreateSuccess(INVALID_DATA, done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on findOrCreate with valid data', function(done) {
|
it('should NOT throw on findOrCreate with valid data', function(done) {
|
||||||
|
@ -355,12 +376,22 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('method updateOrCreate on existing data', function() {
|
describe('method updateOrCreate on existing data', function() {
|
||||||
it('should throw on updateOrCreate(id) with validate:true with invalid data', function(done) {
|
it('should throw on updateOrCreate(id) with validate:true with invalid ' +
|
||||||
callUpdateOrCreateWithExistingUserId(null, { validate: true }, expectValidationError(done));
|
'data', function(done) {
|
||||||
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
null,
|
||||||
|
{ validate: true },
|
||||||
|
expectValidationError(done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on updateOrCreate(id) with validate:false with invalid data', function(done) {
|
it('should NOT throw on updateOrCreate(id) with validate:false with ' +
|
||||||
callUpdateOrCreateWithExistingUserId(null, { validate: false }, expectChangeSuccess(INVALID_DATA, done));
|
'invalid data', function(done) {
|
||||||
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
null,
|
||||||
|
{ validate: false },
|
||||||
|
expectChangeSuccess(INVALID_DATA, done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on updateOrCreate(id) with validate:true with valid data', function(done) {
|
it('should NOT throw on updateOrCreate(id) with validate:true with valid data', function(done) {
|
||||||
|
@ -417,11 +448,9 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('model setting: automaticValidation: true', function() {
|
describe('model setting: automaticValidation: true', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
User.settings.automaticValidation = true;
|
User.settings.automaticValidation = true;
|
||||||
done();
|
done();
|
||||||
|
@ -459,7 +488,12 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on findOrCreate with validate:false with invalid data', function(done) {
|
it('should NOT throw on findOrCreate with validate:false with invalid data', function(done) {
|
||||||
User.findOrCreate(getNewWhere(), INVALID_DATA, { validate: false }, expectCreateSuccess(INVALID_DATA, done));
|
User.findOrCreate(
|
||||||
|
getNewWhere(),
|
||||||
|
INVALID_DATA,
|
||||||
|
{ validate: false },
|
||||||
|
expectCreateSuccess(INVALID_DATA, done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on findOrCreate with validate:true with valid data', function(done) {
|
it('should NOT throw on findOrCreate with validate:true with valid data', function(done) {
|
||||||
|
@ -480,20 +514,40 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('method updateOrCreate on existing data', function() {
|
describe('method updateOrCreate on existing data', function() {
|
||||||
it('should throw on updateOrCreate(id) with validate:true with invalid data', function(done) {
|
it('should throw on updateOrCreate(id) with validate:true with invalid ' +
|
||||||
callUpdateOrCreateWithExistingUserId(null, { validate: true }, expectValidationError(done));
|
'data', function(done) {
|
||||||
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
null,
|
||||||
|
{ validate: true },
|
||||||
|
expectValidationError(done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on updateOrCreate(id) with validate:false with invalid data', function(done) {
|
it('should NOT throw on updateOrCreate(id) with validate:false with ' +
|
||||||
callUpdateOrCreateWithExistingUserId(null, { validate: false }, expectChangeSuccess(INVALID_DATA, done));
|
'invalid data', function(done) {
|
||||||
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
null,
|
||||||
|
{ validate: false },
|
||||||
|
expectChangeSuccess(INVALID_DATA, done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on updateOrCreate(id) with validate:true with valid data', function(done) {
|
it('should NOT throw on updateOrCreate(id) with validate:true with ' +
|
||||||
callUpdateOrCreateWithExistingUserId(NEW_NAME, { validate: true }, expectChangeSuccess(done));
|
'valid data', function(done) {
|
||||||
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
NEW_NAME,
|
||||||
|
{ validate: true },
|
||||||
|
expectChangeSuccess(done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on updateOrCreate(id) with validate:false with valid data', function(done) {
|
it('should NOT throw on updateOrCreate(id) with validate:false with ' +
|
||||||
callUpdateOrCreateWithExistingUserId(NEW_NAME, { validate: false }, expectChangeSuccess(done));
|
'valid data', function(done) {
|
||||||
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
NEW_NAME,
|
||||||
|
{ validate: false },
|
||||||
|
expectChangeSuccess(done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw on updateOrCreate(id) with invalid data', function(done) {
|
it('should throw on updateOrCreate(id) with invalid data', function(done) {
|
||||||
|
@ -501,7 +555,10 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT throw on updateOrCreate(id) with valid data', function(done) {
|
it('should NOT throw on updateOrCreate(id) with valid data', function(done) {
|
||||||
callUpdateOrCreateWithExistingUserId(NEW_NAME, expectChangeSuccess(done));
|
callUpdateOrCreateWithExistingUserId(
|
||||||
|
NEW_NAME,
|
||||||
|
expectChangeSuccess(done)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -542,7 +599,5 @@ describe('optional-validation', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,7 +18,8 @@ var HookMonitor = require('./helpers/hook-monitor');
|
||||||
module.exports = function(dataSource, should, connectorCapabilities) {
|
module.exports = function(dataSource, should, connectorCapabilities) {
|
||||||
if (!connectorCapabilities) connectorCapabilities = {};
|
if (!connectorCapabilities) connectorCapabilities = {};
|
||||||
if (connectorCapabilities.replaceOrCreateReportsNewInstance === undefined) {
|
if (connectorCapabilities.replaceOrCreateReportsNewInstance === undefined) {
|
||||||
console.warn('The connector does not support a recently added feature: replaceOrCreateReportsNewInstance');
|
console.warn('The connector does not support a recently added feature: ' +
|
||||||
|
'replaceOrCreateReportsNewInstance');
|
||||||
}
|
}
|
||||||
describe('Persistence hooks', function() {
|
describe('Persistence hooks', function() {
|
||||||
var ctxRecorder, hookMonitor, expectedError;
|
var ctxRecorder, hookMonitor, expectedError;
|
||||||
|
@ -319,7 +320,6 @@ module.exports = function(dataSource, should, connectorCapabilities) {
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1564,7 +1564,7 @@ module.exports = function(dataSource, should, connectorCapabilities) {
|
||||||
name: 'changed',
|
name: 'changed',
|
||||||
id: data.id,
|
id: data.id,
|
||||||
},
|
},
|
||||||
isNewInstance : false,
|
isNewInstance: false,
|
||||||
}));
|
}));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
@ -2968,7 +2968,6 @@ module.exports = function(dataSource, should, connectorCapabilities) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function nextWithError(err) {
|
function nextWithError(err) {
|
||||||
|
|
|
@ -24,7 +24,6 @@ var getMemoryDataSource = function(settings) {
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('relations', function() {
|
describe('relations', function() {
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
});
|
});
|
||||||
|
@ -63,7 +62,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with scope', function() {
|
describe('with scope', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
Book.hasMany(Chapter);
|
Book.hasMany(Chapter);
|
||||||
done();
|
done();
|
||||||
|
@ -552,7 +550,6 @@ describe('relations', function() {
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('hasMany through', function() {
|
describe('hasMany through', function() {
|
||||||
|
@ -673,7 +670,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
function verify(physician) {
|
function verify(physician) {
|
||||||
physician.patients(function(err, ch) {
|
physician.patients(function(err, ch) {
|
||||||
|
|
||||||
var patients = physician.patients();
|
var patients = physician.patients();
|
||||||
patients.should.eql(ch);
|
patients.should.eql(ch);
|
||||||
|
|
||||||
|
@ -702,7 +698,6 @@ describe('relations', function() {
|
||||||
function verify(physician) {
|
function verify(physician) {
|
||||||
return physician.patients.getAsync()
|
return physician.patients.getAsync()
|
||||||
.then(function(ch) {
|
.then(function(ch) {
|
||||||
|
|
||||||
var patients = physician.patients();
|
var patients = physician.patients();
|
||||||
patients.should.eql(ch);
|
patients.should.eql(ch);
|
||||||
|
|
||||||
|
@ -725,13 +720,13 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
function verify(physician) {
|
function verify(physician) {
|
||||||
//limit plus skip
|
//limit plus skip
|
||||||
physician.patients({ limit:1, skip:1 }, function(err, ch) {
|
physician.patients({ limit: 1, skip: 1 }, function(err, ch) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(ch);
|
should.exist(ch);
|
||||||
ch.should.have.lengthOf(1);
|
ch.should.have.lengthOf(1);
|
||||||
ch[0].name.should.eql('z');
|
ch[0].name.should.eql('z');
|
||||||
//offset plus skip
|
//offset plus skip
|
||||||
physician.patients({ limit:1, offset:1 }, function(err1, ch1) {
|
physician.patients({ limit: 1, offset: 1 }, function(err1, ch1) {
|
||||||
should.not.exist(err1);
|
should.not.exist(err1);
|
||||||
should.exist(ch1);
|
should.exist(ch1);
|
||||||
ch1.should.have.lengthOf(1);
|
ch1.should.have.lengthOf(1);
|
||||||
|
@ -1123,7 +1118,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('hasMany through - collect', function() {
|
describe('hasMany through - collect', function() {
|
||||||
|
@ -1162,8 +1156,17 @@ describe('relations', function() {
|
||||||
|
|
||||||
describe('when custom reverse belongsTo names for both sides', function() {
|
describe('when custom reverse belongsTo names for both sides', function() {
|
||||||
it('can determine the collect via keyThrough', function() {
|
it('can determine the collect via keyThrough', function() {
|
||||||
Physician.hasMany(Patient, { through: Appointment, foreignKey: 'fooId', keyThrough: 'barId' });
|
Physician.hasMany(Patient, {
|
||||||
Patient.hasMany(Physician, { through: Appointment, foreignKey: 'barId', keyThrough: 'fooId', as: 'yyy' });
|
through: Appointment,
|
||||||
|
foreignKey: 'fooId',
|
||||||
|
keyThrough: 'barId',
|
||||||
|
});
|
||||||
|
Patient.hasMany(Physician, {
|
||||||
|
through: Appointment,
|
||||||
|
foreignKey: 'barId',
|
||||||
|
keyThrough: 'fooId',
|
||||||
|
as: 'yyy',
|
||||||
|
});
|
||||||
Appointment.belongsTo(Physician, { as: 'foo' });
|
Appointment.belongsTo(Physician, { as: 'foo' });
|
||||||
Appointment.belongsTo(Patient, { as: 'bar' });
|
Appointment.belongsTo(Patient, { as: 'bar' });
|
||||||
Patient.belongsTo(Address); // jam.
|
Patient.belongsTo(Address); // jam.
|
||||||
|
@ -1217,7 +1220,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when custom reverse belongsTo name for one side only', function() {
|
describe('when custom reverse belongsTo name for one side only', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
Physician.hasMany(Patient, { as: 'xxx', through: Appointment, foreignKey: 'fooId' });
|
Physician.hasMany(Patient, { as: 'xxx', through: Appointment, foreignKey: 'fooId' });
|
||||||
Patient.hasMany(Physician, { as: 'yyy', through: Appointment, keyThrough: 'fooId' });
|
Patient.hasMany(Physician, { as: 'yyy', through: Appointment, keyThrough: 'fooId' });
|
||||||
|
@ -1255,8 +1257,18 @@ describe('relations', function() {
|
||||||
} }});
|
} }});
|
||||||
Address = db.define('Address', { name: String });
|
Address = db.define('Address', { name: String });
|
||||||
|
|
||||||
User.hasMany(User, { as: 'followers', foreignKey: 'followeeId', keyThrough: 'followerId', through: Follow });
|
User.hasMany(User, {
|
||||||
User.hasMany(User, { as: 'following', foreignKey: 'followerId', keyThrough: 'followeeId', through: Follow });
|
as: 'followers',
|
||||||
|
foreignKey: 'followeeId',
|
||||||
|
keyThrough: 'followerId',
|
||||||
|
through: Follow,
|
||||||
|
});
|
||||||
|
User.hasMany(User, {
|
||||||
|
as: 'following',
|
||||||
|
foreignKey: 'followerId',
|
||||||
|
keyThrough: 'followeeId',
|
||||||
|
through: Follow,
|
||||||
|
});
|
||||||
User.belongsTo(Address);
|
User.belongsTo(Address);
|
||||||
Follow.belongsTo(User, { as: 'follower' });
|
Follow.belongsTo(User, { as: 'follower' });
|
||||||
Follow.belongsTo(User, { as: 'followee' });
|
Follow.belongsTo(User, { as: 'followee' });
|
||||||
|
@ -1302,8 +1314,18 @@ describe('relations', function() {
|
||||||
} }});
|
} }});
|
||||||
Address = db.define('Address', { name: String });
|
Address = db.define('Address', { name: String });
|
||||||
|
|
||||||
User.hasMany(User, { as: 'followers', foreignKey: 'followeeId', keyThrough: 'followerId', through: Follow });
|
User.hasMany(User, {
|
||||||
User.hasMany(User, { as: 'following', foreignKey: 'followerId', keyThrough: 'followeeId', through: Follow });
|
as: 'followers',
|
||||||
|
foreignKey: 'followeeId',
|
||||||
|
keyThrough: 'followerId',
|
||||||
|
through: Follow,
|
||||||
|
});
|
||||||
|
User.hasMany(User, {
|
||||||
|
as: 'following',
|
||||||
|
foreignKey: 'followerId',
|
||||||
|
keyThrough: 'followeeId',
|
||||||
|
through: Follow,
|
||||||
|
});
|
||||||
User.belongsTo(Address);
|
User.belongsTo(Address);
|
||||||
Follow.belongsTo(User, { as: 'follower' });
|
Follow.belongsTo(User, { as: 'follower' });
|
||||||
Follow.belongsTo(User, { as: 'followee' });
|
Follow.belongsTo(User, { as: 'followee' });
|
||||||
|
@ -1556,7 +1578,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('polymorphic hasOne', function() {
|
describe('polymorphic hasOne', function() {
|
||||||
|
@ -1703,7 +1724,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('polymorphic hasOne with non standard ids', function() {
|
describe('polymorphic hasOne with non standard ids', function() {
|
||||||
|
@ -2056,7 +2076,6 @@ describe('relations', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('polymorphic hasAndBelongsToMany through', function() {
|
describe('polymorphic hasAndBelongsToMany through', function() {
|
||||||
|
@ -2203,7 +2222,12 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create polymorphic through model', function(done) {
|
it('should create polymorphic through model', function(done) {
|
||||||
PictureLink.findOne({ where: { pictureId: anotherPicture.id, imageableType: 'Author' }}, function(err, link) {
|
PictureLink.findOne({
|
||||||
|
where: {
|
||||||
|
pictureId: anotherPicture.id,
|
||||||
|
imageableType: 'Author',
|
||||||
|
},
|
||||||
|
}, function(err, link) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
link.pictureId.should.eql(anotherPicture.id);
|
link.pictureId.should.eql(anotherPicture.id);
|
||||||
link.imageableId.should.eql(author.id);
|
link.imageableId.should.eql(author.id);
|
||||||
|
@ -2339,7 +2363,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('belongsTo', function() {
|
describe('belongsTo', function() {
|
||||||
|
@ -2549,7 +2572,6 @@ describe('relations', function() {
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('belongsTo with scope', function() {
|
describe('belongsTo with scope', function() {
|
||||||
|
@ -2621,7 +2643,6 @@ describe('relations', function() {
|
||||||
})
|
})
|
||||||
.catch(done);
|
.catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Disable the tests until the issue in
|
// Disable the tests until the issue in
|
||||||
|
@ -2907,11 +2928,9 @@ describe('relations', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('hasOne with scope', function() {
|
describe('hasOne with scope', function() {
|
||||||
|
|
||||||
var Supplier, Account;
|
var Supplier, Account;
|
||||||
var supplierId, accountId;
|
var supplierId, accountId;
|
||||||
|
|
||||||
|
@ -3017,7 +3036,6 @@ describe('relations', function() {
|
||||||
})
|
})
|
||||||
.catch(done);
|
.catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('hasOne with non standard id', function() {
|
describe('hasOne with non standard id', function() {
|
||||||
|
@ -3104,7 +3122,6 @@ describe('relations', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('hasOne with primaryKey different from model PK', function() {
|
describe('hasOne with primaryKey different from model PK', function() {
|
||||||
|
@ -3470,7 +3487,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsOne', function() {
|
describe('embedsOne', function() {
|
||||||
|
|
||||||
var person;
|
var person;
|
||||||
var Passport;
|
var Passport;
|
||||||
var Other;
|
var Other;
|
||||||
|
@ -3480,7 +3496,7 @@ describe('relations', function() {
|
||||||
// db = getSchema();
|
// db = getSchema();
|
||||||
Person = db.define('Person', { name: String });
|
Person = db.define('Person', { name: String });
|
||||||
Passport = tmp.define('Passport',
|
Passport = tmp.define('Passport',
|
||||||
{ name:{ type:'string', required: true }},
|
{ name: { type: 'string', required: true }},
|
||||||
{ idInjection: false }
|
{ idInjection: false }
|
||||||
);
|
);
|
||||||
Address = tmp.define('Address', { street: String }, { idInjection: false });
|
Address = tmp.define('Address', { street: String }, { idInjection: false });
|
||||||
|
@ -3762,7 +3778,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsOne - persisted model', function() {
|
describe('embedsOne - persisted model', function() {
|
||||||
|
|
||||||
// This test spefically uses the Memory connector
|
// This test spefically uses the Memory connector
|
||||||
// in order to test the use of the auto-generated
|
// in order to test the use of the auto-generated
|
||||||
// id, in the sequence of the related model.
|
// id, in the sequence of the related model.
|
||||||
|
@ -3771,7 +3786,7 @@ describe('relations', function() {
|
||||||
db = getMemoryDataSource();
|
db = getMemoryDataSource();
|
||||||
Person = db.define('Person', { name: String });
|
Person = db.define('Person', { name: String });
|
||||||
Passport = db.define('Passport',
|
Passport = db.define('Passport',
|
||||||
{ name:{ type:'string', required: true }}
|
{ name: { type: 'string', required: true }}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -3783,7 +3798,7 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create an item - to offset id', function(done) {
|
it('should create an item - to offset id', function(done) {
|
||||||
Passport.create({ name:'Wilma' }, function(err, p) {
|
Passport.create({ name: 'Wilma' }, function(err, p) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
p.id.should.equal(1);
|
p.id.should.equal(1);
|
||||||
p.name.should.equal('Wilma');
|
p.name.should.equal('Wilma');
|
||||||
|
@ -3814,18 +3829,16 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsOne - generated id', function() {
|
describe('embedsOne - generated id', function() {
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
tmp = getTransientDataSource();
|
tmp = getTransientDataSource();
|
||||||
// db = getSchema();
|
// db = getSchema();
|
||||||
Person = db.define('Person', { name: String });
|
Person = db.define('Person', { name: String });
|
||||||
Passport = tmp.define('Passport',
|
Passport = tmp.define('Passport',
|
||||||
{ id: { type:'string', id: true, generated:true }},
|
{ id: { type: 'string', id: true, generated: true }},
|
||||||
{ name: { type:'string', required: true }}
|
{ name: { type: 'string', required: true }}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -3845,11 +3858,9 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsMany', function() {
|
describe('embedsMany', function() {
|
||||||
|
|
||||||
var address1, address2;
|
var address1, address2;
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -4083,20 +4094,17 @@ describe('relations', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsMany - numeric ids + forceId', function() {
|
describe('embedsMany - numeric ids + forceId', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
tmp = getTransientDataSource();
|
tmp = getTransientDataSource();
|
||||||
// db = getSchema();
|
// db = getSchema();
|
||||||
Person = db.define('Person', { name: String });
|
Person = db.define('Person', { name: String });
|
||||||
Address = tmp.define('Address', {
|
Address = tmp.define('Address', {
|
||||||
id: { type: Number, id:true },
|
id: { type: Number, id: true },
|
||||||
street: String,
|
street: String,
|
||||||
});
|
});
|
||||||
|
|
||||||
db.automigrate(['Person'], done);
|
db.automigrate(['Person'], done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -4120,7 +4128,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsMany - explicit ids', function() {
|
describe('embedsMany - explicit ids', function() {
|
||||||
|
@ -4289,11 +4296,9 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsMany - persisted model', function() {
|
describe('embedsMany - persisted model', function() {
|
||||||
|
|
||||||
var address0, address1, address2;
|
var address0, address1, address2;
|
||||||
var person;
|
var person;
|
||||||
|
|
||||||
|
@ -4415,11 +4420,9 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsMany - relations, scope and properties', function() {
|
describe('embedsMany - relations, scope and properties', function() {
|
||||||
|
|
||||||
var category, job1, job2, job3;
|
var category, job1, job2, job3;
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
|
@ -4647,11 +4650,9 @@ describe('relations', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsMany - polymorphic relations', function() {
|
describe('embedsMany - polymorphic relations', function() {
|
||||||
|
|
||||||
var person1, person2;
|
var person1, person2;
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -4757,7 +4758,6 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include nested related items on scope', function(done) {
|
it('should include nested related items on scope', function(done) {
|
||||||
|
|
||||||
// There's some date duplication going on, so it might
|
// There's some date duplication going on, so it might
|
||||||
// make sense to override toObject on a case-by-case basis
|
// make sense to override toObject on a case-by-case basis
|
||||||
// to sort this out (delete links, keep people).
|
// to sort this out (delete links, keep people).
|
||||||
|
@ -4784,11 +4784,9 @@ describe('relations', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('referencesMany', function() {
|
describe('referencesMany', function() {
|
||||||
|
|
||||||
var job1, job2, job3;
|
var job1, job2, job3;
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -5433,7 +5431,5 @@ describe('relations', function() {
|
||||||
})
|
})
|
||||||
.catch(done);
|
.catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,7 +9,6 @@ var should = require('./init.js');
|
||||||
var db = getSchema(), slave = getSchema(), Model, SlaveModel;
|
var db = getSchema(), slave = getSchema(), Model, SlaveModel;
|
||||||
|
|
||||||
describe('dataSource', function() {
|
describe('dataSource', function() {
|
||||||
|
|
||||||
it('should define Model', function() {
|
it('should define Model', function() {
|
||||||
Model = db.define('Model');
|
Model = db.define('Model');
|
||||||
Model.dataSource.should.eql(db);
|
Model.dataSource.should.eql(db);
|
||||||
|
@ -58,5 +57,4 @@ describe('dataSource', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,7 +9,6 @@ var should = require('./init.js');
|
||||||
var db, Railway, Station;
|
var db, Railway, Station;
|
||||||
|
|
||||||
describe('scope', function() {
|
describe('scope', function() {
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
Railway = db.define('Railway', {
|
Railway = db.define('Railway', {
|
||||||
|
@ -102,11 +101,9 @@ describe('scope', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('scope - order', function() {
|
describe('scope - order', function() {
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
Station = db.define('Station', {
|
Station = db.define('Station', {
|
||||||
|
@ -155,11 +152,9 @@ describe('scope - order', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('scope - filtered count, updateAll and destroyAll', function() {
|
describe('scope - filtered count, updateAll and destroyAll', function() {
|
||||||
|
|
||||||
var stationA;
|
var stationA;
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
|
@ -325,7 +320,6 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('scope - dynamic target class', function() {
|
describe('scope - dynamic target class', function() {
|
||||||
|
@ -410,25 +404,23 @@ describe('scope - dynamic target class', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('scope - dynamic function', function() {
|
describe('scope - dynamic function', function() {
|
||||||
|
|
||||||
var Item, seed = 0;
|
var Item, seed = 0;
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
Item = db.define('Item', { title: Number, creator:Number });
|
Item = db.define('Item', { title: Number, creator: Number });
|
||||||
Item.scope('dynamicQuery', function() {
|
Item.scope('dynamicQuery', function() {
|
||||||
seed++;
|
seed++;
|
||||||
return { where:{ creator:seed }};
|
return { where: { creator: seed }};
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(function(done) {
|
beforeEach(function(done) {
|
||||||
Item.create({ title:1, creator:1 }, function() {
|
Item.create({ title: 1, creator: 1 }, function() {
|
||||||
Item.create({ title:2, creator:2 }, done);
|
Item.create({ title: 2, creator: 2 }, done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
// This file is licensed under the MIT License.
|
// This file is licensed under the MIT License.
|
||||||
// License text available at https://opensource.org/licenses/MIT
|
// License text available at https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
/* eslint-disable camelcase */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if (!process.env.TRAVIS) {
|
if (!process.env.TRAVIS) {
|
||||||
var semicov = require('semicov');
|
var semicov = require('semicov');
|
||||||
|
|
|
@ -16,7 +16,6 @@ var getTransientDataSource = function(settings) {
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('Transient connector', function() {
|
describe('Transient connector', function() {
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
db = getTransientDataSource();
|
db = getTransientDataSource();
|
||||||
TransientModel = db.define('TransientModel', {}, { idInjection: false });
|
TransientModel = db.define('TransientModel', {}, { idInjection: false });
|
||||||
|
@ -82,5 +81,4 @@ describe('Transient connector', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -24,7 +24,6 @@ describe('util.fieldsToArray', function() {
|
||||||
|
|
||||||
it('Turn objects and strings into an array of fields' +
|
it('Turn objects and strings into an array of fields' +
|
||||||
' to include when finding models', function() {
|
' to include when finding models', function() {
|
||||||
|
|
||||||
sample(false).expect(undefined);
|
sample(false).expect(undefined);
|
||||||
sample(null).expect(undefined);
|
sample(null).expect(undefined);
|
||||||
sample({}).expect(undefined);
|
sample({}).expect(undefined);
|
||||||
|
@ -37,7 +36,6 @@ describe('util.fieldsToArray', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should exclude unknown properties', function() {
|
it('should exclude unknown properties', function() {
|
||||||
|
|
||||||
sample(false, true).expect(undefined);
|
sample(false, true).expect(undefined);
|
||||||
sample(null, true).expect(undefined);
|
sample(null, true).expect(undefined);
|
||||||
sample({}, true).expect(undefined);
|
sample({}, true).expect(undefined);
|
||||||
|
@ -77,7 +75,6 @@ describe('util.removeUndefined', function() {
|
||||||
|
|
||||||
var q6 = { where: { x: 1, y: undefined }};
|
var q6 = { where: { x: 1, y: undefined }};
|
||||||
(function() { removeUndefined(q6, 'throw'); }).should.throw(/`undefined` in query/);
|
(function() { removeUndefined(q6, 'throw'); }).should.throw(/`undefined` in query/);
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -94,7 +91,6 @@ describe('util.parseSettings', function() {
|
||||||
should.equal(settings.connector, 'mongodb');
|
should.equal(settings.connector, 'mongodb');
|
||||||
should.equal(settings.w, '2');
|
should.equal(settings.w, '2');
|
||||||
should.equal(settings.url, 'mongodb://x:y@localhost:27017/mydb?w=2');
|
should.equal(settings.url, 'mongodb://x:y@localhost:27017/mydb?w=2');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Parse a url without auth into a settings object', function() {
|
it('Parse a url without auth into a settings object', function() {
|
||||||
|
@ -109,7 +105,6 @@ describe('util.parseSettings', function() {
|
||||||
should.equal(settings.connector, 'mongodb');
|
should.equal(settings.connector, 'mongodb');
|
||||||
should.equal(settings.w, '2');
|
should.equal(settings.w, '2');
|
||||||
should.equal(settings.url, 'mongodb://localhost:27017/mydb/abc?w=2');
|
should.equal(settings.url, 'mongodb://localhost:27017/mydb/abc?w=2');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Parse a url with complex query into a settings object', function() {
|
it('Parse a url with complex query into a settings object', function() {
|
||||||
|
@ -126,7 +121,6 @@ describe('util.parseSettings', function() {
|
||||||
should.equal(settings.x.b, '2');
|
should.equal(settings.x.b, '2');
|
||||||
should.equal(settings.engine, 'InnoDB');
|
should.equal(settings.engine, 'InnoDB');
|
||||||
should.equal(settings.url, 'mysql://127.0.0.1:3306/mydb?x[a]=1&x[b]=2&engine=InnoDB');
|
should.equal(settings.url, 'mysql://127.0.0.1:3306/mydb?x[a]=1&x[b]=2&engine=InnoDB');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Parse a url without auth into a settings object', function() {
|
it('Parse a url without auth into a settings object', function() {
|
||||||
|
@ -139,9 +133,7 @@ describe('util.parseSettings', function() {
|
||||||
should.equal(settings.connector, 'memory');
|
should.equal(settings.connector, 'memory');
|
||||||
should.equal(settings.x, '1');
|
should.equal(settings.x, '1');
|
||||||
should.equal(settings.url, 'memory://?x=1');
|
should.equal(settings.url, 'memory://?x=1');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('mergeSettings', function() {
|
describe('mergeSettings', function() {
|
||||||
|
@ -217,7 +209,6 @@ describe('mergeSettings', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('sortObjectsByIds', function() {
|
describe('sortObjectsByIds', function() {
|
||||||
|
|
||||||
var items = [
|
var items = [
|
||||||
{ id: 1, name: 'a' },
|
{ id: 1, name: 'a' },
|
||||||
{ id: 2, name: 'b' },
|
{ id: 2, name: 'b' },
|
||||||
|
@ -244,11 +235,9 @@ describe('sortObjectsByIds', function() {
|
||||||
var names = sorted.map(function(u) { return u.name; });
|
var names = sorted.map(function(u) { return u.name; });
|
||||||
should.deepEqual(names, ['e', 'c', 'b']);
|
should.deepEqual(names, ['e', 'c', 'b']);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('util.mergeIncludes', function() {
|
describe('util.mergeIncludes', function() {
|
||||||
|
|
||||||
function checkInputOutput(baseInclude, updateInclude, expectedInclude) {
|
function checkInputOutput(baseInclude, updateInclude, expectedInclude) {
|
||||||
var mergedInclude = mergeIncludes(baseInclude, updateInclude);
|
var mergedInclude = mergeIncludes(baseInclude, updateInclude);
|
||||||
should.deepEqual(mergedInclude, expectedInclude,
|
should.deepEqual(mergedInclude, expectedInclude,
|
||||||
|
@ -390,7 +379,6 @@ describe('util.mergeIncludes', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('util.uniq', function() {
|
describe('util.uniq', function() {
|
||||||
|
|
||||||
it('should dedupe an array with duplicate number entries', function() {
|
it('should dedupe an array with duplicate number entries', function() {
|
||||||
var a = [1, 2, 1, 3];
|
var a = [1, 2, 1, 3];
|
||||||
var b = uniq(a);
|
var b = uniq(a);
|
||||||
|
@ -430,7 +418,6 @@ describe('util.uniq', function() {
|
||||||
err.should.be.instanceof(Error);
|
err.should.be.instanceof(Error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('util.toRegExp', function() {
|
describe('util.toRegExp', function() {
|
||||||
|
|
|
@ -23,7 +23,6 @@ function getValidAttributes() {
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('validations', function() {
|
describe('validations', function() {
|
||||||
|
|
||||||
var User, Entry;
|
var User, Entry;
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -61,9 +60,7 @@ describe('validations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('commons', function() {
|
describe('commons', function() {
|
||||||
|
|
||||||
describe('skipping', function() {
|
describe('skipping', function() {
|
||||||
|
|
||||||
it('should NOT skip when `if` is fulfilled', function() {
|
it('should NOT skip when `if` is fulfilled', function() {
|
||||||
User.validatesPresenceOf('pendingPeriod', { if: 'createdByAdmin' });
|
User.validatesPresenceOf('pendingPeriod', { if: 'createdByAdmin' });
|
||||||
var user = new User;
|
var user = new User;
|
||||||
|
@ -103,11 +100,9 @@ describe('validations', function() {
|
||||||
user.pendingPeriod = 1;
|
user.pendingPeriod = 1;
|
||||||
user.isValid().should.be.true;
|
user.isValid().should.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('skipping in async validation', function() {
|
describe('skipping in async validation', function() {
|
||||||
|
|
||||||
it('should skip when `if` is NOT fulfilled', function(done) {
|
it('should skip when `if` is NOT fulfilled', function(done) {
|
||||||
User.validateAsync('pendingPeriod', function(err, done) {
|
User.validateAsync('pendingPeriod', function(err, done) {
|
||||||
if (!this.pendingPeriod) err();
|
if (!this.pendingPeriod) err();
|
||||||
|
@ -163,11 +158,9 @@ describe('validations', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('lifecycle', function() {
|
describe('lifecycle', function() {
|
||||||
|
|
||||||
it('should work on create', function(done) {
|
it('should work on create', function(done) {
|
||||||
delete User.validations;
|
delete User.validations;
|
||||||
User.validatesPresenceOf('name');
|
User.validatesPresenceOf('name');
|
||||||
|
@ -280,7 +273,7 @@ describe('validations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return validation metadata', function() {
|
it('should return validation metadata', function() {
|
||||||
var expected = { name:[{ validation: 'presence', options: {}}] };
|
var expected = { name: [{ validation: 'presence', options: {}}] };
|
||||||
delete User.validations;
|
delete User.validations;
|
||||||
User.validatesPresenceOf('name');
|
User.validatesPresenceOf('name');
|
||||||
var validations = User.validations;
|
var validations = User.validations;
|
||||||
|
@ -290,7 +283,6 @@ describe('validations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('presence', function() {
|
describe('presence', function() {
|
||||||
|
|
||||||
it('should validate presence', function() {
|
it('should validate presence', function() {
|
||||||
User.validatesPresenceOf('name', 'email');
|
User.validatesPresenceOf('name', 'email');
|
||||||
|
|
||||||
|
@ -337,11 +329,9 @@ describe('validations', function() {
|
||||||
user.domain = 'domain';
|
user.domain = 'domain';
|
||||||
user.isValid().should.be.true;
|
user.isValid().should.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('absence', function() {
|
describe('absence', function() {
|
||||||
|
|
||||||
it('should validate absence', function() {
|
it('should validate absence', function() {
|
||||||
User.validatesAbsenceOf('reserved', { if: 'locked' });
|
User.validatesAbsenceOf('reserved', { if: 'locked' });
|
||||||
var u = new User({ reserved: 'foo', locked: true });
|
var u = new User({ reserved: 'foo', locked: true });
|
||||||
|
@ -351,7 +341,6 @@ describe('validations', function() {
|
||||||
var u = new User({ reserved: 'foo', locked: false });
|
var u = new User({ reserved: 'foo', locked: false });
|
||||||
u.isValid().should.be.true;
|
u.isValid().should.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('uniqueness', function() {
|
describe('uniqueness', function() {
|
||||||
|
|
Loading…
Reference in New Issue