.create()
Create a record in the database.
await Something.create(initialValues);
or
var createdRecord = await Something.create(initialValues).fetch();
Argument | Type | Details | |
---|---|---|---|
1 | initialValues | The initial values for the new record. (Note that, if this model is in "schemaful" mode, then any extraneous keys will be silently omitted.) |
Note: For performance reasons, as of Sails v1.0 / Waterline 0.13, the
initialValues
dictionary 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 |
---|---|
For improved performance, the created record is not provided as a result by default. But if you chain .fetch() , then the newly-created record 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 (i.e. from attempting to create a record with a duplicate 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 record will be sent back.Defaults to false . |
For more information on meta keys, see .meta().
To create a user named Finn in the database:
await User.create({name:'Finn'});
return res.ok();
var createdUser = await User.create({name:'Finn'}).fetch();
sails.log('Finn\'s id is:', createdUser.id);
It's important to always handle errors from model methods. But sometimes, you need to look at errors in a more granular way. To learn more about the kinds of errors Waterline returns, and for examples of how to handle them, see Concepts > Models and ORM > Errors.
- This method can be used with
await
, promise chaining, or traditional Node callbacks.