Skip to content

High performance self-hosted photo and video management solution.

1.6 KB
Raw Blame
import { Injectable } from '@nestjs/common';
import { type Insertable, type Kysely, sql } from 'kysely';
import { InjectKysely } from 'nestjs-kysely';
import { DummyValue, GenerateSql } from 'src/decorators.js';
import { SyncEntityType } from 'src/enum.js';
import { DB } from 'src/schema/index.js';
import { SessionSyncCheckpointTable } from 'src/schema/tables/sync-checkpoint.table.js';

@Injectable()
export class SyncCheckpointRepository {
  constructor(@InjectKysely() private db: Kysely<DB>) {}

  @GenerateSql({ params: [DummyValue.UUID] })
  getAll(sessionId: string) {
    return this.db
      .selectFrom('session_sync_checkpoint')
      .select(['type', 'ack'])
      .where('sessionId', '=', sessionId)
      .execute();
  }

  upsertAll(items: Insertable<SessionSyncCheckpointTable>[]) {
    return this.db
      .insertInto('session_sync_checkpoint')
      .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_checkpoint')
      .where('sessionId', '=', sessionId)
      .$if(!!types, (qb) => qb.where('type', 'in', types!))
      .execute();
  }

  @GenerateSql()
  getNow() {
    return this.db
      .selectNoFrom((eb) => [
        eb.fn<string>('immich_uuid_v7', [sql.raw<Date>("now() - interval '1 millisecond'")]).as('nowId'),
      ])
      .executeTakeFirstOrThrow();
  }
}