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