refactor: sync repository (#19581)
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Insertable, Kysely } from 'kysely';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { DB, SessionSyncCheckpoints } from 'src/db';
|
||||
import { DummyValue, GenerateSql } from 'src/decorators';
|
||||
import { SyncEntityType } from 'src/enum';
|
||||
|
||||
@Injectable()
|
||||
export class SyncCheckpointRepository {
|
||||
constructor(@InjectKysely() private db: Kysely<DB>) {}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.UUID] })
|
||||
getAll(sessionId: string) {
|
||||
return this.db
|
||||
.selectFrom('session_sync_checkpoints')
|
||||
.select(['type', 'ack'])
|
||||
.where('sessionId', '=', sessionId)
|
||||
.execute();
|
||||
}
|
||||
|
||||
upsertAll(items: Insertable<SessionSyncCheckpoints>[]) {
|
||||
return this.db
|
||||
.insertInto('session_sync_checkpoints')
|
||||
.values(items)
|
||||
.onConflict((oc) =>
|
||||
oc.columns(['sessionId', 'type']).doUpdateSet((eb) => ({
|
||||
ack: eb.ref('excluded.ack'),
|
||||
})),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.UUID] })
|
||||
deleteAll(sessionId: string, types?: SyncEntityType[]) {
|
||||
return this.db
|
||||
.deleteFrom('session_sync_checkpoints')
|
||||
.where('sessionId', '=', sessionId)
|
||||
.$if(!!types, (qb) => qb.where('type', 'in', types!))
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user