Migrating from Sequelize
postgrelize implements the public API of Sequelize v6 for PostgreSQL. A differential test suite runs the same scenarios on sequelize@6 and on postgrelize and compares the results, so for most applications migrating is a change of the package name.
1. Replace the packages
Remove sequelize and pg-hstore (postgrelize does not use it), and install postgrelize from npmjs.com:
npm uninstall sequelize pg-hstore
npm install postgrelize pgIf you installed the deprecated GitHub Packages name @tormozz48/postgrelize (or the alias postgrelize@npm:@tormozz48/postgrelize), remove the .npmrc scope line, run npm uninstall @tormozz48/postgrelize and install postgrelize as above. Imports from '@tormozz48/postgrelize' become 'postgrelize'; the API is identical.
2. Update imports
Replace 'sequelize' with 'postgrelize'. The exported names are the same as in Sequelize v6:
// before
import { DataTypes, Model, Op, Sequelize, Transaction } from 'sequelize';
// after
import { DataTypes, Model, Op, Sequelize, Transaction } from 'postgrelize';The package ships ES modules and CommonJS. As in v6, the default export and require('postgrelize') are the Sequelize class (import Sequelize from 'postgrelize', const Sequelize = require('postgrelize'), new Sequelize(url)), and every named export is also a property of it (require('postgrelize').DataTypes). One CommonJS difference: require('postgrelize').HasOne, HasMany, BelongsTo and BelongsToMany are the decorators, not v6's association classes. Type helpers (InferAttributes, InferCreationAttributes, CreationOptional, ForeignKey, NonAttribute, ModelStatic, WhereOptions, ...) are exported under their v6 names.
TypeScript types
The type API follows the v6 declarations, so code that compiles against sequelize compiles against postgrelize: Model<TAttributes extends {} = any, TCreationAttributes = TAttributes>, ModelStatic, FindOptions, Includeable, the Sequelize#query overloads (query<T>(sql, { type: QueryTypes.SELECT })), generic Association<S, T> and the association mixin types (BelongsToGetAssociationMixin<T>, HasManyGetAssociationsMixin<T>, ...). A test compiles a corpus of v6-style code against both libraries and lists the v6 type names and option keys that postgrelize still lacks (tests/compat/type-parity/exceptions.json).
Differences to know:
HasMany,HasOne,BelongsToandBelongsToManyare exported as generic types next to the decorators of the same names; useSequelize.HasMany(and the others) for the class value.InitOptions.name({ singular, plural }) works as in v6: it gives the default alias and foreign key ofbelongsTo,hasOne,hasManyandbelongsToManyand the accessor names (name: { singular: 'user', plural: 'users' }makesAgent.belongsTo(User)use aliasuserand foreign keyuserId). A missing form falls back to the inflectedmodelName. Before this release the option was ignored.onDelete: 'CASCADE'withhooks: trueon an association works as in v6: destroying an instance loads the associated rows and destroys each with its own hooks before the row itself is deleted (not for the soft delete of a paranoid model). Withouthooks: truePostgreSQL cascades the delete and no hooks of the target run.QueryInterface#queryGeneratoronly providesselectQuery(tableName, options, model), which renders finder options to a SQL string (values as escaped literals, no scope or paranoid filter), as in v6; the other v6 generator methods do not exist.findAndCountAll({ group })returnscountas an array of{ ...groupedAttributes, count }like v6 (earlier releases ignoredgroupin the count query).FindOptions.replacementsworks as in v6::nameand?placeholders insideliteral()(inwhere,attributes,order, ...) offindAll,findOne,findByPk,countandfindAndCountAllare substituted and written into the SQL text as escaped literals, as in v6, so a placeholder used inSELECTandGROUP BYgives identical expressions (1.6.0; ADR-037). Unlike v6,updateanddestroyacceptreplacementstoo (v6 rejects them forupdate). Before 1.3.0 the finders ignored the option.Model#reload(options)takes the finder options (include,attributes,paranoid,lock,hooks, ...) and reads the row throughfindOne, so default scopes andparanoidapply as in v6: reloading a soft-deleted row needsparanoid: false. Associations the instance was built with are not reloaded unless you passinclude.findByPk(id, { rejectOnEmpty: true })resolves to the model (notM | null), asfindOnedoes.sequelize.queryaccepts a string or aliteral(), not the v6{ query, values }object form.- Option keys that postgrelize does not implement (
searchPath,omitNull, ...) are not in the option types, so the compiler points at them. Model.sequelizeandinstance.sequelizeare typedSequelize(not optional, it throws on a model that is not initialized),WhereAttributeHashdefaults toany, attributeget/setand model-levelvalidatefunctions can be written with a typedthisand a callback stylenext, and the parameters of hook functions areanyso an unannotated hook compiles.
3. Check the connection options
- Only PostgreSQL is supported. Omit
dialector set it to'postgres'; any other value throws. dialectOptionsis passed to thepgpool as is.poolacceptsmax,min,idle,acquire,evictandmaxUses.minandevictare accepted for compatibility but have no effect:pgdoes not keep idle connections open.retry(instance option and per-query / per-transaction option) is supported with the v6 fields and matching rules; it is off by default, as v6's default only lists a SQLite message. Statements inside an open transaction are not retried (v6 retries them). See Retrying failed queries.sequelize.connectionManagerexposesgetConnection(options?),releaseConnection(connection),destroyConnection(connection)and thepoolcounters (size,available,using,waiting,maxSize), and you can reassign the first two on the instance to route connections, as in v6. Withreplication,poolcarries the totals plusreadandwritecounters (v6 has no totals there), andgetConnection({ type: 'read' })reaches a replica (v6 reaches it only for'SELECT'). The other v6 members (initPools,close,refreshTypeParser,pool.acquire/pool.drain, ...) are not provided. See Routing connections.minifyAliases: trueworks as in v6 for include aliases, see Associations.replication: { read, write }works as in v6, see Sessions and scaling.
4. Run your tests
Things that behave the same as in Sequelize v6 include model definition (init, define), all finders, create/bulkCreate/upsert/update/destroy, instance methods and change tracking, hooks, validators, scopes, paranoid models, optimistic locking, associations with include and accessors, nested creation with include, managed and unmanaged transactions with savepoints and CLS (Sequelize.useCLS() and Sequelize.useCLS(namespace) with a cls-hooked namespace), QueryInterface and sync.
Value conventions follow v6 too: BIGINT and DECIMAL values are strings, and upsert returns [instance, null] because PostgreSQL does not report whether a row was inserted (use the onConflict option to get a boolean).
Intentional differences
Some Sequelize v6 behaviours are bugs on PostgreSQL. postgrelize fixes them instead of copying them, for example:
- JSON
wherekeys containing,,"or\are matched correctly. upserton a partial unique index uses the index predicate.sync({ alter: true })keeps the sequence of non-keyautoIncrementcolumns.describeTable,showAllTablesandtableExistsdefault tocurrent_schema()instead ofpublic, so they respectsearch_path.findCreateFindinside a transaction inserts in a savepoint: when another session inserted the row first it returns that row withcreated: false; v6 returns an unsaved instance withcreated: true.order: ['json.path']orders by the jsonb value (numbers sort numerically).- Foreign-key cycles in
sync()are created after all tables exist instead of being skipped.
The full list, with the Sequelize issue numbers and regression tests, is on Sequelize issues fixed in postgrelize. If your code depends on one of these v6 behaviours, it needs a change.
Not supported
These Sequelize v6 features are not implemented:
| Feature | What to do instead |
|---|---|
| Other dialects (MySQL, SQLite, MSSQL…) | postgrelize is PostgreSQL only. |
searchPath query / connection option | Set search_path with dialectOptions: { options: '-c search_path=tenant' }. |
Named or shared ENUM types | Each ENUM attribute gets its own type, enum_<table>_<column>, as v6 does by default. |
create({ ignoreDuplicates }) | create(values, { onConflict: { action: 'nothing' } }) or bulkCreate([...], { ignoreDuplicates }). |
Sequelize v7 (@sequelize/core) API | Not planned. |
Hooks are accepted for every v6 hook name, but model lifecycle hooks, beforeConnect, afterConnect, the sync hooks (beforeBulkSync, afterBulkSync, beforeSync, afterSync), the define hooks of the instance (beforeDefine, afterDefine) and the static Sequelize.beforeInit / Sequelize.afterInit run; the other connection hooks and beforeQuery, beforeAssociate, ... are registered and never called. See Hooks and validation.
API differences
postgrelize is checked against the public API of Sequelize v6.37 (Sequelize, Model, QueryInterface, Transaction, DataTypes and the exports): a test fails when a v6 name is missing and not listed here. The list below is generated from tests/compat/api-surface-exceptions.json and only shrinks.
| Surface | Status | Names |
|---|---|---|
Sequelize statics | Not planned | ABSTRACT, UUIDV1 |
Sequelize statics | Other dialects only | IndexHints, TableHints, db2, mariadb, mssql, mysql, oracle, postgres, snowflake, sqlite |
Sequelize statics | v6 internals | AsyncQueueError, Sequelize, default |
Sequelize instance | Not planned | afterAssociate, afterDefine, afterDisconnect, afterInit, afterPoolAcquire, afterQuery, beforeAssociate, beforeDefine, beforeDisconnect, beforeInit, beforePoolAcquire, beforeQuery |
Sequelize instance | Other dialects only | set |
Sequelize instance | v6 internals | Sequelize, dialect, normalizeAttribute, normalizeDataType |
Model statics | Not planned | afterAssociate, afterBulkSync, afterQuery, beforeAssociate, beforeBulkSync, beforeQuery |
Model statics | v6 internals | Mixin, default, queryGenerator, warnOnInvalidOptions |
Model instance | Not planned | addHook, hasHook, hasHooks, removeHook, runHooks, setValidators |
Transaction statics | v6 internals | Transaction, default |
Transaction instance | v6 internals | cleanup, forceCleanup, prepareEnvironment, setDeferrable |
QueryInterface | Not planned | commitTransaction, decrement, delete, increment, insert, rawSelect, rollbackTransaction, select, setIsolationLevel, startTransaction, update, upsert |
QueryInterface | v6 internals | ensureEnums, normalizeAttribute, pgListEnums |
DataTypes | Not planned | ABSTRACT, UUIDV1 |
DataTypes | Other dialects only | db2, mariadb, mssql, mysql, oracle, postgres, snowflake, sqlite |
| Module exports | Not planned | ABSTRACT, UUIDV1 |
| Module exports | Other dialects only | IndexHints, TableHints |
| Module exports | v6 internals | AsyncQueueError |
Using the PostgreSQL extensions
Once the application runs on postgrelize, you can replace workarounds with native features step by step:
literal()-based upserts andupdateOnDuplicate→onConflictandModel.insert- loops of
update→UPDATE ... FROMandMERGE - large
bulkCreateimports →COPY sequelize.querywith hand-written CTEs and window functions → advanced queries
Extensions are additive: they are new options or new methods, and none of them changes the behaviour of v6 calls.