# Node JS with Sequelize ORM Mysql

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/")[](https://sequelize.org/v3/docs/getting-started/)

### Example usage from [https://sequelize.org](https://sequelize.org/v3/docs/getting-started/)

```
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](https://sequelize.readthedocs.org/en/latest/api/sequelize/).

### 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.

<iframe src="https://www.youtube.com/embed/videoseries?list=PLT5Jhb7lgSBMPrqdq6927wH059h3DiHXg" width="700" height="394" frameborder="0" scrolling="no"></iframe>

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/")[](https://sequelize.org/v3/docs/getting-started/)
