Skip to content

Commit e99836f

Browse files
committed
fix minor issues in the nest.js v3 docs, switch to SQLite
1 parent 89846e0 commit e99836f

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

versioned_docs/version-3.x/recipe/nestjs.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,41 @@ This guide describes different ways to use ZenStack in a [NestJS](https://nestjs
1010

1111
ZenStack offers a standard ORM component that you can use in your NestJS application just like any other ORM. To get started, create a `DbService` by extending the `ZenStackClient` constructor:
1212

13-
```ts title="db.service.ts"
14-
import { ZenStackClient } from '@zenstackhq/orm';
15-
import { PostgresDialect } from '@zenstackhq/orm/dialects/postgres';
16-
import { schema, type SchemaType } from './zenstack/schema';
17-
import { Pool } from 'pg';
13+
```ts title="src/db/db.service.ts"
14+
import { ClientOptions, ZenStackClient } from '@zenstackhq/orm';
15+
import { schema, type SchemaType } from '../../zenstack/schema';
16+
import { SqliteDialect } from '@zenstackhq/orm/dialects/sqlite';
17+
import SQLite from 'better-sqlite3';
1818

1919
export class DbService extends ZenStackClient<
2020
SchemaType,
2121
ClientOptions<SchemaType>
2222
> {
2323
constructor() {
2424
super(schema, {
25-
dialect: new PostgresDialect({
26-
pool: new Pool({ connectionString: process.env.DATABASE_URL })
27-
}),
25+
dialect: new SqliteDialect({ database: new SQLite('./zenstack/dev.db') }),
2826
});
2927
}
3028
}
3129
```
3230

3331
You can then register this service as a provider in a module:
3432

35-
```ts title="app.module.ts"
33+
```ts title="src/app.module.ts"
3634
import { Module } from '@nestjs/common';
3735
import { AppController } from './app.controller';
38-
import { DbService } from './db.service';
36+
import { DbService } from './db/db.service';
3937

4038
@Module({
4139
controllers: [AppController],
4240
providers: [DbService],
4341
})
42+
export class AppModule {}
4443
```
4544

4645
Finally, inject the `DbService` in your controllers or other services to access the database:
4746

48-
```ts title="app.controller.ts"
47+
```ts title="src/app.controller.ts"
4948
import { Controller, Get } from '@nestjs/common';
5049
import { DbService } from './db.service';
5150

@@ -66,12 +65,13 @@ To leverage ZenStack's [built-in access control features](../orm/access-control/
6665

6766
In the sample below, we provide an access-controlled `DbService` under the name "AUTH_DB":
6867

69-
```ts title="app.module.ts"
68+
```ts title="src/app.module.ts"
7069
import { Module, Scope } from '@nestjs/common';
70+
import { AppController } from './app.controller';
71+
import { DbService } from './db/db.service';
7172
import { REQUEST } from '@nestjs/core';
7273
import type { Request } from 'express';
7374
import { PolicyPlugin } from '@zenstackhq/plugin-policy';
74-
import { DbService } from './db.service';
7575
// your authentication helper
7676
import { getRequestUser } from './auth.util';
7777

@@ -101,11 +101,12 @@ import { getRequestUser } from './auth.util';
101101
},
102102
],
103103
})
104+
export class AppModule {}
104105
```
105106

106107
Now you can choose to use the access-controlled `DbService` in your controllers or services by injecting it with the "AUTH_DB" token:
107108

108-
```ts title="app.controller.ts"
109+
```ts title="src/app.controller.ts"
109110
import { Controller, Get, Inject } from '@nestjs/common';
110111
import { DbService } from './db.service';
111112

@@ -126,11 +127,12 @@ export class AppController {
126127

127128
ZenStack also provides [API handlers](../service/api-handler/) that process CRUD requests automatically for you. To use it, create a catch-all route in your controller and forward the request to the API handler:
128129

129-
```ts title="app.controller.ts"
130+
```ts title="src/app.controller.ts"
130131
import { Controller, All, Inject, Param, Query, Req, Res } from '@nestjs/common';
131132
import { RestApiHandler } from '@zenstackhq/server/api';
132133
import type { Request, Response } from 'express';
133-
import { DbService } from './db.service';
134+
import { DbService } from './db/db.service';
135+
import { schema } from '../zenstack/schema';
134136

135137
@Controller('api')
136138
export class AppController {

0 commit comments

Comments
 (0)