How can I properly compile the type of schema?

Hello. I'm currently using Turborepo to manage the monorepo. I want to separate the schema declaration of drizzle to internal package. But when I tries to export typeof schema/drizzle instance, it loses the information about tables. I tried using tsc, esbuild, tsup but still no luck. Is there anyone successfully compiled type of the schema?
2 Replies
정제훈
정제훈OP2mo ago
// schema.ts
import { pgEnum, pgTable, text, timestamp, unique } from "drizzle-orm/pg-core";

export const providerTypeEnum = pgEnum("providerType", [
"google",
"naver",
"kakao",
"apple",
]);

export const users = pgTable(
"user",
{
id: text().primaryKey().notNull(),
providerType: providerTypeEnum("provider_type").notNull(),
providerId: text().notNull(),
username: text().notNull(),
email: text().notNull(),
avatar: text(),
},
(t) => ({
unique: unique().on(t.providerId, t.providerType),
}),
);

export const sessions = pgTable("session", {
id: text().primaryKey().notNull(),
userId: text()
.notNull()
.references(() => users.id),
expiresAt: timestamp({ mode: "date" }).notNull(),
});
// schema.ts
import { pgEnum, pgTable, text, timestamp, unique } from "drizzle-orm/pg-core";

export const providerTypeEnum = pgEnum("providerType", [
"google",
"naver",
"kakao",
"apple",
]);

export const users = pgTable(
"user",
{
id: text().primaryKey().notNull(),
providerType: providerTypeEnum("provider_type").notNull(),
providerId: text().notNull(),
username: text().notNull(),
email: text().notNull(),
avatar: text(),
},
(t) => ({
unique: unique().on(t.providerId, t.providerType),
}),
);

export const sessions = pgTable("session", {
id: text().primaryKey().notNull(),
userId: text()
.notNull()
.references(() => users.id),
expiresAt: timestamp({ mode: "date" }).notNull(),
});
compiles to
정제훈
정제훈OP2mo ago
and importing from this .d.ts file doesn't reproduce same result when importing like import * from "./schema" Oh, actually it still gives a problem without compile. directly export raw ts file doesn't do anything.
export * from "drizzle-orm";
export * from "drizzle-orm/postgres-js";
import postgres from "postgres";
export { postgres };
export * from "drizzle-orm";
export * from "drizzle-orm/postgres-js";
import postgres from "postgres";
export { postgres };
exporting like this from db repo fixes it!
Want results from more Discord servers?
Add your server