Rails ActiveRecord Migrations similar in Node/ExpressJs Rails ActiveRecord Migrations similar in Node/ExpressJs node.js node.js

Rails ActiveRecord Migrations similar in Node/ExpressJs


I think you can use Sequelize, which is really inspired of ActiveRecord. Maybe you'll need to use the package Migrat too, which look like a really good migration tool


From a quick Google search I pulled in four different packages/libraries you could try:


I think you should use Sequelize, the syntax is very simple and the curve learn is fast. Here are same samples of how to define Model in sequelize:

const User = sequelize.define('user', {  username: { type: Sequelize.STRING, allowNull: false },  password: { type: Sequelize.STRING, allowNull: false },  admin: { type: Sequelize.BOOLEAN, defaultValue: false }});

After you call on your code the sync() method Sequelize will CREATE TABLE IF NOT EXISTS all your models adding id (that you could change too), created_at and updated_at (timestamps) and you can make calls like that:

sequelize.sync()  .then(() => User.create({    username: 'administrator',    password: 'yahoo',    admin: true  }))  .then(userCreated => {    console.log(userCreated.toJSON());  });

You can make a very complex queries with little code and mix them with express.js to response to your clients, for example:

User.findAll({  where: {    [Op.iLike] : { username: 'Jack%' },    admin: true  } }).then(usersList => {   if (!usersList) {      response.status(404).json({ error: 'not found' });      return null;    }   for (var i = 0; i < usersList.length; i++) {    usersList[I].something = 42;   }   response.json(usersList);   return null; });

This will produce something like: SELECT "id", "username", "password", "admin", "created_at", "updated_at" WHERE "admin" = true AND "username" ILIKE 'Jack%';