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,18 +27,23 @@ 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')
id: this.idService.gen(), .insert()
updatedAt: new Date(), .values({
userId: userId, id: this.idService.gen(),
domain: domain, updatedAt: new Date(),
scope: scope, userId: userId,
key: key, domain: domain,
value: value, scope: scope,
}, { key: key,
conflictPaths: ['userId', 'key', 'scope', 'domain'], value: value,
upsertType: 'on-conflict-do-update', })
}); .orUpdate(
['updatedAt', 'value'],
['userId', 'key', 'scope', 'domain'],
{ upsertType: 'on-conflict-do-update' }
)
.execute();
if (domain == null) { if (domain == null) {
// TODO: サードパーティアプリが傍受出来てしまうのでどうにかする // TODO: サードパーティアプリが傍受出来てしまうのでどうにかする