0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00
logto/packages/core/src/database/utils.ts

16 lines
617 B
TypeScript
Raw Normal View History

2021-06-23 12:09:42 -05:00
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']>
),
});