Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nexus Prisma - How to handle createdAt and updatedAt with crud globally?

First thing I came up with, is by calling computedInputs in the nexusPrisma option. But it won't work since they need to be handled differently depending on the situation, but globally:

1. create -> createdAt = now, updatedAt = null
2. update -> createdAt = keep as it is, updatedAt = now

In order to make it work, I need to set computedInputs individually like so:

t.crud.createOneX({
  computedInputs: {
    createdAt: () => DateTime.utc().toString(),
    updatedAt: () => null,
  },
});

t.crud.updateOneX({
  computedInputs: {
    createdAt: () => undefined,
    updatedAt: () => DateTime.utc().toString(),
  },
});

While this might work, I'm unable to "compute" these inputs on the nested models. In order to prevent passing createdAt/updatedAt, I have to create another t.crud on that resource as well, without these timestamps.

The last workaround for this that might work, is to not use t.crud at all, which is a bummer.

like image 268
Eliya Cohen Avatar asked Dec 28 '20 08:12

Eliya Cohen


People also ask

What is crud in Prisma?

If you don't already know, CRUD is an acronym for Create, Read, Update, Delete. Prisma provides the toolset we'll need to perform these operations on our data out of the box through a set of intuitive functions. We'll take a brief look at each of these here to see how to use them.

How do I create data in Prisma?

Prisma provides two main functions that allow you to create data. These are create and createMany. Creating data is super simple with the create function. This function takes in an object with a data key and a select key. It returns a Promise with a User object.

What does the Prisma API Plugin do?

It gives you an API to project fields from models defined in your Prisma schema into your GraphQL API. It also gives you an API to build GraphQL root fields that allow your API clients to query and mutate data. Note: The Prisma team is currently rewriting the plugin to make it maintainable longterm.

What is the Nexus plugin?

Why Nexus? This plugin integrates Prisma into Nexus. It gives you an API to project fields from models defined in your Prisma schema into your GraphQL API. It also gives you an API to build GraphQL root fields that allow your API clients to query and mutate data.


1 Answers

Thank you for asking the question.

Prisma can handle the createdAt and updatedAt columns for you in your models. Add the @default(now()) and @updatedAt attributes to your createdAt and updatedAt columns respectively. You can add the columns to your models like this:

model Post {
  id               Int                @id @default(autoincrement())
  title            String
  content          String?
  published        Boolean?           @default(false)
  createdAt        DateTime           @default(now())
  updatedAt        DateTime           @updatedAt
}

You can learn more on the @default(now()) and @updatedAt in our docs.

In case you run into any other issues and queries, I'll be happy to help 🙂

like image 157
Alex Ruheni Avatar answered Oct 10 '22 03:10

Alex Ruheni