Replies: 3 comments 2 replies
-
You can create chunks normal objects and reuse them in actual table definitions. This example creates reusable
|
Beta Was this translation helpful? Give feedback.
-
been playing with drizzle for the first time and stumbled onto this problem. using example from issue #730 I was able to construct the following: base.ts
users.ts
user_role.ts
|
Beta Was this translation helpful? Give feedback.
-
Thought I'd add here - thanks to the above for getting me most of the way! I have a few compositions (baseTable and then a appTable which "inherits" from baseTable) and in Pg import type {
BuildExtraConfigColumns,
HasDefault,
IsPrimaryKey,
NotNull,
} from "drizzle-orm";
import type {
PgColumnBuilderBase,
PgTableExtraConfigValue,
PgTimestampBuilderInitial,
PgUUIDBuilderInitial,
} from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { pgTable, timestamp, uuid } from "drizzle-orm/pg-core";
export interface BaseColumns {
id: NotNull<IsPrimaryKey<NotNull<PgUUIDBuilderInitial<"id">>>>;
createdAt: NotNull<HasDefault<PgTimestampBuilderInitial<"created_at">>>;
updatedAt: NotNull<HasDefault<PgTimestampBuilderInitial<"updated_at">>>;
}
export const baseTable = <
TTableName extends string,
TColumnsMap extends Record<string, PgColumnBuilderBase>,
>(
name: TTableName,
columns: TColumnsMap,
extraConfig?: (
self: BuildExtraConfigColumns<TTableName, TColumnsMap & BaseColumns, "pg">,
) => PgTableExtraConfigValue[],
) => {
return pgTable(
name,
{
id: uuid(`id`).notNull().primaryKey(),
createdAt: timestamp("created_at")
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
updatedAt: timestamp("updated_at")
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
...columns,
},
(self) => {
const baseExtraConfig: PgTableExtraConfigValue[] = [];
if (extraConfig) {
const fullyTypedSelf = self as BuildExtraConfigColumns<
TTableName,
TColumnsMap & BaseColumns,
"pg"
>;
return [...baseExtraConfig, ...extraConfig(fullyTypedSelf)];
}
return baseExtraConfig;
},
);
}; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm looking for a way to either clone a table definition (with another table name) or inherit a table from another one.
I tried recomposing a new schema from pieces of
getTableConfig
but it seems already built, so typescript won't allow that.Is there any way to do a bit of meta programming around the schemas?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions