mirror of
https://github.com/immich-app/immich.git
synced 2025-12-18 17:23:16 +03:00
* refactor: database repository * fix error reindex check * chore: remove WIP code --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com>
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { Expression, sql } from 'kysely';
|
|
import { Between, LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
|
|
|
|
/**
|
|
* Allows optional values unlike the regular Between and uses MoreThanOrEqual
|
|
* or LessThanOrEqual when only one parameter is specified.
|
|
*/
|
|
export function OptionalBetween<T>(from?: T, to?: T) {
|
|
if (from && to) {
|
|
return Between(from, to);
|
|
} else if (from) {
|
|
return MoreThanOrEqual(from);
|
|
} else if (to) {
|
|
return LessThanOrEqual(to);
|
|
}
|
|
}
|
|
|
|
export const asUuid = (id: string | Expression<string>) => sql<string>`${id}::uuid`;
|
|
|
|
export const anyUuid = (ids: string[]) => sql<string>`any(${`{${ids}}`}::uuid[])`;
|
|
|
|
export const asVector = (embedding: number[]) => sql<string>`${`[${embedding}]`}::vector`;
|
|
|
|
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
|
|
*/
|
|
export type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
|
|
|
|
/** Recursive version of {@link Expand} from the same source. */
|
|
export type ExpandRecursively<T> = T extends object
|
|
? T extends infer O
|
|
? { [K in keyof O]: ExpandRecursively<O[K]> }
|
|
: never
|
|
: T;
|