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

@entitykit/nestjs

v0.1.0-alpha.2

Published

Lifecycle-safe EntityKit integration for NestJS applications.

Readme

Nest owns one provider data source for the application. Feature services receive a runner that creates a fresh DbContext for each unit of work and always disposes it. A context is never accidentally shared between concurrent requests.

[!IMPORTANT] This package begins with the coordinated 0.1.0-alpha.2 EntityKit family. It was not part of the 0.1.0-alpha.1 six-package release.

The integration targets NestJS 12 and emits native ESM. Configure the consuming application with "type": "module" and TypeScript module and moduleResolution set to NodeNext. Nest's decorators also require:

{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Install

Install the coordinated family after alpha.2 is promoted:

npm install @entitykit/core@alpha @entitykit/postgres@alpha @entitykit/nestjs@alpha \
  pg @nestjs/common@^12 @nestjs/core@^12 reflect-metadata rxjs

Before promotion, consume the workspace build; the alpha.1 registry family does not contain @entitykit/nestjs.

Define a context

Passing a data source to DbContext now configures it by convention, so a server context only needs its sets and model:

import { DbContext, type ModelBuilder } from "@entitykit/core";

export class User {
  id!: string;
  email!: string;
}

export class AppDbContext extends DbContext {
  readonly users = this.set(User);

  protected override model(model: ModelBuilder): void {
    model.entity(User, entity => {
      entity.toTable("users");
      entity.hasKey(user => user.id);
      entity.property(user => user.id).hasColumnType("text").isRequired();
      entity.property(user => user.email).hasColumnType("text").isRequired();
    });
  }
}

Register once

import { Module } from "@nestjs/common";
import { EntityKitModule } from "@entitykit/nestjs";
import { createPostgresDataSource } from "@entitykit/postgres";
import { AppDbContext } from "./app-db-context.js";

const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
  throw new Error("DATABASE_URL is required");
}

@Module({
  imports: [
    EntityKitModule.forRoot({
      dataSource: createPostgresDataSource(databaseUrl),
    }),
    EntityKitModule.forFeature([AppDbContext]),
  ],
})
export class AppModule {}

Use forRootAsync() when configuration comes from injected Nest providers. The root module is global; forFeature() exports only the runners a feature declares.

The example below uses the optional @nestjs/config package:

npm install @nestjs/config
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { EntityKitModule } from "@entitykit/nestjs";
import { createPostgresDataSource } from "@entitykit/postgres";

@Module({
  imports: [
    EntityKitModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        dataSource: createPostgresDataSource(
          config.getOrThrow<string>("DATABASE_URL"),
        ),
      }),
    }),
  ],
})
export class DatabaseModule {}

Run one unit of work

import { Injectable } from "@nestjs/common";
import {
  EntityKitContextRunner,
  InjectEntityKitContextRunner,
} from "@entitykit/nestjs";
import { AppDbContext, type User } from "./app-db-context.js";

@Injectable()
export class UsersService {
  constructor(
    @InjectEntityKitContextRunner(AppDbContext)
    private readonly contexts: EntityKitContextRunner<AppDbContext>,
  ) {}

  list(): Promise<User[]> {
    return this.contexts.run(db => db.users.orderBy(user => user.email).toArray());
  }
}

run() awaits the callback and context disposal. If both fail, it throws an AggregateError containing both failures instead of hiding either one.

Request-specific context arguments

Constructor arguments after the data source flow through run():

import {
  type DbContextOptionsBuilder,
  type EntityKitDataSource,
} from "@entitykit/core";
import type { EntityKitContextRunner } from "@entitykit/nestjs";
import { AppDbContext } from "./app-db-context.js";

class TenantDbContext extends AppDbContext {
  constructor(source: EntityKitDataSource, private readonly tenantId: string) {
    super(source);
  }

  protected override configure(options: DbContextOptionsBuilder): void {
    options.useTenantScope(() => this.tenantId);
  }
}

function listOrders(
  contexts: EntityKitContextRunner<TenantDbContext, [tenantId: string]>,
  trustedTenantId: string,
) {
  return contexts.run(db => db.users.toArray(), trustedTenantId);
}

Keep authentication and tenant authorization in your application. Pass only a trusted server-side tenant identity into the context.

Shutdown and ownership

By default, EntityKit closes the data source during Nest application shutdown. Call app.enableShutdownHooks() if OS signals should trigger Nest lifecycle hooks. Use ownership: "external" only when another component owns disposal.

Do not register a raw DbContext as a singleton or request-scoped provider. Contexts are non-concurrent units of work, and Nest does not run lifecycle hooks for request-scoped providers. The runner is the safe boundary.

For a complete framework walkthrough and the Next.js 16 Node-runtime pattern, read the framework guide.

License

MIT