Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In this video we'll install Sequelize and set our project up with it.
Getting Started
If you're following along, be sure to install the application's dependencies (listed in the provided package.json
file) by running the following command:
npm install
After the application's dependencies have finished installing, run the following command to fix any security vulnerabilities that can be automatically fixed:
npm audit fix
Note: Some security vulnerabilities need to be manually reviewed and updated to be fixed. For the purposes of this workshop, it's okay to hold off on updating these kinds of vulnerabilities.
Sequelize Installation
npm install sequelize
npm install sqlite3
npm install sequelize-cli
Note: As of npm 5.0, the npm install
command will save dependencies by default, so the --save
option isn't necessary to include.
String Based Operators Warning
If you install Sequelize 4.0 (or later), you'll receive the following warning when starting the application:
String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
Updating your Sequelize configuration file (/config/config.json
) to the following will disable string based operators and prevent the warning from displaying:
{
"development": {
"dialect": "sqlite",
"storage": "development.db",
"operatorsAliases": false
},
"test": {
"dialect": "sqlite",
"storage": "test.db",
"operatorsAliases": false
},
"production": {
"dialect": "sqlite",
"storage": "production.db",
"operatorsAliases": false
}
}
For more information about this issue, see http://docs.sequelizejs.com/manual/tutorial/querying.html#operators-security.
Commands
npm 5.2 introduced the
npx
utility, which can be used in place ofnode_modules/.bin/
when interacting with tools like the Sequelize CLI.
To obtain a list of commands:
npx sequelize help
To initialize the project:
npx sequelize init
To see how the model:create
command works:
npx sequelize model:create --help
To generate a model, use the following command pattern:
npx sequelize model:create --name ModelName --attributes attribute1:data_type,attribute2:data_type
To generate the "Article" model shown in the video, use the command:
npx sequelize model:create --name Article --attributes title:string,author:string,body:text
Running this command with version 4.0 of the Sequelize CLI generates the following model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Article = sequelize.define('Article', {
title: DataTypes.STRING,
author: DataTypes.STRING,
body: DataTypes.TEXT
}, {});
Article.associate = function(models) {
// associations can be defined here
};
return Article;
};
Sequelize Methods Used
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up