Drizzle/better-auth + Local PSQL

I have the biggest issue trying to setup drizzle with my project and I don't believe it should be this complicated as I am facing currently. Someone with more experience please help me out and explain to me what I am doing wrong.

So I am trying to setup better-auth with Drizzle + my local PostgreSQL and I am using the latest version of Next js for my project.

I basically start of my initialising my next js project by running the regular command npx create-next-app@latest (app router no src-folder) and go then install drizzle following the docs and run the commands that are shown in the docs/guide. I create my
.env
file and setup my DATABASE_URL="postgres://postgres:mypassword@localhost:5432/postgres" and move on to creating my db directory and place the index.ts file inside of it which holds this code.

import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

export function createClient(connectionString: string) {
  const client = postgres(connectionString);
  return drizzle(client, { schema });
}

export type DbClient = ReturnType<typeof createClient>;

export function createMigrationClient(connectionString: string) {
  const psql = postgres(connectionString, { max: 1 });
  const db = drizzle(psql, { schema });

  return db;
}


And I then create my schema.ts file. This is basically just copied over from the docs. Afterwards I move on to create my drizzle.config.ts file but I slightly modify it.

import 'dotenv/config';
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
  out: './drizzle',
  schema: './src/db/schema.ts',
  dialect: 'postgresql',
  dbCredentials: {
    url: process.env.DATABASE_URL!,
  },
});


What I change here is that I change the schema: './src/db/schema.ts', to schema: './db/schema.ts', for app directory in next js.
Was this page helpful?