From 4651edbc4e226b9d724df25bc38e4ef0b8ccd38a Mon Sep 17 00:00:00 2001 From: dakkar Date: Tue, 24 Jun 2025 13:47:07 +0100 Subject: [PATCH] better upsert query the default `.upsert` method would clobber the `id` --- .../backend/src/core/RegistryApiService.ts | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/core/RegistryApiService.ts b/packages/backend/src/core/RegistryApiService.ts index c59b79a49a..2c7ad4026d 100644 --- a/packages/backend/src/core/RegistryApiService.ts +++ b/packages/backend/src/core/RegistryApiService.ts @@ -27,18 +27,23 @@ export class RegistryApiService { public async set(userId: MiUser['id'], domain: string | null, scope: string[], key: string, value: any) { // TODO: 作成できるキーの数を制限する - await this.registryItemsRepository.upsert({ - id: this.idService.gen(), - updatedAt: new Date(), - userId: userId, - domain: domain, - scope: scope, - key: key, - value: value, - }, { - conflictPaths: ['userId', 'key', 'scope', 'domain'], - upsertType: 'on-conflict-do-update', - }); + await this.registryItemsRepository.createQueryBuilder('item') + .insert() + .values({ + id: this.idService.gen(), + updatedAt: new Date(), + userId: userId, + domain: domain, + scope: scope, + key: key, + value: value, + }) + .orUpdate( + ['updatedAt', 'value'], + ['userId', 'key', 'scope', 'domain'], + { upsertType: 'on-conflict-do-update' } + ) + .execute(); if (domain == null) { // TODO: サードパーティアプリが傍受出来てしまうのでどうにかする