@prisma-next/adapter-postgres 0.4.0-dev.9 → 0.4.2

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.
Files changed (54) hide show
  1. package/dist/adapter-hNElNHo4.mjs +60 -0
  2. package/dist/adapter-hNElNHo4.mjs.map +1 -0
  3. package/dist/adapter.d.mts +2 -8
  4. package/dist/adapter.d.mts.map +1 -1
  5. package/dist/adapter.mjs +1 -1
  6. package/dist/column-types.d.mts.map +1 -1
  7. package/dist/column-types.mjs +1 -1
  8. package/dist/column-types.mjs.map +1 -1
  9. package/dist/control.d.mts +3 -70
  10. package/dist/control.d.mts.map +1 -1
  11. package/dist/control.mjs +19 -160
  12. package/dist/control.mjs.map +1 -1
  13. package/dist/{descriptor-meta-DemWrTfB.mjs → descriptor-meta-RTDzyrae.mjs} +33 -9
  14. package/dist/descriptor-meta-RTDzyrae.mjs.map +1 -0
  15. package/dist/operation-types.d.mts +21 -0
  16. package/dist/operation-types.d.mts.map +1 -0
  17. package/dist/operation-types.mjs +1 -0
  18. package/dist/runtime.d.mts +1 -1
  19. package/dist/runtime.mjs +8 -7
  20. package/dist/runtime.mjs.map +1 -1
  21. package/dist/{adapter-7pXt8ej9.mjs → sql-renderer-pEaSP82_.mjs} +102 -101
  22. package/dist/sql-renderer-pEaSP82_.mjs.map +1 -0
  23. package/dist/{types-DxaTd7aP.d.mts → types-CfRPdAk8.d.mts} +1 -1
  24. package/dist/{types-DxaTd7aP.d.mts.map → types-CfRPdAk8.d.mts.map} +1 -1
  25. package/dist/types.d.mts +1 -1
  26. package/package.json +22 -16
  27. package/src/core/adapter.ts +4 -626
  28. package/src/core/control-adapter.ts +21 -47
  29. package/src/core/descriptor-meta.ts +25 -5
  30. package/src/core/enum-control-hooks.ts +7 -2
  31. package/src/core/json-schema-validator.ts +2 -1
  32. package/src/core/sql-renderer.ts +710 -0
  33. package/src/exports/column-types.ts +1 -1
  34. package/src/exports/control.ts +9 -4
  35. package/src/exports/operation-types.ts +1 -0
  36. package/src/exports/runtime.ts +5 -4
  37. package/src/types/operation-types.ts +11 -0
  38. package/dist/adapter-7pXt8ej9.mjs.map +0 -1
  39. package/dist/codec-ids-BwjcIf74.mjs +0 -29
  40. package/dist/codec-ids-BwjcIf74.mjs.map +0 -1
  41. package/dist/codec-types.d.mts +0 -107
  42. package/dist/codec-types.d.mts.map +0 -1
  43. package/dist/codec-types.mjs +0 -3
  44. package/dist/codecs-C3wlpdV7.mjs +0 -385
  45. package/dist/codecs-C3wlpdV7.mjs.map +0 -1
  46. package/dist/descriptor-meta-DemWrTfB.mjs.map +0 -1
  47. package/dist/sql-utils-CSfAGEwF.mjs +0 -78
  48. package/dist/sql-utils-CSfAGEwF.mjs.map +0 -1
  49. package/src/core/codec-ids.ts +0 -30
  50. package/src/core/codecs.ts +0 -645
  51. package/src/core/default-normalizer.ts +0 -145
  52. package/src/core/json-schema-type-expression.ts +0 -131
  53. package/src/core/sql-utils.ts +0 -111
  54. package/src/exports/codec-types.ts +0 -44
@@ -1,145 +0,0 @@
1
- import type { ColumnDefault } from '@prisma-next/contract/types';
2
-
3
- /**
4
- * Pre-compiled regex patterns for performance.
5
- * These are compiled once at module load time rather than on each function call.
6
- */
7
- const NEXTVAL_PATTERN = /^nextval\s*\(/i;
8
- const NOW_FUNCTION_PATTERN = /^(now\s*\(\s*\)|CURRENT_TIMESTAMP)$/i;
9
- const CLOCK_TIMESTAMP_PATTERN = /^clock_timestamp\s*\(\s*\)$/i;
10
- const TIMESTAMP_CAST_SUFFIX = /::timestamp(?:tz|\s+(?:with|without)\s+time\s+zone)?$/i;
11
- const TEXT_CAST_SUFFIX = /::text$/i;
12
- const NOW_LITERAL_PATTERN = /^'now'$/i;
13
- const UUID_PATTERN = /^gen_random_uuid\s*\(\s*\)$/i;
14
- const UUID_OSSP_PATTERN = /^uuid_generate_v4\s*\(\s*\)$/i;
15
- const NULL_PATTERN = /^NULL(?:::.+)?$/i;
16
- const TRUE_PATTERN = /^true$/i;
17
- const FALSE_PATTERN = /^false$/i;
18
- const NUMERIC_PATTERN = /^-?\d+(\.\d+)?$/;
19
- const STRING_LITERAL_PATTERN = /^'((?:[^']|'')*)'(?:::(?:"[^"]+"|[\w\s]+)(?:\(\d+\))?)?$/;
20
-
21
- /**
22
- * Returns the canonical expression for a timestamp default function, or undefined
23
- * if the expression is not a recognized timestamp default.
24
- *
25
- * Keeps now()/CURRENT_TIMESTAMP and clock_timestamp() distinct:
26
- * - now(), CURRENT_TIMESTAMP, ('now'::text)::timestamp... → 'now()'
27
- * - clock_timestamp(), clock_timestamp()::timestamptz → 'clock_timestamp()'
28
- *
29
- * These are semantically different in Postgres: now() returns the transaction
30
- * start time (constant within a transaction), while clock_timestamp() returns
31
- * the actual wall-clock time (can differ across rows in a single INSERT).
32
- */
33
- function canonicalizeTimestampDefault(expr: string): string | undefined {
34
- if (NOW_FUNCTION_PATTERN.test(expr)) return 'now()';
35
- if (CLOCK_TIMESTAMP_PATTERN.test(expr)) return 'clock_timestamp()';
36
-
37
- if (!TIMESTAMP_CAST_SUFFIX.test(expr)) return undefined;
38
-
39
- let inner = expr.replace(TIMESTAMP_CAST_SUFFIX, '').trim();
40
-
41
- // Strip outer parentheses
42
- if (inner.startsWith('(') && inner.endsWith(')')) {
43
- inner = inner.slice(1, -1).trim();
44
- }
45
-
46
- if (NOW_FUNCTION_PATTERN.test(inner)) return 'now()';
47
- if (CLOCK_TIMESTAMP_PATTERN.test(inner)) return 'clock_timestamp()';
48
-
49
- // Handle 'now'::text form (Postgres casts the string literal 'now' through ::text)
50
- inner = inner.replace(TEXT_CAST_SUFFIX, '').trim();
51
- if (NOW_LITERAL_PATTERN.test(inner)) return 'now()';
52
-
53
- return undefined;
54
- }
55
-
56
- /**
57
- * Parses a raw Postgres column default expression into a normalized ColumnDefault.
58
- * This enables semantic comparison between contract defaults and introspected schema defaults.
59
- *
60
- * Used by the migration diff layer to normalize raw database defaults during comparison,
61
- * keeping the introspection layer focused on faithful data capture.
62
- *
63
- * @param rawDefault - Raw default expression from information_schema.columns.column_default
64
- * @param nativeType - Native column type, used for type-aware parsing (bigint tagging, JSON detection)
65
- * @returns Normalized ColumnDefault or undefined if the expression cannot be parsed
66
- */
67
- export function parsePostgresDefault(
68
- rawDefault: string,
69
- nativeType?: string,
70
- ): ColumnDefault | undefined {
71
- const trimmed = rawDefault.trim();
72
- const normalizedType = nativeType?.toLowerCase();
73
- const isBigInt = normalizedType === 'bigint' || normalizedType === 'int8';
74
-
75
- // Autoincrement: nextval('tablename_column_seq'::regclass)
76
- if (NEXTVAL_PATTERN.test(trimmed)) {
77
- return { kind: 'function', expression: 'autoincrement()' };
78
- }
79
-
80
- // Timestamp defaults: now()/CURRENT_TIMESTAMP → 'now()', clock_timestamp() → 'clock_timestamp()'
81
- const canonicalTimestamp = canonicalizeTimestampDefault(trimmed);
82
- if (canonicalTimestamp) {
83
- return { kind: 'function', expression: canonicalTimestamp };
84
- }
85
-
86
- // gen_random_uuid()
87
- if (UUID_PATTERN.test(trimmed)) {
88
- return { kind: 'function', expression: 'gen_random_uuid()' };
89
- }
90
-
91
- // uuid_generate_v4() from uuid-ossp extension
92
- if (UUID_OSSP_PATTERN.test(trimmed)) {
93
- return { kind: 'function', expression: 'gen_random_uuid()' };
94
- }
95
-
96
- // NULL or NULL::type — explicit null default
97
- if (NULL_PATTERN.test(trimmed)) {
98
- return { kind: 'literal', value: null };
99
- }
100
-
101
- // Boolean literals
102
- if (TRUE_PATTERN.test(trimmed)) {
103
- return { kind: 'literal', value: true };
104
- }
105
- if (FALSE_PATTERN.test(trimmed)) {
106
- return { kind: 'literal', value: false };
107
- }
108
-
109
- // Numeric literals (integer or decimal)
110
- if (NUMERIC_PATTERN.test(trimmed)) {
111
- const num = Number(trimmed);
112
- if (!Number.isFinite(num)) return undefined;
113
- if (isBigInt && !Number.isSafeInteger(num)) {
114
- return { kind: 'literal', value: trimmed };
115
- }
116
- return { kind: 'literal', value: num };
117
- }
118
-
119
- // String literals: 'value'::type or just 'value'
120
- // Match: 'some text'::text, 'hello'::character varying, 'value', etc.
121
- // Strip the ::type cast so the normalized expression matches what contract authors write.
122
- const stringMatch = trimmed.match(STRING_LITERAL_PATTERN);
123
- if (stringMatch?.[1] !== undefined) {
124
- const unescaped = stringMatch[1].replace(/''/g, "'");
125
- if (normalizedType === 'json' || normalizedType === 'jsonb') {
126
- try {
127
- return { kind: 'literal', value: JSON.parse(unescaped) };
128
- } catch {
129
- // Keep legacy behavior for malformed/non-JSON string content.
130
- }
131
- }
132
- if (isBigInt && NUMERIC_PATTERN.test(unescaped)) {
133
- const num = Number(unescaped);
134
- if (Number.isSafeInteger(num)) {
135
- return { kind: 'literal', value: num };
136
- }
137
- return { kind: 'literal', value: unescaped };
138
- }
139
- return { kind: 'literal', value: unescaped };
140
- }
141
-
142
- // Unrecognized expression - return as a function with the raw expression
143
- // This preserves the information for debugging while still being comparable
144
- return { kind: 'function', expression: trimmed };
145
- }
@@ -1,131 +0,0 @@
1
- type JsonSchemaRecord = Record<string, unknown>;
2
-
3
- const MAX_DEPTH = 32;
4
-
5
- function isRecord(value: unknown): value is JsonSchemaRecord {
6
- return typeof value === 'object' && value !== null;
7
- }
8
-
9
- function escapeStringLiteral(str: string): string {
10
- return str
11
- .replace(/\\/g, '\\\\')
12
- .replace(/'/g, "\\'")
13
- .replace(/\n/g, '\\n')
14
- .replace(/\r/g, '\\r');
15
- }
16
-
17
- function quotePropertyKey(key: string): string {
18
- return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : `'${escapeStringLiteral(key)}'`;
19
- }
20
-
21
- function renderLiteral(value: unknown): string {
22
- if (typeof value === 'string') {
23
- return `'${escapeStringLiteral(value)}'`;
24
- }
25
- if (typeof value === 'number' || typeof value === 'boolean') {
26
- return String(value);
27
- }
28
- if (value === null) {
29
- return 'null';
30
- }
31
- return 'unknown';
32
- }
33
-
34
- function renderUnion(items: readonly unknown[], depth: number): string {
35
- const rendered = items.map((item) => render(item, depth));
36
- return rendered.join(' | ');
37
- }
38
-
39
- function renderObjectType(schema: JsonSchemaRecord, depth: number): string {
40
- const properties = isRecord(schema['properties']) ? schema['properties'] : {};
41
- const required = Array.isArray(schema['required'])
42
- ? new Set(schema['required'].filter((key): key is string => typeof key === 'string'))
43
- : new Set<string>();
44
- const keys = Object.keys(properties).sort((left, right) => left.localeCompare(right));
45
-
46
- if (keys.length === 0) {
47
- const additionalProperties = schema['additionalProperties'];
48
- if (additionalProperties === true || additionalProperties === undefined) {
49
- return 'Record<string, unknown>';
50
- }
51
- return `Record<string, ${render(additionalProperties, depth)}>`;
52
- }
53
-
54
- const renderedProperties = keys.map((key) => {
55
- const valueSchema = (properties as JsonSchemaRecord)[key];
56
- const optionalMarker = required.has(key) ? '' : '?';
57
- return `${quotePropertyKey(key)}${optionalMarker}: ${render(valueSchema, depth)}`;
58
- });
59
-
60
- return `{ ${renderedProperties.join('; ')} }`;
61
- }
62
-
63
- function renderArrayType(schema: JsonSchemaRecord, depth: number): string {
64
- if (Array.isArray(schema['items'])) {
65
- return `readonly [${schema['items'].map((item) => render(item, depth)).join(', ')}]`;
66
- }
67
-
68
- if (schema['items'] !== undefined) {
69
- const itemType = render(schema['items'], depth);
70
- const needsParens = itemType.includes(' | ') || itemType.includes(' & ');
71
- return needsParens ? `(${itemType})[]` : `${itemType}[]`;
72
- }
73
-
74
- return 'unknown[]';
75
- }
76
-
77
- function render(schema: unknown, depth: number): string {
78
- if (depth > MAX_DEPTH || !isRecord(schema)) {
79
- return 'JsonValue';
80
- }
81
-
82
- const nextDepth = depth + 1;
83
-
84
- if ('const' in schema) {
85
- return renderLiteral(schema['const']);
86
- }
87
-
88
- if (Array.isArray(schema['enum'])) {
89
- return schema['enum'].map((value) => renderLiteral(value)).join(' | ');
90
- }
91
-
92
- if (Array.isArray(schema['oneOf'])) {
93
- return renderUnion(schema['oneOf'], nextDepth);
94
- }
95
-
96
- if (Array.isArray(schema['anyOf'])) {
97
- return renderUnion(schema['anyOf'], nextDepth);
98
- }
99
-
100
- if (Array.isArray(schema['allOf'])) {
101
- return schema['allOf'].map((item) => render(item, nextDepth)).join(' & ');
102
- }
103
-
104
- if (Array.isArray(schema['type'])) {
105
- return schema['type'].map((item) => render({ ...schema, type: item }, nextDepth)).join(' | ');
106
- }
107
-
108
- switch (schema['type']) {
109
- case 'string':
110
- return 'string';
111
- case 'number':
112
- case 'integer':
113
- return 'number';
114
- case 'boolean':
115
- return 'boolean';
116
- case 'null':
117
- return 'null';
118
- case 'array':
119
- return renderArrayType(schema, nextDepth);
120
- case 'object':
121
- return renderObjectType(schema, nextDepth);
122
- default:
123
- break;
124
- }
125
-
126
- return 'JsonValue';
127
- }
128
-
129
- export function renderTypeScriptTypeFromJsonSchema(schema: unknown): string {
130
- return render(schema, 0);
131
- }
@@ -1,111 +0,0 @@
1
- /**
2
- * Shared SQL utility functions for the Postgres adapter.
3
- *
4
- * These functions handle safe SQL identifier and literal escaping
5
- * with security validations to prevent injection and encoding issues.
6
- */
7
-
8
- /**
9
- * Error thrown when an invalid SQL identifier or literal is detected.
10
- * Boundary layers map this to structured envelopes.
11
- */
12
- export class SqlEscapeError extends Error {
13
- constructor(
14
- message: string,
15
- public readonly value: string,
16
- public readonly kind: 'identifier' | 'literal',
17
- ) {
18
- super(message);
19
- this.name = 'SqlEscapeError';
20
- }
21
- }
22
-
23
- /**
24
- * Maximum length for PostgreSQL identifiers (NAMEDATALEN - 1).
25
- */
26
- const MAX_IDENTIFIER_LENGTH = 63;
27
-
28
- /**
29
- * Validates and quotes a PostgreSQL identifier (table, column, type, schema names).
30
- *
31
- * Security validations:
32
- * - Rejects null bytes which could cause truncation or unexpected behavior
33
- * - Rejects empty identifiers
34
- * - Warns on identifiers exceeding PostgreSQL's 63-character limit
35
- *
36
- * @throws {SqlEscapeError} If the identifier contains null bytes or is empty
37
- */
38
- export function quoteIdentifier(identifier: string): string {
39
- if (identifier.length === 0) {
40
- throw new SqlEscapeError('Identifier cannot be empty', identifier, 'identifier');
41
- }
42
- if (identifier.includes('\0')) {
43
- throw new SqlEscapeError(
44
- 'Identifier cannot contain null bytes',
45
- identifier.replace(/\0/g, '\\0'),
46
- 'identifier',
47
- );
48
- }
49
- // PostgreSQL will truncate identifiers longer than 63 characters.
50
- // We don't throw here because it's not a security issue, but callers should be aware.
51
- if (identifier.length > MAX_IDENTIFIER_LENGTH) {
52
- // Log warning in development, but don't fail - PostgreSQL handles truncation
53
- console.warn(
54
- `Identifier "${identifier.slice(0, 20)}..." exceeds PostgreSQL's ${MAX_IDENTIFIER_LENGTH}-character limit and will be truncated`,
55
- );
56
- }
57
- return `"${identifier.replace(/"/g, '""')}"`;
58
- }
59
-
60
- /**
61
- * Escapes a string literal for safe use in SQL statements.
62
- *
63
- * Security validations:
64
- * - Rejects null bytes which could cause truncation or unexpected behavior
65
- *
66
- * Note: This assumes PostgreSQL's `standard_conforming_strings` is ON (default since PG 9.1).
67
- * Backslashes are treated as literal characters, not escape sequences.
68
- *
69
- * @throws {SqlEscapeError} If the value contains null bytes
70
- */
71
- export function escapeLiteral(value: string): string {
72
- if (value.includes('\0')) {
73
- throw new SqlEscapeError(
74
- 'Literal value cannot contain null bytes',
75
- value.replace(/\0/g, '\\0'),
76
- 'literal',
77
- );
78
- }
79
- return value.replace(/'/g, "''");
80
- }
81
-
82
- /**
83
- * Builds a qualified name (schema.object) with proper quoting.
84
- */
85
- export function qualifyName(schemaName: string, objectName: string): string {
86
- return `${quoteIdentifier(schemaName)}.${quoteIdentifier(objectName)}`;
87
- }
88
-
89
- /**
90
- * Validates that an enum value doesn't exceed PostgreSQL's label length limit.
91
- *
92
- * PostgreSQL enum labels have a maximum length of NAMEDATALEN-1 (63 bytes by default).
93
- * Unlike identifiers, enum labels that exceed this limit cause an error rather than
94
- * silent truncation.
95
- *
96
- * @param value - The enum value to validate
97
- * @param enumTypeName - Name of the enum type (for error messages)
98
- * @throws {SqlEscapeError} If the value exceeds the maximum length
99
- */
100
- export function validateEnumValueLength(value: string, enumTypeName: string): void {
101
- // PostgreSQL uses byte length, not character length. For simplicity, we use
102
- // character length as a conservative approximation (multi-byte chars would fail earlier).
103
- if (value.length > MAX_IDENTIFIER_LENGTH) {
104
- throw new SqlEscapeError(
105
- `Enum value "${value.slice(0, 20)}..." for type "${enumTypeName}" exceeds PostgreSQL's ` +
106
- `${MAX_IDENTIFIER_LENGTH}-character label limit`,
107
- value,
108
- 'literal',
109
- );
110
- }
111
- }
@@ -1,44 +0,0 @@
1
- /**
2
- * Codec type definitions for Postgres adapter.
3
- *
4
- * This file exports type-only definitions for codec input/output types.
5
- * These types are imported by contract.d.ts files for compile-time type inference.
6
- *
7
- * Runtime codec implementations are provided by the adapter's codec registry.
8
- */
9
-
10
- import type { JsonValue } from '@prisma-next/contract/types';
11
- import type { CodecTypes as CoreCodecTypes } from '../core/codecs';
12
-
13
- export type CodecTypes = CoreCodecTypes;
14
-
15
- export type { JsonValue };
16
- export { dataTypes } from '../core/codecs';
17
-
18
- type Branded<T, Shape extends Record<string, unknown>> = T & {
19
- readonly [K in keyof Shape]: Shape[K];
20
- };
21
-
22
- type BrandedString<Shape extends Record<string, unknown>> = Branded<string, Shape>;
23
-
24
- export type Char<N extends number> = BrandedString<{ __charLength: N }>;
25
- export type Varchar<N extends number> = BrandedString<{ __varcharLength: N }>;
26
- export type Numeric<P extends number, S extends number | undefined = undefined> = BrandedString<{
27
- __numericPrecision: P;
28
- __numericScale: S;
29
- }>;
30
- export type Bit<N extends number> = BrandedString<{ __bitLength: N }>;
31
- export type VarBit<N extends number> = BrandedString<{ __varbitLength: N }>;
32
- export type Timestamp<P extends number | undefined = undefined> = BrandedString<{
33
- __timestampPrecision: P;
34
- }>;
35
- export type Timestamptz<P extends number | undefined = undefined> = BrandedString<{
36
- __timestamptzPrecision: P;
37
- }>;
38
- export type Time<P extends number | undefined = undefined> = BrandedString<{ __timePrecision: P }>;
39
- export type Timetz<P extends number | undefined = undefined> = BrandedString<{
40
- __timetzPrecision: P;
41
- }>;
42
- export type Interval<P extends number | undefined = undefined> = BrandedString<{
43
- __intervalPrecision: P;
44
- }>;