@tstdl/base 0.93.40 → 0.93.42
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/enums.d.ts
ADDED
package/orm/enums.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { tryGetEnumName } from '../enumeration/index.js';
|
|
2
|
+
import { toSnakeCase } from '../utils/string/casing.js';
|
|
3
|
+
import { isUndefined } from '../utils/type-guards.js';
|
|
4
|
+
const enumNames = new Map();
|
|
5
|
+
export function registerEnum(enumeration, name) {
|
|
6
|
+
enumNames.set(enumeration, toSnakeCase(name));
|
|
7
|
+
}
|
|
8
|
+
export function getEnumName(enumeration) {
|
|
9
|
+
const baseName = enumNames.get(enumeration) ?? tryGetEnumName(enumeration);
|
|
10
|
+
if (isUndefined(baseName)) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
return toSnakeCase(baseName);
|
|
14
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isDefined } from '../../utils/type-guards.js';
|
|
2
|
-
import {
|
|
2
|
+
import { registerEnum } from '../enums.js';
|
|
3
|
+
import { getDrizzleTableFromType, getPgEnum } from './drizzle/schema-converter.js';
|
|
3
4
|
/**
|
|
4
5
|
* Represents a database schema, providing methods to access tables and enums within that schema.
|
|
5
6
|
* @template SchemaName The name of the PostgreSQL schema.
|
|
@@ -10,7 +10,5 @@ export declare const getDrizzleTableFromType: typeof _getDrizzleTableFromType;
|
|
|
10
10
|
export declare function getColumnDefinitions(table: PgTableWithColumns<any>): ColumnDefinition[];
|
|
11
11
|
export declare function getColumnDefinitionsMap(table: PgTableWithColumns<any>): ColumnDefinitionsMap;
|
|
12
12
|
export declare function _getDrizzleTableFromType<T extends EntityType, S extends string>(type: T, fallbackSchemaName?: S): PgTableFromType<T, S>;
|
|
13
|
-
export declare function registerEnum(enumeration: Enumeration, name: string): void;
|
|
14
|
-
export declare function getEnumName(enumeration: Enumeration): string | undefined;
|
|
15
13
|
export declare function getPgEnum(schema: string | PgSchema, enumeration: Enumeration, context?: ConverterContext): PgEnum<[string, ...string[]]>;
|
|
16
14
|
export {};
|
|
@@ -3,7 +3,6 @@ import { toCamelCase, toSnakeCase } from 'drizzle-orm/casing';
|
|
|
3
3
|
import { boolean, check, doublePrecision, foreignKey, index, integer, jsonb, numeric, pgSchema, primaryKey, text, unique, uniqueIndex, uuid } from 'drizzle-orm/pg-core';
|
|
4
4
|
import { match, P } from 'ts-pattern';
|
|
5
5
|
import { MultiKeyMap } from '../../../data-structures/multi-key-map.js';
|
|
6
|
-
import { tryGetEnumName } from '../../../enumeration/enumeration.js';
|
|
7
6
|
import { NotSupportedError } from '../../../errors/not-supported.error.js';
|
|
8
7
|
import { JsonPath } from '../../../json-path/json-path.js';
|
|
9
8
|
import { reflectionRegistry } from '../../../reflection/registry.js';
|
|
@@ -17,6 +16,7 @@ import { fromEntries, mapObjectKeysToSnakeCase, objectEntries } from '../../../u
|
|
|
17
16
|
import { assertDefined, assertDefinedPass, isArray, isDefined, isNotNull, isNotNullOrUndefined, isNull, isString, isUndefined } from '../../../utils/type-guards.js';
|
|
18
17
|
import { resolveValueOrProvider } from '../../../utils/value-or-provider.js';
|
|
19
18
|
import { bytea, numericDate, timestamp, tsvector } from '../../data-types/index.js';
|
|
19
|
+
import { getEnumName } from '../../enums.js';
|
|
20
20
|
import { JsonSchema, NumericDateSchema, NumericSchema, TimestampSchema, TsVectorSchema, UuidSchema } from '../../schemas/index.js';
|
|
21
21
|
import { decryptBytes, encryptBytes } from '../encryption.js';
|
|
22
22
|
import { convertQuery, resolveTargetColumn, resolveTargetColumns } from '../query-converter.js';
|
|
@@ -323,18 +323,7 @@ function getPostgresBaseColumn(columnName, dbSchema, schema, reflectionData, con
|
|
|
323
323
|
throw new NotSupportedError(`Schema "${schema.constructor.name}" not supported on type "${context.type.name}" for property "${context.property}"`);
|
|
324
324
|
});
|
|
325
325
|
}
|
|
326
|
-
const enumNames = new Map();
|
|
327
326
|
const enums = new MultiKeyMap();
|
|
328
|
-
export function registerEnum(enumeration, name) {
|
|
329
|
-
enumNames.set(enumeration, toSnakeCase(name));
|
|
330
|
-
}
|
|
331
|
-
export function getEnumName(enumeration) {
|
|
332
|
-
const baseName = enumNames.get(enumeration) ?? tryGetEnumName(enumeration);
|
|
333
|
-
if (isUndefined(baseName)) {
|
|
334
|
-
return undefined;
|
|
335
|
-
}
|
|
336
|
-
return toSnakeCase(baseName);
|
|
337
|
-
}
|
|
338
327
|
export function getPgEnum(schema, enumeration, context) {
|
|
339
328
|
const dbSchema = isString(schema) ? getDbSchema(schema) : schema;
|
|
340
329
|
const enumName = getEnumName(enumeration);
|
package/orm/sqls.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
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 type { EnumerationObject, EnumerationValue } from '../types/types.js';
|
|
1
8
|
import { type AnyColumn, type Column, type SQL, type SQLChunk } from 'drizzle-orm';
|
|
2
9
|
import type { GetSelectTableSelection, SelectResultField, TableLike } from 'drizzle-orm/query-builders/select.types';
|
|
3
10
|
import type { TsVectorWeight } from './query/index.js';
|
|
@@ -52,6 +59,8 @@ export type TsHeadlineOptions = {
|
|
|
52
59
|
*/
|
|
53
60
|
fragmentDelimiter?: string;
|
|
54
61
|
};
|
|
62
|
+
export declare function enumValue<T extends EnumerationObject>(enumeration: T, value: EnumerationValue<T>): SQL<string>;
|
|
63
|
+
export declare function singleReferenceCheck<E extends EnumerationObject>(enumeration: E, discriminator: Column, columns: Column[], columnMapping: Record<EnumerationValue<E>, Column | null>): SQL;
|
|
55
64
|
export declare function array<T>(values: SQL<T>[]): SQL<T[]>;
|
|
56
65
|
export declare function array<T = unknown>(values: SQLChunk[]): SQL<T[]>;
|
|
57
66
|
export declare function autoAlias<T>(column: AnyColumn<{
|
package/orm/sqls.js
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* and aggregating data.
|
|
6
|
-
*/
|
|
7
|
-
import { isDefined, isNumber, isString } from '../utils/type-guards.js';
|
|
8
|
-
import { sql, Table } from 'drizzle-orm';
|
|
1
|
+
import { objectEntries } from '../utils/object/object.js';
|
|
2
|
+
import { assertDefined, isDefined, isNotNull, isNumber, isString } from '../utils/type-guards.js';
|
|
3
|
+
import { and, eq, sql, Table } from 'drizzle-orm';
|
|
4
|
+
import { getEnumName } from './enums.js';
|
|
9
5
|
/** Drizzle SQL helper for getting the current transaction's timestamp. Returns a Date object. */
|
|
10
6
|
export const TRANSACTION_TIMESTAMP = sql `transaction_timestamp()`;
|
|
11
7
|
/** Drizzle SQL helper for generating a random UUID (v4). Returns a Uuid string. */
|
|
12
8
|
export const RANDOM_UUID_V4 = sql `gen_random_uuid()`;
|
|
13
9
|
/** Drizzle SQL helper for generating a random UUID (v7). Returns a Uuid string. */
|
|
14
10
|
export const RANDOM_UUID_V7 = sql `uuidv7()`;
|
|
11
|
+
export function enumValue(enumeration, value) {
|
|
12
|
+
const enumName = getEnumName(enumeration);
|
|
13
|
+
assertDefined(enumName, 'Enumeration is not registered.');
|
|
14
|
+
return sql `'${sql.raw(String(value))}'::${sql.raw(enumName)}`;
|
|
15
|
+
}
|
|
16
|
+
export function singleReferenceCheck(enumeration, discriminator, columns, columnMapping) {
|
|
17
|
+
const whens = [];
|
|
18
|
+
for (const [value, column] of objectEntries(columnMapping)) {
|
|
19
|
+
if (isNotNull(column)) {
|
|
20
|
+
whens.push(sql ` WHEN ${enumValue(enumeration, value)} THEN ${column} IS NOT NULL`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return and(sql `CASE ${discriminator}
|
|
24
|
+
${sql.join(whens, sql `\n`)}
|
|
25
|
+
ELSE FALSE
|
|
26
|
+
END`, eq(numNonNulls(...columns), sql.raw('1')));
|
|
27
|
+
}
|
|
15
28
|
export function array(values) {
|
|
16
29
|
const valueString = sql.join(values, sql.raw(', '));
|
|
17
30
|
return sql `ARRAY[${valueString}]`;
|