romeerez / orchid-orm

Orchid ORM
https://orchid-orm.netlify.app/
MIT License
481 stars 13 forks source link

zod schemas #318

Closed minkoonim closed 1 week ago

minkoonim commented 2 weeks ago
const dbTables = {
...
}

export const db = orchidORM(database, dbTables)

export function getSchema<N extends keyof typeof dbTables>(name: N) {
  const table = dbTables[name] as typeof dbTables[N]
  const inputSchema = (table).inputSchema()

  return {
    input: inputSchema,
    query: inputSchema.partial().partial().optional(),
    update: inputSchema.partial(),
  }
}

I'm doing this, as where input is for create, update is for update, query is for "where". All the type hints are correct.

but I'm getting runtime error

 ERROR  [nitro] [uncaughtException] Cannot read properties of undefined (reading 'optional')

  at node_modules/zod/lib/index.mjs:2626:45
  at Array.forEach (<anonymous>)
  at ZodObject.partial (node_modules/zod/lib/index.mjs:2620:37)
  at <anonymous> (server/trpc/routers/design/category.ts:7:51)
  at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
  at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
  at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5)
romeerez commented 2 weeks ago

I guess it's no longer relevant after solving https://github.com/romeerez/orchid-orm/issues/305?

romeerez commented 1 week ago

If this still bothers, feel free to re-open with more details.