Home > Software engineering >  updating note on remix indie stack
updating note on remix indie stack

Time:09-05

Remix has an example repo called indie-stack https://github.com/remix-run/indie-stack. I'm very new to frontend development and I've never really used TS, react or prisma before.

I'm looking through the https://github.com/remix-run/indie-stack/blob/main/app/models/note.server.ts file and trying to figure out how to write a function that would update a note, given a new title and body, by trying to use the getNote function as an example, and prisma's documentation: https://www.prisma.io/docs/concepts/components/prisma-client/crud#update

export function updateNote({
  id,
  body,
  title,
  userId,
}: Pick<Note, "body" | "title" | "id"> & { userId: User["id"] }) {
  return prisma.note.update({
    where: { id, userId },
    data: {
      title,
      body,
    },
  });
}

typescript doens't like the where: { id, userId}, which puzzles me since it's used in the get and delete function.

Type '{ id: string; userId: string; }' is not assignable to type 'NoteWhereUniqueInput'.
  Object literal may only specify known properties, and 'userId' does not exist in type 'NoteWhereUniqueInput'.ts(2322)
index.d.ts(3448, 5): The expected type comes from property 'where' which is declared here on type '{ select?: NoteSelect | null | undefined; include?: NoteInclude | null | undefined; data: (Without<NoteUpdateInput, NoteUncheckedUpdateInput> & NoteUncheckedUpdateInput) | (Without<...> & NoteUpdateInput); where: NoteWhereUniqueInput; }'

Would really appreciate an explanation on why my function is wrong, and how I should write it instead.

- Thanks.

CodePudding user response:

You are calling Prisma update API (the link might not get to right to the update section, click "update" in the right-hand navigation).

That API wants *WhereUniqueInput type as input, and here is relevant part of the docs:

Wraps all unique fields of a model so that individual records can be selected.

Note model has following definition (removed non-significant parts):

model Note {
  id    String @id @default(cuid())
  userId String
}

So, id is the primary key, and therefore it is unique, but userId is just a field without @unique decorator and not a primary key, and thus, it cannot be a part of where in update API call.

But do you really need it? It seems your app is meant to update a note which belongs to current user, so, no need to update userId, and id already unambiguously identifies specific note. I think your function should look like this:

export function updateNote({
  id,
  body,
  title,
}: Pick<Note, "body" | "title" | "id">) {
  return prisma.note.update({
    where: { id },
    data: { 
      title,
      body,
    },
  });
}
  • Related