Skip to content

Commit ae00828

Browse files
committed
WIP
1 parent 4a3bc18 commit ae00828

38 files changed

+831
-84
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React, { FC } from 'react';
2+
import Admonition from '@theme/Admonition';
3+
4+
interface ZModelVsPSLProps {
5+
children: React.ReactNode;
6+
}
7+
8+
const ZModelVsPSL: FC<ZModelVsPSLProps> = ({ children }) => {
9+
return (
10+
<Admonition type="info" title="🔋 ZModel vs PSL">
11+
{children}
12+
</Admonition>
13+
);
14+
};
15+
16+
export default ZModelVsPSL;

versioned_docs/version-3.x/migration/_category_.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
position: 3
1+
position: 4
22
label: Migration
33
collapsible: true
44
collapsed: true

versioned_docs/version-3.x/migration/introduction.md

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
sidebar_position: 1
3+
description: ZenStack migration overview
4+
---
5+
6+
# Overview
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
position: 3
2-
label: Writing Schema
1+
position: 2
2+
label: Data Modeling
33
collapsible: true
44
collapsed: true
55
link:
66
type: generated-index
7-
title: Writing Schema
7+
title: Data Modeling
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
sidebar_position: 4
3+
description: Attributes in ZModel
4+
---
5+
6+
import ZModelVsPSL from '../_components/ZModelVsPSL';
7+
8+
# Attribute
9+
10+
Attributes allow you to attach metadata to models and fields. As you've seen in the previous sections, they are used for many purposes, like adding unique constraints, mapping names, etc. Attributes are also indispensable for modeling relations between models.
11+
12+
## Naming conventions
13+
14+
By convention, attributes attached to models use a double `@@` prefix, while those for fields use a single `@` prefix.
15+
16+
```zmodel
17+
model User {
18+
id Int @id
19+
email String @unique
20+
21+
@@index([email, name])
22+
}
23+
```
24+
25+
## Defining and applying attributes
26+
27+
<ZModelVsPSL>
28+
Prisma schema doesn't allow users to define custom attributes, while ZModel allows it and uses it as a key mechanism for extensibility.
29+
</ZModelVsPSL>
30+
31+
ZModel comes with a rich set of attributes that you can use directly. See [ZModel Language Reference](../reference/zmodel-language.md) for a complete list. You can also define your own custom attributes for specific purposes. Attributes are defined with a list of typed parameters. Parameters can named (default) or positional. Positional parameters can be passed with or without an explicit name. Parameters can also be optional.
32+
33+
Here's an example for how the `@unique` attribute is defined:
34+
35+
```zmodel
36+
attribute @unique(map: String?, length: Int?, sort: SortOrder?)
37+
```
38+
39+
You can apply it in various ways:
40+
41+
```zmodel
42+
model Foo {
43+
x String @unique() // default application
44+
y String @unique('y_unique') // positional parameter
45+
z String @unique(map: 'z_unique', length: 10) // named parameter
46+
}
47+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
sidebar_position: 7
3+
description: Custom types in ZModel
4+
---
5+
6+
import ZModelVsPSL from '../_components/ZModelVsPSL';
7+
8+
# Custom Type
9+
10+
<ZModelVsPSL>
11+
Custom type is a ZModel concept and doesn't exist in PSL.
12+
</ZModelVsPSL>
13+
14+
Besides models, you can also define custom types to encapsulate complex data structures. The main difference between a model and a custom type is that the latter is not backed by a database table.
15+
16+
Here's a simple example:
17+
18+
```zmodel
19+
type Address {
20+
street String
21+
city String
22+
country String
23+
zip Int
24+
}
25+
```
26+
27+
Custom types are defined exactly like models, with the exception that they cannot contain fields that are relations to other models. They can, however, contain fields that are other custom types.
28+
29+
There are two ways to use custom types:
30+
31+
```zmodel
32+
type Address {
33+
street String
34+
city String
35+
country String
36+
zip Int
37+
}
38+
39+
type UserProfile {
40+
gender String
41+
address Address?
42+
}
43+
```
44+
45+
- [Mixin](./mixin.md)
46+
- [Strongly typed JSON fields](./strong-typed-json.md)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
sidebar_position: 2
3+
description: Datasource in ZModel
4+
---
5+
6+
import Tabs from '@theme/Tabs';
7+
import TabItem from '@theme/TabItem';
8+
import ZModelVsPSL from '../_components/ZModelVsPSL';
9+
10+
# Data Source
11+
12+
The `datasource` block provides information about the database your application uses. The ORM relies on it to determine the proper SQL dialect to use when generating queries. If you use [Migration](../migration/), it must also have a `url` field that specifies the database connection string, so that the migration engine knows how to connect to the database. The `env` function can be used to reference environment variables so you can keep sensitive information out of the code.
13+
14+
:::tip
15+
You can use both single quote and double quote for string literals.
16+
:::
17+
18+
Each ZModel schema must have exactly one `datasource` block.
19+
20+
<Tabs>
21+
22+
<TabItem value="postgresql" label="PostgreSQL" default>
23+
```zmodel
24+
datasource db {
25+
provider = 'postgresql'
26+
url = env('DATABASE_URL')
27+
}
28+
```
29+
</TabItem>
30+
31+
<TabItem value="sqlite" label="SQLite">
32+
```zmodel
33+
datasource db {
34+
provider = 'sqlite'
35+
url = 'file:./dev.db'
36+
}
37+
```
38+
</TabItem>
39+
40+
</Tabs>
41+
42+
Currently only PostgreSQL and SQLite are supported. MySql will be supported in a future release. There's no plan for other relational database types or NoSQL databases.
43+
44+
<ZModelVsPSL>
45+
ZenStack's ORM runtime doesn't rely on the `url` information to connect to the database. Instead, you provide the information when constructing an ORM client. More on this in the [ORM](../orm/) section.
46+
</ZModelVsPSL>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
sidebar_position: 4
3+
description: Enums in ZModel
4+
---
5+
6+
# Enum
7+
8+
Enums are simple constructs that allow you to define a set of named values.
9+
10+
```zmodel
11+
enum Role {
12+
USER
13+
ADMIN
14+
}
15+
```
16+
17+
They can be used to type model fields:
18+
19+
```zmodel
20+
model User {
21+
id Int @id
22+
// highlight-next-line
23+
role Role @default(USER)
24+
}
25+
```
26+
27+
Enum field names are added to the global scope and are resolved without qualification. You need to make sure they don't collide with other global names.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
sidebar_position: 8
3+
description: Reusing common fields with mixins
4+
---
5+
6+
import ZModelVsPSL from '../_components/ZModelVsPSL';
7+
8+
# Mixin
9+
10+
<ZModelVsPSL>
11+
Mixin is a ZModel concept and don't exist in PSL.
12+
</ZModelVsPSL>
13+
14+
:::info
15+
Mixin was previously known as "abstract inheritance" in ZenStack v2. It's renamed and changed to use the `with` keyword to distinguish from polymorphic model inheritance.
16+
:::
17+
18+
Very often you'll find many of your models share quite a few common fields. It's tedious and error-prone to repeat them. As a rescue, you can put those fields into custom types, and "mix-in" them into your models.
19+
20+
***Before:***
21+
22+
```zmodel
23+
model User {
24+
id String @id
25+
createdAt DateTime @default(now())
26+
updatedAt DateTime @updatedAt
27+
email String @unique
28+
}
29+
```
30+
31+
***After:***
32+
33+
```zmodel
34+
type BaseFieldsMixin {
35+
id String @id
36+
createdAt DateTime @default(now())
37+
updatedAt DateTime @updatedAt
38+
}
39+
40+
model User with BaseFieldsMixin {
41+
email String @unique
42+
}
43+
```
44+
45+
A model can use multiple mixins as long as their field names don't conflict.
46+
47+
Mixins don't exist at the database level. The fields defined in the mixin types are conceptually inlined into the models that use them.

0 commit comments

Comments
 (0)