Skip to content

Commit

Permalink
Allow to specify flow ID when inserting new rows
Browse files Browse the repository at this point in the history
DB table `flow` is the only table we have where its
`id` column is part of a composite key, alongside
`versionID` column. Since we weren't allowing for
specifying IDs during new row insertion, it wasn't
possible to create a new version of already-existing
flow with the database models library.

We don't want to allow all tables to specify IDs
when inserting new flows, thus we introduce a new
field definition group called `generatedCompositeKey`,
which is among the least intrusive and least hacky
ways of achieving the desired outcome.
  • Loading branch information
Pl217 committed Sep 13, 2023
1 parent 9df1cdc commit d790915
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/db/models/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const FLOW_ID = brandedType<number, FlowId>(t.number);
export default defineIDModel({
tableName: 'flow',
fields: {
generated: {
generatedCompositeKey: {
id: { kind: 'branded-integer', brand: FLOW_ID },
},
required: {
Expand Down
3 changes: 2 additions & 1 deletion src/db/util/id-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export type { FieldsWithSequelize as FieldsWithId };
export const defineIDModel =
<
F extends FieldDefinition,
IDField extends string & keyof F['generated'],
IDField extends string &
(keyof F['generated'] | keyof F['generatedCompositeKey']),
SoftDeletionEnabled extends boolean
>(opts: {
tableName: string;
Expand Down
10 changes: 9 additions & 1 deletion src/db/util/model-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export type FieldDefinition = {
* such ids that use autoIncrement.
*/
generated?: FieldSet;
/**
* Same as `generated`, but indicates that auto-incremented ID is used as
* part of a composite primary key on the table, thus we need to make it
* possible for client code to specify these IDs when inserting new rows.
*/
generatedCompositeKey?: FieldSet;
nonNullWithDefault?: FieldSet;
optional?: FieldSet;
accidentallyOptional?: FieldSet;
Expand All @@ -68,14 +74,16 @@ export type FieldValuesOfSet<Set extends FieldSet | undefined> =
export type InstanceDataOf<F extends FieldDefinition> = FieldValuesOfSet<
F['generated']
> &
FieldValuesOfSet<F['generatedCompositeKey']> &
FieldValuesOfSet<F['nonNullWithDefault']> &
FieldValuesOfSet<F['required']> &
Nullable<FieldValuesOfSet<F['optional']>> &
Nullable<FieldValuesOfSet<F['accidentallyOptional']>>;

export type UserDataOf<F extends FieldDefinition> = Partial<
FieldValuesOfSet<F['nonNullWithDefault']>
FieldValuesOfSet<F['generatedCompositeKey']>
> &
Partial<FieldValuesOfSet<F['nonNullWithDefault']>> &
FieldValuesOfSet<F['required']> &
FieldValuesOfSet<F['accidentallyOptional']> &
Partial<Nullable<FieldValuesOfSet<F['optional']>>>;

0 comments on commit d790915

Please sign in to comment.