Last active
September 17, 2024 22:07
-
-
Save IIvexII/f4a8eedfd05e5327f36c904bf69199d8 to your computer and use it in GitHub Desktop.
Setup Drizzle on nextjs project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# required | |
-------------------------- | |
pnpm add drizzle-orm postgres dotenv | |
pnpm add -D drizzle-kit | |
-------------------------- | |
npx drizzle-kit generate | |
npx drizzle-kit migrate | |
-------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { drizzle } from "drizzle-orm/postgres-js"; | |
import { migrate } from "drizzle-orm/postgres-js/migrator"; | |
import postgres from "postgres"; | |
import dotenv from "dotenv"; | |
dotenv.config(); | |
// for migrations | |
const migrationClient = postgres(process.env.DATABASE_URL as string, { max: 1 }); | |
migrate(drizzle(migrationClient), "./src/drizzle/migrations"); | |
// for query purposes | |
const queryClient = postgres(process.env.DATABASE_URL as string); | |
export const db = drizzle(queryClient); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineConfig } from "drizzle-kit"; | |
export default defineConfig({ | |
schema: "./src/drizzle/schema.ts", | |
out: "./src/drizzle/migrations", | |
dialect: "postgresql", | |
dbCredentials: { | |
url: process.env.DATABASE_URL as string, | |
}, | |
verbose: true, | |
strict: true, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { pgTable, uuid, varchar } from "drizzle-orm/pg-core"; | |
export const userTable = pgTable("user", { | |
id: uuid("id").primaryKey().defaultRandom(), | |
name: varchar("name", {length: 255}).notNull() | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATABASE_URL="postgresql://<username>:<password>@localhost:5432/<db>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment