redwoodjs / redwood

The App Framework for Startups
https://redwoodjs.com
MIT License
17k stars 974 forks source link

Support Prisma findFirst in SDL/Service Generators #1399

Open dthyresson opened 3 years ago

dthyresson commented 3 years ago

Current CRUD generation support fetching a unique record of the model by its id.

There may be times where it is useful to findFirst such that Prisma does not throw an exception if not found -- or you have multiple matching records and need just one.

The findFirst query returns the first record in a list that matches your criteria. findFirst accepts the same arguments as findMany.

https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud#findfirst

Perhaps:

export const firstModel = async ({ input }) => {
  return await db.model.findOne({
    where: { input },
  })
}

By adding this and showcasing it in the generator, perhaps more developers would opt to use that versus a findMany with and order and a take of 1 ... or worse: using the existing findMany() option and returning the first array indexed item at [0].

Open for discussion to see if helpful to have added.

jtoar commented 2 years ago

@dthyresson Thoughts on this one now that it's been a while? Think it's still worthwhile? Can queue back up for discussion

ageddesi commented 8 months ago

@dthyresson @jtoar Just taking a look back at the beginning of the issues list and thought this would be interesting to implement if it is still something we would find useful.