Skip to content

Getting started ​

postgrelize is an ORM for Node.js that implements the Sequelize v6 public API for PostgreSQL only. Code written for Sequelize v6 on PostgreSQL runs unchanged in most cases. On top of that, postgrelize exposes PostgreSQL features that Sequelize cannot express: ON CONFLICT upserts with expressions, MERGE, CTEs, COPY, LISTEN/NOTIFY and more.

Requirements ​

  • Node.js 20 or newer (CI tests Node 20, 22 and 24)
  • PostgreSQL 16, 17 or 18 (some features need a newer server; each page says so)
  • the pg driver

Install ​

Install the package from npmjs.com together with pg:

bash
npm install postgrelize pg

Imports read from 'postgrelize', as in every example of this guide. The old GitHub Packages name @tormozz48/postgrelize is deprecated.

Connect ​

Create one Sequelize instance per database and share it across your application. It accepts a connection URI or the usual Sequelize options:

ts
import { Sequelize } from 'postgrelize';

const sequelize = new Sequelize(process.env.DATABASE_URL!, {
  logging: false,
  pool: { max: 10 },
});

await sequelize.authenticate(); // throws a ConnectionError subclass when the server is unreachable

The constructor also takes (database, username, password, options) or a single options object with host, port, database, username, password, ssl and dialectOptions (passed to the pg pool). dialect may be omitted; any value other than 'postgres' is rejected. The session time zone defaults to UTC (timezone: '+00:00'), as in Sequelize v6.

Call sequelize.close() when the process shuts down.

Define a model and query it ​

Models are TypeScript classes. InferAttributes and InferCreationAttributes derive the attribute types from the declared fields; CreationOptional marks attributes that the database fills in.

ts
import { DataTypes, Model, Op, type Sequelize } from 'postgrelize';
import type { CreationOptional, InferAttributes, InferCreationAttributes } from 'postgrelize';
ts
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  declare id: CreationOptional<number>;
  declare email: string;
  declare active: CreationOptional<boolean>;
}

User.init(
  {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    email: { type: DataTypes.STRING, allowNull: false, unique: true },
    active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
  },
  { sequelize },
);

await sequelize.sync();

sequelize.sync() creates missing tables. Use it in tests and prototypes; production schemas are usually managed with migrations (see Schema, sync and migrations).

The familiar Sequelize v6 methods work as before:

ts
const ann = await User.create({ email: 'ann@example.com' });
await User.bulkCreate([
  { email: 'bob@example.com' },
  { email: 'eve@example.com', active: false },
]);

const active = await User.findAll({ where: { active: true }, order: [['email', 'ASC']] });
const count = await User.count({ where: { email: { [Op.iLike]: '%@example.com' } } });

ann.active = false;
await ann.save();
await User.destroy({ where: { active: false } });

Every example in this guide is executed against PostgreSQL in CI, so the code you see is code that runs.

Logging ​

logging takes a function receiving each SQL statement (or false). With benchmark: true the function also receives the execution time in milliseconds. Statements inside a transaction are prefixed with the transaction id:

ts
const sequelize = new Sequelize(url, { logging: (sql) => logger.debug(sql) });
// Executing (default): SELECT ...
// Executing (6f1c...): INSERT INTO ...

All values are sent as bound parameters ($1, $2, ...), never interpolated into the SQL text.

Errors ​

Errors are the Sequelize v6 classes, exported from the package root: ValidationError, UniqueConstraintError, ForeignKeyConstraintError, ExclusionConstraintError, DatabaseError, TimeoutError, ConnectionRefusedError and the other ConnectionError subclasses, EmptyResultError, OptimisticLockError. Check them with instanceof:

ts
import { UniqueConstraintError } from 'postgrelize';

try {
  await User.create({ email: 'ann@example.com' });
} catch (error) {
  if (error instanceof UniqueConstraintError) {
    // error.fields: { email: 'ann@example.com' }
  }
}

Next steps ​

Released under the MIT License.