better upsert query

the default `.upsert` method would clobber the `id`
This commit is contained in:
dakkar 2025-06-24 13:47:07 +01:00
parent 3aa5ec713c
commit 4651edbc4e

View file

@ -27,7 +27,9 @@ export class RegistryApiService {
public async set(userId: MiUser['id'], domain: string | null, scope: string[], key: string, value: any) { public async set(userId: MiUser['id'], domain: string | null, scope: string[], key: string, value: any) {
// TODO: 作成できるキーの数を制限する // TODO: 作成できるキーの数を制限する
await this.registryItemsRepository.upsert({ await this.registryItemsRepository.createQueryBuilder('item')
.insert()
.values({
id: this.idService.gen(), id: this.idService.gen(),
updatedAt: new Date(), updatedAt: new Date(),
userId: userId, userId: userId,
@ -35,10 +37,13 @@ export class RegistryApiService {
scope: scope, scope: scope,
key: key, key: key,
value: value, value: value,
}, { })
conflictPaths: ['userId', 'key', 'scope', 'domain'], .orUpdate(
upsertType: 'on-conflict-do-update', ['updatedAt', 'value'],
}); ['userId', 'key', 'scope', 'domain'],
{ upsertType: 'on-conflict-do-update' }
)
.execute();
if (domain == null) { if (domain == null) {
// TODO: サードパーティアプリが傍受出来てしまうのでどうにかする // TODO: サードパーティアプリが傍受出来てしまうのでどうにかする