Skip to main content

Change the data model

Data Model in Marblism

In Marblism, your data models are defined in the /models/models.zmodel file. This file uses the ZenStack schema language, which is an extension of Prisma's schema definition language.

This powerful combination allows for efficient database operations, type safety, auto-generated back-end and enhanced features like permissions.

Here's a basic example of how a model might look:

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]

@@allow('create,read', true)
@@allow('update,delete', auth() == this)
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int

@@allow('read', published == true)
@@allow('create,update,delete', auth() == author)
}

In this example, we define two models: User and Post. Each model has fields with types and attributes. The @@allow directives are ZenStack-specific and define access control rules.

Adding a new model

To add a new model to your application:

  1. Open the models.zmodel file in your project.
  2. Define your new model using ZenStack schema syntax. For example:
model NewModel {
id Int @id @default(uuid())
name String
dateCreated DateTime @default(now())
}

Editing a model property

To edit a property of an existing model:

  1. Locate the model in your models.zmodel file.
  2. Modify the property as needed. For example, changing a field type or adding a new field:
model ExistingModel {
id Int @id @default(uuid())
name String
age Int? // New field
content String @default('') // New field with an empty default value
}
tip

Tip: to avoid having to deal with migration always add fields as optional (using the '?' suffix) or give them a default value.

Applying changes

After modifying your data model, you need to apply these changes to your API and database.

When using the workspace, a popup will appear automatically as soon as you edit your models in the models.zmodel file. This popup allows you to run both pnpm run crud:sync and pnpm run database:sync commands with a single click, streamlining the process of applying your model changes.

Key Features

  1. Type Safety: ZenStack inherits Prisma's strong typing, ensuring type safety across your application.

  2. Access Control: Use @@allow and @@deny directives to define fine-grained access control rules directly in your schema.

  3. Validation: Define custom validation rules using ZenStack's enhanced schema language.

  4. Relations: Define relationships between models easily, just like in Prisma.

  5. Auto-generated CRUD Operations: Marblism automatically generates CRUD endpoints based on your ZenStack schema.

Using Your Models

Once defined, these models are used throughout your Marblism application. The auto-generated API endpoints will allow you to interact with your data. Read more about it here: Querying the API.

For more detailed information on ZenStack's features and syntax, refer to the official ZenStack documentation.