Enums

Cchrlie__4/11/2023
Can pgEnum be converted to zod enums?
Bbloberenober4/11/2023
yes
also, you can use drizzle-zod to do that for you
Cchrlie__4/13/2023
im using drizzle for the schema but i don't know how to do it for the enum
im trying to do this
const insertHistorySchemaWithProviders = insertHistorySchema.extend({
  providers: z
    .array(
      z.object({
        id: z.string().min(1),
        type: // Enum used in insertHistorySchema,
      }),
    )
    .min(1),
});

ok i think i found how
const insertHistorySchemaWithProviders = insertHistorySchema.extend({
  providers: z
    .array(
      z.object({
        id: z.string().min(1),
        type: z.enum(historyTypeEnum.enumValues),
      }),
    )
    .min(1),
});
Bbloberenober4/13/2023
you can just do insertHistorySchema.shape.historyType?
it's just a Zod object
you can use any Zod API on it
Cchrlie__4/13/2023
amazing mate
not sure enums are working.. check this i have this schema:

const insertHistorySchemaWithProviders = insertHistorySchema.extend({
  providers: z
    .array(
      z.object({
        id: z.number(),
        type: insertHistorySchema.shape.type,
      }),
    )
    .min(1),
});


but zod is giving me this error:
: /Users/carlos/code/nubi_monorepo/node_modules/.pnpm/zod@3.21.4/node_modules/zod/lib/types.js:2633
api:dev:         if (this._def.values.indexOf(input.data) === -1) {
api:dev:                              ^
api:dev:
api:dev:
api:dev: TypeError: this._def.values.indexOf is not a function


if i debug the zod codebase this is what we are sending to zod as this._def.values:

api:dev: this._def.values [Function (anonymous)] {
api:dev:   enumName: 'history_type',
api:dev:   enumValues: [ 'deposit', 'bail', 'mixed' ],
api:dev:   [Symbol(isPgEnum)]: true
api:dev: }
Cchrlie__4/13/2023
this looks fine not sure whats going on
Bbloberenober4/14/2023
looks like a bug
let me check
Cchrlie__4/14/2023
yeah,,, but the code looks fine

    } else if (column instanceof PgEnumColumn) {
            type = z.enum(column.enum.enumValues as [string, ...string[]]);

ill try to check why and make a aPR
Bbloberenober4/14/2023
I'm currently reworking the internal API for enums a bit, which affects how they are processed by drizzle-zod, so it'll be easier for me to do this on my own
Cchrlie__4/14/2023
great! in the meantime ill just hardcode the values
Bbloberenober4/14/2023
@charli can you update drizzle-orm and drizzle-zod to latest and try again?
Cchrlie__4/14/2023
i updated to:

- drizzle-orm 0.23.8
+ drizzle-orm 0.23.13

still same error
Cchrlie__4/15/2023
Cchrlie__4/15/2023
now the enum is converted to string instead of enum
Cchrlie__4/15/2023
also enum values are shown like this
Cchrlie__4/15/2023
upgraded again and now its fixed!
thanks man!

Looking for more? Join the community!

Recommended Posts
Typescript build fails with large number of columnsI have a table with ~460 columns, and on trying to run typescript on my app, I'm getting "error TS25many-to-one transactionHello, I want to create transaction involving arbitrary number of inserts. For example, Artist (one[Bug] Postgres migrations are broken on 0.23.10Re-running the same postgres migration twice just throws an error that a particular table already exdrizzle-kit doesn't pull foreign keys from supabase/postgres?It's hard to really turn this into a proper bug report because, honestly, with Supabase there could Why does drizzle-zod create ZodString's instead of z.ZodStrings for mySQL?I was testing out drizzle-zod for mySQL, and I know it's still actively being worked on. It seems liQuery for Prisma like insert in multiple joined tablesIs there a way to achieve insert in multiple tables joined by foreign keys in one query like Prisma?How to filter length in where for postgres?I have a text field, which I want to select only if it's length is higher than 50. How should I do tIs there currently a way to utilize onUpdate?Trying to run `ON UPDATE NOW()` for a `last_updated` column for whenever the row is changed.Migrations not working on github actionsAny suggestions to debug this on actions? Is it possible that the database is not created?"The supplied SQL string contains more than one statement" after running "generate:sqlite"I just updated one of my schemas with a few columns and now my project is broken. This is the gener[bug] Some sql statement looks broken when bulk insert```ts await db.insert(users) .values( { name: 'Andrew', createdAt: new Date(), Return first or throwI have a hard time believing this hasn't been asked yet, but my search results have yet to yield anyCan't find meta/_journal.json file when running migrateTrying to run `migrate()` from a lambda function and I can't figure out why it's unable to find the Wrong type on decimalsHello, I have encountered something interesting. The first screenshot shows what I'm inserting into CheckHello there, from this discussion: https://discord.com/channels/1043890932593987624/1090486363251544"Extend" tables by other tablesHey all, first of all great work with drizzle. Haven't had the time to play around with it so far. Bmaximum call stack exceededWeird bug where whenever I add a `where` `eq` clause getting maximum call stack exceededDrizzle recommended way to migrate resetApologies if this is a stupid question, I just recently migrated out of prisma and into drizzle. Onecompound uniqueIndexIn Prisma, I have in my schema definition the following declaration that requires userId and timestaplanetscale: how to index col using drizzle?Hi, seems most of the samples are pg. I'm using planetscale, and no having any luck figuring out how