You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+22-23Lines changed: 22 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,7 +118,7 @@ npm install -D @types/pg
118
118
Run the following command to sync schema to the database for local development:
119
119
120
120
```bash
121
-
npx zenstack db push
121
+
npx zen db push
122
122
```
123
123
124
124
> Under the hood, the command uses `prisma db push` to do the job.
@@ -127,17 +127,17 @@ See [database migration](#database-migration) for how to use migration to manage
127
127
128
128
## Compiling ZModel schema
129
129
130
-
ZModel needs to be compiled to TypeScript before being used to create a database client. Simply run the following command:
130
+
ZModel needs to be compiled to TypeScript before being used to create a database db. Simply run the following command:
131
131
132
132
```bash
133
-
npx zenstack generate
133
+
npx zen generate
134
134
```
135
135
136
136
A `schema.ts` file will be created inside the `zenstack` folder. The file should be included as part of your source tree for compilation/bundling. You may choose to include or ignore it in source control (and generate on the fly during build). Just remember to rerun the "generate" command whenever you make changes to the ZModel schema.
137
137
138
138
## Creating ZenStack client
139
139
140
-
Now you can use the compiled TypeScript schema to instantiate a database client.
140
+
Now you can use the compiled TypeScript schema to instantiate a database db.
141
141
142
142
### SQLite
143
143
@@ -147,7 +147,7 @@ import { schema } from './zenstack/schema';
@@ -205,7 +204,7 @@ ZenStack uses Kysely to handle database operations, and it also directly exposes
205
204
Please check [Kysely documentation](https://kysely.dev/docs/intro) for more details. Here're a few quick examples:
206
205
207
206
```ts
208
-
awaitclient.$qb
207
+
awaitdb.$qb
209
208
.selectFrom('User')
210
209
.leftJoin('Post', 'Post.authorId', 'User.id')
211
210
.select(['User.id', 'User.email', 'Post.title'])
@@ -215,7 +214,7 @@ await client.$qb
215
214
Query builder can also be "blended" into ORM API calls as a local escape hatch for building complex filter conditions. It allows for greater flexibility without forcing you to entirely resort to the query builder API.
216
215
217
216
```ts
218
-
awaitclient.user.findMany({
217
+
awaitdb.user.findMany({
219
218
where: {
220
219
age: { gt: 18 },
221
220
// "eb" is a Kysely expression builder
@@ -243,7 +242,7 @@ ZenStack v3 allows you to define database-evaluated computed fields with the fol
243
242
2. Provide its implementation using query builder when constructing `ZenStackClient`
244
243
245
244
```ts
246
-
const client = new ZenStackClient(schema, {
245
+
const db = new ZenStackClient(schema, {
247
246
...
248
247
computedFields: {
249
248
User: {
@@ -279,7 +278,7 @@ _Coming soon..._
279
278
280
279
### Runtime plugins
281
280
282
-
V3 introduces a new runtime plugin mechanism that allows you to tap into the ORM's query execution in various ways. A plugin implements the [RuntimePlugin](./packages/runtime/src/client/plugin.ts#L121) interface, and can be installed with the `ZenStackClient.$use` API.
281
+
V3 introduces a new runtime plugin mechanism that allows you to tap into the ORM's query execution in various ways. A plugin implements the [RuntimePlugin](./packages/runtime/src/client/plugin.ts#L121) interface, and can be installed with the `ZenStackdb.$use` API.
283
282
284
283
You can use a plugin to achieve the following goals:
285
284
@@ -288,7 +287,7 @@ You can use a plugin to achieve the following goals:
288
287
ORM query interception allows you to intercept the high-level ORM API calls. The interceptor's configuration is compatible with Prisma's [query client extension](https://www.prisma.io/docs/orm/prisma-client/client-extensions/query).
289
288
290
289
```ts
291
-
client.$use({
290
+
db.$use({
292
291
id: 'cost-logger',
293
292
onQuery: {
294
293
$allModels: {
@@ -312,7 +311,7 @@ Kysely query interception allows you to intercept the low-level query builder AP
312
311
Kysely query interception works against the low-level Kysely `OperationNode` structures. It's harder to implement but can guarantee intercepting all CRUD operations.
313
312
314
313
```ts
315
-
client.$use({
314
+
db.$use({
316
315
id: 'insert-interception-plugin',
317
316
onKyselyQuery({query, proceed}) {
318
317
if (query.kind==='InsertQueryNode') {
@@ -332,7 +331,7 @@ function sanitizeInsertData(query: InsertQueryNode) {
332
331
Another popular interception use case is, instead of intercepting calls, "listening on" entity changes.
333
332
334
333
```ts
335
-
client.$use({
334
+
db.$use({
336
335
id: 'mutation-hook-plugin',
337
336
beforeEntityMutation({ model, action }) {
338
337
console.log(`Before ${model} ${action}`);
@@ -346,7 +345,7 @@ client.$use({
346
345
You can provide an extra `mutationInterceptionFilter` to control what to intercept, and opt in for loading the affected entities before and/or after the mutation.
347
346
348
347
```ts
349
-
client.$use({
348
+
db.$use({
350
349
id: 'mutation-hook-plugin',
351
350
mutationInterceptionFilter: ({ model }) => {
352
351
return {
@@ -375,19 +374,19 @@ ZenStack v3 delegates database schema migration to Prisma. The CLI provides Pris
375
374
- Sync schema to dev database and create a migration record:
376
375
377
376
```bash
378
-
npx zenstack migrate dev
377
+
npx zen migrate dev
379
378
```
380
379
381
380
- Deploy new migrations:
382
381
383
382
```bash
384
-
npx zenstack migrate deploy
383
+
npx zen migrate deploy
385
384
```
386
385
387
386
- Reset dev database
388
387
389
388
```bash
390
-
npx zenstack migrate reset
389
+
npx zen migrate reset
391
390
```
392
391
393
392
See [Prisma Migrate](https://www.prisma.io/docs/orm/prisma-migrate) documentation for more details.
@@ -398,7 +397,7 @@ See [Prisma Migrate](https://www.prisma.io/docs/orm/prisma-migrate) documentatio
398
397
1. Remove "@prisma/client" dependency
399
398
1. Install "better-sqlite3" or "pg" based on database type
400
399
1. Move "schema.prisma" to "zenstack" folder and rename it to "schema.zmodel"
401
-
1. Run `npx zenstack generate`
400
+
1. Run `npx zen generate`
402
401
1. Replace `new PrismaClient()` with `new ZenStackClient(schema, { ... })`
0 commit comments