Skip to main content

Why Switch?

Sequelize requires a lot of ceremony — defining models with DataTypes, manually syncing associations, and verbose query syntax. IlanaORM follows the Eloquent convention-over-configuration approach: less boilerplate, more readable code.

Model Definition

const { DataTypes } = require('sequelize');

const User = sequelize.define('User', {
  name: { type: DataTypes.STRING, allowNull: false },
  email: { type: DataTypes.STRING, allowNull: false, unique: true },
  role: { type: DataTypes.ENUM('user', 'admin'), defaultValue: 'user' },
}, {
  tableName: 'users',
  paranoid: true, // soft deletes
});

Querying

// Find all
await User.findAll();

// Find by primary key
await User.findByPk(1);

// Find with condition
await User.findOne({ where: { email: 'alice@example.com' } });

// Find with AND conditions
await User.findAll({
  where: { role: 'admin', email: { [Op.like]: '%@company.com' } }
});

Creating Records

const user = await User.create({
  name: 'Alice',
  email: 'alice@example.com',
});

Updating Records

const user = await User.findByPk(1);
await user.update({ name: 'Alicia' });

// or bulk update
await User.update({ role: 'admin' }, { where: { email: { [Op.like]: '%@company.com' } } });

Deleting Records

const user = await User.findByPk(1);
await user.destroy(); // soft delete if paranoid: true

// Force delete
await user.destroy({ force: true });

// Restore
await user.restore();

Associations / Relationships

// Definition (in two files + index.js to wire them up)
User.hasMany(Post, { foreignKey: 'user_id', as: 'posts' });
Post.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

// Querying with include
await User.findByPk(1, {
  include: [{ model: Post, as: 'posts' }]
});

Pagination

const { count, rows } = await Post.findAndCountAll({
  limit: 10,
  offset: (page - 1) * 10,
});

Transactions

const t = await sequelize.transaction();
try {
  await User.create({ name: 'Alice' }, { transaction: t });
  await Post.create({ title: 'Hello' }, { transaction: t });
  await t.commit();
} catch (err) {
  await t.rollback();
}

Migration Path

  1. Replace sequelize.define(...) with a class extending Model
  2. Move association definitions into model methods (hasMany, belongsTo, etc.)
  3. Replace include: [...] with .with(...)
  4. Replace Op.like, Op.gt, etc. with .where('col', 'like', '%value%')
  5. Delete the Sequelize models/index.js wiring file — IlanaORM doesn’t need it