Skip to main content

Command Palette

Search for a command to run...

Node JS with Sequelize ORM Mysql

Published
2 min readView as Markdown
Node JS with Sequelize ORM Mysql
T

I'm a full-stack software developer creating open-source projects and writing about modern JavaScript client-side and server-side. Working remotely from India.

Sequelize is a promise-based ORM for Node.js and io.js. It supports the dialects PostgreSQL, MySQL, MariaDB, SQLite and MSSQL and features solid transaction support, relations, read replication and more. from

[Getting Started - Sequelize | The Node.js / io.js ORM for PostgreSQL, MySQL, SQLite and MSSQL
Sequelize is available via NPM. $ npm install --save sequelize # And one of the following: $ npm install --save pg…sequelize.org](https://sequelize.org/v3/docs/getting-started/ "https://sequelize.org/v3/docs/getting-started/")

Example usage from https://sequelize.org

var Sequelize = require('sequelize');  
var sequelize = new Sequelize('database', 'username', 'password');
var User = sequelize.define('user', {  
  username: Sequelize.STRING,  
  birthday: Sequelize.DATE  
});
sequelize.sync().then(function() {  
  return User.create({  
    username: 'janedoe',  
    birthday: new Date(1980, 6, 20)  
  });  
}).then(function(jane) {  
  console.log(jane.get({  
    plain: true  
  }));  
});

Basic Installation

Sequelize is available via NPM.

$ npm install --save sequelize
# And one of the following:  
$ npm install --save pg pg-hstore  
$ npm install --save mysql // For both mysql and mariadb dialects  
$ npm install --save sqlite3  
$ npm install --save tedious // MSSQL

Setting up a connection

Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database.

var sequelize = new Sequelize('database', 'username', 'password', {  
  host: 'localhost',  
  dialect: 'mysql'|'mariadb'|'sqlite'|'postgres'|'mssql',
 pool: {  
    max: 5,  
    min: 0,  
    idle: 10000  
  },
 // SQLite only  
  storage: 'path/to/database.sqlite'  
});
// Or you can simply use a connection uri  
var sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');

The Sequelize constructor takes a whole slew of options that are available via the API reference.

Your first model

Models are defined with sequelize.define('name', {attributes}, {options}).

var User = sequelize.define('user', {  
  firstName: {  
    type: Sequelize.STRING,  
    field: 'first_name' // Will result in an attribute that is firstName when user facing but first_name in the database  
  },  
  lastName: {  
    type: Sequelize.STRING  
  }  
}, {  
  freezeTableName: true // Model tableName will be the same as the model name  
});
User.sync({force: true}).then(function () {  
  // Table created  
  return User.create({  
    firstName: 'John',  
    lastName: 'Hancock'  
  });  
});

Based on My learning I have posted good set of Videos to learn more about sequelize, how it works with Node JS, how to write Migrations and seeders.

Please like share & subscribe these Tutorials.

Reference —

[Getting Started - Sequelize | The Node.js / io.js ORM for PostgreSQL, MySQL, SQLite and MSSQL
Sequelize is available via NPM. $ npm install --save sequelize # And one of the following: $ npm install --save pg…sequelize.org](https://sequelize.org/v3/docs/getting-started/ "https://sequelize.org/v3/docs/getting-started/")

More from this blog

C

Code with tkssharma || blogs for developers

349 posts

I’m Tarun, I am Publisher, Trainer Developer, working on Enterprise and open source Technologies JavaScript frameworks