Skip to main content

Change the data model

Marblism generates all the data models for your entities you need. But as your app evolves, you may need to add more attributes or more entities to your app.

All your data models are located in the /models folder.

When you change the model, it automatically updates the database and the associated CRUD endpoints in the dev environment.

Change the attributes of an entity

Let's say we want to add a new string hashtag attribute on our Tweet entity.

/models/tweet.zmodel

model Tweet {

id String @id @default(uuid())
userId String?
user User? @relation(fields: [userId], references: [id], name:"user")
content String?
hashtag String // add hashtag attribute

dateCreated DateTime @default(now())
dateUpdated DateTime @updatedAt @default(now())
}
info

Marblism uses ZenStack/Prisma data model, to learn more about all the supported types, read this.

Add a new entity

Suppose we want to create a new entity Like to keep track of each like on the tweets.

1. Create a new file like.zmodel in the /models folder

import "./tweet.zmodel"
import "./user.zmodel"

model Like {

id String @id @default(uuid())
tweetId String
tweet Tweet? @relation(fields: [postId], references: [id], name:"post")
userId String
user User? @relation(fields: [userId], references: [id], name:"user")

dateCreated DateTime @default(now())
dateUpdated DateTime @updatedAt @default(now())

@@allow("all", true)
}

2. Import the new model in the schema.zmodel

import "./account.zmodel";
import "./user.zmodel";
import "./role.zmodel";
import "./session.zmodel";
import "./verificationToken.zmodel";
import "./tweet.zmodel";

import "./like.zmodel"; // import our new model

generator client {
provider = "prisma-client-js"
}
...

You can now use it in your code, for example to fetch all the likes from a spefific tweet:

const { data: likes } = Api.like.findMany.useQuery({
where: {
tweetId: '1',
},
})

console.log(`The tweet #1 has ${likes.length} likes`)