@prisma-next/target-postgres 0.4.0-dev.9 → 0.5.0-dev.1
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/dist/codec-ids-CojIXVf9.mjs +29 -0
- package/dist/codec-ids-CojIXVf9.mjs.map +1 -0
- package/dist/codec-ids.d.mts +28 -0
- package/dist/codec-ids.d.mts.map +1 -0
- package/dist/codec-ids.mjs +3 -0
- package/dist/codec-types.d.mts +42 -0
- package/dist/codec-types.d.mts.map +1 -0
- package/dist/codec-types.mjs +3 -0
- package/dist/codecs-BoahtY_Q.mjs +385 -0
- package/dist/codecs-BoahtY_Q.mjs.map +1 -0
- package/dist/codecs-D-F2KJqt.d.mts +299 -0
- package/dist/codecs-D-F2KJqt.d.mts.map +1 -0
- package/dist/codecs.d.mts +2 -0
- package/dist/codecs.mjs +3 -0
- package/dist/control.d.mts +1 -9
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +23 -5101
- package/dist/control.mjs.map +1 -1
- package/dist/data-transform-CxFRBIUp.d.mts +32 -0
- package/dist/data-transform-CxFRBIUp.d.mts.map +1 -0
- package/dist/data-transform-VfEGzXWt.mjs +39 -0
- package/dist/data-transform-VfEGzXWt.mjs.map +1 -0
- package/dist/data-transform.d.mts +2 -0
- package/dist/data-transform.mjs +3 -0
- package/dist/default-normalizer-DNOpRoOF.mjs +131 -0
- package/dist/default-normalizer-DNOpRoOF.mjs.map +1 -0
- package/dist/default-normalizer.d.mts +19 -0
- package/dist/default-normalizer.d.mts.map +1 -0
- package/dist/default-normalizer.mjs +3 -0
- package/dist/{descriptor-meta-DkvCmY98.mjs → descriptor-meta-BVoVtyp-.mjs} +1 -1
- package/dist/{descriptor-meta-DkvCmY98.mjs.map → descriptor-meta-BVoVtyp-.mjs.map} +1 -1
- package/dist/errors-AFvEPZ1R.mjs +34 -0
- package/dist/errors-AFvEPZ1R.mjs.map +1 -0
- package/dist/errors.d.mts +27 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +3 -0
- package/dist/issue-planner-CFjB0_oO.mjs +879 -0
- package/dist/issue-planner-CFjB0_oO.mjs.map +1 -0
- package/dist/issue-planner.d.mts +85 -0
- package/dist/issue-planner.d.mts.map +1 -0
- package/dist/issue-planner.mjs +3 -0
- package/dist/migration.d.mts +90 -0
- package/dist/migration.d.mts.map +1 -0
- package/dist/migration.mjs +24 -0
- package/dist/migration.mjs.map +1 -0
- package/dist/native-type-normalizer-CInai_oY.mjs +38 -0
- package/dist/native-type-normalizer-CInai_oY.mjs.map +1 -0
- package/dist/native-type-normalizer.d.mts +18 -0
- package/dist/native-type-normalizer.d.mts.map +1 -0
- package/dist/native-type-normalizer.mjs +3 -0
- package/dist/op-factory-call-BKlruaiC.mjs +605 -0
- package/dist/op-factory-call-BKlruaiC.mjs.map +1 -0
- package/dist/op-factory-call-C3bWXKSP.d.mts +304 -0
- package/dist/op-factory-call-C3bWXKSP.d.mts.map +1 -0
- package/dist/op-factory-call.d.mts +3 -0
- package/dist/op-factory-call.mjs +3 -0
- package/dist/pack.d.mts +1 -1
- package/dist/pack.mjs +1 -1
- package/dist/planner-CLUvVhUN.mjs +98 -0
- package/dist/planner-CLUvVhUN.mjs.map +1 -0
- package/dist/planner-ddl-builders-Dxvw1LHw.mjs +132 -0
- package/dist/planner-ddl-builders-Dxvw1LHw.mjs.map +1 -0
- package/dist/planner-ddl-builders.d.mts +22 -0
- package/dist/planner-ddl-builders.d.mts.map +1 -0
- package/dist/planner-ddl-builders.mjs +3 -0
- package/dist/planner-identity-values-Dju-o5GF.mjs +91 -0
- package/dist/planner-identity-values-Dju-o5GF.mjs.map +1 -0
- package/dist/planner-identity-values.d.mts +20 -0
- package/dist/planner-identity-values.d.mts.map +1 -0
- package/dist/planner-identity-values.mjs +3 -0
- package/dist/planner-produced-postgres-migration-CRRTno6Z.d.mts +20 -0
- package/dist/planner-produced-postgres-migration-CRRTno6Z.d.mts.map +1 -0
- package/dist/planner-produced-postgres-migration-DSSPq8QS.mjs +33 -0
- package/dist/planner-produced-postgres-migration-DSSPq8QS.mjs.map +1 -0
- package/dist/planner-produced-postgres-migration.d.mts +5 -0
- package/dist/planner-produced-postgres-migration.mjs +3 -0
- package/dist/planner-schema-lookup-B7lkypwn.mjs +29 -0
- package/dist/planner-schema-lookup-B7lkypwn.mjs.map +1 -0
- package/dist/planner-schema-lookup.d.mts +22 -0
- package/dist/planner-schema-lookup.d.mts.map +1 -0
- package/dist/planner-schema-lookup.mjs +3 -0
- package/dist/planner-sql-checks-7jkgm9TX.mjs +241 -0
- package/dist/planner-sql-checks-7jkgm9TX.mjs.map +1 -0
- package/dist/planner-sql-checks.d.mts +55 -0
- package/dist/planner-sql-checks.d.mts.map +1 -0
- package/dist/planner-sql-checks.mjs +3 -0
- package/dist/planner-target-details-DH-azLu-.d.mts +11 -0
- package/dist/planner-target-details-DH-azLu-.d.mts.map +1 -0
- package/dist/planner-target-details.d.mts +2 -0
- package/dist/planner-target-details.mjs +1 -0
- package/dist/planner.d.mts +68 -0
- package/dist/planner.d.mts.map +1 -0
- package/dist/planner.mjs +4 -0
- package/dist/postgres-migration-BjA3Zmts.d.mts +50 -0
- package/dist/postgres-migration-BjA3Zmts.d.mts.map +1 -0
- package/dist/postgres-migration-qtmtbONe.mjs +52 -0
- package/dist/postgres-migration-qtmtbONe.mjs.map +1 -0
- package/dist/render-ops-D6_DHdOK.mjs +8 -0
- package/dist/render-ops-D6_DHdOK.mjs.map +1 -0
- package/dist/render-ops.d.mts +11 -0
- package/dist/render-ops.d.mts.map +1 -0
- package/dist/render-ops.mjs +3 -0
- package/dist/render-typescript-1rF_SB4g.mjs +85 -0
- package/dist/render-typescript-1rF_SB4g.mjs.map +1 -0
- package/dist/render-typescript.d.mts +15 -0
- package/dist/render-typescript.d.mts.map +1 -0
- package/dist/render-typescript.mjs +3 -0
- package/dist/runtime.d.mts +15 -3
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +10 -1
- package/dist/runtime.mjs.map +1 -1
- package/dist/shared-Bxkt8pNO.d.mts +41 -0
- package/dist/shared-Bxkt8pNO.d.mts.map +1 -0
- package/dist/sql-utils-r-Lw535w.mjs +76 -0
- package/dist/sql-utils-r-Lw535w.mjs.map +1 -0
- package/dist/sql-utils.d.mts +59 -0
- package/dist/sql-utils.d.mts.map +1 -0
- package/dist/sql-utils.mjs +3 -0
- package/dist/statement-builders-BPnmt6wx.mjs +116 -0
- package/dist/statement-builders-BPnmt6wx.mjs.map +1 -0
- package/dist/statement-builders.d.mts +23 -0
- package/dist/statement-builders.d.mts.map +1 -0
- package/dist/statement-builders.mjs +3 -0
- package/dist/tables-BmdW_FWO.mjs +477 -0
- package/dist/tables-BmdW_FWO.mjs.map +1 -0
- package/dist/types-ClK03Ojd.d.mts +10 -0
- package/dist/types-ClK03Ojd.d.mts.map +1 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +1 -0
- package/package.json +38 -19
- package/src/core/codec-ids.ts +30 -0
- package/src/core/codecs.ts +645 -0
- package/src/core/default-normalizer.ts +131 -0
- package/src/core/descriptor-meta.ts +1 -1
- package/src/core/errors.ts +33 -0
- package/src/core/json-schema-type-expression.ts +131 -0
- package/src/core/migrations/issue-planner.ts +832 -0
- package/src/core/migrations/op-factory-call.ts +858 -0
- package/src/core/migrations/operations/columns.ts +285 -0
- package/src/core/migrations/operations/constraints.ts +191 -0
- package/src/core/migrations/operations/data-transform.ts +119 -0
- package/src/core/migrations/operations/dependencies.ts +36 -0
- package/src/core/migrations/operations/enums.ts +113 -0
- package/src/core/migrations/operations/indexes.ts +61 -0
- package/src/core/migrations/operations/raw.ts +15 -0
- package/src/core/migrations/operations/shared.ts +67 -0
- package/src/core/migrations/operations/tables.ts +63 -0
- package/src/core/migrations/planner-ddl-builders.ts +1 -1
- package/src/core/migrations/planner-produced-postgres-migration.ts +67 -0
- package/src/core/migrations/planner-recipes.ts +1 -1
- package/src/core/migrations/planner-sql-checks.ts +1 -1
- package/src/core/migrations/planner-strategies.ts +592 -151
- package/src/core/migrations/planner-target-details.ts +0 -6
- package/src/core/migrations/planner.ts +65 -785
- package/src/core/migrations/postgres-migration.ts +73 -0
- package/src/core/migrations/render-ops.ts +9 -0
- package/src/core/migrations/render-typescript.ts +105 -0
- package/src/core/migrations/runner.ts +2 -4
- package/src/core/native-type-normalizer.ts +49 -0
- package/src/core/sql-utils.ts +104 -0
- package/src/exports/codec-ids.ts +1 -0
- package/src/exports/codec-types.ts +51 -0
- package/src/exports/codecs.ts +2 -0
- package/src/exports/control.ts +9 -142
- package/src/exports/data-transform.ts +1 -0
- package/src/exports/default-normalizer.ts +1 -0
- package/src/exports/errors.ts +1 -0
- package/src/exports/issue-planner.ts +1 -0
- package/src/exports/migration.ts +46 -0
- package/src/exports/native-type-normalizer.ts +1 -0
- package/src/exports/op-factory-call.ts +25 -0
- package/src/exports/planner-ddl-builders.ts +8 -0
- package/src/exports/planner-identity-values.ts +1 -0
- package/src/exports/planner-produced-postgres-migration.ts +1 -0
- package/src/exports/planner-schema-lookup.ts +6 -0
- package/src/exports/planner-sql-checks.ts +11 -0
- package/src/exports/planner-target-details.ts +1 -0
- package/src/exports/planner.ts +1 -0
- package/src/exports/render-ops.ts +1 -0
- package/src/exports/render-typescript.ts +1 -0
- package/src/exports/runtime.ts +19 -4
- package/src/exports/sql-utils.ts +7 -0
- package/src/exports/statement-builders.ts +7 -0
- package/src/exports/types.ts +1 -0
- package/dist/migration-builders.d.mts +0 -88
- package/dist/migration-builders.d.mts.map +0 -1
- package/dist/migration-builders.mjs +0 -3
- package/dist/operation-descriptors-CxymFSgK.mjs +0 -52
- package/dist/operation-descriptors-CxymFSgK.mjs.map +0 -1
- package/src/core/migrations/descriptor-planner.ts +0 -464
- package/src/core/migrations/operation-descriptors.ts +0 -166
- package/src/core/migrations/operation-resolver.ts +0 -929
- package/src/core/migrations/planner-reconciliation.ts +0 -798
- package/src/core/migrations/scaffolding.ts +0 -140
- package/src/exports/migration-builders.ts +0 -56
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified codec definitions for Postgres adapter.
|
|
3
|
+
*
|
|
4
|
+
* This file contains a single source of truth for all codec information:
|
|
5
|
+
* - Scalar names
|
|
6
|
+
* - Type IDs
|
|
7
|
+
* - Codec implementations (runtime)
|
|
8
|
+
* - Type information (compile-time)
|
|
9
|
+
*
|
|
10
|
+
* This structure is used both at runtime (to populate the registry) and
|
|
11
|
+
* at compile time (to derive CodecTypes).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { JsonValue } from '@prisma-next/contract/types';
|
|
15
|
+
import type { Codec, CodecMeta, CodecTrait } from '@prisma-next/sql-relational-core/ast';
|
|
16
|
+
import { codec, defineCodecs, sqlCodecDefinitions } from '@prisma-next/sql-relational-core/ast';
|
|
17
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
18
|
+
import { type as arktype } from 'arktype';
|
|
19
|
+
import {
|
|
20
|
+
PG_BIT_CODEC_ID,
|
|
21
|
+
PG_BOOL_CODEC_ID,
|
|
22
|
+
PG_CHAR_CODEC_ID,
|
|
23
|
+
PG_ENUM_CODEC_ID,
|
|
24
|
+
PG_FLOAT_CODEC_ID,
|
|
25
|
+
PG_FLOAT4_CODEC_ID,
|
|
26
|
+
PG_FLOAT8_CODEC_ID,
|
|
27
|
+
PG_INT_CODEC_ID,
|
|
28
|
+
PG_INT2_CODEC_ID,
|
|
29
|
+
PG_INT4_CODEC_ID,
|
|
30
|
+
PG_INT8_CODEC_ID,
|
|
31
|
+
PG_INTERVAL_CODEC_ID,
|
|
32
|
+
PG_JSON_CODEC_ID,
|
|
33
|
+
PG_JSONB_CODEC_ID,
|
|
34
|
+
PG_NUMERIC_CODEC_ID,
|
|
35
|
+
PG_TEXT_CODEC_ID,
|
|
36
|
+
PG_TIME_CODEC_ID,
|
|
37
|
+
PG_TIMESTAMP_CODEC_ID,
|
|
38
|
+
PG_TIMESTAMPTZ_CODEC_ID,
|
|
39
|
+
PG_TIMETZ_CODEC_ID,
|
|
40
|
+
PG_VARBIT_CODEC_ID,
|
|
41
|
+
PG_VARCHAR_CODEC_ID,
|
|
42
|
+
} from './codec-ids';
|
|
43
|
+
import { renderTypeScriptTypeFromJsonSchema } from './json-schema-type-expression';
|
|
44
|
+
|
|
45
|
+
const lengthParamsSchema = arktype({
|
|
46
|
+
length: 'number.integer > 0',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const numericParamsSchema = arktype({
|
|
50
|
+
precision: 'number.integer > 0 & number.integer <= 1000',
|
|
51
|
+
'scale?': 'number.integer >= 0',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const precisionParamsSchema = arktype({
|
|
55
|
+
'precision?': 'number.integer >= 0 & number.integer <= 6',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
function renderLength(typeName: string, typeParams: Record<string, unknown>): string | undefined {
|
|
59
|
+
const length = typeParams['length'];
|
|
60
|
+
if (length === undefined) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
if (typeof length !== 'number' || !Number.isFinite(length) || !Number.isInteger(length)) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`renderOutputType: expected integer "length" in typeParams for ${typeName}, got ${String(length)}`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return `${typeName}<${length}>`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function renderPrecision(typeName: string, typeParams: Record<string, unknown>): string {
|
|
72
|
+
const precision = typeParams['precision'];
|
|
73
|
+
if (precision === undefined) {
|
|
74
|
+
return typeName;
|
|
75
|
+
}
|
|
76
|
+
if (
|
|
77
|
+
typeof precision !== 'number' ||
|
|
78
|
+
!Number.isFinite(precision) ||
|
|
79
|
+
!Number.isInteger(precision)
|
|
80
|
+
) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`renderOutputType: expected integer "precision" in typeParams for ${typeName}, got ${String(precision)}`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return `${typeName}<${precision}>`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function renderJsonOutputType(typeParams: Record<string, unknown>): string {
|
|
89
|
+
const typeName = typeParams['type'];
|
|
90
|
+
if (typeof typeName === 'string' && typeName.trim().length > 0) {
|
|
91
|
+
return typeName.trim();
|
|
92
|
+
}
|
|
93
|
+
const schema = typeParams['schemaJson'];
|
|
94
|
+
if (schema && typeof schema === 'object') {
|
|
95
|
+
return renderTypeScriptTypeFromJsonSchema(schema);
|
|
96
|
+
}
|
|
97
|
+
throw new Error(
|
|
98
|
+
`renderOutputType: JSON codec typeParams must contain "type" (string) or "schemaJson" (object), got keys: ${Object.keys(typeParams).join(', ')}`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function aliasCodec<
|
|
103
|
+
Id extends string,
|
|
104
|
+
TTraits extends readonly CodecTrait[],
|
|
105
|
+
TWire,
|
|
106
|
+
TJs,
|
|
107
|
+
TParams,
|
|
108
|
+
THelper,
|
|
109
|
+
>(
|
|
110
|
+
base: Codec<string, TTraits, TWire, TJs, TParams, THelper>,
|
|
111
|
+
options: {
|
|
112
|
+
readonly typeId: Id;
|
|
113
|
+
readonly targetTypes: readonly string[];
|
|
114
|
+
readonly meta?: CodecMeta;
|
|
115
|
+
},
|
|
116
|
+
): Codec<Id, TTraits, TWire, TJs, TParams, THelper> {
|
|
117
|
+
return {
|
|
118
|
+
id: options.typeId,
|
|
119
|
+
targetTypes: options.targetTypes,
|
|
120
|
+
...ifDefined('meta', options.meta),
|
|
121
|
+
...ifDefined('paramsSchema', base.paramsSchema),
|
|
122
|
+
...ifDefined('init', base.init),
|
|
123
|
+
...ifDefined('encode', base.encode),
|
|
124
|
+
...ifDefined('traits', base.traits),
|
|
125
|
+
...ifDefined('renderOutputType', base.renderOutputType),
|
|
126
|
+
decode: base.decode,
|
|
127
|
+
encodeJson: base.encodeJson,
|
|
128
|
+
decodeJson: base.decodeJson,
|
|
129
|
+
} as Codec<Id, TTraits, TWire, TJs, TParams, THelper>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const sqlCharCodec = sqlCodecDefinitions.char.codec;
|
|
133
|
+
const sqlVarcharCodec = sqlCodecDefinitions.varchar.codec;
|
|
134
|
+
const sqlIntCodec = sqlCodecDefinitions.int.codec;
|
|
135
|
+
const sqlFloatCodec = sqlCodecDefinitions.float.codec;
|
|
136
|
+
const sqlTextCodec = sqlCodecDefinitions.text.codec;
|
|
137
|
+
const sqlTimestampCodec = sqlCodecDefinitions.timestamp.codec;
|
|
138
|
+
|
|
139
|
+
// Create individual codec instances
|
|
140
|
+
const pgTextCodec = codec({
|
|
141
|
+
typeId: PG_TEXT_CODEC_ID,
|
|
142
|
+
targetTypes: ['text'],
|
|
143
|
+
traits: ['equality', 'order', 'textual'],
|
|
144
|
+
encode: (value: string): string => value,
|
|
145
|
+
decode: (wire: string): string => wire,
|
|
146
|
+
meta: {
|
|
147
|
+
db: {
|
|
148
|
+
sql: {
|
|
149
|
+
postgres: {
|
|
150
|
+
nativeType: 'text',
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const pgCharCodec = aliasCodec(sqlCharCodec, {
|
|
158
|
+
typeId: PG_CHAR_CODEC_ID,
|
|
159
|
+
targetTypes: ['character'],
|
|
160
|
+
meta: {
|
|
161
|
+
db: {
|
|
162
|
+
sql: {
|
|
163
|
+
postgres: {
|
|
164
|
+
nativeType: 'character',
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const pgVarcharCodec = aliasCodec(sqlVarcharCodec, {
|
|
172
|
+
typeId: PG_VARCHAR_CODEC_ID,
|
|
173
|
+
targetTypes: ['character varying'],
|
|
174
|
+
meta: {
|
|
175
|
+
db: {
|
|
176
|
+
sql: {
|
|
177
|
+
postgres: {
|
|
178
|
+
nativeType: 'character varying',
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const pgIntCodec = aliasCodec(sqlIntCodec, {
|
|
186
|
+
typeId: PG_INT_CODEC_ID,
|
|
187
|
+
targetTypes: ['int4'],
|
|
188
|
+
meta: {
|
|
189
|
+
db: {
|
|
190
|
+
sql: {
|
|
191
|
+
postgres: {
|
|
192
|
+
nativeType: 'integer',
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const pgFloatCodec = aliasCodec(sqlFloatCodec, {
|
|
200
|
+
typeId: PG_FLOAT_CODEC_ID,
|
|
201
|
+
targetTypes: ['float8'],
|
|
202
|
+
meta: {
|
|
203
|
+
db: {
|
|
204
|
+
sql: {
|
|
205
|
+
postgres: {
|
|
206
|
+
nativeType: 'double precision',
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
const pgInt4Codec = codec({
|
|
214
|
+
typeId: PG_INT4_CODEC_ID,
|
|
215
|
+
targetTypes: ['int4'],
|
|
216
|
+
traits: ['equality', 'order', 'numeric'],
|
|
217
|
+
encode: (value: number): number => value,
|
|
218
|
+
decode: (wire: number): number => wire,
|
|
219
|
+
meta: {
|
|
220
|
+
db: {
|
|
221
|
+
sql: {
|
|
222
|
+
postgres: {
|
|
223
|
+
nativeType: 'integer',
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const pgNumericCodec = codec<
|
|
231
|
+
typeof PG_NUMERIC_CODEC_ID,
|
|
232
|
+
readonly ['equality', 'order', 'numeric'],
|
|
233
|
+
string,
|
|
234
|
+
string
|
|
235
|
+
>({
|
|
236
|
+
typeId: PG_NUMERIC_CODEC_ID,
|
|
237
|
+
targetTypes: ['numeric', 'decimal'],
|
|
238
|
+
traits: ['equality', 'order', 'numeric'],
|
|
239
|
+
encode: (value: string): string => value,
|
|
240
|
+
decode: (wire: string | number): string => {
|
|
241
|
+
if (typeof wire === 'number') return String(wire);
|
|
242
|
+
return wire;
|
|
243
|
+
},
|
|
244
|
+
paramsSchema: numericParamsSchema,
|
|
245
|
+
renderOutputType: (typeParams) => {
|
|
246
|
+
const precision = typeParams['precision'];
|
|
247
|
+
if (precision === undefined) return undefined;
|
|
248
|
+
if (
|
|
249
|
+
typeof precision !== 'number' ||
|
|
250
|
+
!Number.isFinite(precision) ||
|
|
251
|
+
!Number.isInteger(precision)
|
|
252
|
+
) {
|
|
253
|
+
throw new Error(
|
|
254
|
+
`renderOutputType: expected integer "precision" in typeParams for Numeric, got ${String(precision)}`,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
const scale = typeParams['scale'];
|
|
258
|
+
return typeof scale === 'number' ? `Numeric<${precision}, ${scale}>` : `Numeric<${precision}>`;
|
|
259
|
+
},
|
|
260
|
+
meta: {
|
|
261
|
+
db: {
|
|
262
|
+
sql: {
|
|
263
|
+
postgres: {
|
|
264
|
+
nativeType: 'numeric',
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const pgInt2Codec = codec({
|
|
272
|
+
typeId: PG_INT2_CODEC_ID,
|
|
273
|
+
targetTypes: ['int2'],
|
|
274
|
+
traits: ['equality', 'order', 'numeric'],
|
|
275
|
+
encode: (value: number): number => value,
|
|
276
|
+
decode: (wire: number): number => wire,
|
|
277
|
+
meta: {
|
|
278
|
+
db: {
|
|
279
|
+
sql: {
|
|
280
|
+
postgres: {
|
|
281
|
+
nativeType: 'smallint',
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
const pgInt8Codec = codec({
|
|
289
|
+
typeId: PG_INT8_CODEC_ID,
|
|
290
|
+
targetTypes: ['int8'],
|
|
291
|
+
traits: ['equality', 'order', 'numeric'],
|
|
292
|
+
encode: (value: number): number => value,
|
|
293
|
+
decode: (wire: number): number => wire,
|
|
294
|
+
meta: {
|
|
295
|
+
db: {
|
|
296
|
+
sql: {
|
|
297
|
+
postgres: {
|
|
298
|
+
nativeType: 'bigint',
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const pgFloat4Codec = codec({
|
|
306
|
+
typeId: PG_FLOAT4_CODEC_ID,
|
|
307
|
+
targetTypes: ['float4'],
|
|
308
|
+
traits: ['equality', 'order', 'numeric'],
|
|
309
|
+
encode: (value: number): number => value,
|
|
310
|
+
decode: (wire: number): number => wire,
|
|
311
|
+
meta: {
|
|
312
|
+
db: {
|
|
313
|
+
sql: {
|
|
314
|
+
postgres: {
|
|
315
|
+
nativeType: 'real',
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
const pgFloat8Codec = codec({
|
|
323
|
+
typeId: PG_FLOAT8_CODEC_ID,
|
|
324
|
+
targetTypes: ['float8'],
|
|
325
|
+
traits: ['equality', 'order', 'numeric'],
|
|
326
|
+
encode: (value: number): number => value,
|
|
327
|
+
decode: (wire: number): number => wire,
|
|
328
|
+
meta: {
|
|
329
|
+
db: {
|
|
330
|
+
sql: {
|
|
331
|
+
postgres: {
|
|
332
|
+
nativeType: 'double precision',
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
const pgTimestampCodec = codec<
|
|
340
|
+
typeof PG_TIMESTAMP_CODEC_ID,
|
|
341
|
+
readonly ['equality', 'order'],
|
|
342
|
+
string | Date,
|
|
343
|
+
string | Date
|
|
344
|
+
>({
|
|
345
|
+
typeId: PG_TIMESTAMP_CODEC_ID,
|
|
346
|
+
targetTypes: ['timestamp'],
|
|
347
|
+
traits: ['equality', 'order'],
|
|
348
|
+
encode: (value: string | Date): string => {
|
|
349
|
+
if (value instanceof Date) return value.toISOString();
|
|
350
|
+
if (typeof value === 'string') return value;
|
|
351
|
+
return String(value);
|
|
352
|
+
},
|
|
353
|
+
decode: (wire: string | Date): string => {
|
|
354
|
+
if (wire instanceof Date) return wire.toISOString();
|
|
355
|
+
return wire;
|
|
356
|
+
},
|
|
357
|
+
encodeJson: (value: string | Date) => (value instanceof Date ? value.toISOString() : value),
|
|
358
|
+
decodeJson: (json) => {
|
|
359
|
+
if (typeof json !== 'string') {
|
|
360
|
+
throw new Error(`Expected ISO date string for pg/timestamp@1, got ${typeof json}`);
|
|
361
|
+
}
|
|
362
|
+
const date = new Date(json);
|
|
363
|
+
if (Number.isNaN(date.getTime())) {
|
|
364
|
+
throw new Error(`Invalid ISO date string for pg/timestamp@1: ${json}`);
|
|
365
|
+
}
|
|
366
|
+
return date;
|
|
367
|
+
},
|
|
368
|
+
paramsSchema: precisionParamsSchema,
|
|
369
|
+
renderOutputType: (typeParams) => renderPrecision('Timestamp', typeParams),
|
|
370
|
+
meta: {
|
|
371
|
+
db: {
|
|
372
|
+
sql: {
|
|
373
|
+
postgres: {
|
|
374
|
+
nativeType: 'timestamp without time zone',
|
|
375
|
+
},
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
const pgTimestamptzCodec = codec<
|
|
382
|
+
typeof PG_TIMESTAMPTZ_CODEC_ID,
|
|
383
|
+
readonly ['equality', 'order'],
|
|
384
|
+
string | Date,
|
|
385
|
+
string | Date
|
|
386
|
+
>({
|
|
387
|
+
typeId: PG_TIMESTAMPTZ_CODEC_ID,
|
|
388
|
+
targetTypes: ['timestamptz'],
|
|
389
|
+
traits: ['equality', 'order'],
|
|
390
|
+
encode: (value: string | Date): string => {
|
|
391
|
+
if (value instanceof Date) return value.toISOString();
|
|
392
|
+
if (typeof value === 'string') return value;
|
|
393
|
+
return String(value);
|
|
394
|
+
},
|
|
395
|
+
decode: (wire: string | Date): string => {
|
|
396
|
+
if (wire instanceof Date) return wire.toISOString();
|
|
397
|
+
return wire;
|
|
398
|
+
},
|
|
399
|
+
encodeJson: (value: string | Date) => (value instanceof Date ? value.toISOString() : value),
|
|
400
|
+
decodeJson: (json) => {
|
|
401
|
+
if (typeof json !== 'string') {
|
|
402
|
+
throw new Error(`Expected ISO date string for pg/timestamptz@1, got ${typeof json}`);
|
|
403
|
+
}
|
|
404
|
+
const date = new Date(json);
|
|
405
|
+
if (Number.isNaN(date.getTime())) {
|
|
406
|
+
throw new Error(`Invalid ISO date string for pg/timestamptz@1: ${json}`);
|
|
407
|
+
}
|
|
408
|
+
return date;
|
|
409
|
+
},
|
|
410
|
+
paramsSchema: precisionParamsSchema,
|
|
411
|
+
renderOutputType: (typeParams) => renderPrecision('Timestamptz', typeParams),
|
|
412
|
+
meta: {
|
|
413
|
+
db: {
|
|
414
|
+
sql: {
|
|
415
|
+
postgres: {
|
|
416
|
+
nativeType: 'timestamp with time zone',
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
const pgTimeCodec = codec<typeof PG_TIME_CODEC_ID, readonly ['equality', 'order'], string, string>({
|
|
424
|
+
typeId: PG_TIME_CODEC_ID,
|
|
425
|
+
targetTypes: ['time'],
|
|
426
|
+
traits: ['equality', 'order'],
|
|
427
|
+
encode: (value: string): string => value,
|
|
428
|
+
decode: (wire: string): string => wire,
|
|
429
|
+
paramsSchema: precisionParamsSchema,
|
|
430
|
+
renderOutputType: (typeParams) => renderPrecision('Time', typeParams),
|
|
431
|
+
meta: {
|
|
432
|
+
db: {
|
|
433
|
+
sql: {
|
|
434
|
+
postgres: {
|
|
435
|
+
nativeType: 'time',
|
|
436
|
+
},
|
|
437
|
+
},
|
|
438
|
+
},
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
const pgTimetzCodec = codec<
|
|
443
|
+
typeof PG_TIMETZ_CODEC_ID,
|
|
444
|
+
readonly ['equality', 'order'],
|
|
445
|
+
string,
|
|
446
|
+
string
|
|
447
|
+
>({
|
|
448
|
+
typeId: PG_TIMETZ_CODEC_ID,
|
|
449
|
+
targetTypes: ['timetz'],
|
|
450
|
+
traits: ['equality', 'order'],
|
|
451
|
+
encode: (value: string): string => value,
|
|
452
|
+
decode: (wire: string): string => wire,
|
|
453
|
+
paramsSchema: precisionParamsSchema,
|
|
454
|
+
renderOutputType: (typeParams) => renderPrecision('Timetz', typeParams),
|
|
455
|
+
meta: {
|
|
456
|
+
db: {
|
|
457
|
+
sql: {
|
|
458
|
+
postgres: {
|
|
459
|
+
nativeType: 'timetz',
|
|
460
|
+
},
|
|
461
|
+
},
|
|
462
|
+
},
|
|
463
|
+
},
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
const pgBoolCodec = codec({
|
|
467
|
+
typeId: PG_BOOL_CODEC_ID,
|
|
468
|
+
targetTypes: ['bool'],
|
|
469
|
+
traits: ['equality', 'boolean'],
|
|
470
|
+
encode: (value: boolean): boolean => value,
|
|
471
|
+
decode: (wire: boolean): boolean => wire,
|
|
472
|
+
meta: {
|
|
473
|
+
db: {
|
|
474
|
+
sql: {
|
|
475
|
+
postgres: {
|
|
476
|
+
nativeType: 'boolean',
|
|
477
|
+
},
|
|
478
|
+
},
|
|
479
|
+
},
|
|
480
|
+
},
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
const pgBitCodec = codec<typeof PG_BIT_CODEC_ID, readonly ['equality', 'order'], string, string>({
|
|
484
|
+
typeId: PG_BIT_CODEC_ID,
|
|
485
|
+
targetTypes: ['bit'],
|
|
486
|
+
traits: ['equality', 'order'],
|
|
487
|
+
encode: (value: string): string => value,
|
|
488
|
+
decode: (wire: string): string => wire,
|
|
489
|
+
paramsSchema: lengthParamsSchema,
|
|
490
|
+
renderOutputType: (typeParams) => renderLength('Bit', typeParams),
|
|
491
|
+
meta: {
|
|
492
|
+
db: {
|
|
493
|
+
sql: {
|
|
494
|
+
postgres: {
|
|
495
|
+
nativeType: 'bit',
|
|
496
|
+
},
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
},
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
const pgVarbitCodec = codec<
|
|
503
|
+
typeof PG_VARBIT_CODEC_ID,
|
|
504
|
+
readonly ['equality', 'order'],
|
|
505
|
+
string,
|
|
506
|
+
string
|
|
507
|
+
>({
|
|
508
|
+
typeId: PG_VARBIT_CODEC_ID,
|
|
509
|
+
targetTypes: ['bit varying'],
|
|
510
|
+
traits: ['equality', 'order'],
|
|
511
|
+
encode: (value: string): string => value,
|
|
512
|
+
decode: (wire: string): string => wire,
|
|
513
|
+
paramsSchema: lengthParamsSchema,
|
|
514
|
+
renderOutputType: (typeParams) => renderLength('VarBit', typeParams),
|
|
515
|
+
meta: {
|
|
516
|
+
db: {
|
|
517
|
+
sql: {
|
|
518
|
+
postgres: {
|
|
519
|
+
nativeType: 'bit varying',
|
|
520
|
+
},
|
|
521
|
+
},
|
|
522
|
+
},
|
|
523
|
+
},
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
const pgEnumCodec = codec({
|
|
527
|
+
typeId: PG_ENUM_CODEC_ID,
|
|
528
|
+
targetTypes: ['enum'],
|
|
529
|
+
traits: ['equality', 'order'],
|
|
530
|
+
encode: (value: string): string => value,
|
|
531
|
+
decode: (wire: string): string => wire,
|
|
532
|
+
renderOutputType: (typeParams) => {
|
|
533
|
+
const values = typeParams['values'];
|
|
534
|
+
if (!Array.isArray(values)) {
|
|
535
|
+
throw new Error(
|
|
536
|
+
`renderOutputType: expected array "values" in typeParams for enum, got ${typeof values}`,
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
return values
|
|
540
|
+
.map((value) => `'${String(value).replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`)
|
|
541
|
+
.join(' | ');
|
|
542
|
+
},
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
const pgIntervalCodec = codec<
|
|
546
|
+
typeof PG_INTERVAL_CODEC_ID,
|
|
547
|
+
readonly ['equality', 'order'],
|
|
548
|
+
string | Record<string, unknown>,
|
|
549
|
+
string
|
|
550
|
+
>({
|
|
551
|
+
typeId: PG_INTERVAL_CODEC_ID,
|
|
552
|
+
targetTypes: ['interval'],
|
|
553
|
+
traits: ['equality', 'order'],
|
|
554
|
+
encode: (value: string): string => value,
|
|
555
|
+
decode: (wire: string | Record<string, unknown>): string => {
|
|
556
|
+
if (typeof wire === 'string') return wire;
|
|
557
|
+
return JSON.stringify(wire);
|
|
558
|
+
},
|
|
559
|
+
paramsSchema: precisionParamsSchema,
|
|
560
|
+
renderOutputType: (typeParams) => renderPrecision('Interval', typeParams),
|
|
561
|
+
meta: {
|
|
562
|
+
db: {
|
|
563
|
+
sql: {
|
|
564
|
+
postgres: {
|
|
565
|
+
nativeType: 'interval',
|
|
566
|
+
},
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
},
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
const pgJsonCodec = codec({
|
|
573
|
+
typeId: PG_JSON_CODEC_ID,
|
|
574
|
+
targetTypes: ['json'],
|
|
575
|
+
traits: [],
|
|
576
|
+
encode: (value: string | JsonValue): string => JSON.stringify(value),
|
|
577
|
+
decode: (wire: string | JsonValue): JsonValue =>
|
|
578
|
+
typeof wire === 'string' ? JSON.parse(wire) : wire,
|
|
579
|
+
renderOutputType: renderJsonOutputType,
|
|
580
|
+
meta: {
|
|
581
|
+
db: {
|
|
582
|
+
sql: {
|
|
583
|
+
postgres: {
|
|
584
|
+
nativeType: 'json',
|
|
585
|
+
},
|
|
586
|
+
},
|
|
587
|
+
},
|
|
588
|
+
},
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
const pgJsonbCodec = codec({
|
|
592
|
+
typeId: PG_JSONB_CODEC_ID,
|
|
593
|
+
targetTypes: ['jsonb'],
|
|
594
|
+
traits: ['equality'],
|
|
595
|
+
encode: (value: string | JsonValue): string => JSON.stringify(value),
|
|
596
|
+
decode: (wire: string | JsonValue): JsonValue =>
|
|
597
|
+
typeof wire === 'string' ? JSON.parse(wire) : wire,
|
|
598
|
+
renderOutputType: renderJsonOutputType,
|
|
599
|
+
meta: {
|
|
600
|
+
db: {
|
|
601
|
+
sql: {
|
|
602
|
+
postgres: {
|
|
603
|
+
nativeType: 'jsonb',
|
|
604
|
+
},
|
|
605
|
+
},
|
|
606
|
+
},
|
|
607
|
+
},
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
// Build codec definitions using the builder DSL
|
|
611
|
+
const codecs = defineCodecs()
|
|
612
|
+
.add('char', sqlCharCodec)
|
|
613
|
+
.add('varchar', sqlVarcharCodec)
|
|
614
|
+
.add('int', sqlIntCodec)
|
|
615
|
+
.add('float', sqlFloatCodec)
|
|
616
|
+
.add('sql-text', sqlTextCodec)
|
|
617
|
+
.add('sql-timestamp', sqlTimestampCodec)
|
|
618
|
+
.add('text', pgTextCodec)
|
|
619
|
+
.add('character', pgCharCodec)
|
|
620
|
+
.add('character varying', pgVarcharCodec)
|
|
621
|
+
.add('integer', pgIntCodec)
|
|
622
|
+
.add('double precision', pgFloatCodec)
|
|
623
|
+
.add('int4', pgInt4Codec)
|
|
624
|
+
.add('int2', pgInt2Codec)
|
|
625
|
+
.add('int8', pgInt8Codec)
|
|
626
|
+
.add('float4', pgFloat4Codec)
|
|
627
|
+
.add('float8', pgFloat8Codec)
|
|
628
|
+
.add('numeric', pgNumericCodec)
|
|
629
|
+
.add('timestamp', pgTimestampCodec)
|
|
630
|
+
.add('timestamptz', pgTimestamptzCodec)
|
|
631
|
+
.add('time', pgTimeCodec)
|
|
632
|
+
.add('timetz', pgTimetzCodec)
|
|
633
|
+
.add('bool', pgBoolCodec)
|
|
634
|
+
.add('bit', pgBitCodec)
|
|
635
|
+
.add('bit varying', pgVarbitCodec)
|
|
636
|
+
.add('interval', pgIntervalCodec)
|
|
637
|
+
.add('enum', pgEnumCodec)
|
|
638
|
+
.add('json', pgJsonCodec)
|
|
639
|
+
.add('jsonb', pgJsonbCodec);
|
|
640
|
+
|
|
641
|
+
// Export derived structures directly from codecs builder
|
|
642
|
+
export const codecDefinitions = codecs.codecDefinitions;
|
|
643
|
+
export const dataTypes = codecs.dataTypes;
|
|
644
|
+
|
|
645
|
+
export type CodecTypes = typeof codecs.CodecTypes;
|