Skip to content

Commit

Permalink
Add function to obtain table columns
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Nov 20, 2024
1 parent dc5888a commit f68d7a1
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/util/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { FieldDefinition } from '../db/util/model-definition';

/**
* This type allows us to restrict assignments of other types.
*
Expand All @@ -21,3 +23,30 @@ export type Brand<T, S extends { s: symbol }, Label extends string = ''> = T & {

export const createBrandedValue = <T, B extends Brand<T, any, any>>(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;
};

0 comments on commit f68d7a1

Please sign in to comment.