mirror of
https://github.com/immich-app/immich.git
synced 2026-03-07 10:37:22 +03:00
refactor(server): use kysely (#12857)
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
import AsyncLock from 'async-lock';
|
||||
import { sql } from 'kysely';
|
||||
import semver from 'semver';
|
||||
import { POSTGRES_VERSION_RANGE, VECTOR_VERSION_RANGE, VECTORS_VERSION_RANGE } from 'src/constants';
|
||||
import { DB } from 'src/db';
|
||||
import { IConfigRepository } from 'src/interfaces/config.interface';
|
||||
import {
|
||||
DatabaseExtension,
|
||||
@@ -15,13 +17,14 @@ import {
|
||||
VectorUpdateResult,
|
||||
} from 'src/interfaces/database.interface';
|
||||
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
||||
import { UPSERT_COLUMNS } from 'src/utils/database';
|
||||
import { isValidInteger } from 'src/validation';
|
||||
import { DataSource, EntityManager, QueryRunner } from 'typeorm';
|
||||
import { DataSource, EntityManager, EntityMetadata, QueryRunner } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class DatabaseRepository implements IDatabaseRepository {
|
||||
private vectorExtension: VectorExtension;
|
||||
readonly asyncLock = new AsyncLock();
|
||||
private readonly asyncLock = new AsyncLock();
|
||||
|
||||
constructor(
|
||||
@InjectDataSource() private dataSource: DataSource,
|
||||
@@ -32,6 +35,13 @@ export class DatabaseRepository implements IDatabaseRepository {
|
||||
this.logger.setContext(DatabaseRepository.name);
|
||||
}
|
||||
|
||||
init() {
|
||||
for (const metadata of this.dataSource.entityMetadatas) {
|
||||
const table = metadata.tableName as keyof DB;
|
||||
UPSERT_COLUMNS[table] = this.getUpsertColumns(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
async reconnect() {
|
||||
try {
|
||||
if (this.dataSource.isInitialized) {
|
||||
@@ -249,4 +259,10 @@ export class DatabaseRepository implements IDatabaseRepository {
|
||||
private async releaseLock(lock: DatabaseLock, queryRunner: QueryRunner): Promise<void> {
|
||||
return queryRunner.query('SELECT pg_advisory_unlock($1)', [lock]);
|
||||
}
|
||||
|
||||
private getUpsertColumns(metadata: EntityMetadata) {
|
||||
return Object.fromEntries(
|
||||
metadata.ownColumns.map((column) => [column.propertyName, sql<string>`excluded.${sql.ref(column.propertyName)}`]),
|
||||
) as any;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user