From f68d7a16c495fcead3655423914935f6d152a9f1 Mon Sep 17 00:00:00 2001 From: manelcecs Date: Thu, 14 Nov 2024 09:49:07 +0100 Subject: [PATCH] Add function to obtain table columns --- src/util/types.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/util/types.ts b/src/util/types.ts index 118ea506..d1b514cf 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -1,3 +1,5 @@ +import type { FieldDefinition } from '../db/util/model-definition'; + /** * This type allows us to restrict assignments of other types. * @@ -21,3 +23,30 @@ export type Brand = T & { export const createBrandedValue = >(v: T): B => v as unknown as B; + +export const getTableColumns = < + T extends { _internals: { fields: FieldDefinition } }, +>( + table: T +): string[] => { + const { + generated, + generatedCompositeKey, + nonNullWithDefault, + required, + optional, + accidentallyOptional, + } = table._internals.fields; + + // Collect columns from all subsets + const columns = [ + ...Object.keys(generated ?? {}), + ...Object.keys(generatedCompositeKey ?? {}), + ...Object.keys(nonNullWithDefault ?? {}), + ...Object.keys(required ?? {}), + ...Object.keys(optional ?? {}), + ...Object.keys(accidentallyOptional ?? {}), + ]; + + return columns; +};