Reusable pgTable column helper - losing type : [x: string] instead of user_id

Hi, I am trying to create a reusable column to use accross multiple tables. If I use the following :
export const userId = char("user_id", {
length: 10,
}).references(() => users.id);
export const userId = char("user_id", {
length: 10,
}).references(() => users.id);
I get the correct type in my KyselyDatabase interface. user_id: ColumnType<string, string, string>; I'd like it to be a function instead. But when I do the following :
export const userId = (name?: string) => {
return char(name ?? "user_id", {
length: 10,
}).references(() => users.id);
};
export const userId = (name?: string) => {
return char(name ?? "user_id", {
length: 10,
}).references(() => users.id);
};
I get the following type (thus breaking types accross all my db queries) [x: string]: ColumnType<string | null, string, string>;
4 Replies
pvman
pvman5mo ago
When I use a var : char(name...) I have a return type of <PgCharBuilderInitial<string ... and if I use directly char("user_id") , I get the correct one : <PgCharBuilderInitial<"user_id" ...
Sillvva
Sillvva5mo ago
Use a generic:
export function userId<TName extends string>(name?: TName) {
return char(name ?? "user_id", {
length: 10
}).references(() => users.id);
};
export function userId<TName extends string>(name?: TName) {
return char(name ?? "user_id", {
length: 10
}).references(() => users.id);
};
pvman
pvman5mo ago
Thanks a bunch!!! Works perfectly.
Seifeddine  LARGAT
hello , please i'm trying to do something similar but i'm getting a circular dependency here is where i define the shared columns :
const tokenCols = {
id: serial('id').primaryKey(),
userId: text('user_id')
.notNull()
.references(() => usersTable.id, { onDelete: 'cascade' }),
email: text('email').notNull(),
token: text('token').notNull(),
expiresAt: timestamp('expires_at', { withTimezone: true, mode: 'date' }).notNull()
};

const timestampsCols = {
createdAt: timestamp('created_at', { mode: 'date' }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { mode: 'date' }).$onUpdate(() => new Date())
};

const coordinatesCols = {
latitude: text('latitude'),
longitude: text('longitude')
};

function lower(email: AnyPgColumn): SQL {
return sql`lower(${email})`;
}

export {lower, coordinatesCols, timestampsCols, tokenCols };
const tokenCols = {
id: serial('id').primaryKey(),
userId: text('user_id')
.notNull()
.references(() => usersTable.id, { onDelete: 'cascade' }),
email: text('email').notNull(),
token: text('token').notNull(),
expiresAt: timestamp('expires_at', { withTimezone: true, mode: 'date' }).notNull()
};

const timestampsCols = {
createdAt: timestamp('created_at', { mode: 'date' }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { mode: 'date' }).$onUpdate(() => new Date())
};

const coordinatesCols = {
latitude: text('latitude'),
longitude: text('longitude')
};

function lower(email: AnyPgColumn): SQL {
return sql`lower(${email})`;
}

export {lower, coordinatesCols, timestampsCols, tokenCols };
error:
Cannot access 'timestampsCols' before initialization
Cannot access 'timestampsCols' before initialization
FYI: each table is defined in a single file (i'm using a barrel file to export the hole schema )
Want results from more Discord servers?
Add your server