"Extend" tables by other tables

Hey all, first of all great work with drizzle. Haven't had the time to play around with it so far. But just from how I currently use other orms. I have an AbstractEntity "class" which isn't an actual db table, but shares the common column each table should have e.g. a unique id. Basically like a parent class, but for DB tables. Is it possible to do something similar. Without trying: Can I just create an object using the given types/constructors from drizzle and then spread them? I hope that makes sense
1 Reply
icarus
icarus2y ago
yea the table builders just take regular js objects for columns, so u can simply do something like this:
const commonColumns = {
id: serial("id").primaryKey(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
};

export const users = mysqlTable("users", {
...commonColumns,
username: varchar("username", { length: 128 }).notNull(),
bio: varchar("bio", { length: 255 }),
});

export const cars = mysqlTable("cars", {
...commonColumns,
name: varchar("name", { length: 128 }),
});
const commonColumns = {
id: serial("id").primaryKey(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
};

export const users = mysqlTable("users", {
...commonColumns,
username: varchar("username", { length: 128 }).notNull(),
bio: varchar("bio", { length: 255 }),
});

export const cars = mysqlTable("cars", {
...commonColumns,
name: varchar("name", { length: 128 }),
});
Want results from more Discord servers?
Add your server