Skip to content

Commit 9151941

Browse files
authored
doc: v3 postgres multi-schema guide (#505)
* doc: v3 postgres multi-schema guide * update
1 parent 5e12739 commit 9151941

File tree

6 files changed

+83
-4
lines changed

6 files changed

+83
-4
lines changed

src/pages/v3/_components/Service.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ export {
7575
<TabItem label="Client Code" value="client">
7676
<CodeBlock language="tsx" title="React Example" className="p-4 xl:text-lg">
7777
{`import { schema } from './zenstack';
78-
import { useQueryHooks } from '@zenstackhq/query/react';
78+
import { useClientQueries } from '@zenstackhq/tanstack-query/react';
7979
8080
export function UserPosts({ userId }: { userId: number }) {
8181
// use auto-generated hook to query user with posts
82-
const { user: userHooks } = useQueryHooks(schema);
83-
const { data, isLoading } = userHooks.useFindUnique({
82+
const client = useClientQueries(schema);
83+
const { data, isLoading } = client.user.useFindUnique({
8484
where: { id: userId },
8585
include: { posts: true }
8686
});

versioned_docs/version-3.x/migrate-prisma.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,4 @@ Here's a list of Prisma features that are not supported in ZenStack v3:
270270
| [JSON Filters](https://www.prisma.io/docs/orm/reference/prisma-client-reference#json-filters) | Yes | |
271271
| [Full-Text Search](https://www.prisma.io/docs/orm/prisma-client/queries/full-text-search) | Yes | |
272272
| [Comparing Columns](https://www.prisma.io/docs/orm/reference/prisma-client-reference#compare-columns-in-the-same-table) | Yes | |
273-
| [Postgres Multi-Schema](https://www.prisma.io/docs/orm/prisma-schema/data-model/multi-schema) | Yes | |
273+
| [Database Seeding](https://www.prisma.io/docs/orm/prisma-migrate/workflows/seeding) | Yes | |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
position: 7
2+
label: Recipes
3+
collapsible: true
4+
collapsed: true
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Working With PostgreSQL Schemas
6+
7+
PostgreSQL has the concept of [Schema](https://www.postgresql.org/docs/current/ddl-schemas.html) that allows you to organize database objects into logical groups. This guide explains how to use multiple schemas in PostgreSQL with ZenStack.
8+
9+
Please note that the "Schema" term used in this guide refers specifically to the PostgreSQL concept, and has nothing to do with generic database schemas or the ZModel schema.
10+
11+
## Default Schema
12+
13+
By default, ZenStack assumes that all your models are in the default "public" schema and prepends table names with `public.` when generating SQL queries. You can change the default schema in the `datasource` block of ZModel:
14+
15+
```zmodel
16+
datasource db {
17+
provider = 'postgresql'
18+
defaultSchema = 'my_schema'
19+
}
20+
```
21+
22+
:::info
23+
If you're migrating from Prisma, you may have set the schema on the database url with the `?schema=` query parameter. This is not supported in ZenStack.
24+
:::
25+
26+
## Using Multiple Schemas
27+
28+
If you use multiple Postgres schemas, you can add a `schemas` field to the datasource block, and then use the `@@schema` attribute to specify the schema to use for each model:
29+
30+
```zmodel
31+
datasource db {
32+
provider = 'postgresql'
33+
schemas = ['auth', 'post']
34+
}
35+
36+
model User {
37+
id Int @id @default(autoincrement())
38+
name String
39+
40+
@@schema('auth')
41+
}
42+
43+
model Post {
44+
id Int @id @default(autoincrement())
45+
title String
46+
47+
@@schema('post')
48+
}
49+
```
50+
51+
For models that don't have the `@@schema` attribute, the default schema (either "public" or the one specified in `defaultSchema`) will be used.

versioned_docs/version-3.x/reference/zmodel/attribute.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,22 @@ model User {
374374
}
375375
```
376376

377+
### @@schema
378+
379+
```zmodel
380+
attribute @@schema(_ map: String)
381+
```
382+
383+
Specifies the schema to use in a multi-schema PostgreSQL database. See [Working With PostgreSQL Schemas](../../recipe/postgres-multi-schema.md) for more details.
384+
385+
```zmodel
386+
model User {
387+
id Int @id
388+
name String
389+
@@schema("auth")
390+
}
391+
```
392+
377393
### @id
378394

379395
```zmodel

versioned_docs/version-3.x/reference/zmodel/datasource.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ datasource NAME {
3232

3333
The `url` option is only used by the migration engine to connect to the database. The ORM runtime doesn't rely on it. Instead, you provide the connection information when constructing an ORM client.
3434

35+
- **`defaultSchema`**:
36+
37+
(PostgreSQL only) The default schema to use for models that don't have an explicit `@@schema` attribute. Defaults to "public". See [Working With PostgreSQL Schemas](../../recipe/postgres-multi-schema.md) for more details.
38+
39+
- **`schemas`**:
40+
41+
(PostgreSQL only) List of schemas to use. If specified, you can use the `@@schema` attribute to specify the schema name for a model. See [Working With PostgreSQL Schemas](../../recipe/postgres-multi-schema.md) for more details.
42+
3543
## Example
3644

3745
```zmodel

0 commit comments

Comments
 (0)