@prisma-next/adapter-postgres 0.3.0-dev.10 → 0.3.0-dev.113
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/LICENSE +201 -0
- package/README.md +64 -2
- package/dist/adapter-CWmWEFe1.mjs +361 -0
- package/dist/adapter-CWmWEFe1.mjs.map +1 -0
- package/dist/adapter.d.mts +23 -0
- package/dist/adapter.d.mts.map +1 -0
- package/dist/adapter.mjs +3 -0
- package/dist/codec-ids-Bsm9c7ns.mjs +29 -0
- package/dist/codec-ids-Bsm9c7ns.mjs.map +1 -0
- package/dist/codec-types.d.mts +141 -0
- package/dist/codec-types.d.mts.map +1 -0
- package/dist/codec-types.mjs +3 -0
- package/dist/codecs-DgJcyEBR.mjs +254 -0
- package/dist/codecs-DgJcyEBR.mjs.map +1 -0
- package/dist/column-types.d.mts +110 -0
- package/dist/column-types.d.mts.map +1 -0
- package/dist/column-types.mjs +180 -0
- package/dist/column-types.mjs.map +1 -0
- package/dist/control.d.mts +77 -0
- package/dist/control.d.mts.map +1 -0
- package/dist/control.mjs +773 -0
- package/dist/control.mjs.map +1 -0
- package/dist/descriptor-meta-l_dv8Nnn.mjs +884 -0
- package/dist/descriptor-meta-l_dv8Nnn.mjs.map +1 -0
- package/dist/runtime.d.mts +19 -0
- package/dist/runtime.d.mts.map +1 -0
- package/dist/runtime.mjs +99 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/sql-utils-CSfAGEwF.mjs +78 -0
- package/dist/sql-utils-CSfAGEwF.mjs.map +1 -0
- package/dist/types-aQLL6QVb.d.mts +19 -0
- package/dist/types-aQLL6QVb.d.mts.map +1 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +1 -0
- package/package.json +39 -46
- package/src/core/adapter.ts +529 -256
- package/src/core/codec-ids.ts +28 -0
- package/src/core/codecs.ts +385 -36
- package/src/core/control-adapter.ts +404 -179
- package/src/core/control-mutation-defaults.ts +335 -0
- package/src/core/default-normalizer.ts +138 -0
- package/src/core/descriptor-meta.ts +296 -9
- package/src/core/enum-control-hooks.ts +733 -0
- package/src/core/json-schema-type-expression.ts +131 -0
- package/src/core/json-schema-validator.ts +53 -0
- package/src/core/sql-utils.ts +111 -0
- package/src/core/standard-schema.ts +71 -0
- package/src/core/types.ts +5 -3
- package/src/exports/codec-types.ts +73 -1
- package/src/exports/column-types.ts +233 -9
- package/src/exports/control.ts +20 -9
- package/src/exports/runtime.ts +76 -19
- package/dist/chunk-HD5YISNQ.js +0 -47
- package/dist/chunk-HD5YISNQ.js.map +0 -1
- package/dist/chunk-J3XSOAM2.js +0 -162
- package/dist/chunk-J3XSOAM2.js.map +0 -1
- package/dist/chunk-T6S3A6VT.js +0 -301
- package/dist/chunk-T6S3A6VT.js.map +0 -1
- package/dist/core/adapter.d.ts +0 -19
- package/dist/core/adapter.d.ts.map +0 -1
- package/dist/core/codecs.d.ts +0 -110
- package/dist/core/codecs.d.ts.map +0 -1
- package/dist/core/control-adapter.d.ts +0 -33
- package/dist/core/control-adapter.d.ts.map +0 -1
- package/dist/core/descriptor-meta.d.ts +0 -72
- package/dist/core/descriptor-meta.d.ts.map +0 -1
- package/dist/core/types.d.ts +0 -16
- package/dist/core/types.d.ts.map +0 -1
- package/dist/exports/adapter.d.ts +0 -2
- package/dist/exports/adapter.d.ts.map +0 -1
- package/dist/exports/adapter.js +0 -8
- package/dist/exports/adapter.js.map +0 -1
- package/dist/exports/codec-types.d.ts +0 -11
- package/dist/exports/codec-types.d.ts.map +0 -1
- package/dist/exports/codec-types.js +0 -7
- package/dist/exports/codec-types.js.map +0 -1
- package/dist/exports/column-types.d.ts +0 -17
- package/dist/exports/column-types.d.ts.map +0 -1
- package/dist/exports/column-types.js +0 -49
- package/dist/exports/column-types.js.map +0 -1
- package/dist/exports/control.d.ts +0 -8
- package/dist/exports/control.d.ts.map +0 -1
- package/dist/exports/control.js +0 -279
- package/dist/exports/control.js.map +0 -1
- package/dist/exports/runtime.d.ts +0 -15
- package/dist/exports/runtime.d.ts.map +0 -1
- package/dist/exports/runtime.js +0 -20
- package/dist/exports/runtime.js.map +0 -1
- package/dist/exports/types.d.ts +0 -2
- package/dist/exports/types.d.ts.map +0 -1
- package/dist/exports/types.js +0 -1
- package/dist/exports/types.js.map +0 -1
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import type { ExecutionMutationDefaultValue } from '@prisma-next/contract/types';
|
|
2
|
+
import type {
|
|
3
|
+
ControlMutationDefaultFunctionEntry,
|
|
4
|
+
ControlMutationDefaultGeneratorDescriptor,
|
|
5
|
+
PslScalarTypeDescriptor,
|
|
6
|
+
} from '@prisma-next/family-sql/control';
|
|
7
|
+
import {
|
|
8
|
+
builtinGeneratorRegistryMetadata,
|
|
9
|
+
resolveBuiltinGeneratedColumnDescriptor,
|
|
10
|
+
} from '@prisma-next/ids';
|
|
11
|
+
import type {
|
|
12
|
+
DefaultFunctionLoweringContext,
|
|
13
|
+
DefaultFunctionLoweringHandler,
|
|
14
|
+
} from '@prisma-next/sql-contract-psl';
|
|
15
|
+
|
|
16
|
+
type LoweredDefaultResult = ReturnType<DefaultFunctionLoweringHandler>;
|
|
17
|
+
type ParsedDefaultFunctionCall = Parameters<DefaultFunctionLoweringHandler>[0]['call'];
|
|
18
|
+
|
|
19
|
+
function invalidArgumentDiagnostic(input: {
|
|
20
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
21
|
+
readonly span: ParsedDefaultFunctionCall['span'];
|
|
22
|
+
readonly message: string;
|
|
23
|
+
}): LoweredDefaultResult {
|
|
24
|
+
return {
|
|
25
|
+
ok: false,
|
|
26
|
+
diagnostic: {
|
|
27
|
+
code: 'PSL_INVALID_DEFAULT_FUNCTION_ARGUMENT',
|
|
28
|
+
message: input.message,
|
|
29
|
+
sourceId: input.context.sourceId,
|
|
30
|
+
span: input.span,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function executionGenerator(
|
|
36
|
+
id: ExecutionMutationDefaultValue['id'],
|
|
37
|
+
params?: Record<string, unknown>,
|
|
38
|
+
): LoweredDefaultResult {
|
|
39
|
+
return {
|
|
40
|
+
ok: true,
|
|
41
|
+
value: {
|
|
42
|
+
kind: 'execution',
|
|
43
|
+
generated: {
|
|
44
|
+
kind: 'generator',
|
|
45
|
+
id,
|
|
46
|
+
...(params ? { params } : {}),
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function expectNoArgs(input: {
|
|
53
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
54
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
55
|
+
readonly usage: string;
|
|
56
|
+
}): LoweredDefaultResult | undefined {
|
|
57
|
+
if (input.call.args.length === 0) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return invalidArgumentDiagnostic({
|
|
61
|
+
context: input.context,
|
|
62
|
+
span: input.call.span,
|
|
63
|
+
message: `Default function "${input.call.name}" does not accept arguments. Use ${input.usage}.`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function parseIntegerArgument(raw: string): number | undefined {
|
|
68
|
+
const trimmed = raw.trim();
|
|
69
|
+
if (!/^-?\d+$/.test(trimmed)) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const value = Number(trimmed);
|
|
73
|
+
if (!Number.isInteger(value)) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseStringLiteral(raw: string): string | undefined {
|
|
80
|
+
const match = raw.trim().match(/^(['"])(.*)\1$/s);
|
|
81
|
+
if (!match) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return match[2] ?? '';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function lowerAutoincrement(input: {
|
|
88
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
89
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
90
|
+
}): LoweredDefaultResult {
|
|
91
|
+
const maybeNoArgs = expectNoArgs({
|
|
92
|
+
call: input.call,
|
|
93
|
+
context: input.context,
|
|
94
|
+
usage: '`autoincrement()`',
|
|
95
|
+
});
|
|
96
|
+
if (maybeNoArgs) {
|
|
97
|
+
return maybeNoArgs;
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
ok: true,
|
|
101
|
+
value: {
|
|
102
|
+
kind: 'storage',
|
|
103
|
+
defaultValue: {
|
|
104
|
+
kind: 'function',
|
|
105
|
+
expression: 'autoincrement()',
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function lowerNow(input: {
|
|
112
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
113
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
114
|
+
}): LoweredDefaultResult {
|
|
115
|
+
const maybeNoArgs = expectNoArgs({
|
|
116
|
+
call: input.call,
|
|
117
|
+
context: input.context,
|
|
118
|
+
usage: '`now()`',
|
|
119
|
+
});
|
|
120
|
+
if (maybeNoArgs) {
|
|
121
|
+
return maybeNoArgs;
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
ok: true,
|
|
125
|
+
value: {
|
|
126
|
+
kind: 'storage',
|
|
127
|
+
defaultValue: {
|
|
128
|
+
kind: 'function',
|
|
129
|
+
expression: 'now()',
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function lowerUuid(input: {
|
|
136
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
137
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
138
|
+
}): LoweredDefaultResult {
|
|
139
|
+
if (input.call.args.length === 0) {
|
|
140
|
+
return executionGenerator('uuidv4');
|
|
141
|
+
}
|
|
142
|
+
if (input.call.args.length !== 1) {
|
|
143
|
+
return invalidArgumentDiagnostic({
|
|
144
|
+
context: input.context,
|
|
145
|
+
span: input.call.span,
|
|
146
|
+
message:
|
|
147
|
+
'Default function "uuid" accepts at most one version argument: `uuid()`, `uuid(4)`, or `uuid(7)`.',
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
const version = parseIntegerArgument(input.call.args[0]?.raw ?? '');
|
|
151
|
+
if (version === 4) {
|
|
152
|
+
return executionGenerator('uuidv4');
|
|
153
|
+
}
|
|
154
|
+
if (version === 7) {
|
|
155
|
+
return executionGenerator('uuidv7');
|
|
156
|
+
}
|
|
157
|
+
return invalidArgumentDiagnostic({
|
|
158
|
+
context: input.context,
|
|
159
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
160
|
+
message:
|
|
161
|
+
'Default function "uuid" supports only `uuid()`, `uuid(4)`, or `uuid(7)` in SQL PSL provider v1.',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function lowerCuid(input: {
|
|
166
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
167
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
168
|
+
}): LoweredDefaultResult {
|
|
169
|
+
if (input.call.args.length === 0) {
|
|
170
|
+
return {
|
|
171
|
+
ok: false,
|
|
172
|
+
diagnostic: {
|
|
173
|
+
code: 'PSL_UNKNOWN_DEFAULT_FUNCTION',
|
|
174
|
+
message:
|
|
175
|
+
'Default function "cuid()" is not supported in SQL PSL provider v1. Use `cuid(2)` instead.',
|
|
176
|
+
sourceId: input.context.sourceId,
|
|
177
|
+
span: input.call.span,
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
if (input.call.args.length !== 1) {
|
|
182
|
+
return invalidArgumentDiagnostic({
|
|
183
|
+
context: input.context,
|
|
184
|
+
span: input.call.span,
|
|
185
|
+
message: 'Default function "cuid" accepts exactly one version argument: `cuid(2)`.',
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
const version = parseIntegerArgument(input.call.args[0]?.raw ?? '');
|
|
189
|
+
if (version === 2) {
|
|
190
|
+
return executionGenerator('cuid2');
|
|
191
|
+
}
|
|
192
|
+
return invalidArgumentDiagnostic({
|
|
193
|
+
context: input.context,
|
|
194
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
195
|
+
message: 'Default function "cuid" supports only `cuid(2)` in SQL PSL provider v1.',
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function lowerUlid(input: {
|
|
200
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
201
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
202
|
+
}): LoweredDefaultResult {
|
|
203
|
+
const maybeNoArgs = expectNoArgs({
|
|
204
|
+
call: input.call,
|
|
205
|
+
context: input.context,
|
|
206
|
+
usage: '`ulid()`',
|
|
207
|
+
});
|
|
208
|
+
if (maybeNoArgs) {
|
|
209
|
+
return maybeNoArgs;
|
|
210
|
+
}
|
|
211
|
+
return executionGenerator('ulid');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function lowerNanoid(input: {
|
|
215
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
216
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
217
|
+
}): LoweredDefaultResult {
|
|
218
|
+
if (input.call.args.length === 0) {
|
|
219
|
+
return executionGenerator('nanoid');
|
|
220
|
+
}
|
|
221
|
+
if (input.call.args.length !== 1) {
|
|
222
|
+
return invalidArgumentDiagnostic({
|
|
223
|
+
context: input.context,
|
|
224
|
+
span: input.call.span,
|
|
225
|
+
message:
|
|
226
|
+
'Default function "nanoid" accepts at most one size argument: `nanoid()` or `nanoid(<2-255>)`.',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
const size = parseIntegerArgument(input.call.args[0]?.raw ?? '');
|
|
230
|
+
if (size !== undefined && size >= 2 && size <= 255) {
|
|
231
|
+
return executionGenerator('nanoid', { size });
|
|
232
|
+
}
|
|
233
|
+
return invalidArgumentDiagnostic({
|
|
234
|
+
context: input.context,
|
|
235
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
236
|
+
message: 'Default function "nanoid" size argument must be an integer between 2 and 255.',
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function lowerDbgenerated(input: {
|
|
241
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
242
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
243
|
+
}): LoweredDefaultResult {
|
|
244
|
+
if (input.call.args.length !== 1) {
|
|
245
|
+
return invalidArgumentDiagnostic({
|
|
246
|
+
context: input.context,
|
|
247
|
+
span: input.call.span,
|
|
248
|
+
message:
|
|
249
|
+
'Default function "dbgenerated" requires exactly one string argument: `dbgenerated("...")`.',
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
const rawExpression = parseStringLiteral(input.call.args[0]?.raw ?? '');
|
|
253
|
+
if (rawExpression === undefined) {
|
|
254
|
+
return invalidArgumentDiagnostic({
|
|
255
|
+
context: input.context,
|
|
256
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
257
|
+
message: 'Default function "dbgenerated" argument must be a string literal.',
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
if (rawExpression.trim().length === 0) {
|
|
261
|
+
return invalidArgumentDiagnostic({
|
|
262
|
+
context: input.context,
|
|
263
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
264
|
+
message: 'Default function "dbgenerated" argument cannot be empty.',
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
ok: true,
|
|
269
|
+
value: {
|
|
270
|
+
kind: 'storage',
|
|
271
|
+
defaultValue: {
|
|
272
|
+
kind: 'function',
|
|
273
|
+
expression: rawExpression,
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const postgresDefaultFunctionRegistryEntries = [
|
|
280
|
+
['autoincrement', { lower: lowerAutoincrement, usageSignatures: ['autoincrement()'] }],
|
|
281
|
+
['now', { lower: lowerNow, usageSignatures: ['now()'] }],
|
|
282
|
+
['uuid', { lower: lowerUuid, usageSignatures: ['uuid()', 'uuid(4)', 'uuid(7)'] }],
|
|
283
|
+
['cuid', { lower: lowerCuid, usageSignatures: ['cuid(2)'] }],
|
|
284
|
+
['ulid', { lower: lowerUlid, usageSignatures: ['ulid()'] }],
|
|
285
|
+
['nanoid', { lower: lowerNanoid, usageSignatures: ['nanoid()', 'nanoid(<2-255>)'] }],
|
|
286
|
+
['dbgenerated', { lower: lowerDbgenerated, usageSignatures: ['dbgenerated("...")'] }],
|
|
287
|
+
] satisfies ReadonlyArray<readonly [string, ControlMutationDefaultFunctionEntry]>;
|
|
288
|
+
|
|
289
|
+
const postgresPslScalarTypeDescriptors = new Map<string, PslScalarTypeDescriptor>([
|
|
290
|
+
['String', { codecId: 'pg/text@1', nativeType: 'text' }],
|
|
291
|
+
['Boolean', { codecId: 'pg/bool@1', nativeType: 'bool' }],
|
|
292
|
+
['Int', { codecId: 'pg/int4@1', nativeType: 'int4' }],
|
|
293
|
+
['BigInt', { codecId: 'pg/int8@1', nativeType: 'int8' }],
|
|
294
|
+
['Float', { codecId: 'pg/float8@1', nativeType: 'float8' }],
|
|
295
|
+
['Decimal', { codecId: 'pg/numeric@1', nativeType: 'numeric' }],
|
|
296
|
+
['DateTime', { codecId: 'pg/timestamptz@1', nativeType: 'timestamptz' }],
|
|
297
|
+
['Json', { codecId: 'pg/jsonb@1', nativeType: 'jsonb' }],
|
|
298
|
+
['Bytes', { codecId: 'pg/bytea@1', nativeType: 'bytea' }],
|
|
299
|
+
]);
|
|
300
|
+
|
|
301
|
+
export function createPostgresDefaultFunctionRegistry(): ReadonlyMap<
|
|
302
|
+
string,
|
|
303
|
+
ControlMutationDefaultFunctionEntry
|
|
304
|
+
> {
|
|
305
|
+
return new Map(postgresDefaultFunctionRegistryEntries);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function createPostgresMutationDefaultGeneratorDescriptors(): readonly ControlMutationDefaultGeneratorDescriptor[] {
|
|
309
|
+
return builtinGeneratorRegistryMetadata.map(({ id, applicableCodecIds }) => ({
|
|
310
|
+
id,
|
|
311
|
+
applicableCodecIds,
|
|
312
|
+
resolveGeneratedColumnDescriptor: ({ generated }) => {
|
|
313
|
+
if (generated.kind !== 'generator' || generated.id !== id) {
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
const descriptor = resolveBuiltinGeneratedColumnDescriptor({
|
|
317
|
+
id,
|
|
318
|
+
...(generated.params ? { params: generated.params } : {}),
|
|
319
|
+
});
|
|
320
|
+
return {
|
|
321
|
+
codecId: descriptor.type.codecId,
|
|
322
|
+
nativeType: descriptor.type.nativeType,
|
|
323
|
+
...(descriptor.type.typeRef ? { typeRef: descriptor.type.typeRef } : {}),
|
|
324
|
+
...(descriptor.typeParams ? { typeParams: descriptor.typeParams } : {}),
|
|
325
|
+
};
|
|
326
|
+
},
|
|
327
|
+
}));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function createPostgresPslScalarTypeDescriptors(): ReadonlyMap<
|
|
331
|
+
string,
|
|
332
|
+
PslScalarTypeDescriptor
|
|
333
|
+
> {
|
|
334
|
+
return new Map(postgresPslScalarTypeDescriptors);
|
|
335
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
if (isBigInt) {
|
|
112
|
+
return { kind: 'literal', value: { $type: 'bigint', value: trimmed } };
|
|
113
|
+
}
|
|
114
|
+
const num = Number(trimmed);
|
|
115
|
+
if (!Number.isFinite(num)) return undefined;
|
|
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
|
+
return { kind: 'literal', value: unescaped };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Unrecognized expression - return as a function with the raw expression
|
|
136
|
+
// This preserves the information for debugging while still being comparable
|
|
137
|
+
return { kind: 'function', expression: trimmed };
|
|
138
|
+
}
|