npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@chibi.pl/nest-sequelize-db

v0.0.6

Published

NestJS Sequelize DB Module for easy use of databases in NestJS applications

Readme

@chibi.pl/nest-sequelize-db

Utilities for using sequelize-typescript with NestJS: a global database module, reusable entity and service base classes, configuration storage, and Umzug migration helpers.

Requirements

  • Node.js 18 or newer
  • NestJS 9 or newer
  • @nestjs/sequelize, sequelize, and sequelize-typescript
  • A Sequelize dialect driver, such as pg, mysql2, sqlite3, tedious, or oracledb

Installation

npm install @chibi.pl/nest-sequelize-db @nestjs/sequelize sequelize sequelize-typescript

Install the driver for the database used by your application. For PostgreSQL:

npm install pg

For local SQLite development or tests:

npm install sqlite3

ConfigurationDbService uses Nest event and schedule decorators, so applications using it also need:

npm install @nestjs/event-emitter @nestjs/schedule

Quick start

Register DbModule once in the application root. It is global, so feature modules can inject exported providers without re-importing it.

import { Module } from '@nestjs/common';
import { DbModule } from '@chibi.pl/nest-sequelize-db';

@Module({
  imports: [
    DbModule.forRoot({
      dialect: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'app',
      password: 'secret',
      database: 'app_db',
      logging: false,
    }),
  ],
})
export class AppModule {}

The same options can be supplied with environment variables:

DB_DIALECT=postgres
DB_HOST=127.0.0.1
DB_PORT=5432
DB_USER=app
DB_PASS=secret
DB_DATABASE=app_db
DB_SSL=false
DB_LOGGING=false
DB_BENCHMARK=false
GLOBAL_SEARCH_LIMIT=400

When an explicit option is supplied to DbModule.forRoot(), it takes precedence over its corresponding environment variable.

Define an entity

Extend AbstractEntity to add createdBy, updatedBy, deletedBy, unDeletedAt, and unDeletedBy audit columns. Sequelize provides the usual id, createdAt, updatedAt, and optional deletedAt columns according to the table settings.

import { Column, DataType, Table } from 'sequelize-typescript';
import { AbstractEntity } from '@chibi.pl/nest-sequelize-db';

@Table({
  tableName: 'users',
  paranoid: true,
})
export class UserEntity extends AbstractEntity {
  @Column(DataType.STRING)
  name!: string;

  @Column(DataType.STRING)
  email!: string;
}

Define a database service

Extend AbstractDbService to receive common CRUD operations, safe pagination, transaction helpers, include defaults, and allowed-sort validation.

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { AbstractDbService } from '@chibi.pl/nest-sequelize-db';
import { UserEntity } from './user.entity';

@Injectable()
export class UsersDbService extends AbstractDbService<UserEntity> {
  protected allowedSortingColumns: Array<keyof UserEntity & string> = [
    'id',
    'name',
    'createdAt',
  ];

  protected defaultOrder = [['createdAt', 'DESC']] as const;

  constructor(
    @InjectModel(UserEntity)
    model: typeof UserEntity,
  ) {
    super(model);
  }
}

Useful methods include:

  • create(attributes, options)
  • getById(id, options), getByIds(ids, options), and getAllByIds(ids, options)
  • deleteById(id, options) and deleteByIds(ids, options)
  • findAll(params, options) and find(params, options)
  • beginTransaction() and executeTransaction(callback, transaction?)
  • getLimitAndPage(limit?, page?)
  • prepareOrFieldQueryILike(query, separator?), which uses LIKE outside PostgreSQL

findAll() and find() validate array-based ordering against allowedSortingColumns. Configure that list in each concrete service before accepting sort fields from requests.

Register a feature module

Use DbModule.forFeature() to register models and services. Registered models are added to the Sequelize connection during application bootstrap and synchronized with alter: true outside tests.

import { Module } from '@nestjs/common';
import { DbModule } from '@chibi.pl/nest-sequelize-db';
import { UserEntity } from './user.entity';
import { UsersDbService } from './users-db.service';

@Module({
  imports: [
    DbModule.forFeature({
      models: [UserEntity],
      services: [UsersDbService],
      migrationsPath: __dirname + '/migrations',
      moduleName: 'UsersModule',
    }),
  ],
  exports: [UsersDbService],
})
export class UsersModule {}

moduleName is optional but recommended because it identifies the source module in migration logs.

Multiple database connections

Set connectionName on the root module, then inject the named Sequelize connection using Nest’s standard @InjectConnection() API. Named connection tokens also use SEQUELIZE_<UPPERCASE_NAME>.

DbModule.forRoot({
  connectionName: 'analytics',
  dialect: 'postgres',
  host: 'localhost',
  database: 'analytics',
  username: 'app',
  password: 'secret',
});

Migrations

The module discovers .ts and .js files in each migrationsPath passed to forFeature(), then applies pending migrations with Umzug for non-SQLite connections. Export a migration object with up and down methods:

import { DataType } from 'sequelize-typescript';
import type { MigrationBaseType } from '@chibi.pl/nest-sequelize-db';

export const migration: MigrationBaseType = {
  async up(queryInterface) {
    await queryInterface.createTable('roles', {
      id: {
        type: DataType.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      name: {
        type: DataType.STRING,
        allowNull: false,
      },
    });
  },
  async down(queryInterface) {
    await queryInterface.dropTable('roles');
  },
};

For TypeScript migrations at runtime, install ts-node in the consuming application. In a compiled Nest application, pass the path to the compiled migration files.

MigrationHelperClass.from(queryInterface) provides table and column inspection plus helpers to add only missing columns.

import { DataType } from 'sequelize-typescript';
import { MigrationHelperClass } from '@chibi.pl/nest-sequelize-db';

const helper = await MigrationHelperClass.from(queryInterface);
await helper.prepare('users');
await helper.addMissingColumns('users', {
  displayName: { type: DataType.STRING, allowNull: true },
});

Configuration storage

ConfigurationEntity stores JSON values by string key. Register it and a provider for ConfigurationDbService:

import { Module } from '@nestjs/common';
import {
  ConfigurationDbService,
  ConfigurationEntity,
  DbModule,
} from '@chibi.pl/nest-sequelize-db';

@Module({
  imports: [
    DbModule.forFeature({
      models: [ConfigurationEntity],
      modelProviders: [
        {
          provide: 'DB_REPOSITORY-CONFIGURATION',
          useValue: ConfigurationEntity,
        },
      ],
      services: [ConfigurationDbService],
    }),
  ],
})
export class ConfigurationModule {}

Use it from an injected service:

await configurationDbService.set('mail.host', 'smtp.example.com');
const host = await configurationDbService.get<string>('mail.host');
const mail = await configurationDbService.getWildcard<Record<string, unknown>>('mail.');

The service caches reads and emits configuration.created, configuration.set, configuration.updated, configuration.modified, and configuration.read events. It refreshes changed values every minute. Emit configuration.reload or configuration.refresh to trigger a refresh through Nest’s event emitter.

Testing

This package includes unit tests using an in-memory SQLite database. Run them with:

yarn test

Run coverage with:

yarn test:cov

Publishing

Before the first release:

yarn install
yarn test
yarn build
npm login
npm publish --access public

Use npm pack --dry-run to inspect the exact tarball contents without publishing. The prepack script builds dist automatically before npm pack and npm publish.

License

MIT