UNPKG

sails

Version:

API-driven framework for building realtime apps, using MVC conventions (based on Express and Socket.io)

59 lines (47 loc) 1.85 kB
/** * Module dependencies */ var _ = require('@sailshq/lodash'); var actionUtil = require('../actionUtil'); var formatUsageError = require('../formatUsageError'); /** * Find Records * * http://sailsjs.com/docs/reference/blueprint-api/find * * An API call to find and return model instances from the data adapter * using the specified criteria. If an id was specified, just the instance * with that unique id will be returned. * */ module.exports = function findRecords (req, res) { var parseBlueprintOptions = req.options.parseBlueprintOptions || req._sails.config.blueprints.parseBlueprintOptions; // Set the blueprint action for parseBlueprintOptions. req.options.blueprintAction = 'find'; var queryOptions = parseBlueprintOptions(req); var Model = req._sails.models[queryOptions.using]; Model .find(queryOptions.criteria, queryOptions.populates).meta(queryOptions.meta) .exec(function found(err, matchingRecords) { if (err) { // If this is a usage error coming back from Waterline, // (e.g. a bad criteria), then respond w/ a 400 status code. // Otherwise, it's something unexpected, so use 500. switch (err.name) { case 'UsageError': return res.badRequest(formatUsageError(err, req)); default: return res.serverError(err); } }//-• if (req._sails.hooks.pubsub && req.isSocket) { Model.subscribe(req, _.pluck(matchingRecords, Model.primaryKey)); // Only `._watch()` for new instances of the model if // `autoWatch` is enabled. if (req.options.autoWatch) { Model._watch(req); } // Also subscribe to instances of all associated models _.each(matchingRecords, function (record) { actionUtil.subscribeDeep(req, record); }); }//>- return res.ok(matchingRecords); });//</ .find().exec() > };