Creating persistent instances

While an instance created with .build() requires an explicit .save() call to be stored in the database, .create() omits that requirement altogether and automatically stores your instance's data once called.

  1. Task.create({ title: 'foo', description: 'bar', deadline: new Date() }).then(task => {
  2. // you can now access the newly created task via the variable task
  3. })

It is also possible to define which attributes can be set via the create method. This can be especially very handy if you create database entries based on a form which can be filled by a user. Using that would for example allow you to restrict the User model to set only a username and an address but not an admin flag:

  1. User.create({ username: 'barfooz', isAdmin: true }, { fields: [ 'username' ] }).then(user => {
  2. // let's assume the default of isAdmin is false:
  3. console.log(user.get({
  4. plain: true
  5. })) // => { username: 'barfooz', isAdmin: false }
  6. })