Replies: 1 comment
-
idk about SQLite (I guess quite similar), but here is how I managed to do it for pg: import { type BuildColumns, type TableRelationsHelpers, relations } from 'drizzle-orm';
import { type PgTableWithColumns } from 'drizzle-orm/pg-core';
import { materialProfile, user } from '#/db/schema/schema';
import { type commonSettingsColumns } from '#/db/schema/utils';
export function createSettingsTableRelations<
TBuild extends BuildColumns<
TTable['_']['name'],
typeof commonSettingsColumns,
TTable['_']['config']['dialect']
>,
TTable extends PgTableWithColumns<{
columns: Pick<TBuild, 'materialProfileId' | 'lastUpdatedByUserId'>;
name: string;
dialect: 'pg';
schema: undefined;
}>,
>(settingsTable: TTable) {
const helpers = ({ one }: TableRelationsHelpers<TTable['_']['name']>) => ({
materialProfile: one(materialProfile, {
fields: [settingsTable.materialProfileId],
references: [materialProfile.id],
}),
lastUpdatedByUser: one(user, {
fields: [settingsTable.lastUpdatedByUserId],
references: [user.id],
}),
});
return relations<TTable['_']['name'], ReturnType<typeof helpers>>(settingsTable, helpers);
} export const commonSettingsColumns = {
materialProfileId: cuid2('material_profile_id')
.references(() => materialProfile.id)
.primaryKey(),
lastUpdatedByUserId: uuid('last_updated_by_user_id')
.references(() => user.id)
.notNull(),
updatedAt: timestamp('updated_at').notNull(),
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to create a function that takes in a SQLite table that at least has the column {id: number}:
After
CMD + click
'ing through TypeScript types, I'm burnt! There's so many types with generics it's hard to figure out what to do. This is the closet I got, which still gives some errors, but I'm hoping there a way to be less verbose for the columns:Is there a simpler type helper that essentially says "this column is a SQLite integer" without having to pass so many specifics? My main concern is that as Drizzle receives updates, I may be missing required properties again.
Beta Was this translation helpful? Give feedback.
All reactions