@prisma-next/adapter-postgres 0.3.0-dev.7 → 0.3.0-dev.70
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-DtehReRR.mjs +271 -0
- package/dist/adapter-DtehReRR.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-BfC_5c-4.mjs +207 -0
- package/dist/codecs-BfC_5c-4.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 +111 -0
- package/dist/control.d.mts.map +1 -0
- package/dist/control.mjs +462 -0
- package/dist/control.mjs.map +1 -0
- package/dist/descriptor-meta-ilnFI7bx.mjs +921 -0
- package/dist/descriptor-meta-ilnFI7bx.mjs.map +1 -0
- package/dist/runtime.d.mts +19 -0
- package/dist/runtime.d.mts.map +1 -0
- package/dist/runtime.mjs +85 -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-CXO7EB60.d.mts +19 -0
- package/dist/types-CXO7EB60.d.mts.map +1 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +1 -0
- package/package.json +37 -46
- package/src/core/adapter.ts +139 -28
- package/src/core/codec-ids.ts +28 -0
- package/src/core/codecs.ts +325 -23
- package/src/core/control-adapter.ts +400 -178
- package/src/core/default-normalizer.ts +90 -0
- package/src/core/descriptor-meta.ts +221 -9
- package/src/core/enum-control-hooks.ts +735 -0
- package/src/core/json-schema-type-expression.ts +131 -0
- package/src/core/json-schema-validator.ts +53 -0
- package/src/core/parameterized-types.ts +118 -0
- package/src/core/sql-utils.ts +111 -0
- package/src/core/standard-schema.ts +71 -0
- package/src/exports/codec-types.ts +73 -1
- package/src/exports/column-types.ts +233 -9
- package/src/exports/control.ts +16 -9
- package/src/exports/runtime.ts +61 -18
- 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,131 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
JsonSchemaValidateFn,
|
|
3
|
+
JsonSchemaValidationError,
|
|
4
|
+
JsonSchemaValidationResult,
|
|
5
|
+
} from '@prisma-next/sql-relational-core/query-lane-context';
|
|
6
|
+
import Ajv from 'ajv';
|
|
7
|
+
|
|
8
|
+
export type { JsonSchemaValidateFn, JsonSchemaValidationError, JsonSchemaValidationResult };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Shared Ajv instance for all JSON Schema validators.
|
|
12
|
+
* Reusing a single instance avoids ~50-100KB memory overhead per compiled schema.
|
|
13
|
+
*/
|
|
14
|
+
let sharedAjv: Ajv | undefined;
|
|
15
|
+
|
|
16
|
+
function getSharedAjv(): Ajv {
|
|
17
|
+
if (!sharedAjv) {
|
|
18
|
+
sharedAjv = new Ajv({ allErrors: false, strict: false });
|
|
19
|
+
}
|
|
20
|
+
return sharedAjv;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Compiles a JSON Schema object into a reusable validate function using Ajv.
|
|
25
|
+
*
|
|
26
|
+
* The returned function validates a value against the schema and returns
|
|
27
|
+
* a structured result with error details on failure.
|
|
28
|
+
*
|
|
29
|
+
* Uses a shared Ajv instance and fail-fast mode (`allErrors: false`)
|
|
30
|
+
* to minimize memory and CPU overhead.
|
|
31
|
+
*
|
|
32
|
+
* @param schema - A JSON Schema object (draft-07 compatible)
|
|
33
|
+
* @returns A validate function
|
|
34
|
+
*/
|
|
35
|
+
export function compileJsonSchemaValidator(schema: Record<string, unknown>): JsonSchemaValidateFn {
|
|
36
|
+
const ajv = getSharedAjv();
|
|
37
|
+
const validate = ajv.compile(schema);
|
|
38
|
+
|
|
39
|
+
return (value: unknown): JsonSchemaValidationResult => {
|
|
40
|
+
const valid = validate(value);
|
|
41
|
+
if (valid) {
|
|
42
|
+
return { valid: true };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const errors: JsonSchemaValidationError[] = (validate.errors ?? []).map((err) => ({
|
|
46
|
+
path: err.instancePath || '/',
|
|
47
|
+
message: err.message ?? 'unknown validation error',
|
|
48
|
+
keyword: err.keyword,
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
return { valid: false, errors };
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utility for expanding parameterized Postgres types to their full SQL representation.
|
|
3
|
+
*
|
|
4
|
+
* This module provides a single source of truth for type expansion logic, used by:
|
|
5
|
+
* - Schema verification (verify-sql-schema.ts) via the expandNativeType codec control hook
|
|
6
|
+
* - Migration planner (planner.ts) via direct import
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
PG_BIT_CODEC_ID,
|
|
13
|
+
PG_CHAR_CODEC_ID,
|
|
14
|
+
PG_INTERVAL_CODEC_ID,
|
|
15
|
+
PG_NUMERIC_CODEC_ID,
|
|
16
|
+
PG_TIME_CODEC_ID,
|
|
17
|
+
PG_TIMESTAMP_CODEC_ID,
|
|
18
|
+
PG_TIMESTAMPTZ_CODEC_ID,
|
|
19
|
+
PG_TIMETZ_CODEC_ID,
|
|
20
|
+
PG_VARBIT_CODEC_ID,
|
|
21
|
+
PG_VARCHAR_CODEC_ID,
|
|
22
|
+
SQL_CHAR_CODEC_ID,
|
|
23
|
+
SQL_VARCHAR_CODEC_ID,
|
|
24
|
+
} from './codec-ids';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Input for expanding parameterized native types.
|
|
28
|
+
*/
|
|
29
|
+
export interface ExpandNativeTypeInput {
|
|
30
|
+
readonly nativeType: string;
|
|
31
|
+
readonly codecId?: string;
|
|
32
|
+
readonly typeParams?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Set of codec IDs that use the 'length' parameter */
|
|
36
|
+
const LENGTH_CODEC_IDS: Set<string> = new Set([
|
|
37
|
+
SQL_CHAR_CODEC_ID,
|
|
38
|
+
SQL_VARCHAR_CODEC_ID,
|
|
39
|
+
PG_CHAR_CODEC_ID,
|
|
40
|
+
PG_VARCHAR_CODEC_ID,
|
|
41
|
+
PG_BIT_CODEC_ID,
|
|
42
|
+
PG_VARBIT_CODEC_ID,
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
/** Set of codec IDs that use the 'precision' parameter for temporal types */
|
|
46
|
+
const TEMPORAL_PRECISION_CODEC_IDS: Set<string> = new Set([
|
|
47
|
+
PG_TIMESTAMP_CODEC_ID,
|
|
48
|
+
PG_TIMESTAMPTZ_CODEC_ID,
|
|
49
|
+
PG_TIME_CODEC_ID,
|
|
50
|
+
PG_TIMETZ_CODEC_ID,
|
|
51
|
+
PG_INTERVAL_CODEC_ID,
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Validates that a value is a valid type parameter number.
|
|
56
|
+
* Type parameters must be finite, non-negative integers.
|
|
57
|
+
*/
|
|
58
|
+
function isValidTypeParamNumber(value: unknown): value is number {
|
|
59
|
+
return (
|
|
60
|
+
typeof value === 'number' && Number.isFinite(value) && Number.isInteger(value) && value >= 0
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Expands a parameterized native type to its full SQL representation.
|
|
66
|
+
*
|
|
67
|
+
* For example:
|
|
68
|
+
* - { nativeType: 'character varying', typeParams: { length: 255 } } -> 'character varying(255)'
|
|
69
|
+
* - { nativeType: 'numeric', typeParams: { precision: 10, scale: 2 } } -> 'numeric(10,2)'
|
|
70
|
+
* - { nativeType: 'timestamp without time zone', typeParams: { precision: 3 } } -> 'timestamp without time zone(3)'
|
|
71
|
+
*
|
|
72
|
+
* Returns the original nativeType if:
|
|
73
|
+
* - No typeParams are provided
|
|
74
|
+
* - No codecId is provided
|
|
75
|
+
* - The codecId is not a known parameterized type
|
|
76
|
+
* - The typeParams values are invalid
|
|
77
|
+
*/
|
|
78
|
+
export function expandParameterizedNativeType(input: ExpandNativeTypeInput): string {
|
|
79
|
+
const { nativeType, codecId, typeParams } = input;
|
|
80
|
+
|
|
81
|
+
if (!typeParams || !codecId) {
|
|
82
|
+
return nativeType;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Length-parameterized types: char, varchar, bit, varbit
|
|
86
|
+
if (LENGTH_CODEC_IDS.has(codecId)) {
|
|
87
|
+
const length = typeParams['length'];
|
|
88
|
+
if (isValidTypeParamNumber(length)) {
|
|
89
|
+
return `${nativeType}(${length})`;
|
|
90
|
+
}
|
|
91
|
+
return nativeType;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Numeric with precision and optional scale
|
|
95
|
+
if (codecId === PG_NUMERIC_CODEC_ID) {
|
|
96
|
+
const precision = typeParams['precision'];
|
|
97
|
+
const scale = typeParams['scale'];
|
|
98
|
+
|
|
99
|
+
if (isValidTypeParamNumber(precision)) {
|
|
100
|
+
if (isValidTypeParamNumber(scale)) {
|
|
101
|
+
return `${nativeType}(${precision},${scale})`;
|
|
102
|
+
}
|
|
103
|
+
return `${nativeType}(${precision})`;
|
|
104
|
+
}
|
|
105
|
+
return nativeType;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Temporal types with precision: timestamp, timestamptz, time, timetz, interval
|
|
109
|
+
if (TEMPORAL_PRECISION_CODEC_IDS.has(codecId)) {
|
|
110
|
+
const precision = typeParams['precision'];
|
|
111
|
+
if (isValidTypeParamNumber(precision)) {
|
|
112
|
+
return `${nativeType}(${precision})`;
|
|
113
|
+
}
|
|
114
|
+
return nativeType;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return nativeType;
|
|
118
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
type UnknownRecord = Record<string, unknown>;
|
|
2
|
+
|
|
3
|
+
type StandardSchemaJsonSchemaField = {
|
|
4
|
+
readonly output?: unknown;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Runtime view of the Standard Schema protocol.
|
|
9
|
+
* Reads `~standard.jsonSchema.output` for the serializable JSON Schema representation,
|
|
10
|
+
* and `.expression` for an optional TypeScript type expression string (Arktype-specific).
|
|
11
|
+
*
|
|
12
|
+
* This differs from the compile-time `StandardSchemaLike` in `codec-types.ts`, which reads
|
|
13
|
+
* `~standard.types.output` for TypeScript type narrowing in contract.d.ts.
|
|
14
|
+
*/
|
|
15
|
+
export type StandardSchemaLike = {
|
|
16
|
+
readonly '~standard'?: {
|
|
17
|
+
readonly version?: number;
|
|
18
|
+
readonly jsonSchema?: StandardSchemaJsonSchemaField;
|
|
19
|
+
};
|
|
20
|
+
readonly expression?: unknown;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function isObjectLike(value: unknown): value is UnknownRecord {
|
|
24
|
+
return (typeof value === 'object' || typeof value === 'function') && value !== null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function resolveOutputJsonSchemaField(schema: StandardSchemaLike): unknown {
|
|
28
|
+
const jsonSchema = schema['~standard']?.jsonSchema;
|
|
29
|
+
if (!jsonSchema) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (typeof jsonSchema.output === 'function') {
|
|
34
|
+
return jsonSchema.output({
|
|
35
|
+
target: 'draft-07',
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return jsonSchema.output;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function extractStandardSchemaOutputJsonSchema(
|
|
43
|
+
schema: StandardSchemaLike,
|
|
44
|
+
): UnknownRecord | undefined {
|
|
45
|
+
const outputSchema = resolveOutputJsonSchemaField(schema);
|
|
46
|
+
if (!isObjectLike(outputSchema)) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return outputSchema;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function extractStandardSchemaTypeExpression(
|
|
54
|
+
schema: StandardSchemaLike,
|
|
55
|
+
): string | undefined {
|
|
56
|
+
const expression = schema.expression;
|
|
57
|
+
if (typeof expression !== 'string') {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const trimmedExpression = expression.trim();
|
|
62
|
+
if (trimmedExpression.length === 0) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return trimmedExpression;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function isStandardSchemaLike(value: unknown): value is StandardSchemaLike {
|
|
70
|
+
return isObjectLike(value) && isObjectLike((value as StandardSchemaLike)['~standard']);
|
|
71
|
+
}
|
|
@@ -7,5 +7,77 @@
|
|
|
7
7
|
* Runtime codec implementations are provided by the adapter's codec registry.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
import type { CodecTypes as CoreCodecTypes, JsonValue } from '../core/codecs';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Compile-time view of the Standard Schema protocol.
|
|
14
|
+
* Reads `~standard.types.output` to resolve TypeScript output types for contract.d.ts.
|
|
15
|
+
*
|
|
16
|
+
* This differs from the runtime `StandardSchemaLike` in `standard-schema.ts`, which reads
|
|
17
|
+
* `~standard.jsonSchema.output` for the serializable JSON Schema representation.
|
|
18
|
+
* Both are needed: this one drives compile-time type narrowing, the other drives
|
|
19
|
+
* build-time contract emission.
|
|
20
|
+
*/
|
|
21
|
+
type StandardSchemaLike = {
|
|
22
|
+
readonly '~standard'?: {
|
|
23
|
+
readonly types?: {
|
|
24
|
+
readonly output?: unknown;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type ResolveStandardSchemaOutput<P> = P extends { readonly schema: infer Schema }
|
|
30
|
+
? Schema extends { readonly infer: infer Output }
|
|
31
|
+
? Output
|
|
32
|
+
: Schema extends {
|
|
33
|
+
readonly '~standard': { readonly types?: { readonly output?: infer Output } };
|
|
34
|
+
}
|
|
35
|
+
? Output extends undefined
|
|
36
|
+
? JsonValue
|
|
37
|
+
: Output
|
|
38
|
+
: JsonValue
|
|
39
|
+
: JsonValue;
|
|
40
|
+
|
|
41
|
+
export type CodecTypes = CoreCodecTypes & {
|
|
42
|
+
readonly 'pg/json@1': CoreCodecTypes['pg/json@1'] & {
|
|
43
|
+
readonly parameterizedOutput: <P extends { readonly schema?: StandardSchemaLike }>(
|
|
44
|
+
params: P,
|
|
45
|
+
) => ResolveStandardSchemaOutput<P>;
|
|
46
|
+
};
|
|
47
|
+
readonly 'pg/jsonb@1': CoreCodecTypes['pg/jsonb@1'] & {
|
|
48
|
+
readonly parameterizedOutput: <P extends { readonly schema?: StandardSchemaLike }>(
|
|
49
|
+
params: P,
|
|
50
|
+
) => ResolveStandardSchemaOutput<P>;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type { JsonValue };
|
|
11
55
|
export { dataTypes } from '../core/codecs';
|
|
56
|
+
|
|
57
|
+
type Branded<T, Shape extends Record<string, unknown>> = T & {
|
|
58
|
+
readonly [K in keyof Shape]: Shape[K];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
type BrandedString<Shape extends Record<string, unknown>> = Branded<string, Shape>;
|
|
62
|
+
|
|
63
|
+
export type Char<N extends number> = BrandedString<{ __charLength: N }>;
|
|
64
|
+
export type Varchar<N extends number> = BrandedString<{ __varcharLength: N }>;
|
|
65
|
+
export type Numeric<P extends number, S extends number | undefined = undefined> = BrandedString<{
|
|
66
|
+
__numericPrecision: P;
|
|
67
|
+
__numericScale: S;
|
|
68
|
+
}>;
|
|
69
|
+
export type Bit<N extends number> = BrandedString<{ __bitLength: N }>;
|
|
70
|
+
export type VarBit<N extends number> = BrandedString<{ __varbitLength: N }>;
|
|
71
|
+
export type Timestamp<P extends number | undefined = undefined> = BrandedString<{
|
|
72
|
+
__timestampPrecision: P;
|
|
73
|
+
}>;
|
|
74
|
+
export type Timestamptz<P extends number | undefined = undefined> = BrandedString<{
|
|
75
|
+
__timestamptzPrecision: P;
|
|
76
|
+
}>;
|
|
77
|
+
export type Time<P extends number | undefined = undefined> = BrandedString<{ __timePrecision: P }>;
|
|
78
|
+
export type Timetz<P extends number | undefined = undefined> = BrandedString<{
|
|
79
|
+
__timetzPrecision: P;
|
|
80
|
+
}>;
|
|
81
|
+
export type Interval<P extends number | undefined = undefined> = BrandedString<{
|
|
82
|
+
__intervalPrecision: P;
|
|
83
|
+
}>;
|