W
Wasp•3mo ago
Sarmad

CreatedAt, UpdatedAt, DeletedAt

Hey Guys! I want to ask how do you manage the timestamps for your models. I wanted to have createdAt, updatedAt, and deletedAt attribute for all my models but adding these fields manually to all models seems a bit tedious and against the DRY principle. Is there a better way? I thought of creating a base model with these defined and extending all other models from it but prisma doesnt support inheritence either. Before adding these properties in each model, i would like to ask for some guidance as i am new to this framework. below is my Task model. model Task { id Int @id @default(autoincrement()) description String isDone Boolean @default(false) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt deletedAt DateTime? } P.S: DeletedAt is ofcourse in case of soft delete which is my requirement, in case of hard delete the record simply doesnt exist Regards, Sarmad
6 Replies
kapa.ai
kapa.ai•3mo ago
Hi @Sarmad. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hello Sarmad, In Wasp, you define your data models using Prisma Schema Language (PSL) within the entity declaration. As you've mentioned, Prisma doesn't support inheritance, so you'll have to manually add createdAt, updatedAt, and deletedAt fields to each of your models. Here's how you can define these fields in your Task entity:
entity Task {=psl
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
psl=}
entity Task {=psl
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
psl=}
The createdAt field is set to the current time by default when a new task is created. The updatedAt field is automatically updated whenever the task is updated. The deletedAt field can be used to implement soft deletion by setting it to the time when the task is deleted. Unfortunately, there's no built-in way to automatically add these fields to all models in Wasp or Prisma. You'll have to add them to each model manually. For more information on defining entities in Wasp, you can refer to the official documentation (https://wasp-lang.dev/docs/data-model/entities#defining-an-entity). Regards, kapa.ai ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sarmad, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Sarmad
SarmadOP•3mo ago
@kapa.ai is there something similar to mongodb's timestamp: true?
kapa.ai
kapa.ai•3mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Unfortunately, as of now, Wasp and its underlying database layer Prisma do not support a feature similar to MongoDB's timestamp: true which automatically adds createdAt and updatedAt fields to the schema. In Prisma, you have to manually add these fields to each model in your Prisma schema. Here's how you can define these fields in Prisma:
model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
The createdAt field is set to the current time by default when a new task is created. The updatedAt field is automatically updated whenever the task is updated. As for MongoDB support, it's currently not available in Wasp. This was confirmed in a Discord support thread (https://discord.com/channels/686873244791210014/1196714311633092668) where a Wasp team member mentioned that while Prisma does support MongoDB, Wasp does not yet support it. For more information on defining entities in Wasp, you can refer to the official documentation (https://wasp-lang.dev/docs/data-model/entities#defining-an-entity). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sarmad, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos•3mo ago
@Sarmad good question! So this is Prisma specific, so it compeltely depends on how this is doable in Prisma. Unforuntaely yeah, there is no way to DRY this in Prisma, you have to copy this for every model. I do wonder why they do it like that, why not offer some way to extend it (I also used mongoose in the past so I would certainly not repeat it there) but that is how they do it at the moment.
Sarmad
SarmadOP•3mo ago
@martinsos thankyou for clarifying this 🙌
martinsos
martinsos•3mo ago
Btw related issue on the Prisma repo: https://github.com/prisma/prisma/issues/2371
Want results from more Discord servers?
Add your server