Skip to main content

Operations (CRUD) Reference

The four major operations that are typically performed on data are Create, Read, Update and Delete (CRUD). Using these four operations, we can effectively work with the data in our database. Assume we have a simple Name model defined:

// Define a "Name" model

const Name = sequelize.define('Name', {
fName: Sequelize.STRING, // first Name
lName: Sequelize.STRING, // Last Name
});

We can use the following code to Create new names, Read a list of names, Update a specific name and lastly Delete a name from the "Name" table in our database

Create

To create new names in our Name table, we can use the following code:

sequelize.sync().then(() => {
Name.create({
fName: 'Kyler',
lName: 'Odin',
}).then(() => {
console.log('Kyler Odin created');
});

Name.create({
fName: 'Grier',
lName: 'Garrick',
}).then(() => {
console.log('Grier Garrick created');
});

Name.create({
fName: 'Kolby',
lName: 'Greyson',
}).then(() => {
console.log('Kolby Greyson created');
});
});

In the above code we create three new objects following the fields defined in our "Name" model. Since our "Name" model is synchronized with the database, this adds three new records - each with their own unique "id" value, as well as "createdAt" and "updatedAt" values for the implicit primary key and timestamp columns. The create function automatically persists the new object to the database and since it also returns a promise, we can execute code after the operation is complete. In this case we simply output the name to the console.

Read

To read entries from our Name table, we can use the following code:

sequelize.sync().then(() => {
// return all first names only
Name.findAll({
attributes: ['fName'],
}).then((data) => {
console.log('All first names');
for (let i = 0; i < data.length; i++) {
console.log(data[i].fName);
}
});

// return all first names where id == 2
Name.findAll({
attributes: ['fName'],
where: {
id: 2,
},
}).then((data) => {
console.log('All first names where id == 2');
for (let i = 0; i < data.length; i++) {
console.log(data[i].fName);
}
});
});

Here, we are once again using a reference to our "Name" model. This time we are using it to fetch data from the "Name" table using the findAll() method. This method takes a number of configuration options in it's object parameter, such as attributes, which allows you to limit the columns that are returned (in this case we only want 'fName') and a where parameter that enables us to specify conditions that the data must meet to be returned. In the above example, id must have a value of 2. See the documentation for advanced queries for more detailed query information.

Lastly, we can also specify an order that the returned data should be in, ie:

sequelize.sync().then(() => {
// return all first names only
Name.findAll({ order: ['fName'] }).then((data) => {
console.log('All data');
for (let i = 0; i < data.length; i++) {
console.log(data[i].fName);
}
});
});

NOTE Trying to log a model instance directly to console.log (ie: console.log(data[i])) will produce a lot of clutter, since Sequelize instances have a lot of things attached to them. Instead, you can use the .toJSON() method (which automatically guarantees the instances to be JSON.stringify-ed well). See sequelize.org - logging instances for more information.

Update

To update existing names in our Name table, we can use the following code:

sequelize.sync().then(() => {
// update User 2's last name to "James"
// NOTE: this also updates the "updatedAt field"
Name.update(
{
lName: 'James',
},
{
where: { id: 2 }, // only update user with id == 2
}
).then(() => {
console.log('successfully updated user 2');
});
});

In order to "update" a record in the "Name" table, we make use of the update method. This method takes two parameters: an object that contains all of the properties and (updated) values for a record, and a second object that is used to specify options for the update - most importantly, the "where" property. The "where" property contains an object that is used to specify exactly which record should be updated. In this case, it is the row that has an id value of 2.

Delete

To delete existing names in our Name table, we can use the following code:

sequelize.sync().then(() => {
// remove User 3 from the database
Name.destroy({
where: { id: 3 }, // only remove user with id == 3
}).then(() => {
console.log('successfully removed user 3');
});
});

The delete functionality is actually achieved via a method called destroy. In this case, we invoke the destroy method on the model that contains the record that we wish to remove (ie, "Name"). It takes a single options object as it's only parameter and like the update function, the most important option is the "where" property. The "where" property contains an object that is used to specify exactly which record should be removed. In this case, it is the row that has an id value of 3.