From 85754fab92855d6dec22d91990353bcd941920f6 Mon Sep 17 00:00:00 2001 From: Pl217 Date: Wed, 13 Sep 2023 14:37:00 +0200 Subject: [PATCH 1/2] Allow to specify flow ID when inserting new rows 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. --- src/db/models/flow.ts | 2 +- src/db/util/id-model.ts | 3 ++- src/db/util/model-definition.ts | 10 +++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/db/models/flow.ts b/src/db/models/flow.ts index 74e35b6e..18b36cd1 100644 --- a/src/db/models/flow.ts +++ b/src/db/models/flow.ts @@ -12,7 +12,7 @@ export const FLOW_ID = brandedType(t.number); export default defineIDModel({ tableName: 'flow', fields: { - generated: { + generatedCompositeKey: { id: { kind: 'branded-integer', brand: FLOW_ID }, }, required: { diff --git a/src/db/util/id-model.ts b/src/db/util/id-model.ts index c21fa044..ea0af2e1 100644 --- a/src/db/util/id-model.ts +++ b/src/db/util/id-model.ts @@ -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; diff --git a/src/db/util/model-definition.ts b/src/db/util/model-definition.ts index 9da4ab1c..b4829d26 100644 --- a/src/db/util/model-definition.ts +++ b/src/db/util/model-definition.ts @@ -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; @@ -68,14 +74,16 @@ export type FieldValuesOfSet = export type InstanceDataOf = FieldValuesOfSet< F['generated'] > & + FieldValuesOfSet & FieldValuesOfSet & FieldValuesOfSet & Nullable> & Nullable>; export type UserDataOf = Partial< - FieldValuesOfSet + FieldValuesOfSet > & + Partial> & FieldValuesOfSet & FieldValuesOfSet & Partial>>; From b354f71467ecbc796c4e03623339e55970fdaca3 Mon Sep 17 00:00:00 2001 From: Pl217 Date: Fri, 22 Sep 2023 10:20:57 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=96=20Bump=20version=20to=20`v6.1.?= =?UTF-8?q?0`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index be01076e..a0daf565 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@unocha/hpc-api-core", - "version": "6.0.0", + "version": "6.1.0", "description": "Core libraries supporting HPC.Tools API Backend", "license": "Apache-2.0", "private": false,