@tstdl/base 0.93.44 → 0.93.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -97,6 +97,9 @@ let DocumentManagementAiService = DocumentManagementAiService_1 = class Document
97
97
  const env_1 = { stack: [], error: void 0, hasError: false };
98
98
  try {
99
99
  const document = await this.#documentRepository.loadByQuery({ tenantId, id: documentId });
100
+ if (isNotNull(document.typeId)) {
101
+ return document.typeId;
102
+ }
100
103
  const fileContentStream = this.#documentFileService.getContentStream(document);
101
104
  const tmpFile = __addDisposableResource(env_1, await TemporaryFile.from(fileContentStream), true);
102
105
  const filePart = await this.#aiService.processFile({ path: tmpFile.path, mimeType: document.mimeType });
package/orm/sqls.d.ts CHANGED
@@ -4,9 +4,9 @@
4
4
  * simplifying common SQL operations like generating UUIDs, working with intervals,
5
5
  * and aggregating data.
6
6
  */
7
- import type { EnumerationObject, EnumerationValue } from '../types/types.js';
8
- import { type AnyColumn, type Column, type SQL, type SQLChunk } from 'drizzle-orm';
7
+ import { Column, type AnyColumn, type SQL, type SQLChunk } from 'drizzle-orm';
9
8
  import type { GetSelectTableSelection, SelectResultField, TableLike } from 'drizzle-orm/query-builders/select.types';
9
+ import type { EnumerationObject, EnumerationValue } from '../types/types.js';
10
10
  import { type PgEnumFromEnumeration } from './enums.js';
11
11
  import type { TsVectorWeight } from './query/index.js';
12
12
  import type { Uuid } from './types.js';
@@ -61,7 +61,30 @@ export type TsHeadlineOptions = {
61
61
  fragmentDelimiter?: string;
62
62
  };
63
63
  export declare function enumValue<T extends EnumerationObject>(enumeration: T, dbEnum: PgEnumFromEnumeration<T> | string | null, value: EnumerationValue<T>): SQL<string>;
64
- export declare function singleReferenceCheck<T extends EnumerationObject>(enumeration: T, dbEnum: PgEnumFromEnumeration<T> | string | null, discriminator: Column, columns: Column[], columnMapping: Record<EnumerationValue<T>, Column | null>): SQL;
64
+ export declare function exclusiveNotNull(columns: Column[]): SQL;
65
+ /**
66
+ * Generates a SQL `CASE ... WHEN ... END` statement for dynamic condition mapping based on an enumeration.
67
+ *
68
+ * This function is particularly useful for scenarios where you need to apply different logical
69
+ * conditions based on the value of a "type" or "status" column (the discriminator). The resulting
70
+ * `CASE` statement returns a boolean value (`TRUE` or `FALSE`).
71
+ *
72
+ * @param enumeration - The enumeration object
73
+ *
74
+ * @param discriminator - The column whose value is being evaluated in the `CASE` statement (e.g., the `status` column).
75
+ *
76
+ * @param conditionMapping - An object that maps
77
+ * each enumeration key to the condition that should be applied when the discriminator matches that key's value.
78
+ * The value can be:
79
+ * - `boolean`: Directly results in `THEN TRUE` or `THEN FALSE`.
80
+ * - `Column`: A reference to another column. The condition applied to it is determined by `defaultColumnCondition`.
81
+ * - `SQL`: A raw SQL chunk for a custom, complex condition.
82
+ *
83
+ * @param defaultColumnCondition - An optional function
84
+ * that defines the default condition to apply when a `Column` is provided in `conditionMapping`.
85
+ * By default, it generates an `IS NOT NULL` check.
86
+ */
87
+ export declare function enumerationCaseWhen<T extends EnumerationObject>(enumeration: T, discriminator: Column, conditionMapping: Record<EnumerationValue<T>, Column | boolean | SQL>, defaultColumnCondition?: (column: Column) => SQL<unknown>): SQL;
65
88
  export declare function array<T>(values: SQL<T>[]): SQL<T[]>;
66
89
  export declare function array<T = unknown>(values: SQLChunk[]): SQL<T[]>;
67
90
  export declare function autoAlias<T>(column: AnyColumn<{
package/orm/sqls.js CHANGED
@@ -1,6 +1,13 @@
1
+ /**
2
+ * @module
3
+ * Provides utility SQL functions and constants for use with Drizzle ORM,
4
+ * simplifying common SQL operations like generating UUIDs, working with intervals,
5
+ * and aggregating data.
6
+ */
7
+ import { Column, eq, sql, isNotNull as sqlIsNotNull, Table } from 'drizzle-orm';
8
+ import { match, P } from 'ts-pattern';
1
9
  import { objectEntries } from '../utils/object/object.js';
2
- import { assertDefined, isDefined, isNotNull, isNull, isNumber, isString } from '../utils/type-guards.js';
3
- import { and, eq, sql, Table } from 'drizzle-orm';
10
+ import { assertDefined, isDefined, isInstanceOf, isNull, isNumber, isString } from '../utils/type-guards.js';
4
11
  import { getEnumName } from './enums.js';
5
12
  /** Drizzle SQL helper for getting the current transaction's timestamp. Returns a Date object. */
6
13
  export const TRANSACTION_TIMESTAMP = sql `transaction_timestamp()`;
@@ -17,18 +24,45 @@ export function enumValue(enumeration, dbEnum, value) {
17
24
  const enumType = isString(dbEnum) ? sql `"${sql.raw(dbEnum)}"` : dbEnum;
18
25
  return sql `'${sql.raw(String(value))}'::${enumType}`;
19
26
  }
20
- export function singleReferenceCheck(enumeration, dbEnum, discriminator, columns, columnMapping) {
27
+ export function exclusiveNotNull(columns) {
28
+ return eq(numNonNulls(...columns), sql.raw('1'));
29
+ }
30
+ /**
31
+ * Generates a SQL `CASE ... WHEN ... END` statement for dynamic condition mapping based on an enumeration.
32
+ *
33
+ * This function is particularly useful for scenarios where you need to apply different logical
34
+ * conditions based on the value of a "type" or "status" column (the discriminator). The resulting
35
+ * `CASE` statement returns a boolean value (`TRUE` or `FALSE`).
36
+ *
37
+ * @param enumeration - The enumeration object
38
+ *
39
+ * @param discriminator - The column whose value is being evaluated in the `CASE` statement (e.g., the `status` column).
40
+ *
41
+ * @param conditionMapping - An object that maps
42
+ * each enumeration key to the condition that should be applied when the discriminator matches that key's value.
43
+ * The value can be:
44
+ * - `boolean`: Directly results in `THEN TRUE` or `THEN FALSE`.
45
+ * - `Column`: A reference to another column. The condition applied to it is determined by `defaultColumnCondition`.
46
+ * - `SQL`: A raw SQL chunk for a custom, complex condition.
47
+ *
48
+ * @param defaultColumnCondition - An optional function
49
+ * that defines the default condition to apply when a `Column` is provided in `conditionMapping`.
50
+ * By default, it generates an `IS NOT NULL` check.
51
+ */
52
+ export function enumerationCaseWhen(enumeration, discriminator, conditionMapping, defaultColumnCondition = (column) => sqlIsNotNull(column)) {
21
53
  const whens = [];
22
- for (const [value, column] of objectEntries(columnMapping)) {
23
- if (isNotNull(column)) {
24
- whens.push(sql ` WHEN ${enumValue(enumeration, dbEnum, value)} THEN ${column} IS NOT NULL`);
25
- }
54
+ for (const [key, value] of objectEntries(conditionMapping)) {
55
+ const condition = match(value)
56
+ .with(P.boolean, (bool) => bool ? sql `TRUE` : sql `FALSE`)
57
+ .when((value) => isInstanceOf(value, Column), (col) => defaultColumnCondition(col))
58
+ .otherwise((rawSql) => rawSql);
59
+ whens.push(sql ` WHEN ${enumValue(enumeration, null, key)} THEN ${condition}`);
26
60
  }
27
- return and(sql `
61
+ return sql `
28
62
  CASE ${discriminator}
29
63
  ${sql.join(whens, sql `\n`)}
30
64
  ELSE FALSE
31
- END`, eq(numNonNulls(...columns), sql.raw('1')));
65
+ END`;
32
66
  }
33
67
  export function array(values) {
34
68
  const valueString = sql.join(values, sql.raw(', '));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.93.44",
3
+ "version": "0.93.45",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,4 +1,4 @@
1
- import type { AbstractConstructor, BinaryData, JsonPrimitive, PascalCase, Primitive, Type, TypedArray } from '../types/index.js';
1
+ import type { AbstractConstructor, AbstractType, BinaryData, JsonPrimitive, PascalCase, Primitive, Type, TypedArray } from '../types/index.js';
2
2
  export type AssertionMessage = string | (() => string);
3
3
  export type IsFunction<T> = <U extends T = T>(value: any) => value is U;
4
4
  export type IsNotFunction<T> = <V>(value: V) => value is Exclude<V, T>;
@@ -269,9 +269,9 @@ export declare const assertReadableStream: <T = any>(value: any, message?: Asser
269
269
  export declare const assertNotReadableStream: AssertNotFunction<ReadableStream>;
270
270
  export declare const assertReadableStreamPass: <T = any>(value: any, message?: AssertionMessage) => ReadableStream<T>;
271
271
  export declare const assertNotReadableStreamPass: AssertNotPassFunction<ReadableStream>;
272
- export declare const isInstanceOf: <T>(value: any, type: Type<T>) => value is T;
273
- export declare const isNotInstanceOf: <V, T>(value: V, type: Type<T>) => value is Exclude<V, T>;
274
- export declare const assertInstanceOf: <T>(value: any, type: Type<T>, message?: AssertionMessage) => asserts value is T;
275
- export declare const assertNotInstanceOf: <V, T>(value: V, type: Type<T>, message?: AssertionMessage) => asserts value is Exclude<V, T>;
276
- export declare const assertInstanceOfPass: <T>(value: any, type: Type<T>, message?: AssertionMessage) => T;
272
+ export declare const isInstanceOf: <T>(value: any, type: AbstractType<T>) => value is T;
273
+ export declare const isNotInstanceOf: <V, T>(value: V, type: AbstractType<T>) => value is Exclude<V, T>;
274
+ export declare const assertInstanceOf: <T>(value: any, type: AbstractType<T>, message?: AssertionMessage) => asserts value is T;
275
+ export declare const assertNotInstanceOf: <V, T>(value: V, type: AbstractType<T>, message?: AssertionMessage) => asserts value is Exclude<V, T>;
276
+ export declare const assertInstanceOfPass: <T>(value: any, type: AbstractType<T>, message?: AssertionMessage) => T;
277
277
  export declare const assertNotInstanceOfPass: <V, T>(value: V, type: Type<T>, message?: AssertionMessage) => Exclude<V, T>;