I've been setting up a project with Drizzle + Next.js + Vercel for my serverless backend. I want to use the ORM api of Drizzle, therefore i reference my database like so :
import { drizzle } from "drizzle-orm/vercel-postgres";
import { sql } from "@vercel/postgres";
import { users } from "./schema";
import * as schema from "./schema";
export const db = drizzle(sql, { schema });
I noticed you need to pass the {schema} as a parameter to infer typing for typescript and use the ORM relations you define in your schemas.
But then i noticed that the drizzle-kit provides a drizzle.config.ts file which also references your schema like so:
import "@/lib/config";
import { defineConfig } from "drizzle-kit";
export default defineConfig({
schema: "./lib/schema.ts",
out: "./drizzle",
driver: "pg",
dbCredentials: {
connectionString: process.env.POSTGRES_URL,
},
verbose: true,
strict: true,
});
So naturally, i thought i did not need the second optional part of "drizzle(sql, { schema });" if i use drizzle-kit but this does not seem to be the case.
The Drizzle documentation specifies :
Drizzle Kit lets you split your schema in different files and even have multiple schemas for different databases in one project. You can rapidly prototype database schema and push it directly to the database.
So why do i need to redefine my schemas location twice ? What is the purpose of my drizzle.config.ts file ?
Thanks a lot
I tried to pass the config object directly to drizzle like so :
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import {config} from "@/drizzle.config";
// import dotenv from 'dotenv'
// dotenv.config({ path: '.env.local' })
// use dotenv or this custom next js script to load .env.local variables in process.env for Node.js backend
import { loadEnvConfig } from "@next/env";
const projectDir = process.cwd();
loadEnvConfig(projectDir);
export const db = drizzle(sql, config);
or like this :
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import {config} from "@/drizzle.config";
// import dotenv from 'dotenv'
// dotenv.config({ path: '.env.local' })
// use dotenv or this custom next js script to load .env.local variables in process.env for Node.js backend
import { loadEnvConfig } from "@next/env";
const projectDir = process.cwd();
loadEnvConfig(projectDir);
export const db = drizzle(sql, {schema: config.schema});
but this did not work
If you're not using the relational query builder feature of drizzle, then you don't need to specify your schema upfront. The getting started docs don't reference schema. I've just removed the second argument to drizzle all-together and I'm still getting type-safety in my queries. See relational query builder docs that explain you must use schema if using relational query builder.
Side note: I ran into a lot of circular dependency issues importing all of my tables into a single schema object, so I'd recommend removing it based on my experience.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With