Sequelize

Object-relational mapping (ORM) in computer science is a programming technique that makes an object out of relational table structure and vice versa, converting data between incompatible type systems.

Sequelize is a promise-based ORM for Node.js. It supports the dialects PostgreSQL, MySQL, MariaDB, SQLite and MSSQL.

Ejemplo de uso combinado de Express y Sequelize

En este ejemplo se muestra una aplicación express que permite la creación/destrucción de usuarios y tareas para los mismos usando sqlite. Es un fork de sequelize/express-example

sequelize  init                   Initializes the project. [init:config, init:migrations, init:seeders, init:models]
sequelize  init:config            Initializes the configuration.
sequelize  init:migrations        Initializes the migrations.
sequelize  init:models            Initializes the models.
sequelize  init:seeders           Initializes the seeders.
~/src/javascript/learning/sequelize/exp-exa]$ tree -I node_modules
.
├── app.js
├── bin
│   └── www
├── config
│   └── config.json
├── migrations
├── models
│   └── index.js
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
├── seeders
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

11 directories, 11 files
[~/src/javascript/learning/sequelize/exp-exa]$ node_modules/sequelize-cli/bin/sequelize init

Sequelize [Node: 4.5.0, CLI: 2.4.0, ORM: 3.25.1]

Loaded configuration file "config/config.json".
Using environment "development".
The file config/config.json already exists. Run "sequelize init --force" to overwrite it.
tree -I node_modules
.
├── app.js
├── bin
│   └── www
├── config
│   └── config.json
├── migrations
├── models
│   └── index.js
├── npm-debug.log
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
├── seeders
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

11 directories, 12 files
[~/src/javascript/learning/sequelize/exp-exa]$  cat config/config.json 
{
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}
~/src/javascript/learning/sequelize/exp-exa]$ node_modules/sequelize-cli/bin/sequelize model:create --name User --attributes username:string

Sequelize [Node: 4.5.0, CLI: 2.4.0, ORM: 3.25.1]

Loaded configuration file "config/config.json".
Using environment "development".
[~/src/javascript/learning/sequelize/exp-exa]$ cat models/user.js 
'use strict';
module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define('User', {
    username: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
      }
    }
  });
  return User;
};
[~/src/javascript/learning/sequelize/exp-exa]$ node_modules/.bin/sequelize model:create --name Task --attributes title:string

Sequelize [Node: 4.5.0, CLI: 2.4.0, ORM: 3.25.1]

Loaded configuration file "config/config.json".
Using environment "development".
[~/src/javascript/learning/sequelize/exp-exa]$ cat models/task.js 
'use strict';
module.exports = function(sequelize, DataTypes) {
  var Task = sequelize.define('Task', {
    title: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
      }
    }
  });
  return Task;
};
[~/src/javascript/learning/sequelize/exp-exa]$ cat migrations/20161116212217-create-user.js 
'use strict';
module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      username: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable('Users');
  }
};

Material del Curso de MiriadaX "Desarrollo de servicios en la nube con HTML5, Javascript y node.js"

Video Tutorial por Code Cast

  1. Sequelize: An Introduction -
  2. Sequelize: Getting Started
  3. Sequelize: Defining Models
  4. Sequelize: Validation
  5. Sequelize: Hooks
  6. Sequelize: Inserting Data
  • Sequelize By Eric Stevens

Migrations

  • Migrations are a convenient way to alter your database schema over time in a consistent and easy way.
  • They use a Embedded DSL in the host language, so that you don't have to write SQL by hand, allowing your schema and changes to be database independent.
  • You can think of each migration as being a new 'version' of the database.

The Wikipedia says:

  • In software engineering, schema migration (also database migration, database change management) refers to the management of incremental, reversible changes to relational database schemas.
  • A schema migration is performed on a database whenever it is necessary to update or revert that database's schema to some newer or older version.
  • Migrations are performed programmatically by using a schema migration tool -- When invoked with a specified desired schema version, the tool automates the successive application or reversal of an appropriate sequence of schema changes until it is brought to the desired state.

  • Tech Talk: Sequelize CLI and Migrations

results matching ""

    No results matching ""