singgih
singgih
DTDrizzle Team
Created by singgih on 8/7/2024 in #help
Modify Table type object
i have helper function that can create SQL from cross database.
function extractKeys<T extends object>(obj: T) {
return Object.keys(obj) as (keyof T)[];
}

type ReturnCrossTable<T extends Table> = {
[Key in keyof T["_"]["columns"]]: string | SQLWrapper;
} & {
table: SQL;
};

export function crossDBTable<T extends Table>(
dbName: string,
data: T,
): ReturnCrossTable<T> {
const columns = getTableColumns(data);

return extractKeys(columns).reduce(
(acc, key) => {
return Object.assign(acc, {
[key]: sql.join([sql.identifier(dbName), columns[key]], sql`.`),
});
},
{
table: sql.join([sql.identifier(dbName), data], sql`.`),
},
) as ReturnCrossTable<T>;
}
function extractKeys<T extends object>(obj: T) {
return Object.keys(obj) as (keyof T)[];
}

type ReturnCrossTable<T extends Table> = {
[Key in keyof T["_"]["columns"]]: string | SQLWrapper;
} & {
table: SQL;
};

export function crossDBTable<T extends Table>(
dbName: string,
data: T,
): ReturnCrossTable<T> {
const columns = getTableColumns(data);

return extractKeys(columns).reduce(
(acc, key) => {
return Object.assign(acc, {
[key]: sql.join([sql.identifier(dbName), columns[key]], sql`.`),
});
},
{
table: sql.join([sql.identifier(dbName), data], sql`.`),
},
) as ReturnCrossTable<T>;
}
then i want to field table can return with type Table from drizzle but with table name modified.
3 replies