I'm trying to add schema for the crud operations for my db. I'm adding schema to my drizzle() options but it does nothing (I'dont have any error inside index.ts file). I have to manually import all tables that I want to use. If I don't do that and insert table name anyway (no autocompletion from typescript) I'm getting an error: Cannot find name 'users'. Right now my code looks like this:
// index.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import * as schema from "./schema";
import * as dotenv from "dotenv";
dotenv.config();
const pool = new Pool({
connectionString: process.env.MAIN_DB_URL,
});
export const mainDb = drizzle(pool, { schema: schema });
// schema.ts
export const users = pgTable("users", {
id: varchar("id", { length: 255 }).primaryKey(),
login: varchar("login", { length: 255 }).notNull().unique(),
firstName: varchar("first_name", { length: 255 }).notNull(),
lastName: varchar("last_name", { length: 255 }).notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});
export const userRelations = relations(users, ({ one }) => ({
password: one(passwords, {
fields: [users.id],
references: [passwords.userId],
}),
}));
export const passwords = pgTable("passwords", {
userId: varchar("user_id", { length: 255 })
.notNull()
.unique()
.references(() => users.id),
createdAt: timestamp("created_at").notNull().defaultNow(),
});
// some other file
import { mainDb } from "../index";
// I have to import each table or I get an error like "Cannot find name 'users'"
import { users } from "../schema";
...
await mainDb
.insert(users)
.values({
id: id,
login: "test",
firstName: "John",
lastName: "Doe",
})
"drizzle-orm": "^0.28.5",
"drizzle-kit": "^0.19.13",
"pg": "^8.11.3",
"typescript": "5.1.6",
Also It would be good to know how can I set schema for the db form multiple schemas inside schema folder