@tstdl/base 0.93.45 → 0.93.46

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.
package/orm/sqls.d.ts CHANGED
@@ -61,7 +61,15 @@ 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 exclusiveNotNull(columns: Column[]): SQL;
64
+ /**
65
+ *
66
+ * Generates a SQL condition that ensures exactly one of the specified columns is non-null and allows for custom conditions.
67
+ * @param enumeration The enumeration object
68
+ * @param discriminator The column used to discriminate between different enumeration values
69
+ * @param conditionMapping An object mapping enumeration values to columns and conditions
70
+ */
71
+ export declare function exclusiveReference<T extends EnumerationObject>(enumeration: T, discriminator: Column, conditionMapping: Record<EnumerationValue<T>, Column | [Column, condition: boolean | SQL]>): SQL;
72
+ export declare function exclusiveNotNull(...columns: Column[]): SQL;
65
73
  /**
66
74
  * Generates a SQL `CASE ... WHEN ... END` statement for dynamic condition mapping based on an enumeration.
67
75
  *
package/orm/sqls.js 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 { Column, eq, sql, isNotNull as sqlIsNotNull, Table } from 'drizzle-orm';
7
+ import { and, Column, eq, sql, isNotNull as sqlIsNotNull, Table } from 'drizzle-orm';
8
8
  import { match, P } from 'ts-pattern';
9
- import { objectEntries } from '../utils/object/object.js';
9
+ import { mapObjectValues, objectEntries, objectValues } from '../utils/object/object.js';
10
10
  import { assertDefined, isDefined, isInstanceOf, isNull, isNumber, isString } from '../utils/type-guards.js';
11
11
  import { getEnumName } from './enums.js';
12
12
  /** Drizzle SQL helper for getting the current transaction's timestamp. Returns a Date object. */
@@ -24,7 +24,24 @@ export function enumValue(enumeration, dbEnum, value) {
24
24
  const enumType = isString(dbEnum) ? sql `"${sql.raw(dbEnum)}"` : dbEnum;
25
25
  return sql `'${sql.raw(String(value))}'::${enumType}`;
26
26
  }
27
- export function exclusiveNotNull(columns) {
27
+ /**
28
+ *
29
+ * Generates a SQL condition that ensures exactly one of the specified columns is non-null and allows for custom conditions.
30
+ * @param enumeration The enumeration object
31
+ * @param discriminator The column used to discriminate between different enumeration values
32
+ * @param conditionMapping An object mapping enumeration values to columns and conditions
33
+ */
34
+ export function exclusiveReference(enumeration, discriminator, conditionMapping) {
35
+ const columns = objectValues(conditionMapping).map((value) => isInstanceOf(value, Column) ? value : value[0]);
36
+ const mapping = mapObjectValues(conditionMapping, (value) => {
37
+ if (isInstanceOf(value, Column)) {
38
+ return value;
39
+ }
40
+ return value[1];
41
+ });
42
+ return and(exclusiveNotNull(...columns), enumerationCaseWhen(enumeration, discriminator, mapping));
43
+ }
44
+ export function exclusiveNotNull(...columns) {
28
45
  return eq(numNonNulls(...columns), sql.raw('1'));
29
46
  }
30
47
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.93.45",
3
+ "version": "0.93.46",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"