Aviary
Channels

Database

Persist notifications so you can show an in-app feed. A NotificationStore interface, a bundled in-memory store, and TypeORM / MikroORM / Prisma / Drizzle adapters.

The database channel writes a notification to a store so you can render an in-app feed — an unread count, a notifications dropdown, a "mark all as read" button. It persists through a NotificationStore abstraction: a bundled in-memory store for development, and ORM adapters (TypeORM, MikroORM, Prisma, Drizzle) for production. The row shape mirrors Laravel's notifications table.

Install

pnpm add @dudousxd/nestjs-notifications-database

Register the channel

app.module.ts
import { DatabaseChannelModule } from '@dudousxd/nestjs-notifications-database';

@Module({
  imports: [
    DatabaseChannelModule.forRoot(), // bundled in-memory store
  ],
})
export class AppModule {}

DatabaseChannelModule.forRoot() takes:

OptionTypeDefaultDescription
storeType<NotificationStore>InMemoryStoreStore class to instantiate.
importsDynamicModule['imports'][]Extra modules to import (e.g. the ORM module the store depends on).
globalbooleantrueRegister globally so the channel is discoverable app-wide.
tenancy'inherit' | false | { enabled?, kinds?, family?, required?, nullRows? }'inherit'Scope the inbox and stamp rows by the request's tenant from @dudousxd/nestjs-context. See Multi-tenancy.

There's also DatabaseChannelModule.forFeature(), which registers only the channel and reuses a NOTIFICATION_STORE token provided elsewhere — that's how the ORM adapter modules below wire in.

The notification side

Annotate the payload method with the @Database() handle and return a plain object — that object becomes the persisted data:

invoice-paid.notification.ts
import { type Notifiable, Notification } from '@dudousxd/nestjs-notifications-core';
import { Database } from '@dudousxd/nestjs-notifications-database';

@Notification()
export class InvoicePaid {
  constructor(private invoiceId: string, private amount: number) {}

  @Database()
  toDatabase({ notifiable }: ChannelContext): Record<string, unknown> {
    return { invoiceId: this.invoiceId, amount: this.amount };
  }
}

The Database handle also works as a via() token (via() { return [Database]; }) for explicit routing. For compile-time checks, implement DatabaseNotification alongside the decorator.

toDatabase() is optional when you route with an explicit via(). If it's absent the channel falls back to toArray() (Laravel parity), and finally to a structural copy of the notification's own properties.

The notifiable reference

Persisting needs to know whose notification this is, as a { type, id } pair. The channel resolves it in two ways:

  1. routeNotificationFor('database') may return a { type, id } reference directly.
  2. Otherwise it calls the notifiable's toNotifiableRef().

If neither is available it throws, asking you to provide one:

user.ts
export class User implements Notifiable {
  constructor(public id: number) {}

  routeNotificationFor() {
    return undefined;
  }

  toNotifiableRef() {
    return { type: 'User', id: this.id };
  }
}

Stored shape

Each persisted notification is a StoredNotification:

interface StoredNotification {
  id: string;
  type: string;            // notification class name, e.g. "InvoicePaid"
  notifiableType: string;  // reference type, e.g. "User"
  notifiableId: string;    // reference id
  data: Record<string, unknown>; // what toDatabase() returned
  readAt: Date | null;
  createdAt: Date;
  updatedAt: Date;
}

The stored type is the notification's notificationName(notification) — an instance-level notificationType() override when implemented, else the class-level @Notification({ name }), else the class name. See notificationType() for when to reach for the instance override (e.g. one generic notification class carrying many event names). This is also the value the filter[type] inbox filter matches against.

The store

A store implements NotificationStore. These are the methods you get for reading and managing a feed:

MethodReturnsDescription
save(notification)Promise<StoredNotification>Persist a new row (the channel calls this).
markAsRead(id)Promise<void>Mark one notification read.
markAllAsRead(notifiableType, notifiableId)Promise<void>Mark every unread row for a notifiable read.
getForNotifiable(notifiableType, notifiableId, types?)Promise<StoredNotification[]>All notifications for a notifiable, newest first. Optional types: string[] restricts to matching type values.
getUnread(notifiableType, notifiableId, types?)Promise<StoredNotification[]>Unread notifications for a notifiable. Same optional types filter.
delete(id)Promise<void>Remove one notification.
deleteOwned(id, owner)Promise<boolean>Optional. Remove one notification only when it belongs to owner ({ notifiableType, notifiableId, tenantId? }); true when a row matched.
markAsReadOwned(id, owner)Promise<boolean>Optional. Mark one read only when it belongs to owner; true when a row matched, including an already-read one.
findById(id)Promise<StoredNotification | null>Optional. Fetch one row — the ownership fallback when the two scoped mutations are absent.
countUnread(notifiableType, notifiableId, tenantId?, types?)Promise<number>Optional. Count unread rows in the database (a COUNT, no rows loaded). unreadCount() uses it when present and otherwise counts the rows getUnread returns.

types is absent or an empty array by default, matching every type (unchanged behavior) — it's purely additive. PaginateForNotifiableOptions carries the same types?: string[] for the paginated read path. Every bundled adapter (in-memory, TypeORM, MikroORM, Prisma, Drizzle) implements the filter; see In-app notifications for the higher-level NotificationsQueryService and REST filter[type] query param this powers.

Every bundled store implements countUnread. A custom store should implement it too: the React inbox polls the unread count, and without it each poll loads every unread row and its payload.

The store is bound to the NOTIFICATION_STORE token, so inject it to build your feed endpoints:

import { Inject } from '@nestjs/common';
import { NOTIFICATION_STORE, type NotificationStore } from '@dudousxd/nestjs-notifications-database';

constructor(@Inject(NOTIFICATION_STORE) private store: NotificationStore) {}

The bundled InMemoryStore keeps rows in a Map. It's perfect for tests and prototyping — but it's not durable, so use a persistence adapter in production.

Reading notifications in-app

You usually don't inject the raw store. The NotificationsQueryService (provided by the database module) wraps it with the read/manage methods you reach for — all, unread, unreadCount, paginate, markAsRead, markAllAsRead, delete — accepting a notifiable or a { type, id } ref. There's also an optional REST controller you can mount in one call.

See In-app notifications for the full recipe.

Persistence adapters

For real persistence, pair the channel with an adapter. Each adapter provides a store bound to the NOTIFICATION_STORE token, plus the schema it needs (a NotificationEntity for the ORM adapters, a Notification model for Prisma). You register the adapter's store module alongside DatabaseChannelModule.forFeature() — the adapter provides the store token, forFeature() registers the channel that consumes it.

pnpm add @dudousxd/nestjs-notifications-database-typeorm @nestjs/typeorm typeorm

Add the adapter's NotificationEntity to your TypeOrmModule.forRoot() entities (or migrations), then wire the store and channel:

app.module.ts
import { DatabaseChannelModule } from '@dudousxd/nestjs-notifications-database';
import { TypeOrmNotificationStoreModule } from '@dudousxd/nestjs-notifications-database-typeorm';

@Module({
  imports: [
    // ...TypeOrmModule.forRoot({ entities: [NotificationEntity, ...] })
    TypeOrmNotificationStoreModule.forFeature(),
    DatabaseChannelModule.forFeature(),
  ],
})
export class AppModule {}

TypeOrmNotificationStoreModule.forFeature() registers TypeOrmModule.forFeature([NotificationEntity]) internally and binds TypeOrmNotificationStore to NOTIFICATION_STORE. The entity (NotificationEntity) is exported if you'd rather register it yourself.

Neither adapter fits your schema? Implement NotificationStore yourself and pass it as the store option to DatabaseChannelModule.forRoot(). The interface is six methods.

Schema

By default the channel is self-contained: on boot it creates the notifications table if it's missing, non-destructively — it only adds what's absent and never drops anything. That's autoCreateSchema: true, the default on both DatabaseChannelModule.forRoot() and DatabaseChannelModule.forFeature(). Install an adapter, register the module, and the feed works on first run with no migration step.

Auto-create is non-destructive — it only creates missing tables, columns and indexes, so it's safe to leave on. Turn it off (autoCreateSchema: false) for strict, migration-only environments where the app must never touch DDL.

To own the schema with your ORM's migrations instead, disable auto-create and use the exported migration helpers:

app.module.ts
DatabaseChannelModule.forFeature({ autoCreateSchema: false }),

Call createNotificationsTable(queryRunner) inside a TypeORM migration. It's non-destructive (creates the table only if it doesn't exist) and driver-portable — the column types come from the entity metadata. It leaves an existing table alone; see the inbox index for adding the index to one.

migrations/1700000000000-AddNotifications.ts
import { createNotificationsTable } from '@dudousxd/nestjs-notifications-database-typeorm';
import type { MigrationInterface, QueryRunner } from 'typeorm';

export class AddNotifications1700000000000 implements MigrationInterface {
  async up(queryRunner: QueryRunner): Promise<void> {
    await createNotificationsTable(queryRunner);
  }

  async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable('notifications', true);
  }
}

The inbox index

Every adapter declares notifications_notifiable_idx on (notifiable_type, notifiable_id, created_at). It serves the inbox reads (WHERE notifiable = ? ORDER BY created_at DESC) and the unread count. The tenant column is not part of it, so reads without a tenant keep the created_at ordering from the index.

How an existing notifications table gets it:

  • autoCreateSchema on (TypeORM, MikroORM, Drizzle): the next boot creates it. TypeORM matches indexes by name and only adds missing ones; MikroORM's schema fingerprint changes, so the table is healed once; Drizzle's ensureSchema() runs CREATE INDEX IF NOT EXISTS.

  • TypeORM migrations: createNotificationsTable() does nothing when the table exists, so add the index in a new migration. Building it from the entity metadata keeps the column names right under any naming strategy:

    migrations/1730000000000-AddNotificationsInboxIndex.ts
    import { NotificationEntity } from '@dudousxd/nestjs-notifications-database-typeorm';
    import { type MigrationInterface, type QueryRunner, TableIndex } from 'typeorm';
    
    const INDEX = 'notifications_notifiable_idx';
    
    export class AddNotificationsInboxIndex1730000000000 implements MigrationInterface {
      async up(queryRunner: QueryRunner): Promise<void> {
        const metadata = queryRunner.connection.getMetadata(NotificationEntity);
        const index = metadata.indices.find((i) => i.name === INDEX);
        if (index) await queryRunner.createIndex('notifications', TableIndex.create(index));
      }
    
      async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.dropIndex('notifications', INDEX);
      }
    }
  • MikroORM migrations: a new migration with this.addSql(await notificationsSchemaSql(this.getEntityManager())) emits the CREATE INDEX, as does MikroORM's own migration:create when NotificationEntity is discovered.

  • Prisma: add the @@index line to the model above and run prisma migrate dev.

  • Drizzle with drizzle-kit: the index is part of createNotificationTables() (named <table>_notifiable_idx when you pass custom tableNames), so drizzle-kit migrations include it.

Building the index locks writes on a large table for the duration of the build (a plain CREATE INDEX on Postgres), and with autoCreateSchema on it runs during boot. On a big table, create it yourself beforehand, for example with CREATE INDEX CONCURRENTLY on Postgres, using the same name notifications_notifiable_idx and your table's actual column names; the boot then finds it and skips it.

Managed tables (schema-diff tooling)

The MikroORM, TypeORM and Drizzle adapters export notificationsManagedTables() — the table name(s) this store creates/manages (['notifications']), derived from the same set ensureSchema() scopes its fingerprint to. Feed it to your ORM's schema-diff exclude list (e.g. MikroORM's schemaGenerator.skipTables) so a consumer never hand-maintains this table name separately — mirroring durableManagedTables() / telescopeManagedTables() from the sibling @dudousxd ecosystem libs.

import { notificationsManagedTables } from '@dudousxd/nestjs-notifications-database-mikro-orm';

schemaGenerator: { skipTables: notificationsManagedTables() },

Database schema

Keep the library's tables (notifications, and the digest tables) in a database schema of their own, e.g. notifications, instead of your app's default one. On Postgres that's a schema; on MySQL it's a database. The schema is created at boot when it's missing (with auto-create on), and every statement the store issues names it.

The stores never set search_path. Don't point search_path at the library's schema yourself to get the same effect: behind PgBouncer in transaction mode it leaks from one client to the next.

Pass schema to the store modules, and register the entities built for that schema in your DataSource (or turn on autoLoadEntities):

app.module.ts
import {
  TypeOrmNotificationStoreModule,
  TypeOrmPendingDigestStoreModule,
  digestWindowEntity,
  notificationEntity,
  pendingDigestEntity,
} from '@dudousxd/nestjs-notifications-database-typeorm';

TypeOrmModule.forRoot({
  type: 'postgres', // or 'mysql'
  // ...
  entities: [
    notificationEntity({ schema: 'notifications' }),
    pendingDigestEntity({ schema: 'notifications' }),
    digestWindowEntity({ schema: 'notifications' }),
  ],
}),
TypeOrmNotificationStoreModule.forFeature({ schema: 'notifications' }),
TypeOrmPendingDigestStoreModule.forFeature({ schema: 'notifications' }),

The migration helpers take the same option: createNotificationsTable(queryRunner, { schema }), createPendingDigestTables(queryRunner, { schema }), and notificationsManagedTables({ schema }) returns the qualified name.

A few rules apply to every adapter:

  • The name must start with a letter or _, then letters, digits or _ (TypeORM and MikroORM also allow $), at most 63 characters. Drizzle refuses 'public': leave schema unset for it.
  • SQLite has no schemas: setting schema there fails at boot.
  • The schema is created only when it's missing. If the database user can't create it, the error names the grant to add (Postgres: GRANT CREATE ON DATABASE <db> TO <role>; MySQL: GRANT CREATE ON `<schema>`.* TO <user>), or create the schema yourself first.
  • Without schema, nothing changes.

On this page