Edit Page

.createEach()

Create a set of records in the database.

await Something.createEach(initialValues);

or

Usage

#
Argument Type Details
1 initialValues Array? An array of dictionaries with attributes for the new records.

Note: For performance reasons, as of Sails v1.0 / Waterline 0.13, the dictionaries in the initialValues array passed into this model method will be mutated in-place in most situations (whereas in Sails/Waterline v0.12, this was not necessarily the case).

Result
#
Type Description
Array? of Dictionary The created records are not provided as a result by default, in order to optimize for performance. To override the default setting, chain .fetch() and the newly created records will be sent back. (Be aware that this requires an extra database query in some adapters.)
Errors
#
Name Type When?
UsageError Error Thrown if something invalid was passed in.
AdapterError Error Thrown if something went wrong in the database adapter. See Concepts > Models and ORM > Errors for an example of how to negotiate a uniqueness error (arising from an attempt to create a record with a duplicate value that would violate a uniqueness constraint).
Error Error Thrown if anything else unexpected happens.

See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.

Meta keys
#
Key Type Details
fetch Boolean If set to true, then the created records will be sent back.

Defaults to false.

For more information on meta keys, see .meta().

Example

#

To create users named Finn and Jake in the database:

await User.createEach([{name:'Finn'}, {name: 'Jake'}]);
Fetching newly created records
#
var createdUsers = User.createEach([{name:'Finn'}, {name: 'Jake'}]).fetch();
sails.log(`Created ${createdUsers.length} user${createdUsers.length===1?'':'s'}.`);

Notes

#
  • This method can be used with await, promise chaining, or traditional Node callbacks.
  • The number of records you can add with .createEach is limited by the maximum query size of the particular database you’re using. MySQL has a 4MB limit by default, but this can be changed via the max_allowed_packet setting. MongoDB imposes a 16MB limit on single documents, but essentially has no limit on the number of documents that can be created at once. PostgreSQL has a very large (around 1GB) maximum size. Consult your database’s documentation for more information about query limitations.
  • Another thing to watch out for when doing very large bulk inserts is the maximum number of bound variables. This varies per databases but refers to the number of values being substituted in a query. See maxmimum allowable parameters for more details.
  • When using .fetch() and manually specifying primary key values for new records, the sort order of returned records is not guaranteed (it varies depending on the database adapter in use).

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference