2025-03-06 13:33:24 -05:00
|
|
|
import { Expression, sql } from 'kysely';
|
2025-01-09 11:15:41 -05:00
|
|
|
import { Between, LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
|
2023-12-08 11:15:46 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 11:15:41 -05:00
|
|
|
export const asUuid = (id: string | Expression<string>) => sql<string>`${id}::uuid`;
|
2024-02-17 11:00:55 -06:00
|
|
|
|
2025-01-09 11:15:41 -05:00
|
|
|
export const anyUuid = (ids: string[]) => sql<string>`any(${`{${ids}}`}::uuid[])`;
|
2024-02-12 20:50:47 -05:00
|
|
|
|
2025-01-21 19:12:28 +01:00
|
|
|
export const asVector = (embedding: number[]) => sql<string>`${`[${embedding}]`}::vector`;
|
2024-02-12 20:50:47 -05:00
|
|
|
|
2025-03-06 16:00:18 +01:00
|
|
|
export const unnest = (array: string[]) => sql<Record<string, string>>`unnest(array[${sql.join(array)}]::text[])`;
|
|
|
|
|
|
2025-03-06 13:33:24 -05:00
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-09 11:15:41 -05:00
|
|
|
/**
|
|
|
|
|
* 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;
|