mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
16 lines
617 B
TypeScript
16 lines
617 B
TypeScript
|
import { IdentifierSqlTokenType, sql } from 'slonik';
|
||
|
|
||
|
type Table = { table: string; fields: Record<string, string> };
|
||
|
type FieldIdentifiers<Key extends string | number | symbol> = {
|
||
|
[key in Key]: IdentifierSqlTokenType;
|
||
|
};
|
||
|
|
||
|
export const convertToIdentifiers = <T extends Table>({ table, fields }: T) => ({
|
||
|
table: sql.identifier([table]),
|
||
|
fields: Object.entries<string>(fields).reduce(
|
||
|
(previous, [key, value]) => ({ ...previous, [key]: sql.identifier([table, value]) }),
|
||
|
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
|
||
|
{} as FieldIdentifiers<keyof T['fields']>
|
||
|
),
|
||
|
});
|