@tstdl/base 0.93.39 → 0.93.41
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,6 +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
13
|
export declare function getPgEnum(schema: string | PgSchema, enumeration: Enumeration, context?: ConverterContext): PgEnum<[string, ...string[]]>;
|
|
15
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,14 +323,10 @@ 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
327
|
export function getPgEnum(schema, enumeration, context) {
|
|
332
328
|
const dbSchema = isString(schema) ? getDbSchema(schema) : schema;
|
|
333
|
-
const enumName =
|
|
329
|
+
const enumName = getEnumName(enumeration);
|
|
334
330
|
if (isUndefined(enumName)) {
|
|
335
331
|
const errorMessage = 'Enum is not registered. Please register it using `databaseSchema.getEnum(MyEnum)` before use.';
|
|
336
332
|
if (isDefined(context)) {
|
|
@@ -340,7 +336,7 @@ export function getPgEnum(schema, enumeration, context) {
|
|
|
340
336
|
}
|
|
341
337
|
const values = (isArray(enumeration) ? enumeration : enumValues(enumeration))
|
|
342
338
|
.map((value) => value.toString());
|
|
343
|
-
const dbEnum = dbSchema.enum(
|
|
339
|
+
const dbEnum = dbSchema.enum(enumName, values);
|
|
344
340
|
if (enums.has([dbSchema.schemaName, enumeration])) {
|
|
345
341
|
enums.set([dbSchema.schemaName, enumeration], dbEnum);
|
|
346
342
|
}
|
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>): 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,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* simplifying common SQL operations like generating UUIDs, working with intervals,
|
|
5
|
-
* and aggregating data.
|
|
6
|
-
*/
|
|
7
|
-
import { isDefined, isNumber, isString } from '../utils/type-guards.js';
|
|
8
|
-
import { sql, Table } from 'drizzle-orm';
|
|
1
|
+
import { assertDefined, isDefined, isNumber, isString } from '../utils/type-guards.js';
|
|
2
|
+
import { and, eq, sql, Table } from 'drizzle-orm';
|
|
3
|
+
import { getEnumName } from './enums.js';
|
|
9
4
|
/** Drizzle SQL helper for getting the current transaction's timestamp. Returns a Date object. */
|
|
10
5
|
export const TRANSACTION_TIMESTAMP = sql `transaction_timestamp()`;
|
|
11
6
|
/** Drizzle SQL helper for generating a random UUID (v4). Returns a Uuid string. */
|
|
12
7
|
export const RANDOM_UUID_V4 = sql `gen_random_uuid()`;
|
|
13
8
|
/** Drizzle SQL helper for generating a random UUID (v7). Returns a Uuid string. */
|
|
14
9
|
export const RANDOM_UUID_V7 = sql `uuidv7()`;
|
|
10
|
+
export function enumValue(enumeration, value) {
|
|
11
|
+
const enumName = getEnumName(enumeration);
|
|
12
|
+
assertDefined(enumName, 'Enumeration is not registered.');
|
|
13
|
+
return sql `'${sql.raw(String(value))}'::${sql.raw(enumName)}`;
|
|
14
|
+
}
|
|
15
|
+
export function singleReferenceCheck(enumeration, discriminator, columns, columnMapping) {
|
|
16
|
+
const whens = [];
|
|
17
|
+
for (const [value, column] of Object.entries(columnMapping)) {
|
|
18
|
+
whens.push(sql ` WHEN ${enumValue(enumeration, value)} THEN ${column} IS NOT NULL`);
|
|
19
|
+
}
|
|
20
|
+
return and(sql `
|
|
21
|
+
CASE ${discriminator}
|
|
22
|
+
${sql.join(whens, sql `\n`)}
|
|
23
|
+
ELSE FALSE
|
|
24
|
+
END
|
|
25
|
+
`, eq(numNonNulls(...columns), sql.raw('1')));
|
|
26
|
+
}
|
|
15
27
|
export function array(values) {
|
|
16
28
|
const valueString = sql.join(values, sql.raw(', '));
|
|
17
29
|
return sql `ARRAY[${valueString}]`;
|