How can I read the table name and schema name of a created drizzle table instance?

Given a schema defined like this.
schema.ts
import { pgSchema, serial, text, varchar } from "drizzle-orm/pg-core";

export const core = pgSchema(“core”);

export const users = core.table("users", {
id: serial("id").primaryKey(),
fullName: text("full_name"),
phone: varchar("phone", { length: 256 }),
});

export type User = typeof users.$inferSelect;
schema.ts
import { pgSchema, serial, text, varchar } from "drizzle-orm/pg-core";

export const core = pgSchema(“core”);

export const users = core.table("users", {
id: serial("id").primaryKey(),
fullName: text("full_name"),
phone: varchar("phone", { length: 256 }),
});

export type User = typeof users.$inferSelect;
I have this file, where I'm importing the users table schema above. How can I access the string value of the table name and schema name that this users table refers to?
page.tsx

import { users } from "./schema";

// Expected output: “core.users”
function myTableName() {
const tableName = users._.name;
const tableSchema = users._.schema;

const fullName = `${tableSchema}.${tableName}`;

console.log(`My table name: ${fullName}`);

return fullName;
}
page.tsx

import { users } from "./schema";

// Expected output: “core.users”
function myTableName() {
const tableName = users._.name;
const tableSchema = users._.schema;

const fullName = `${tableSchema}.${tableName}`;

console.log(`My table name: ${fullName}`);

return fullName;
}
I would expect the above function to work, since typescript autocompletes the ._.name and ._.schema values in my editor, but when the code runs, the underscore property is undefined. Is there something excluding this property from the final build? Please let me know if there is a way to accomplish this or if it’s just not possible with the current version. Thanks!
3 Replies
Angelelz
Angelelz9mo ago
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Angelelz
Angelelz9mo ago
Everything in the _ is there only for type information. It doesn't have anything at runtime
zach
zach9mo ago
Perfect, thank you!
Want results from more Discord servers?
Add your server