NestJS
This guide shows how to integrate @liorandb/driver with NestJS using a dedicated provider so you can inject a shared LioranClient (and optionally DB) anywhere in your app.
Install
npm i @liorandb/driver
1) Create a LioranDB module
This module creates one LioranClient, connects once during startup, and exports it for injection.
- TypeScript
- JavaScript
import { Global, Module } from "@nestjs/common";
import { LioranClient } from "@liorandb/driver";
export const LIORAN_CLIENT = Symbol("LIORAN_CLIENT");
@Global()
@Module({
providers: [
{
provide: LIORAN_CLIENT,
useFactory: async () => {
const uri = process.env.LIORAN_URI;
if (!uri) throw new Error("Missing env var: LIORAN_URI");
const client = new LioranClient(uri);
await client.connect();
return client;
},
},
],
exports: [LIORAN_CLIENT],
})
export class LioranDbModule {}
const { Global, Module } = require("@nestjs/common");
const { LioranClient } = require("@liorandb/driver");
const LIORAN_CLIENT = Symbol("LIORAN_CLIENT");
let LioranDbModule = class LioranDbModule {};
LioranDbModule = Global()(
Module({
providers: [
{
provide: LIORAN_CLIENT,
useFactory: async () => {
const uri = process.env.LIORAN_URI;
if (!uri) throw new Error("Missing env var: LIORAN_URI");
const client = new LioranClient(uri);
await client.connect();
return client;
},
},
],
exports: [LIORAN_CLIENT],
})(LioranDbModule)
);
module.exports = { LioranDbModule, LIORAN_CLIENT };
2) Inject and use the client
import { Inject, Injectable } from "@nestjs/common";
import type { LioranClient } from "@liorandb/driver";
import { LIORAN_CLIENT } from "./liorandb.module";
@Injectable()
export class ItemsService {
constructor(@Inject(LIORAN_CLIENT) private readonly client: LioranClient) {}
async list() {
const db = await this.client.db("default");
const items = db.collection<{ sku: string; qty: number }>("items");
return items.find({}).toArray();
}
}
Notes
close()is a no-op for the driver; you can still add shutdown hooks if you want, but it’s usually unnecessary.- Keep
LIORAN_URIin environment configuration, not hardcoded in providers.