Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/entity-types-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Module `@zenstackhq/runtime/types` contains type definitions of entities, filter

Suppose a `User` model is defined in ZModel:

```prisma
```zmodel
model User {
id String @id @default(cuid())
email String @unique @email
Expand Down
2 changes: 1 addition & 1 deletion docs/entity-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Module `@zenstackhq/runtime/types` contains type definitions of entities, filter

Suppose a `User` model is defined in ZModel:

```prisma
```zmodel
model User {
id String @id @default(cuid())
email String @unique @email
Expand Down
36 changes: 18 additions & 18 deletions docs/get-started/learning-the-zmodel-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Every model needs to include exactly one `datasource` declaration, providing inf

The recommended way is to load the connection string from an environment variable, like:

```prisma
```zmodel
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
Expand All @@ -24,7 +24,7 @@ Data models define the shapes of entities in your application domain. They inclu

Here's an example of a blog post model:

```prisma
```zmodel
model Post {
// the mandatory primary key of this model with a default UUID value
id String @id @default(uuid())
Expand Down Expand Up @@ -59,7 +59,7 @@ Attributes attached to fields are prefixed with '@', and those to models are pre

Here're some examples of commonly used attributes:

```prisma
```zmodel
model Post {
// @id is a field attribute, marking the field as a primary key
// @default is another field attribute for specifying a default value for the field if it's not given at creation time
Expand Down Expand Up @@ -97,7 +97,7 @@ ZenStack inherits most attributes and functions from Prisma, and added a number

You can override the default setting with the `saltLength` or `salt` named parameters, like:

```prisma
```zmodel
model User {
password String @password(saltLength: 16)
}
Expand All @@ -115,7 +115,7 @@ If both `saltLength` and `salt` parameters are provided, `salt` is used.

E.g.:

```prisma
```zmodel
model User {
password String @password @omit
}
Expand All @@ -129,7 +129,7 @@ The special `@relation` attribute expresses relations between data models. Here'

- One-to-one

```prisma
```zmodel
model User {
id String @id
profile Profile?
Expand All @@ -144,7 +144,7 @@ model Profile {

- One-to-many

```prisma
```zmodel
model User {
id String @id
posts Post[]
Expand All @@ -159,7 +159,7 @@ model Post {

- Many-to-many

```prisma
```zmodel
model Space {
id String @id
members Membership[]
Expand Down Expand Up @@ -194,7 +194,7 @@ This document serves as a quick overview for starting with the ZModel language.

Access policies express authorization logic in a declarative way. They use `@@allow` and `@@deny` rules to specify the eligibility of an operation over a model entity. The signatures of the attributes are:

```prisma
```zmodel
@@allow(operation, condition)
@@deny(operation, condition)
```
Expand All @@ -216,26 +216,26 @@ You can use `auth()` to:

- Check if a user is logged in

```prisma
```zmodel
@@deny('all', auth() == null)
```

- Access user's fields

```prisma
```zmodel
@@allow('update', auth().role == 'ADMIN')
```

- Compare user identity

```prisma
```zmodel
// owner is a relation field to User model
@@allow('update', auth() == owner)
```

### A simple example with Post model

```prisma
```zmodel
model Post {
// reject all operations if user's not logged in
@@deny('all', auth() == null)
Expand All @@ -250,7 +250,7 @@ model Post {

### A more complex example with multi-user spaces

```prisma
```zmodel
model Space {
id String @id
members Membership[]
Expand Down Expand Up @@ -322,7 +322,7 @@ model User {

As you've seen in the examples above, you can access fields from relations in policy expressions. For example, to express "a user can be read by any user sharing a space" in the `User` model, you can directly read into its `membership` field.

```prisma
```zmodel
@@allow('read', membership?[space.members?[user == auth()]])
```

Expand Down Expand Up @@ -358,15 +358,15 @@ Collection predicate expressions are boolean expressions used to express conditi

The `condition` expression has direct access to fields defined in the model of `collection`. E.g.:

```prisma
```zmodel
@@allow('read', members?[user == auth()])
```

, in condition `user == auth()`, `user` refers to the `user` field in model `Membership`, because the collection `members` is resolved to `Membership` model.

Also, collection predicates can be nested to express complex conditions involving multi-level relation lookup. E.g.:

```prisma
```zmodel
@@allow('read', membership?[space.members?[user == auth()]])
```

Expand Down Expand Up @@ -434,7 +434,7 @@ The following attributes can be used to attach field constraints:

### Sample usage

```prisma
```zmodel
model User {
id String @id
handle String @regex("^[0-9a-zA-Z]{4,16}$")
Expand Down
2 changes: 1 addition & 1 deletion docs/get-started/next-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ npm i @zenstackhq/runtime

Here's an example of using a Postgres database with connection string specified in `DATABASE_URL` environment variable:

```prisma
```zmodel
datasource db {
provider = 'postgresql'
url = env('DATABASE_URL')
Expand Down
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/ga.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-typescript.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-json.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/zenstack@dev/bundle/res/prism-zmodel.js"></script>
</body>

<style>
Expand Down
2 changes: 1 addition & 1 deletion docs/integrating-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default NextAuth(authOptions);

NextAuth is agnostic about the type of underlying database, but it requires certain table structures, depending on how you configure it. Your ZModel definitions should reflect these requirements. A sample `User` model is shown here (to be used with `CredentialsProvider`):

```prisma
```zmodel
model User {
id String @id @default(cuid())
email String @unique @email
Expand Down
12 changes: 6 additions & 6 deletions docs/modeling-your-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The very first thing to do is to configure how to connect to your database.

Here's an example for using a PosgreSQL with is connection string read from `DATABASE_URL` environment variable:

```prisma
```zmodel
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
Expand All @@ -25,7 +25,7 @@ Data models define the shapes of business entities in your app. A data model con

Here's an example of a blog post model:

```prisma
```zmodel
model Post {
// @id attribute marks a field as unique identifier,
// mapped to database table's primary key
Expand Down Expand Up @@ -59,7 +59,7 @@ Here are some examples:

- One-to-one

```prisma
```zmodel
model User {
id String @id
profile Profile?
Expand All @@ -74,7 +74,7 @@ model Profile {

- One-to-many

```prisma
```zmodel
model User {
id String @id
posts Post[]
Expand All @@ -89,7 +89,7 @@ model Post {

- Many-to-many

```prisma
```zmodel
model Space {
id String @id
members Membership[]
Expand Down Expand Up @@ -135,7 +135,7 @@ A few quick notes before diving into examples:

Let's look at a few examples now:

```prisma
```zmodel
model User {
id String @id
posts Post[]
Expand Down
20 changes: 10 additions & 10 deletions docs/zmodel-access-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Access policies use `@@allow` and `@@deny` rules to specify the eligibility of a

- `@@allow`

```prisma
```zmodel
attribute @@allow(_ operation: String, _ condition: Boolean)
```

Expand All @@ -17,7 +17,7 @@ Access policies use `@@allow` and `@@deny` rules to specify the eligibility of a

- `@@deny`

```prisma
```zmodel
attribute @@deny(_ operation: String, _ condition: Boolean)
```

Expand All @@ -36,19 +36,19 @@ You can use `auth()` to:

- Check if a user is logged in

```prisma
```zmodel
@@deny('all', auth() == null)
```

- Access user's fields

```prisma
```zmodel
@@allow('update', auth().role == 'ADMIN')
```

- Compare user identity

```prisma
```zmodel
// owner is a relation field to User model
@@allow('update', auth() == owner)
```
Expand All @@ -57,7 +57,7 @@ You can use `auth()` to:

As you've seen in the examples above, you can access fields from relations in policy expressions. For example, to express "a user can be read by any user sharing a space" in the `User` model, you can directly read into its `membership` field.

```prisma
```zmodel
@@allow('read', membership?[space.members?[user == auth()]])
```

Expand Down Expand Up @@ -93,15 +93,15 @@ Collection predicate expressions are boolean expressions used to express conditi

The `condition` expression has direct access to fields defined in the model of `collection`. E.g.:

```prisma
```zmodel
@@allow('read', members?[user == auth()])
```

, in condition `user == auth()`, `user` refers to the `user` field in model `Membership`, because the collection `members` is resolved to `Membership` model.

Also, collection predicates can be nested to express complex conditions involving multi-level relation lookup. E.g.:

```prisma
```zmodel
@@allow('read', membership?[space.members?[user == auth()]])
```

Expand All @@ -119,7 +119,7 @@ A data model can contain arbitrary number of policy rules. The logic of combinin

### A simple example with Post model

```prisma
```zmodel
model Post {
// reject all operations if user's not logged in
@@deny('all', auth() == null)
Expand All @@ -134,7 +134,7 @@ model Post {

### A more complex example with multi-user spaces

```prisma
```zmodel
model Space {
id String @id
members Membership[]
Expand Down
Loading