mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor: database repository (#16593)
* refactor: database repository * fix error reindex check * chore: remove WIP code --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com>
This commit is contained in:
parent
fe931faf17
commit
2cdbb0a37c
17 changed files with 197 additions and 315 deletions
|
|
@ -1,6 +1,4 @@
|
|||
import { Expression, RawBuilder, sql, ValueExpression } from 'kysely';
|
||||
import { InsertObject } from 'node_modules/kysely/dist/cjs';
|
||||
import { DB } from 'src/db';
|
||||
import { Expression, sql } from 'kysely';
|
||||
import { Between, LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
|
||||
|
||||
/**
|
||||
|
|
@ -17,27 +15,6 @@ export function OptionalBetween<T>(from?: T, to?: T) {
|
|||
}
|
||||
}
|
||||
|
||||
// populated by the database repository at bootstrap
|
||||
export const UPSERT_COLUMNS = {} as { [T in keyof DB]: { [K in keyof DB[T]]: RawBuilder<unknown> } };
|
||||
|
||||
/** Generates the columns for an upsert statement, excluding the conflict keys.
|
||||
* Assumes that all entries have the same keys. */
|
||||
export function mapUpsertColumns<T extends keyof DB>(
|
||||
table: T,
|
||||
entry: InsertObject<DB, T>,
|
||||
conflictKeys: readonly (keyof DB[T])[],
|
||||
) {
|
||||
const columns = UPSERT_COLUMNS[table] as { [K in keyof DB[T]]: RawBuilder<unknown> };
|
||||
const upsertColumns: Partial<Record<keyof typeof entry, RawBuilder<unknown>>> = {};
|
||||
for (const entryColumn in entry) {
|
||||
if (!conflictKeys.includes(entryColumn as keyof DB[T])) {
|
||||
upsertColumns[entryColumn as keyof typeof entry] = columns[entryColumn as keyof DB[T]];
|
||||
}
|
||||
}
|
||||
|
||||
return upsertColumns as Expand<Record<keyof typeof entry, ValueExpression<DB, T, any>>>;
|
||||
}
|
||||
|
||||
export const asUuid = (id: string | Expression<string>) => sql<string>`${id}::uuid`;
|
||||
|
||||
export const anyUuid = (ids: string[]) => sql<string>`any(${`{${ids}}`}::uuid[])`;
|
||||
|
|
@ -46,6 +23,16 @@ export const asVector = (embedding: number[]) => sql<string>`${`[${embedding}]`}
|
|||
|
||||
export const unnest = (array: string[]) => sql<Record<string, string>>`unnest(array[${sql.join(array)}]::text[])`;
|
||||
|
||||
export const removeUndefinedKeys = <T extends object>(update: T, template: unknown) => {
|
||||
for (const key in update) {
|
||||
if ((template as T)[key] === undefined) {
|
||||
delete update[key];
|
||||
}
|
||||
}
|
||||
|
||||
return update;
|
||||
};
|
||||
|
||||
/**
|
||||
* Mainly for type debugging to make VS Code display a more useful tooltip.
|
||||
* Source: https://stackoverflow.com/a/69288824
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue