access drizzle schema, to use with drizzle plugins

default discord avatar
kris0x9 months ago
7

how can i access drizzle table objects from payload object?



i'd like to generate zod schemas

https://orm.drizzle.team/docs/zod

from my collections

  • discord user avatar
    denolfe
    9 months ago

    This is accessible via payload.db. Documentation here:

    https://payloadcms.com/docs/database/postgres#access-to-drizzle
  • default discord avatar
    kris0x9 months ago

    Yes I tried it here. I can get payload.db.drizzle.


    But then getting to the tables the types get lost



    The generated zod insert schema doesn't have the autocomplete



    Or maybe my ide needed restart. Will check and get back to you



    // Schema for inserting a user - can be used to validate API requests
    import {createInsertSchema, createSelectSchema} from "drizzle-zod";
    import payload from "payload";
    import {z} from "zod";
    
    const insertUserSchema = createInsertSchema(payload.db.tables.users);
    
    // Schema for selecting a user - can be used to validate API responses
    const selectUserSchema = createSelectSchema(payload.db.tables.users);
    
    type InsertUser = z.infer<typeof insertUserSchema>;


    /Users//bun-payloadv2/src/server.ts:35
    const insertUserSchema = createInsertSchema(payload.db.tables.users);
    ^
    TypeError: Cannot read properties of undefined (reading 'tables')
    at Object.<anonymous> (/Users//bun-payloadv2/src/server.ts:35:56)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module.m._compile (/Users//bun-payloadv2/node_modules/ts-node/src/index.ts:1056:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Object.require.extensions.<computed> [as .ts] (/Users//bun-payloadv2/node_modules/ts-node/src/index.ts:1059:12)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at main (/Users//bun-payloadv2/node_modules/ts-node/src/bin.ts:198:14)
    at Object.<anonymous> (/Users//bun-payloadv2/node_modules/ts-node/src/bin.ts:288:3)
    [nodemon] app crashed - waiting for file changes before starting...
    ^[[1;2A^[[1;2A[nodemon] restarting due to changes...
    [nodemon] starting `ts-node src/server.ts -- -I`
    [23:45:56] INFO (payload): Starting Payload...
    [23:45:56] INFO (payload): Payload Admin URL: /admin
    [23:45:56] INFO (payload): Server listening on http://localhost:3000
    [nodemon] restarting due to changes...
    [nodemon] starting `ts-node src/server.ts -- -I`
    
    /Users//bun-payloadv2/src/payload.config.ts:41
    const insertUserSchema = createInsertSchema(payload.db.tables.users);


    so i don't get type inference



    and it fails like above in runtime



    if we can get this to work, it would be trully next level



    i could use these zod schemas for my trpc router 🤯

    image.png
  • discord user avatar
    denolfe
    9 months ago

    I think

    payload.db.schema

    is likely what you're looking for.

  • default discord avatar
    magicspon8 months ago

    @kris did you get anywhere with this?

  • default discord avatar
    kris0x8 months ago

    Not yet 😞

  • default discord avatar
    magicspon8 months ago

    are you seeing any types on

    payload.db.drizzel.query

    ?





    payload/trpc/drizzle would be rather splendid!

    PixelSnap_2023-11-05_at_21.16.032x.png
  • default discord avatar
    willviles8 months ago

    The fact typings are lost from Drizzle is a real problem 😢 I've got some complex queries that I'd like to use Drizzle for - and I'm also using trpc, so there's currently a lot of zod schemas defined which would be unnecessary if typings were fixed and

    drizzle-zod

    was an option.



    Could Payload's generated types be used as a generic in a similar way to the Local API to ensure Drizzle types are maintained? If no, what's the blocker on that?

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.