zachr
zachr
DTDrizzle Team
Created by zachr on 2/3/2024 in #help
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!
4 replies