First
First
DTDrizzle Team
Created by First on 8/20/2024 in #help
Drizzle Migration but with TS code
Ah, my bad, found the GitHub issue (https://github.com/drizzle-team/drizzle-orm/discussions/1339) 🙇🏼‍♂️
7 replies
DTDrizzle Team
Created by First on 8/20/2024 in #help
Drizzle Migration but with TS code
Found it, This is what I mean.. I think payloadCMS has some custom drizzle version made for them, like this import type { DrizzleSnapshotJSON } from 'drizzle-kit/payload' (code) which they have a support of up() and down() function, where you can write your own typescript along with SQL by payload.db.drizzle.execute and it would save into migration schema
export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
await payload.db.drizzle.execute(sql`
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
"email" varchar NOT NULL,
"reset_password_token" varchar,
"reset_password_expiration" timestamp(3) with time zone,
"salt" varchar,
"hash" varchar,
"login_attempts" numeric,
"lock_until" timestamp(3) with time zone
);
....
export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
await payload.db.drizzle.execute(sql`
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
"email" varchar NOT NULL,
"reset_password_token" varchar,
"reset_password_expiration" timestamp(3) with time zone,
"salt" varchar,
"hash" varchar,
"login_attempts" numeric,
"lock_until" timestamp(3) with time zone
);
....
This way, we could re-use a lot of SQL query by save into some utils file. e.g. we could have some trigger PSQL function that we might run every end of some migration, we could make the migration code cleaner by extract it into iteral string in TS. here is a code (https://github.com/payloadcms/vercel-deploy-payload-postgres/blob/main/src/migrations/20240709_153941_initial.ts) Not sure when the official drizzle-kit will support this kind of functionality?
7 replies
DTDrizzle Team
Created by First on 8/20/2024 in #help
Drizzle Migration but with TS code
Thank you!
7 replies
DTDrizzle Team
Created by First on 8/20/2024 in #help
Drizzle Migration but with TS code
Drizzle doesn't provide any way to use TS code during migrations, you'd have to use SQL
Ah, sure then. seems it's not possible if I could leverage the migration schema created by drizzle to do some one-time scripting along with migration code. 🙇🏼‍♂️
7 replies