DT
Drizzle Team•12mo ago
omar

How to create a variable with the same name as the table name?

I have my schema defined like this.
schema.ts
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";

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

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

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

export type User = typeof users.$inferSelect;
And then I have this file, where I'm trying to query the users.
page.tsx
import db from "@/db";
import { users, User } from "@/db/schema";

export default async function Home() {
const usrs: Array<User> = await db.select().from(users);

console.log("hello");
console.log("users: ", users);

return (
<div>
{usrs.map((usr) => (
<p key={usr.id}>{usr.fullName}</p>
))}
</div>
);
}
page.tsx
import db from "@/db";
import { users, User } from "@/db/schema";

export default async function Home() {
const usrs: Array<User> = await db.select().from(users);

console.log("hello");
console.log("users: ", users);

return (
<div>
{usrs.map((usr) => (
<p key={usr.id}>{usr.fullName}</p>
))}
</div>
);
}
Is there anyway I can reuse the name of the table? For example, const users? Also, I'm new to Postgres and JS ORMs in general, coming from Entity Framework, is there a way to define the model/schema in singular terms and then the ORM rename the table to a plural form? I mean, my schema would be User and then in the database, it will be Users. Thanks!
7 Replies
Angelelz
Angelelz•12mo ago
The javascript name of your table can be whatever you want. The string you pass as the first parameter will end up being the actual name in the database I've seen people suggesting naming your tables [name]Table, for example, in your case usersTable That'll avoid name colisions. And in javascript, whenever you have name colisions, you can rename variables at the import like this:
import { users as usersTable, User } from "@/db/schema"
import { users as usersTable, User } from "@/db/schema"
omar
omarOP•12mo ago
But in the database, won't the table name be "usersTable". I find this a bit odd. I mean, it's a table. Why call it "sometihngTable".. Any other way to do it?
Angelelz
Angelelz•12mo ago
export const usersTable = pgTable("users", { // <-- then name in the database will be users
export const usersTable = pgTable("users", { // <-- then name in the database will be users
omar
omarOP•12mo ago
Opps, sorry.. Thank you, though. 🙂
hugo
hugo•12mo ago
I like to name my drizzle table variables in NameCap: export const Users = pgTable(‘users’)…
Angelelz
Angelelz•12mo ago
I usually leave User for the type. but this is just preference
omar
omarOP•12mo ago
How can I omit the id though from the type so I can use later?
Want results from more Discord servers?
Add your server