Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between .save and .create in Sequelizejs?

I'm new to Sequelize, and trying hard to understand how this very strange, new world of ORMs works. Once thing that I can't seem to understand is the difference between ".create" and ".save" in Sequelizejs. I have written test functions with both, and besides having slightly different syntax, they both seem to do exactly the same thing.

This is using the ".save" method

 models.User.build({
    username: req.body.username,
    password: req.body.password,
    first_name: req.body.firstName,
    last_name: req.body.lastName
  })
  .save()
  .then(function(task){
    // some function...
  })
  .catch(function(error){
    // some function...
  });

This is using the ".create" method

  models.User.create({
    username: req.body.username,
    password: req.body.password,
    first_name: req.body.firstName,
    last_name: req.body.lastName
  }).then(function(data) {
    // some function...
  });

What am I not seeing here?

like image 707
Jonathan Avatar asked May 02 '15 12:05

Jonathan


People also ask

How do you define a model in Sequelize?

Models can be defined in two equivalent ways in Sequelize: Calling sequelize. define(modelName, attributes, options) Extending Model and calling init(attributes, options)

What does Sequelize reload do?

The reload call generates a SELECT query to get the up-to-date data from the database.

What is an instance in Sequelize?

Instances are the individual results from a Sequelize call. In SequelizeMock, these objects have most of the same attributes as in the normal Sequelize Instance, however they also have a few additional bits of functionality.

What is Sequelize and why it is used?

Sequelize is a Node. js-based Object Relational Mapper that makes it easy to work with MySQL, MariaDB, SQLite, PostgreSQL databases, and more. An Object Relational Mapper performs functions like handling database records by representing the data as objects.


1 Answers

As described in the docs http://docs.sequelizejs.com/en/latest/docs/instances/

The method .build() creates a non-persistent instance, which means that the data are not been saved on the database yet, but stored only in the memory, during the execution. When your program stops (server crash, end of the execution or something like that), instances created with .build() will be lost.

This is where .save() do its job. It stores the data of the instance built by the .build() method in the the database.

This approach allows you to manipulate instances in the way you need before storing it in the database.

The .create() method simply .build() and .save() an instance in the same command. It's a convinience for simple cases where you have no need to manipulate instances, allowing you to store the data in the database with a single command. To ilustrate:

This:

User.build({ name: "John" }).save().then(function(newUser){
    console.log(newUser.name); // John
    // John is now in your db!
}).catch(function(error){
    // error
});

is the same as this:

User.create({ name: "John"}).then(function(newUser){
    console.log(newUser.name); // John
    // John is now in your db!
}).catch(function(error){
    // error
});

But you can do something like this:

var user = User.build({ name: "John"}); // nothing in your db yet

user.name = "Doe"; // still, nothing on your db

user.save().then(function(newUser){
    console.log(newUser.name); // Doe
    // Doe is now in your db!
}).catch(function(error){
    // error
});

Basically, .build() and .save() gives you the ability to modify an instance after it has been instanciated, but before you store it's data in the database.

like image 126
Matheus Dal'Pizzol Avatar answered Oct 11 '22 10:10

Matheus Dal'Pizzol