Sequelize Postgres Utilities
sequelize-pg-utilities is an opinionated set of database utilities designed to simplify the configuration and connection process for PostgreSQL databases when used with Sequelize or the Sequelize CLI. It standardizes database configuration by integrating values from config files, environment variables, and sensible defaults, and also provides a mechanism for creating the database itself. The current stable version is 2.0.2, with recent updates focused on dependency upgrades and maintenance. The library differentiates itself by addressing the configuration discrepancies between Sequelize and the Sequelize CLI and offering built-in database creation capabilities, abstracting away common setup complexities for PostgreSQL users. Releases typically occur as needed for dependency updates, bug fixes, or significant API changes.
Common errors
-
Cannot find module 'pg'
cause The 'pg' package, which is the PostgreSQL client, became a peer dependency in v2.0.0 and must be installed manually.fixInstall 'pg' as a direct dependency: `npm install pg` or `yarn add pg`. -
TypeError: Cannot read properties of undefined (reading 'dialectOptions')
cause Incorrect or missing configuration for `sequelize-pg-utilities`, leading to an incomplete options object being passed to Sequelize.fixDouble-check your `config.json` and environment variables. Ensure `configure(config)` receives a valid object and that the resulting `options` object is properly structured before passing to `new Sequelize()`. -
ReferenceError: isNew is not defined
cause Attempting to access the `isNew` property from the `configure` function's return object in versions 2.0.0 and later.fixReplace all instances of `isNew` with `dbNew` in your code, as the property name was standardized in v2.0.0.
Warnings
- breaking The `operatorsAliases` option was completely removed. If your project still relies on this Sequelize feature, you must either downgrade or refactor your queries.
- breaking The internal `pgtools` dependency was removed, and `pg` was transitioned to a peer dependency. This means `pg` must now be explicitly installed in your project.
- breaking The return object from `configure` standardized its flag for indicating a newly created database. The mixed usage of `dbNew` and `isNew` was consolidated to exclusively use `dbNew`.
- gotcha The package explicitly set its type to `commonjs` in version 1.2.2. While Node.js has CJS-ESM interoperability, projects primarily using ESM may encounter unexpected behavior or require specific configurations.
Install
-
npm install sequelize-pg-utilities -
yarn add sequelize-pg-utilities -
pnpm add sequelize-pg-utilities
Imports
- configure function
import { configure } from 'sequelize-pg-utilities';const { configure } = require('sequelize-pg-utilities'); - Full module object
import pgUtilities from 'sequelize-pg-utilities';
const pgUtilities = require('sequelize-pg-utilities'); // then use pgUtilities.configure(...) - TypeScript types
import type { ConfigureResult, DbConfig } from 'sequelize-pg-utilities';
Quickstart
const { configure } = require('sequelize-pg-utilities');
const { Sequelize } = require('sequelize'); // Assuming Sequelize is installed
// Mock a config/config.json structure
const appConfig = {
development: {
username: process.env.DB_USER ?? 'my-dev-user',
password: process.env.DB_PASS ?? 'my-dev-password',
database: process.env.DB_NAME ?? 'my-project-development'
},
test: { /* ... */ },
production: { /* ... */ }
};
// To simulate different environments for quickstart
process.env.NODE_ENV = 'development';
async function initializeDatabase() {
try {
const { name, user, password, options, dbNew } = await configure(appConfig);
console.log(`Database '${name}' initialized. New database created: ${dbNew}`);
console.log(`Attempting to connect with user: ${user}`);
const sequelize = new Sequelize(name, user, password, options);
await sequelize.authenticate();
console.log('Database connection has been established successfully.');
// Example: Run a simple query
const [results] = await sequelize.query('SELECT 1+1 AS solution');
console.log('Query result:', results[0].solution);
await sequelize.close();
console.log('Database connection closed.');
} catch (error) {
console.error('Unable to connect to the database or an error occurred:', error);
process.exit(1);
}
}
initializeDatabase();