Heya friends, does anyone have a good way of gener...
# orm-help
k
Heya friends, does anyone have a good way of generating an assertion function based on a dynamic type like those we get from prisma queries? For example:
Copy code
const users = await prismaRead.user.findMany({
  select: {
    firstName: true,
    email: true,
    id: true,
    team: true,
  },
  orderBy: {
    createdAt: 'asc',
  },
})
const data: LoaderData = {users}
return data // this gets sent over the network as json

// then later, on the other side of a network request, in a React component for example:

const data = useLoaderData()
assert(data, LoaderData)
data.users[0].firstName // <-- autocompletes
Basically, I want something like https://docs.superstructjs.org/ except the other way around. I'd like the ability to generate an assertion function based on a type. Yes I know that TypeScript is stripped at build-time. But maybe there's a way I could restructure this to put my query options in a variable and pass that object to both prisma and this runtime utility?
The more I think about it, the more I think we'd need some kind of compilation plugin that can know the types to properly generate the assertion function 😬
I believe such plugins exist for babel, but I'm using esbuild 😞
a
it’s very easy when i using graphql codegen
k
I guess I'd need to build my own codegen tool if I want to have it for my use case
sigh
r
Something like this with Prisma’s built-in types would be a great option.