.exec(function(err) { if (err) { return done(err); } assert.equal(generatedQuery.joins.length, 2); assert.equal(generatedQuery.joins[0].parent, 'user'); assert.equal(generatedQuery.joins[0].parentKey, 'user_id'); assert.equal(generatedQuery.joins[0].child, 'car_drivers__user_cars'); assert.equal(generatedQuery.joins[0].childKey, 'user_cars'); assert.equal(generatedQuery.joins[0].select, false); assert.equal(generatedQuery.joins[0].removeParentKey, false); assert.equal(generatedQuery.joins[1].parent, 'car_drivers__user_cars'); assert.equal(generatedQuery.joins[1].parentKey, 'car_drivers'); assert.equal(generatedQuery.joins[1].child, 'car'); assert.equal(generatedQuery.joins[1].childKey, 'car_id'); assert(_.isArray(generatedQuery.joins[1].criteria.select)); assert.equal(generatedQuery.joins[1].criteria.select[0], 'car_id'); assert.equal(generatedQuery.joins[1].criteria.select[1], 'car_name'); assert(_.isArray(generatedQuery.joins[1].criteria.sort)); assert(generatedQuery.joins[1].criteria.sort[0].car_name); assert.equal(generatedQuery.joins[1].removeParentKey, false); return done(); });
.exec(function(err, stadiums) { if (err) { return done(err); } assert(_.isArray(stadiums)); assert.equal(stadiums.length, 1); assert(_.isArray(stadiums[0].teams)); assert.equal(stadiums[0].teams.length, 1); return done(); });
return function leaveRoom (sockets, roomName, cb) { // Make cb optional cb = cb || function(){}; // Make sure "sockets" is an array if (!_.isArray(sockets)) { sockets = [sockets]; } async.each(sockets, function(socket, nextSocket) { // If a string was sent, try to look up a socket with that ID if (typeof socket !== 'object' && socket !== null) { // If we don't find one, it could be on another server, so use // the cross-server "removeRoomMembersFromRooms" if (!app.io.sockets.connected[socket]) { return app.sockets.removeRoomMembersFromRooms(socket, roomName, nextSocket); } // Otherwise get the socket object and continue socket = app.io.sockets.connected[socket]; } // If it's not a valid socket object, bail socket = app.sockets.parseSocket(socket); if (!socket) { app.log.warn("Attempted to call `sailsSockets.leave`, but the first argument was not a socket."); return; } // See ya! socket.leave(roomName, nextSocket); }, cb); return true; };
emitter.after = function(events, fn) { // Support a single event or an array of events if (!_.isArray(events)) { events = [events]; } // Convert named event dependencies into an array // of async-compatible functions. var dependencies = _.reduce(events, function(dependencies, event) { var handlerFn = function(cb) { if (emitter.warmEvents[event]) { cb(); } else { emitter.once(event, cb); } }; dependencies.push(handlerFn); return dependencies; }, []); // When all events have fired, call `fn` // (all arguments passed to `emit()` calls are discarded) async.parallel(dependencies, function(err) { if (err) sails.log.error(err); return fn(); }); };
module.exports = function hasSchemaCheck(context) { // If hasSchema is defined on the collection, return the value if (_.has(Object.getPrototypeOf(context), 'hasSchema')) { return Object.getPrototypeOf(context).hasSchema; } // Grab the first connection used if (!context.connection || !_.isArray(context.connection)) { return true; } var connection = context.connections[_.first(context.connection)]; // Check the user defined config if (_.has(connection, 'config') && _.has(connection.config, 'schema')) { return connection.config.schema; } // Check the defaults defined in the adapter if (!_.has(connection, 'adapter')) { return true; } if (!_.has(connection.adapter, 'schema')) { return true; } return connection.adapter.schema; }
/** * Load built-in hook definitions from `sails.config.hooks` * and put them back into `hooks` (probably `sails.hooks`) * * @api private */ function loadHookDefinitions(hooks, cb) { // Mix in user-configured hook definitions _.extend(hooks, sails.config.hooks); // Make sure these changes to the hooks object get applied // to sails.config.hooks to keep logic consistent // (I think we can get away w/o this, but leaving as a stub) // sails.config.hooks = hooks; // If user configured `loadHooks`, only include those. if (sails.config.loadHooks) { if (!_.isArray(sails.config.loadHooks)) { return cb('Invalid `loadHooks` config. ' + 'Please specify an array of string hook names.\n' + 'You specified ::' + util.inspect(sails.config.loadHooks)); } _.each(hooks, function(def, hookName) { if (!_.contains(sails.config.loadHooks, hookName)) { hooks[hookName] = false; } }); sails.log.verbose('Deliberate partial load-- will only initialize hooks ::', sails.config.loadHooks); } return cb(); }
.exec(function(err, drivers) { if (err) { return done(err); } var driver = drivers[0]; assert(driver); assert(_.isArray(driver.taxis)); assert.equal(driver.taxis.length, 6); // Expected results: // { medallion: 2, model: 'even', note: 'low' }, // { medallion: 3, model: 'odd', note: 'low' }, // { medallion: 4, model: 'even', note: 'low' }, // { medallion: 5, model: 'odd', note: 'high' }, // { medallion: 6, model: 'even', note: 'high' }, // { medallion: 7, model: 'odd', note: 'high' }, assert.equal(driver.taxis[0].medallion, 7); assert.equal(driver.taxis[1].medallion, 6); assert.equal(driver.taxis[2].medallion, 5); assert.equal(driver.taxis[3].medallion, 4); assert.equal(driver.taxis[4].medallion, 3); assert.equal(driver.taxis[5].medallion, 2); return done(); });
query.find({}, {}, function(err, values) { if (err) { return done(err); } assert(_.isArray(values)); return done(); });
module.exports = function CollectionBuilder(collection, datastores, context) { // ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┌─┐┬ ┬ ┌─┐┌─┐┌┬┐┬┌─┐┌┐┌ // ╚╗╔╝╠═╣║ ║ ║║╠═╣ ║ ║╣ │ │ ││ │ ├┤ │ │ ││ ││││ // ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ └─┘└─┘┴─┘┴─┘└─┘└─┘ ┴ ┴└─┘┘└┘ // Throw Error if no Tablename/Identity is set if (!_.has(collection.prototype, 'tableName') && !_.has(collection.prototype, 'identity')) { throw new Error('A tableName or identity property must be set.'); } // Find the datastores used by this collection. If none are specified check // if a default datastores exist. if (!_.has(collection.prototype, 'connection')) { // Check if a default connection was specified if (!_.has(datastores, 'default')) { throw new Error('No adapter was specified for collection: ' + collection.prototype.identity); } // Set the connection as the default collection.prototype.connection = 'default'; } // ╔═╗╔═╗╔╦╗ ┌─┐┌─┐┌┬┐┬┬ ┬┌─┐ ┌┬┐┌─┐┌┬┐┌─┐┌─┐┌┬┐┌─┐┬─┐┌─┐┌─┐ // ╚═╗║╣ ║ ├─┤│ │ │└┐┌┘├┤ ││├─┤ │ ├─┤└─┐ │ │ │├┬┘├┤ └─┐ // ╚═╝╚═╝ ╩ ┴ ┴└─┘ ┴ ┴ └┘ └─┘ ─┴┘┴ ┴ ┴ ┴ ┴└─┘ ┴ └─┘┴└─└─┘└─┘ // Hold the used datastores var usedDatastores = {}; // Normalize connection to array if (!_.isArray(collection.prototype.connection)) { collection.prototype.connection = [collection.prototype.connection]; } // Set the datastores used for the adapter _.each(collection.prototype.connection, function(connName) { // Ensure the named connection exist if (!_.has(datastores, connName)) { throw new Error('The connection ' + connName + ' specified in ' + collection.prototype.identity + ' does not exist.)'); } // Make the datastore as used by the collection usedDatastores[connName] = datastores[connName]; // Add the collection to the datastore listing datastores[connName].collections.push(collection.prototype.identity); }); // ╦╔╗╔╔═╗╔╦╗╔═╗╔╗╔╔╦╗╦╔═╗╔╦╗╔═╗ ┌┬┐┬ ┬┌─┐ ┌─┐┌─┐┬ ┬ ┌─┐┌─┐┌┬┐┬┌─┐┌┐┌ // ║║║║╚═╗ ║ ╠═╣║║║ ║ ║╠═╣ ║ ║╣ │ ├─┤├┤ │ │ ││ │ ├┤ │ │ ││ ││││ // ╩╝╚╝╚═╝ ╩ ╩ ╩╝╚╝ ╩ ╩╩ ╩ ╩ ╚═╝ ┴ ┴ ┴└─┘ └─┘└─┘┴─┘┴─┘└─┘└─┘ ┴ ┴└─┘┘└┘ var configuredCollection = new collection(context, usedDatastores); return configuredCollection; };
return function addRoomMembersToRooms (sourceRoom, destRooms, cb) { // Make cb optional cb = cb || function(){}; // Make sure "sourceRoom" is a string if (!_.isString(sourceRoom)) { if (!cb) {app.log.error("Non string value used as `sourceRoom` argument in `addRoomMembersToRooms`: ", sourceRoom);} return cb(new Error("Non string value used as `sourceRoom` argument in `addRoomMembersToRooms`")); } // Ensure that destRooms is an array if (!_.isArray(destRooms)) { destRooms = [destRooms]; } // If we were sent a socket ID as a room name, and the socket happens to // be connected to this server, take a shortcut if (app.io.sockets.connected[sourceRoom]) { return doJoin(app.io.sockets.connected[sourceRoom], cb); } // Broadcast an admin message telling all other connected servers to // run `addRoomMembersToRooms` with the same arguments, unless the // "remote" flag is set if (!this.remote) { app.hooks.sockets.broadcastAdminMessage('join', {sourceRoom: sourceRoom, destRooms: destRooms}); } // Look up all members of sourceRoom return app.io.sockets.in(sourceRoom).clients(function(err, sourceRoomSocketIds) { if (err) {return cb(err);} // Loop through the socket IDs from the room async.each(sourceRoomSocketIds, function(socketId, nextSocketId) { // Check if the socket is connected to this server (since .clients() may someday work cross-server) if (app.io.sockets.connected[socketId]) { // If so, subscribe it to destRooms return doJoin(app.io.sockets.connected[socketId], nextSocketId); } // If not, just continue return nextSocketId(); }, cb); }); function doJoin(socket, cb) { return async.each(destRooms, function(destRoom, nextRoom) { // Ensure destRoom is a string if (!_.isString(destRoom)) { app.log.warn("Skipping non-string value for room name to add in `addRoomMembersToRooms`: ", destRoom); return nextRoom(); } return socket.join(destRoom, nextRoom); }, cb); } };
module.exports = function processEachRecord(options) { // ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐ // ╚╗╔╝╠═╣║ ║ ║║╠═╣ ║ ║╣ │ │├─┘ │ ││ ││││└─┐ // ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ └─┘┴ ┴ ┴└─┘┘└┘└─┘ if (_.isUndefined(options) || !_.isPlainObject(options)) { throw new Error('Invalid options argument. Options must contain: records, identity, and orm.'); } if (!_.has(options, 'records') || !_.isArray(options.records)) { throw new Error('Invalid option used in options argument. Missing or invalid records.'); } if (!_.has(options, 'identity') || !_.isString(options.identity)) { throw new Error('Invalid option used in options argument. Missing or invalid identity.'); } if (!_.has(options, 'orm') || !_.isPlainObject(options.orm)) { throw new Error('Invalid option used in options argument. Missing or invalid orm.'); } // Key the collections by identity instead of column name var collections = _.reduce(options.orm.collections, function(memo, val) { memo[val.identity] = val; return memo; }, {}); options.orm.collections = collections; // Run all the records through the iterator so that they can be normalized. // > (This should *never* go more than one level deep!) eachRecordDeep(options.records, function iterator(record, WLModel, depth) { if (depth !== 1) { throw new Error('Consistency violation: Incoming new records in a s3q should never necessitate deep iteration! If you are seeing this error, it is probably because of a bug in this adapter, or in Waterline core.'); } _.each(WLModel.definition, function checkAttributes(attrDef) { var columnName = attrDef.columnName; // JSON stringify any type of JSON attributes that have array values because // the queries won't be generated correctly otherwise. if (attrDef.type === 'json' && _.has(record, columnName)) { if (_.isArray(record[columnName]) || _.isString(record[columnName])) { record[columnName] = JSON.stringify(record[columnName]); } } // For attributes using the `bigint` column type, coerce empty string to zero. // This allows use of `type: 'string'` with bigint, which we want because the pg driver // returns bigints as strings. And since the regular `int` field in postgres is too small // to hold JS timestamps, users are forced to use `bigint` to hold them. if (_.get(attrDef, 'autoMigrations.columnType') === 'bigint' && record[columnName] === '') { record[columnName] = 0; } }); }, true, options.identity, options.orm); };
}, function(err, users) { if (err){ return done(err); } if (users.length !== 1) { return done(new Error('Expected there to be exactly one record returned!')); } if (!_.isArray(users[0].pets) || users[0].pets.length !== 0) { return done(new Error('Expected base value for populated `pets` (i.e. empty array)')); } return done(); });//_∏_
findButWithFinalAfterExecLC(true).exec(function (err, result) { try { assert(!err, 'Got unexpected error in test: '+err); assert(_.isArray(result), 'Expecting result to be an array! But instead got: '+result); assert.equal(result.length, 1); assert.equal(result[0], true); } catch (e) { return done(e); } return done(); });
Adapter.find('test', query, function(err, results) { if (err) { return done(err); } assert(_.isArray(results)); assert.equal(results.length, 2); return done(); });
.exec(function(err, driver) { if (err) { return done(err); } assert(_.isArray(driver.taxis)); assert.equal(driver.taxis.length, 2); return done(); });
.exec(function(err, results) { if (err) { return done(err); } assert(_.isArray(results)); assert.equal(results[0].criteria.skip, 0); assert.equal(results[0].criteria.limit, 30); return done(); });
Deferred.prototype.omit = function(attributes) { if(!_.isArray(attributes)) { attributes = [attributes]; } var omit = this._wlQueryInfo.criteria.omit || []; omit = omit.concat(attributes); this._wlQueryInfo.criteria.omit = _.uniq(omit); return this; };
Queryable.Userforqueryableinterface.find({ first_name: { contains: part }}, function(err, users) { if (err) { return done(err); } assert(_.isArray(users)); assert.equal(users.length, 1); assert.equal(users[0].first_name, testName); return done(); });
.exec(function(err, customer) { if (err) { return done(err); } assert(customer); assert(_.isArray(customer.payments)); assert.equal(customer.payments.length, 3); return done(); });
.exec(function(err, users) { if (err) { return done(err); } assert(_.isArray(users)); assert.strictEqual(users.length, 3); assert.strictEqual(users[0].age, 41); return done(); });
.exec(function(err, users) { if (err) { return done(err); } assert(_.isArray(users)); assert.strictEqual(users.length, 7); assert.equal(users[0].first_name, '3 greaterThan_strings_user'); return done(); });
.exec(function(err, users) { if (err) { return done(err); } try { assert(_.isArray(users)); assert.strictEqual(users.length, 10); assert.equal(users[0].last_name, 'updated'); } catch (e) { return done(e); } return done(); });
Deferred.prototype.select = function(attributes) { if(!_.isArray(attributes)) { attributes = [attributes]; } var select = this._wlQueryInfo.criteria.select || []; select = select.concat(attributes); this._wlQueryInfo.criteria.select = _.uniq(select); return this; };
.exec(function(err, payments) { if (err) { return done(err); } assert(_.isArray(payments)); assert.equal(payments.length, 1); assert(_.isNull(payments[0].customer)); return done(); });
.exec(function(err, customer) { if (err) { return done(err); } // console.log('%%%%%%%%% => got customer:',customer); try { assert(customer); assert(_.isArray(customer.payments)); assert.equal(customer.payments.length, 4); } catch (e) { return done(e); } return done(); });
Semantic.Thing.update(thingId, { description: 'An updated thing' }, function(err, things) { if (err) { return done(err); } assert(_.isArray(things)); assert.strictEqual(things.length, 1); assert.equal(things[0].id, thingId); assert.equal(things[0].description, 'An updated thing'); return done(); });
.exec(function(err, results) { if (err) { return done(err); } assert(_.isArray(results)); assert.equal(results[0].criteria.limit, 1); assert.equal(results[0].criteria.skip, 1); assert.equal(results[0].criteria.sort[0].name, 'DESC'); return done(); });
.exec(function(err, customers) { if (err) { return done(err); } assert(_.isArray(customers)); assert.strictEqual(customers.length, 2); assert(_.isArray(customers[0].payments)); assert(_.isArray(customers[1].payments)); assert.strictEqual(customers[0].payments.length, 2, 'Expected 2 payments, but got customers[0] ==> ' +require('util').inspect(customers[0], false, null)); assert.strictEqual(customers[1].payments.length, 2); assert.equal(customers[0].payments[0].amount, 0); assert.equal(customers[0].payments[1].amount, 1, 'Expected amount of second associated payment to === 1, but instead here is the customer:'+util.inspect(customers[0], false, null)); assert.equal(customers[1].payments[0].amount, 0); assert.equal(customers[1].payments[1].amount, 1); return done(); });
Adapter.find('test', query, function(err, results) { if (err) { return done(err); } assert(_.isArray(results)); assert.equal(results.length, 1); assert.equal(_.first(results).fieldA, 'foo_2'); assert.equal(_.first(results).fieldB, 'bAr_2'); return done(); });
.exec(function(err, users) { if (err) { return done(err); } assert(_.isArray(users)); assert.strictEqual(users.length, 2); assert.equal(users[0].first_name, 'OR_user0'); assert.equal(users[1].first_name, 'OR_user1'); return done(); });