.createEach()
Create a set of records in the database.
await Something.createEach(initialValues);
or
var createdRecords = await Something.createEach(initialValues).fetch();
Argument | Type | Details | |
---|---|---|---|
1 | initialValues | 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).
Type | Description |
---|---|
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.) |
Name | Type | When? |
---|---|---|
UsageError | Thrown if something invalid was passed in. | |
AdapterError | 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 | Thrown if anything else unexpected happens. |
See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.
Key | Type | Details |
---|---|---|
fetch | If set to true , then the created records will be sent back.Defaults to false . |
For more information on meta keys, see .meta().
To create users named Finn and Jake in the database:
await User.createEach([{name:'Finn'}, {name: 'Jake'}]);
var createdUsers = User.createEach([{name:'Finn'}, {name: 'Jake'}]).fetch();
sails.log(`Created ${createdUsers.length} user${createdUsers.length===1?'':'s'}.`);
- 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 themax_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).