@prisma-next/adapter-postgres 0.3.0-dev.33 → 0.3.0-dev.36
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/README.md +64 -2
- package/dist/adapter-DB1CK2jM.mjs +265 -0
- package/dist/adapter-DB1CK2jM.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-DcC1nPzh.mjs +206 -0
- package/dist/codecs-DcC1nPzh.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 +405 -0
- package/dist/control.mjs.map +1 -0
- package/dist/descriptor-meta-D7pxo-wo.mjs +996 -0
- package/dist/descriptor-meta-D7pxo-wo.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/types-BY395pUv.d.mts +19 -0
- package/dist/types-BY395pUv.d.mts.map +1 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +1 -0
- package/package.json +32 -41
- package/src/core/adapter.ts +90 -17
- package/src/core/codec-ids.ts +28 -0
- package/src/core/codecs.ts +316 -19
- package/src/core/control-adapter.ts +341 -180
- package/src/core/default-normalizer.ts +77 -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-Y6L4BBLR.js +0 -309
- package/dist/chunk-Y6L4BBLR.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
package/README.md
CHANGED
|
@@ -27,6 +27,7 @@ Provide PostgreSQL-specific adapter implementation, codecs, and capabilities. En
|
|
|
27
27
|
- **Codec Definitions**: Define PostgreSQL codecs for type conversion
|
|
28
28
|
- Wire format to JavaScript type decoding
|
|
29
29
|
- JavaScript type to wire format encoding
|
|
30
|
+
- **Storage Type Control Hooks**: Provide control-plane hooks for contract-defined storage types (e.g., enums)
|
|
30
31
|
- **Codec Types**: Export TypeScript types for PostgreSQL codecs
|
|
31
32
|
- **Descriptors**: Provide adapter descriptors declaring capabilities and codec type imports
|
|
32
33
|
|
|
@@ -97,7 +98,11 @@ flowchart TD
|
|
|
97
98
|
**Codecs (`codecs.ts`)**
|
|
98
99
|
- PostgreSQL codec definitions
|
|
99
100
|
- Type conversion between wire format and JavaScript
|
|
100
|
-
-
|
|
101
|
+
- SQL base codecs: `sql/char`, `sql/varchar`, `sql/int`, `sql/float`
|
|
102
|
+
- PostgreSQL aliases for base codecs: `pg/char`, `pg/varchar`, `pg/int`, `pg/float`
|
|
103
|
+
- Supports PostgreSQL types: `int2`, `int4`, `int8`, `float4`, `float8`, `text`, `bool`, `enum`
|
|
104
|
+
- Supports PostgreSQL types: `int2`, `int4`, `int8`, `float4`, `float8`, `text`, `timestamp`, `timestamptz`, `bool`, `enum`, `json`, `jsonb`
|
|
105
|
+
- Parameterized types: `character(n)`, `character varying(n)`, `numeric(p,s)`, `bit(n)`, `bit varying(n)`, `timestamp(p)`, `timestamptz(p)`, `time(p)`, `timetz(p)`, `interval(p)`
|
|
101
106
|
|
|
102
107
|
**Types (`types.ts`)**
|
|
103
108
|
- PostgreSQL-specific types and utilities
|
|
@@ -122,6 +127,15 @@ flowchart TD
|
|
|
122
127
|
**Types Export (`types.ts`)**
|
|
123
128
|
- Re-exports PostgreSQL-specific types
|
|
124
129
|
|
|
130
|
+
**Column Types Export (`column-types.ts`)**
|
|
131
|
+
- Exports column descriptors for built-in types and enum helpers (`enumType`, `enumColumn(typeRef, nativeType)`)
|
|
132
|
+
- Parameterized helpers: `charColumn(length)`, `varcharColumn(length)`, `numericColumn(precision, scale?)`, `bitColumn(length)`, `varbitColumn(length)`, `timeColumn(precision?)`, `timetzColumn(precision?)`, `intervalColumn(precision?)`
|
|
133
|
+
|
|
134
|
+
- Exports JSON helpers:
|
|
135
|
+
- `jsonColumn`, `jsonbColumn`
|
|
136
|
+
- `json(schema?)`, `jsonb(schema?)` where `schema` is a Standard Schema value (e.g., Arktype)
|
|
137
|
+
- When a schema is provided, `typeParams` metadata is derived from the schema's `~standard` interface
|
|
138
|
+
|
|
125
139
|
## Dependencies
|
|
126
140
|
|
|
127
141
|
- **`@prisma-next/sql-contract`**: SQL contract types
|
|
@@ -142,6 +156,7 @@ flowchart TD
|
|
|
142
156
|
- [ADR 068 - Error mapping to RuntimeError](../../../../docs/architecture%20docs/adrs/ADR%20068%20-%20Error%20mapping%20to%20RuntimeError.md)
|
|
143
157
|
- [ADR 112 - Target Extension Packs](../../../../docs/architecture%20docs/adrs/ADR%20112%20-%20Target%20Extension%20Packs.md)
|
|
144
158
|
- [ADR 114 - Extension codecs & branded types](../../../../docs/architecture%20docs/adrs/ADR%20114%20-%20Extension%20codecs%20&%20branded%20types.md)
|
|
159
|
+
- [ADR 159 - Postgres JSON and JSONB typed columns](../../../../docs/architecture%20docs/adrs/ADR%20159%20-%20Postgres%20JSON%20and%20JSONB%20typed%20columns.md)
|
|
145
160
|
|
|
146
161
|
## Usage
|
|
147
162
|
|
|
@@ -180,6 +195,7 @@ The adapter declares the following PostgreSQL capabilities:
|
|
|
180
195
|
- **`lateral: true`** - Supports LATERAL joins for `includeMany` nested array includes
|
|
181
196
|
- **`jsonAgg: true`** - Supports JSON aggregation functions (`json_agg`) for `includeMany`
|
|
182
197
|
- **`returning: true`** - Supports RETURNING clauses for DML operations (INSERT, UPDATE, DELETE)
|
|
198
|
+
- **`sql.enums: true`** - Supports contract-defined enum storage types
|
|
183
199
|
|
|
184
200
|
**Important**: Capabilities must be declared in **both** places:
|
|
185
201
|
|
|
@@ -241,10 +257,56 @@ DELETE FROM "user" WHERE "user"."id" = $1 RETURNING "user"."id", "user"."email"
|
|
|
241
257
|
|
|
242
258
|
**Note:** MySQL does not support RETURNING clauses. A future MySQL adapter would declare `returning: false` and either reject plans with RETURNING or provide an alternative implementation.
|
|
243
259
|
|
|
260
|
+
## JSON and JSONB support
|
|
261
|
+
|
|
262
|
+
The adapter supports PostgreSQL-native `json` and `jsonb` columns.
|
|
263
|
+
|
|
264
|
+
### Value semantics
|
|
265
|
+
|
|
266
|
+
Both `json` and `jsonb` accept any valid JSON value:
|
|
267
|
+
|
|
268
|
+
- object
|
|
269
|
+
- array
|
|
270
|
+
- string
|
|
271
|
+
- number
|
|
272
|
+
- boolean
|
|
273
|
+
- JSON `null` (distinct from SQL `NULL`)
|
|
274
|
+
|
|
275
|
+
`jsonb` uses normalized binary storage, so whitespace and object key order are not preserved.
|
|
276
|
+
|
|
277
|
+
### Authoring helpers
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
import { json, jsonb } from '@prisma-next/adapter-postgres/column-types';
|
|
281
|
+
import { type as arktype } from 'arktype';
|
|
282
|
+
|
|
283
|
+
const auditPayloadSchema = arktype({
|
|
284
|
+
action: 'string',
|
|
285
|
+
actorId: 'number',
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
table('event', (t) =>
|
|
289
|
+
t
|
|
290
|
+
.column('payload', { type: jsonb(auditPayloadSchema), nullable: false })
|
|
291
|
+
.column('raw', { type: json(), nullable: true }),
|
|
292
|
+
);
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Typed fallback behavior
|
|
296
|
+
|
|
297
|
+
- If a schema value is provided, emitted `contract.d.ts` derives a concrete type from that schema.
|
|
298
|
+
- If no schema is provided, emitted type falls back to `JsonValue`.
|
|
299
|
+
- Runtime values still encode/decode as JSON-compatible values.
|
|
300
|
+
|
|
301
|
+
### Standard Schema integration
|
|
302
|
+
|
|
303
|
+
`json(schema)` and `jsonb(schema)` accept Standard Schema values. Arktype schemas work out of the box via their Standard Schema adapter (`schema['~standard']`).
|
|
304
|
+
|
|
244
305
|
## Exports
|
|
245
306
|
|
|
246
307
|
- `./adapter`: Adapter implementation (`createPostgresAdapter`)
|
|
247
|
-
- `./codec-types`: PostgreSQL codec types (`CodecTypes`, `dataTypes`)
|
|
308
|
+
- `./codec-types`: PostgreSQL codec types (`CodecTypes`, `JsonValue`, `dataTypes`)
|
|
309
|
+
- `./column-types`: Column type descriptors and authoring helpers (`json`, `jsonb`, `jsonColumn`, `jsonbColumn`, `enumType`, `enumColumn`, `textColumn`, `int4Column`, etc.)
|
|
248
310
|
- `./types`: PostgreSQL-specific types
|
|
249
311
|
- `./control`: Control-plane entry point (adapter descriptor)
|
|
250
312
|
- `./runtime`: Runtime-plane entry point (runtime adapter descriptor)
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { m as PG_JSON_CODEC_ID, p as PG_JSONB_CODEC_ID } from "./codec-ids-Bsm9c7ns.mjs";
|
|
2
|
+
import { t as codecDefinitions } from "./codecs-DcC1nPzh.mjs";
|
|
3
|
+
import { createCodecRegistry, isOperationExpr } from "@prisma-next/sql-relational-core/ast";
|
|
4
|
+
import { ifDefined } from "@prisma-next/utils/defined";
|
|
5
|
+
|
|
6
|
+
//#region src/core/adapter.ts
|
|
7
|
+
const VECTOR_CODEC_ID = "pg/vector@1";
|
|
8
|
+
function getCodecParamCast(codecId) {
|
|
9
|
+
if (codecId === VECTOR_CODEC_ID) return "vector";
|
|
10
|
+
if (codecId === PG_JSON_CODEC_ID) return "json";
|
|
11
|
+
if (codecId === PG_JSONB_CODEC_ID) return "jsonb";
|
|
12
|
+
}
|
|
13
|
+
function renderTypedParam(index, codecId) {
|
|
14
|
+
const cast = getCodecParamCast(codecId);
|
|
15
|
+
return cast ? `$${index}::${cast}` : `$${index}`;
|
|
16
|
+
}
|
|
17
|
+
const defaultCapabilities = Object.freeze({
|
|
18
|
+
postgres: {
|
|
19
|
+
orderBy: true,
|
|
20
|
+
limit: true,
|
|
21
|
+
lateral: true,
|
|
22
|
+
jsonAgg: true,
|
|
23
|
+
returning: true
|
|
24
|
+
},
|
|
25
|
+
sql: { enums: true }
|
|
26
|
+
});
|
|
27
|
+
const parameterizedCodecs = Object.values(codecDefinitions).map((definition) => definition.codec).filter((codec$1) => codec$1.paramsSchema !== void 0).map((codec$1) => Object.freeze({
|
|
28
|
+
codecId: codec$1.id,
|
|
29
|
+
paramsSchema: codec$1.paramsSchema,
|
|
30
|
+
...ifDefined("init", codec$1.init)
|
|
31
|
+
}));
|
|
32
|
+
var PostgresAdapterImpl = class {
|
|
33
|
+
familyId = "sql";
|
|
34
|
+
targetId = "postgres";
|
|
35
|
+
profile;
|
|
36
|
+
codecRegistry = (() => {
|
|
37
|
+
const registry = createCodecRegistry();
|
|
38
|
+
for (const definition of Object.values(codecDefinitions)) registry.register(definition.codec);
|
|
39
|
+
return registry;
|
|
40
|
+
})();
|
|
41
|
+
constructor(options) {
|
|
42
|
+
this.profile = Object.freeze({
|
|
43
|
+
id: options?.profileId ?? "postgres/default@1",
|
|
44
|
+
target: "postgres",
|
|
45
|
+
capabilities: defaultCapabilities,
|
|
46
|
+
codecs: () => this.codecRegistry
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
parameterizedCodecs() {
|
|
50
|
+
return parameterizedCodecs;
|
|
51
|
+
}
|
|
52
|
+
lower(ast, context) {
|
|
53
|
+
let sql;
|
|
54
|
+
const params = context.params ? [...context.params] : [];
|
|
55
|
+
if (ast.kind === "select") sql = renderSelect(ast, context.contract);
|
|
56
|
+
else if (ast.kind === "insert") sql = renderInsert(ast, context.contract);
|
|
57
|
+
else if (ast.kind === "update") sql = renderUpdate(ast, context.contract);
|
|
58
|
+
else if (ast.kind === "delete") sql = renderDelete(ast, context.contract);
|
|
59
|
+
else throw new Error(`Unsupported AST kind: ${ast.kind}`);
|
|
60
|
+
return Object.freeze({
|
|
61
|
+
profileId: this.profile.id,
|
|
62
|
+
body: Object.freeze({
|
|
63
|
+
sql,
|
|
64
|
+
params
|
|
65
|
+
})
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function renderSelect(ast, contract) {
|
|
70
|
+
const selectClause = `SELECT ${renderProjection(ast, contract)}`;
|
|
71
|
+
const fromClause = `FROM ${quoteIdentifier(ast.from.name)}`;
|
|
72
|
+
const joinsClause = ast.joins?.length ? ast.joins.map((join) => renderJoin(join, contract)).join(" ") : "";
|
|
73
|
+
const includesClause = ast.includes?.length ? ast.includes.map((include) => renderInclude(include, contract)).join(" ") : "";
|
|
74
|
+
const whereClause = ast.where ? ` WHERE ${renderWhere(ast.where, contract)}` : "";
|
|
75
|
+
const orderClause = ast.orderBy?.length ? ` ORDER BY ${ast.orderBy.map((order) => {
|
|
76
|
+
return `${renderExpr(order.expr, contract)} ${order.dir.toUpperCase()}`;
|
|
77
|
+
}).join(", ")}` : "";
|
|
78
|
+
const limitClause = typeof ast.limit === "number" ? ` LIMIT ${ast.limit}` : "";
|
|
79
|
+
const clauses = [joinsClause, includesClause].filter(Boolean).join(" ");
|
|
80
|
+
return `${selectClause} ${fromClause}${clauses ? ` ${clauses}` : ""}${whereClause}${orderClause}${limitClause}`.trim();
|
|
81
|
+
}
|
|
82
|
+
function renderProjection(ast, contract) {
|
|
83
|
+
return ast.project.map((item) => {
|
|
84
|
+
const expr = item.expr;
|
|
85
|
+
if (expr.kind === "includeRef") return `${quoteIdentifier(`${expr.alias}_lateral`)}.${quoteIdentifier(expr.alias)} AS ${quoteIdentifier(item.alias)}`;
|
|
86
|
+
if (expr.kind === "operation") return `${renderOperation(expr, contract)} AS ${quoteIdentifier(item.alias)}`;
|
|
87
|
+
if (expr.kind === "literal") return `${renderLiteral(expr)} AS ${quoteIdentifier(item.alias)}`;
|
|
88
|
+
return `${renderColumn(expr)} AS ${quoteIdentifier(item.alias)}`;
|
|
89
|
+
}).join(", ");
|
|
90
|
+
}
|
|
91
|
+
function renderWhere(expr, contract) {
|
|
92
|
+
if (expr.kind === "exists") return `${expr.not ? "NOT " : ""}EXISTS (${renderSelect(expr.subquery, contract)})`;
|
|
93
|
+
if (expr.kind === "nullCheck") return renderNullCheck(expr, contract);
|
|
94
|
+
if (expr.kind === "and") {
|
|
95
|
+
if (expr.exprs.length === 0) return "TRUE";
|
|
96
|
+
return `(${expr.exprs.map((part) => renderWhere(part, contract)).join(" AND ")})`;
|
|
97
|
+
}
|
|
98
|
+
if (expr.kind === "or") {
|
|
99
|
+
if (expr.exprs.length === 0) return "FALSE";
|
|
100
|
+
return `(${expr.exprs.map((part) => renderWhere(part, contract)).join(" OR ")})`;
|
|
101
|
+
}
|
|
102
|
+
return renderBinary(expr, contract);
|
|
103
|
+
}
|
|
104
|
+
function renderNullCheck(expr, contract) {
|
|
105
|
+
const rendered = renderExpr(expr.expr, contract);
|
|
106
|
+
const renderedExpr = isOperationExpr(expr.expr) ? `(${rendered})` : rendered;
|
|
107
|
+
return expr.isNull ? `${renderedExpr} IS NULL` : `${renderedExpr} IS NOT NULL`;
|
|
108
|
+
}
|
|
109
|
+
function renderBinary(expr, contract) {
|
|
110
|
+
const leftExpr = expr.left;
|
|
111
|
+
const left = renderExpr(leftExpr, contract);
|
|
112
|
+
const leftRendered = isOperationExpr(leftExpr) ? `(${left})` : left;
|
|
113
|
+
const right = renderBinaryRight(expr.right, contract);
|
|
114
|
+
return `${leftRendered} ${{
|
|
115
|
+
eq: "=",
|
|
116
|
+
neq: "!=",
|
|
117
|
+
gt: ">",
|
|
118
|
+
lt: "<",
|
|
119
|
+
gte: ">=",
|
|
120
|
+
lte: "<=",
|
|
121
|
+
like: "LIKE",
|
|
122
|
+
ilike: "ILIKE",
|
|
123
|
+
in: "IN",
|
|
124
|
+
notIn: "NOT IN"
|
|
125
|
+
}[expr.op]} ${right}`;
|
|
126
|
+
}
|
|
127
|
+
function renderBinaryRight(right, contract) {
|
|
128
|
+
if (right.kind === "col") return renderColumn(right);
|
|
129
|
+
if (right.kind === "param") return renderParam(right, contract);
|
|
130
|
+
if (right.kind === "literal") return renderLiteral(right);
|
|
131
|
+
if (right.kind === "operation") return renderExpr(right, contract);
|
|
132
|
+
if (right.kind === "listLiteral") {
|
|
133
|
+
if (right.values.length === 0) return "(NULL)";
|
|
134
|
+
return `(${right.values.map((value) => value.kind === "param" ? renderParam(value, contract) : renderLiteral(value)).join(", ")})`;
|
|
135
|
+
}
|
|
136
|
+
throw new Error(`Unsupported binary right expression kind: ${right.kind}`);
|
|
137
|
+
}
|
|
138
|
+
function renderColumn(ref) {
|
|
139
|
+
return `${quoteIdentifier(ref.table)}.${quoteIdentifier(ref.column)}`;
|
|
140
|
+
}
|
|
141
|
+
function renderExpr(expr, contract) {
|
|
142
|
+
if (isOperationExpr(expr)) return renderOperation(expr, contract);
|
|
143
|
+
return renderColumn(expr);
|
|
144
|
+
}
|
|
145
|
+
function renderParam(ref, contract, tableName, columnName) {
|
|
146
|
+
if (contract && tableName && columnName) {
|
|
147
|
+
const columnMeta = contract.storage.tables[tableName]?.columns[columnName];
|
|
148
|
+
return renderTypedParam(ref.index, columnMeta?.codecId);
|
|
149
|
+
}
|
|
150
|
+
return `$${ref.index}`;
|
|
151
|
+
}
|
|
152
|
+
function renderLiteral(expr) {
|
|
153
|
+
if (typeof expr.value === "string") return `'${expr.value.replace(/'/g, "''")}'`;
|
|
154
|
+
if (typeof expr.value === "number" || typeof expr.value === "boolean") return String(expr.value);
|
|
155
|
+
if (expr.value === null) return "NULL";
|
|
156
|
+
if (Array.isArray(expr.value)) return `ARRAY[${expr.value.map((v) => renderLiteral({
|
|
157
|
+
kind: "literal",
|
|
158
|
+
value: v
|
|
159
|
+
})).join(", ")}]`;
|
|
160
|
+
return JSON.stringify(expr.value);
|
|
161
|
+
}
|
|
162
|
+
function renderOperation(expr, contract) {
|
|
163
|
+
const self = renderExpr(expr.self, contract);
|
|
164
|
+
const isVectorOperation = expr.forTypeId === VECTOR_CODEC_ID;
|
|
165
|
+
const args = expr.args.map((arg) => {
|
|
166
|
+
if (arg.kind === "col") return renderColumn(arg);
|
|
167
|
+
if (arg.kind === "param") return isVectorOperation ? `$${arg.index}::vector` : renderParam(arg, contract);
|
|
168
|
+
if (arg.kind === "literal") return renderLiteral(arg);
|
|
169
|
+
if (arg.kind === "operation") return renderOperation(arg, contract);
|
|
170
|
+
const _exhaustive = arg;
|
|
171
|
+
throw new Error(`Unsupported argument kind: ${_exhaustive.kind}`);
|
|
172
|
+
});
|
|
173
|
+
let result = expr.lowering.template;
|
|
174
|
+
result = result.replace(/\$\{self\}/g, self);
|
|
175
|
+
for (let i = 0; i < args.length; i++) result = result.replace(new RegExp(`\\$\\{arg${i}\\}`, "g"), args[i] ?? "");
|
|
176
|
+
if (expr.lowering.strategy === "function") return result;
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
function renderJoin(join, _contract) {
|
|
180
|
+
return `${join.joinType.toUpperCase()} JOIN ${quoteIdentifier(join.table.name)} ON ${renderJoinOn(join.on)}`;
|
|
181
|
+
}
|
|
182
|
+
function renderJoinOn(on) {
|
|
183
|
+
if (on.kind === "eqCol") return `${renderColumn(on.left)} = ${renderColumn(on.right)}`;
|
|
184
|
+
throw new Error(`Unsupported join ON expression kind: ${on.kind}`);
|
|
185
|
+
}
|
|
186
|
+
function renderInclude(include, contract) {
|
|
187
|
+
const alias = include.alias;
|
|
188
|
+
const jsonBuildObject = `json_build_object(${include.child.project.map((item) => {
|
|
189
|
+
const expr = renderExpr(item.expr, contract);
|
|
190
|
+
return `'${item.alias}', ${expr}`;
|
|
191
|
+
}).join(", ")})`;
|
|
192
|
+
let whereClause = ` WHERE ${renderJoinOn(include.child.on)}`;
|
|
193
|
+
if (include.child.where) whereClause += ` AND ${renderWhere(include.child.where, contract)}`;
|
|
194
|
+
const childOrderBy = include.child.orderBy?.length ? ` ORDER BY ${include.child.orderBy.map((order) => `${renderExpr(order.expr, contract)} ${order.dir.toUpperCase()}`).join(", ")}` : "";
|
|
195
|
+
const childLimit = typeof include.child.limit === "number" ? ` LIMIT ${include.child.limit}` : "";
|
|
196
|
+
const childTable = quoteIdentifier(include.child.table.name);
|
|
197
|
+
let subquery;
|
|
198
|
+
if (typeof include.child.limit === "number") {
|
|
199
|
+
const columnAliasMap = /* @__PURE__ */ new Map();
|
|
200
|
+
for (const item of include.child.project) if (item.expr.kind === "col") {
|
|
201
|
+
const columnKey = `${item.expr.table}.${item.expr.column}`;
|
|
202
|
+
columnAliasMap.set(columnKey, item.alias);
|
|
203
|
+
}
|
|
204
|
+
const innerColumns = include.child.project.map((item) => {
|
|
205
|
+
return `${renderExpr(item.expr, contract)} AS ${quoteIdentifier(item.alias)}`;
|
|
206
|
+
}).join(", ");
|
|
207
|
+
const childOrderByWithAliases = include.child.orderBy?.length ? ` ORDER BY ${include.child.orderBy.map((order) => {
|
|
208
|
+
if (order.expr.kind === "col") {
|
|
209
|
+
const columnKey = `${order.expr.table}.${order.expr.column}`;
|
|
210
|
+
const alias$1 = columnAliasMap.get(columnKey);
|
|
211
|
+
if (alias$1) return `${quoteIdentifier(alias$1)} ${order.dir.toUpperCase()}`;
|
|
212
|
+
}
|
|
213
|
+
return `${renderExpr(order.expr, contract)} ${order.dir.toUpperCase()}`;
|
|
214
|
+
}).join(", ")}` : "";
|
|
215
|
+
const innerSelect = `SELECT ${innerColumns} FROM ${childTable}${whereClause}${childOrderByWithAliases}${childLimit}`;
|
|
216
|
+
subquery = `(SELECT json_agg(row_to_json(sub.*)) AS ${quoteIdentifier(alias)} FROM (${innerSelect}) sub)`;
|
|
217
|
+
} else if (childOrderBy) subquery = `(SELECT json_agg(${jsonBuildObject}${childOrderBy}) AS ${quoteIdentifier(alias)} FROM ${childTable}${whereClause})`;
|
|
218
|
+
else subquery = `(SELECT json_agg(${jsonBuildObject}) AS ${quoteIdentifier(alias)} FROM ${childTable}${whereClause})`;
|
|
219
|
+
const tableAlias = `${alias}_lateral`;
|
|
220
|
+
return `LEFT JOIN LATERAL ${subquery} AS ${quoteIdentifier(tableAlias)} ON true`;
|
|
221
|
+
}
|
|
222
|
+
function quoteIdentifier(identifier) {
|
|
223
|
+
return `"${identifier.replace(/"/g, "\"\"")}"`;
|
|
224
|
+
}
|
|
225
|
+
function renderInsert(ast, contract) {
|
|
226
|
+
const table = quoteIdentifier(ast.table.name);
|
|
227
|
+
const columns = Object.keys(ast.values).map((col) => quoteIdentifier(col));
|
|
228
|
+
const tableMeta = contract.storage.tables[ast.table.name];
|
|
229
|
+
const values = Object.entries(ast.values).map(([colName, val]) => {
|
|
230
|
+
if (val.kind === "param") {
|
|
231
|
+
const columnMeta = tableMeta?.columns[colName];
|
|
232
|
+
return renderTypedParam(val.index, columnMeta?.codecId);
|
|
233
|
+
}
|
|
234
|
+
if (val.kind === "col") return `${quoteIdentifier(val.table)}.${quoteIdentifier(val.column)}`;
|
|
235
|
+
throw new Error(`Unsupported value kind in INSERT: ${val.kind}`);
|
|
236
|
+
});
|
|
237
|
+
return `${`INSERT INTO ${table} (${columns.join(", ")}) VALUES (${values.join(", ")})`}${ast.returning?.length ? ` RETURNING ${ast.returning.map((col) => `${quoteIdentifier(col.table)}.${quoteIdentifier(col.column)}`).join(", ")}` : ""}`;
|
|
238
|
+
}
|
|
239
|
+
function renderUpdate(ast, contract) {
|
|
240
|
+
const table = quoteIdentifier(ast.table.name);
|
|
241
|
+
const tableMeta = contract.storage.tables[ast.table.name];
|
|
242
|
+
const setClauses = Object.entries(ast.set).map(([col, val]) => {
|
|
243
|
+
const column = quoteIdentifier(col);
|
|
244
|
+
let value;
|
|
245
|
+
if (val.kind === "param") {
|
|
246
|
+
const columnMeta = tableMeta?.columns[col];
|
|
247
|
+
value = renderTypedParam(val.index, columnMeta?.codecId);
|
|
248
|
+
} else if (val.kind === "col") value = `${quoteIdentifier(val.table)}.${quoteIdentifier(val.column)}`;
|
|
249
|
+
else throw new Error(`Unsupported value kind in UPDATE: ${val.kind}`);
|
|
250
|
+
return `${column} = ${value}`;
|
|
251
|
+
});
|
|
252
|
+
const whereClause = ` WHERE ${renderWhere(ast.where, contract)}`;
|
|
253
|
+
const returningClause = ast.returning?.length ? ` RETURNING ${ast.returning.map((col) => `${quoteIdentifier(col.table)}.${quoteIdentifier(col.column)}`).join(", ")}` : "";
|
|
254
|
+
return `UPDATE ${table} SET ${setClauses.join(", ")}${whereClause}${returningClause}`;
|
|
255
|
+
}
|
|
256
|
+
function renderDelete(ast, contract) {
|
|
257
|
+
return `DELETE FROM ${quoteIdentifier(ast.table.name)}${` WHERE ${renderWhere(ast.where, contract)}`}${ast.returning?.length ? ` RETURNING ${ast.returning.map((col) => `${quoteIdentifier(col.table)}.${quoteIdentifier(col.column)}`).join(", ")}` : ""}`;
|
|
258
|
+
}
|
|
259
|
+
function createPostgresAdapter(options) {
|
|
260
|
+
return Object.freeze(new PostgresAdapterImpl(options));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
//#endregion
|
|
264
|
+
export { createPostgresAdapter as t };
|
|
265
|
+
//# sourceMappingURL=adapter-DB1CK2jM.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter-DB1CK2jM.mjs","names":["parameterizedCodecs: ReadonlyArray<CodecParamsDescriptor>","codec","sql: string","_exhaustive: never","subquery: string","alias","value: string"],"sources":["../src/core/adapter.ts"],"sourcesContent":["import type {\n Adapter,\n AdapterProfile,\n BinaryExpr,\n CodecParamsDescriptor,\n ColumnRef,\n DeleteAst,\n IncludeRef,\n InsertAst,\n JoinAst,\n LiteralExpr,\n LowererContext,\n NullCheckExpr,\n OperationExpr,\n ParamRef,\n QueryAst,\n SelectAst,\n UpdateAst,\n WhereExpr,\n} from '@prisma-next/sql-relational-core/ast';\nimport { createCodecRegistry, isOperationExpr } from '@prisma-next/sql-relational-core/ast';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport { PG_JSON_CODEC_ID, PG_JSONB_CODEC_ID } from './codec-ids';\nimport { codecDefinitions } from './codecs';\nimport type { PostgresAdapterOptions, PostgresContract, PostgresLoweredStatement } from './types';\n\nconst VECTOR_CODEC_ID = 'pg/vector@1' as const;\n\nfunction getCodecParamCast(codecId: string | undefined): string | undefined {\n if (codecId === VECTOR_CODEC_ID) {\n return 'vector';\n }\n if (codecId === PG_JSON_CODEC_ID) {\n return 'json';\n }\n if (codecId === PG_JSONB_CODEC_ID) {\n return 'jsonb';\n }\n return undefined;\n}\n\nfunction renderTypedParam(index: number, codecId: string | undefined): string {\n const cast = getCodecParamCast(codecId);\n return cast ? `$${index}::${cast}` : `$${index}`;\n}\n\nconst defaultCapabilities = Object.freeze({\n postgres: {\n orderBy: true,\n limit: true,\n lateral: true,\n jsonAgg: true,\n returning: true,\n },\n sql: {\n enums: true,\n },\n});\n\ntype AdapterCodec = (typeof codecDefinitions)[keyof typeof codecDefinitions]['codec'];\ntype ParameterizedCodec = AdapterCodec & {\n readonly paramsSchema: NonNullable<AdapterCodec['paramsSchema']>;\n};\n\nconst parameterizedCodecs: ReadonlyArray<CodecParamsDescriptor> = Object.values(codecDefinitions)\n .map((definition) => definition.codec)\n .filter((codec): codec is ParameterizedCodec => codec.paramsSchema !== undefined)\n .map((codec) =>\n Object.freeze({\n codecId: codec.id,\n paramsSchema: codec.paramsSchema,\n ...ifDefined('init', codec.init),\n }),\n );\n\nclass PostgresAdapterImpl implements Adapter<QueryAst, PostgresContract, PostgresLoweredStatement> {\n // These fields make the adapter instance structurally compatible with\n // RuntimeAdapterInstance<'sql', 'postgres'> without introducing a runtime-plane dependency.\n readonly familyId = 'sql' as const;\n readonly targetId = 'postgres' as const;\n\n readonly profile: AdapterProfile<'postgres'>;\n private readonly codecRegistry = (() => {\n const registry = createCodecRegistry();\n for (const definition of Object.values(codecDefinitions)) {\n registry.register(definition.codec);\n }\n return registry;\n })();\n\n constructor(options?: PostgresAdapterOptions) {\n this.profile = Object.freeze({\n id: options?.profileId ?? 'postgres/default@1',\n target: 'postgres',\n capabilities: defaultCapabilities,\n codecs: () => this.codecRegistry,\n });\n }\n\n parameterizedCodecs(): ReadonlyArray<CodecParamsDescriptor> {\n return parameterizedCodecs;\n }\n\n lower(ast: QueryAst, context: LowererContext<PostgresContract>) {\n let sql: string;\n const params = context.params ? [...context.params] : [];\n\n if (ast.kind === 'select') {\n sql = renderSelect(ast, context.contract);\n } else if (ast.kind === 'insert') {\n sql = renderInsert(ast, context.contract);\n } else if (ast.kind === 'update') {\n sql = renderUpdate(ast, context.contract);\n } else if (ast.kind === 'delete') {\n sql = renderDelete(ast, context.contract);\n } else {\n throw new Error(`Unsupported AST kind: ${(ast as { kind: string }).kind}`);\n }\n\n return Object.freeze({\n profileId: this.profile.id,\n body: Object.freeze({ sql, params }),\n });\n }\n}\n\nfunction renderSelect(ast: SelectAst, contract?: PostgresContract): string {\n const selectClause = `SELECT ${renderProjection(ast, contract)}`;\n const fromClause = `FROM ${quoteIdentifier(ast.from.name)}`;\n\n const joinsClause = ast.joins?.length\n ? ast.joins.map((join) => renderJoin(join, contract)).join(' ')\n : '';\n const includesClause = ast.includes?.length\n ? ast.includes.map((include) => renderInclude(include, contract)).join(' ')\n : '';\n\n const whereClause = ast.where ? ` WHERE ${renderWhere(ast.where, contract)}` : '';\n const orderClause = ast.orderBy?.length\n ? ` ORDER BY ${ast.orderBy\n .map((order) => {\n const expr = renderExpr(order.expr as ColumnRef | OperationExpr, contract);\n return `${expr} ${order.dir.toUpperCase()}`;\n })\n .join(', ')}`\n : '';\n const limitClause = typeof ast.limit === 'number' ? ` LIMIT ${ast.limit}` : '';\n\n const clauses = [joinsClause, includesClause].filter(Boolean).join(' ');\n return `${selectClause} ${fromClause}${clauses ? ` ${clauses}` : ''}${whereClause}${orderClause}${limitClause}`.trim();\n}\n\nfunction renderProjection(ast: SelectAst, contract?: PostgresContract): string {\n return ast.project\n .map((item) => {\n const expr = item.expr as ColumnRef | IncludeRef | OperationExpr | LiteralExpr;\n if (expr.kind === 'includeRef') {\n // For include references, select the column from the LATERAL join alias\n // The LATERAL subquery returns a single column (the JSON array) with the alias\n // The table is aliased as {alias}_lateral, and the column inside is aliased as the include alias\n // We select it using table_alias.column_alias\n const tableAlias = `${expr.alias}_lateral`;\n return `${quoteIdentifier(tableAlias)}.${quoteIdentifier(expr.alias)} AS ${quoteIdentifier(item.alias)}`;\n }\n if (expr.kind === 'operation') {\n const operation = renderOperation(expr, contract);\n const alias = quoteIdentifier(item.alias);\n return `${operation} AS ${alias}`;\n }\n if (expr.kind === 'literal') {\n const literal = renderLiteral(expr);\n const alias = quoteIdentifier(item.alias);\n return `${literal} AS ${alias}`;\n }\n const column = renderColumn(expr as ColumnRef);\n const alias = quoteIdentifier(item.alias);\n return `${column} AS ${alias}`;\n })\n .join(', ');\n}\n\nfunction renderWhere(expr: WhereExpr, contract?: PostgresContract): string {\n if (expr.kind === 'exists') {\n const notKeyword = expr.not ? 'NOT ' : '';\n const subquery = renderSelect(expr.subquery, contract);\n return `${notKeyword}EXISTS (${subquery})`;\n }\n if (expr.kind === 'nullCheck') {\n return renderNullCheck(expr, contract);\n }\n if (expr.kind === 'and') {\n if (expr.exprs.length === 0) {\n return 'TRUE';\n }\n return `(${expr.exprs.map((part) => renderWhere(part, contract)).join(' AND ')})`;\n }\n if (expr.kind === 'or') {\n if (expr.exprs.length === 0) {\n return 'FALSE';\n }\n return `(${expr.exprs.map((part) => renderWhere(part, contract)).join(' OR ')})`;\n }\n return renderBinary(expr, contract);\n}\n\nfunction renderNullCheck(expr: NullCheckExpr, contract?: PostgresContract): string {\n const rendered = renderExpr(expr.expr as ColumnRef | OperationExpr, contract);\n // Only wrap in parentheses if it's an operation expression\n const renderedExpr = isOperationExpr(expr.expr) ? `(${rendered})` : rendered;\n return expr.isNull ? `${renderedExpr} IS NULL` : `${renderedExpr} IS NOT NULL`;\n}\n\nfunction renderBinary(expr: BinaryExpr, contract?: PostgresContract): string {\n const leftExpr = expr.left as ColumnRef | OperationExpr;\n const left = renderExpr(leftExpr, contract);\n // Only wrap in parentheses if it's an operation expression\n const leftRendered = isOperationExpr(leftExpr) ? `(${left})` : left;\n const right = renderBinaryRight(expr.right, contract);\n const operatorMap: Record<BinaryExpr['op'], string> = {\n eq: '=',\n neq: '!=',\n gt: '>',\n lt: '<',\n gte: '>=',\n lte: '<=',\n like: 'LIKE',\n ilike: 'ILIKE',\n in: 'IN',\n notIn: 'NOT IN',\n };\n\n return `${leftRendered} ${operatorMap[expr.op]} ${right}`;\n}\n\nfunction renderBinaryRight(right: BinaryExpr['right'], contract?: PostgresContract): string {\n if (right.kind === 'col') {\n return renderColumn(right);\n }\n if (right.kind === 'param') {\n return renderParam(right, contract);\n }\n if (right.kind === 'literal') {\n return renderLiteral(right);\n }\n if (right.kind === 'operation') {\n return renderExpr(right, contract);\n }\n if (right.kind === 'listLiteral') {\n if (right.values.length === 0) {\n return '(NULL)';\n }\n const values = right.values.map((value) =>\n value.kind === 'param' ? renderParam(value, contract) : renderLiteral(value),\n );\n return `(${values.join(', ')})`;\n }\n\n throw new Error(`Unsupported binary right expression kind: ${(right as { kind: string }).kind}`);\n}\n\nfunction renderColumn(ref: ColumnRef): string {\n return `${quoteIdentifier(ref.table)}.${quoteIdentifier(ref.column)}`;\n}\n\nfunction renderExpr(expr: ColumnRef | OperationExpr, contract?: PostgresContract): string {\n if (isOperationExpr(expr)) {\n return renderOperation(expr, contract);\n }\n return renderColumn(expr);\n}\n\nfunction renderParam(\n ref: ParamRef,\n contract?: PostgresContract,\n tableName?: string,\n columnName?: string,\n): string {\n if (contract && tableName && columnName) {\n const tableMeta = contract.storage.tables[tableName];\n const columnMeta = tableMeta?.columns[columnName];\n return renderTypedParam(ref.index, columnMeta?.codecId);\n }\n return `$${ref.index}`;\n}\n\nfunction renderLiteral(expr: LiteralExpr): string {\n if (typeof expr.value === 'string') {\n return `'${expr.value.replace(/'/g, \"''\")}'`;\n }\n if (typeof expr.value === 'number' || typeof expr.value === 'boolean') {\n return String(expr.value);\n }\n if (expr.value === null) {\n return 'NULL';\n }\n if (Array.isArray(expr.value)) {\n return `ARRAY[${expr.value.map((v: unknown) => renderLiteral({ kind: 'literal', value: v })).join(', ')}]`;\n }\n return JSON.stringify(expr.value);\n}\n\nfunction renderOperation(expr: OperationExpr, contract?: PostgresContract): string {\n const self = renderExpr(expr.self, contract);\n // For vector operations, cast param arguments to vector type\n const isVectorOperation = expr.forTypeId === VECTOR_CODEC_ID;\n const args = expr.args.map((arg: ColumnRef | ParamRef | LiteralExpr | OperationExpr) => {\n if (arg.kind === 'col') {\n return renderColumn(arg);\n }\n if (arg.kind === 'param') {\n // Cast vector operation parameters to vector type\n return isVectorOperation ? `$${arg.index}::vector` : renderParam(arg, contract);\n }\n if (arg.kind === 'literal') {\n return renderLiteral(arg);\n }\n if (arg.kind === 'operation') {\n return renderOperation(arg, contract);\n }\n const _exhaustive: never = arg;\n throw new Error(`Unsupported argument kind: ${(_exhaustive as { kind: string }).kind}`);\n });\n\n let result = expr.lowering.template;\n result = result.replace(/\\$\\{self\\}/g, self);\n for (let i = 0; i < args.length; i++) {\n result = result.replace(new RegExp(`\\\\$\\\\{arg${i}\\\\}`, 'g'), args[i] ?? '');\n }\n\n if (expr.lowering.strategy === 'function') {\n return result;\n }\n\n return result;\n}\n\nfunction renderJoin(join: JoinAst, _contract?: PostgresContract): string {\n const joinType = join.joinType.toUpperCase();\n const table = quoteIdentifier(join.table.name);\n const onClause = renderJoinOn(join.on);\n return `${joinType} JOIN ${table} ON ${onClause}`;\n}\n\nfunction renderJoinOn(on: JoinAst['on']): string {\n if (on.kind === 'eqCol') {\n const left = renderColumn(on.left);\n const right = renderColumn(on.right);\n return `${left} = ${right}`;\n }\n throw new Error(`Unsupported join ON expression kind: ${on.kind}`);\n}\n\nfunction renderInclude(\n include: NonNullable<SelectAst['includes']>[number],\n contract?: PostgresContract,\n): string {\n const alias = include.alias;\n\n // Build the lateral subquery\n const childProjection = include.child.project\n .map((item: { alias: string; expr: ColumnRef | OperationExpr }) => {\n const expr = renderExpr(item.expr, contract);\n return `'${item.alias}', ${expr}`;\n })\n .join(', ');\n\n const jsonBuildObject = `json_build_object(${childProjection})`;\n\n // Build the ON condition from the include's ON clause - this goes in the WHERE clause\n const onCondition = renderJoinOn(include.child.on);\n\n // Build WHERE clause: combine ON condition with any additional WHERE clauses\n let whereClause = ` WHERE ${onCondition}`;\n if (include.child.where) {\n whereClause += ` AND ${renderWhere(include.child.where, contract)}`;\n }\n\n // Add ORDER BY if present - it goes inside json_agg() call\n const childOrderBy = include.child.orderBy?.length\n ? ` ORDER BY ${include.child.orderBy\n .map(\n (order: { expr: ColumnRef | OperationExpr; dir: string }) =>\n `${renderExpr(order.expr, contract)} ${order.dir.toUpperCase()}`,\n )\n .join(', ')}`\n : '';\n\n // Add LIMIT if present\n const childLimit = typeof include.child.limit === 'number' ? ` LIMIT ${include.child.limit}` : '';\n\n // Build the lateral subquery\n // When ORDER BY is present without LIMIT, it goes inside json_agg() call: json_agg(expr ORDER BY ...)\n // When LIMIT is present (with or without ORDER BY), we need to wrap in a subquery\n const childTable = quoteIdentifier(include.child.table.name);\n let subquery: string;\n if (typeof include.child.limit === 'number') {\n // With LIMIT, we need to wrap in a subquery\n // Select individual columns in inner query, then aggregate\n // Create a map of column references to their aliases for ORDER BY\n // Only ColumnRef can be mapped (OperationExpr doesn't have table/column properties)\n const columnAliasMap = new Map<string, string>();\n for (const item of include.child.project) {\n if (item.expr.kind === 'col') {\n const columnKey = `${item.expr.table}.${item.expr.column}`;\n columnAliasMap.set(columnKey, item.alias);\n }\n }\n\n const innerColumns = include.child.project\n .map((item: { alias: string; expr: ColumnRef | OperationExpr }) => {\n const expr = renderExpr(item.expr, contract);\n return `${expr} AS ${quoteIdentifier(item.alias)}`;\n })\n .join(', ');\n\n // For ORDER BY, use column aliases if the column is in the SELECT list\n const childOrderByWithAliases = include.child.orderBy?.length\n ? ` ORDER BY ${include.child.orderBy\n .map((order: { expr: ColumnRef | OperationExpr; dir: string }) => {\n if (order.expr.kind === 'col') {\n const columnKey = `${order.expr.table}.${order.expr.column}`;\n const alias = columnAliasMap.get(columnKey);\n if (alias) {\n return `${quoteIdentifier(alias)} ${order.dir.toUpperCase()}`;\n }\n }\n return `${renderExpr(order.expr, contract)} ${order.dir.toUpperCase()}`;\n })\n .join(', ')}`\n : '';\n\n const innerSelect = `SELECT ${innerColumns} FROM ${childTable}${whereClause}${childOrderByWithAliases}${childLimit}`;\n subquery = `(SELECT json_agg(row_to_json(sub.*)) AS ${quoteIdentifier(alias)} FROM (${innerSelect}) sub)`;\n } else if (childOrderBy) {\n // With ORDER BY but no LIMIT, ORDER BY goes inside json_agg()\n subquery = `(SELECT json_agg(${jsonBuildObject}${childOrderBy}) AS ${quoteIdentifier(alias)} FROM ${childTable}${whereClause})`;\n } else {\n // No ORDER BY or LIMIT\n subquery = `(SELECT json_agg(${jsonBuildObject}) AS ${quoteIdentifier(alias)} FROM ${childTable}${whereClause})`;\n }\n\n // Return the LATERAL join with ON true (the condition is in the WHERE clause)\n // The subquery returns a single column (the JSON array) with the alias\n // We use a different alias for the table to avoid ambiguity when selecting the column\n const tableAlias = `${alias}_lateral`;\n return `LEFT JOIN LATERAL ${subquery} AS ${quoteIdentifier(tableAlias)} ON true`;\n}\n\nfunction quoteIdentifier(identifier: string): string {\n return `\"${identifier.replace(/\"/g, '\"\"')}\"`;\n}\n\nfunction renderInsert(ast: InsertAst, contract: PostgresContract): string {\n const table = quoteIdentifier(ast.table.name);\n const columns = Object.keys(ast.values).map((col) => quoteIdentifier(col));\n const tableMeta = contract.storage.tables[ast.table.name];\n const values = Object.entries(ast.values).map(([colName, val]) => {\n if (val.kind === 'param') {\n const columnMeta = tableMeta?.columns[colName];\n return renderTypedParam(val.index, columnMeta?.codecId);\n }\n if (val.kind === 'col') {\n return `${quoteIdentifier(val.table)}.${quoteIdentifier(val.column)}`;\n }\n throw new Error(`Unsupported value kind in INSERT: ${(val as { kind: string }).kind}`);\n });\n\n const insertClause = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${values.join(', ')})`;\n const returningClause = ast.returning?.length\n ? ` RETURNING ${ast.returning.map((col) => `${quoteIdentifier(col.table)}.${quoteIdentifier(col.column)}`).join(', ')}`\n : '';\n\n return `${insertClause}${returningClause}`;\n}\n\nfunction renderUpdate(ast: UpdateAst, contract: PostgresContract): string {\n const table = quoteIdentifier(ast.table.name);\n const tableMeta = contract.storage.tables[ast.table.name];\n const setClauses = Object.entries(ast.set).map(([col, val]) => {\n const column = quoteIdentifier(col);\n let value: string;\n if (val.kind === 'param') {\n const columnMeta = tableMeta?.columns[col];\n value = renderTypedParam(val.index, columnMeta?.codecId);\n } else if (val.kind === 'col') {\n value = `${quoteIdentifier(val.table)}.${quoteIdentifier(val.column)}`;\n } else {\n throw new Error(`Unsupported value kind in UPDATE: ${(val as { kind: string }).kind}`);\n }\n return `${column} = ${value}`;\n });\n\n const whereClause = ` WHERE ${renderWhere(ast.where, contract)}`;\n const returningClause = ast.returning?.length\n ? ` RETURNING ${ast.returning.map((col) => `${quoteIdentifier(col.table)}.${quoteIdentifier(col.column)}`).join(', ')}`\n : '';\n\n return `UPDATE ${table} SET ${setClauses.join(', ')}${whereClause}${returningClause}`;\n}\n\nfunction renderDelete(ast: DeleteAst, contract?: PostgresContract): string {\n const table = quoteIdentifier(ast.table.name);\n const whereClause = ` WHERE ${renderWhere(ast.where, contract)}`;\n const returningClause = ast.returning?.length\n ? ` RETURNING ${ast.returning.map((col) => `${quoteIdentifier(col.table)}.${quoteIdentifier(col.column)}`).join(', ')}`\n : '';\n\n return `DELETE FROM ${table}${whereClause}${returningClause}`;\n}\n\nexport function createPostgresAdapter(options?: PostgresAdapterOptions) {\n return Object.freeze(new PostgresAdapterImpl(options));\n}\n"],"mappings":";;;;;;AA0BA,MAAM,kBAAkB;AAExB,SAAS,kBAAkB,SAAiD;AAC1E,KAAI,YAAY,gBACd,QAAO;AAET,KAAI,YAAY,iBACd,QAAO;AAET,KAAI,YAAY,kBACd,QAAO;;AAKX,SAA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,MAAM,CAAC,GAAG,gBAAgB,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,KACnH;;AAKN,SAAgB,sBAAsB,SAAkC;AACtE,QAAO,OAAO,OAAO,IAAI,oBAAoB,QAAQ,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { c as PostgresContract, l as PostgresLoweredStatement, s as PostgresAdapterOptions } from "./types-BY395pUv.mjs";
|
|
2
|
+
import { Adapter, AdapterProfile, CodecParamsDescriptor, LowererContext, QueryAst } from "@prisma-next/sql-relational-core/ast";
|
|
3
|
+
|
|
4
|
+
//#region src/core/adapter.d.ts
|
|
5
|
+
declare class PostgresAdapterImpl implements Adapter<QueryAst, PostgresContract, PostgresLoweredStatement> {
|
|
6
|
+
readonly familyId: "sql";
|
|
7
|
+
readonly targetId: "postgres";
|
|
8
|
+
readonly profile: AdapterProfile<'postgres'>;
|
|
9
|
+
private readonly codecRegistry;
|
|
10
|
+
constructor(options?: PostgresAdapterOptions);
|
|
11
|
+
parameterizedCodecs(): ReadonlyArray<CodecParamsDescriptor>;
|
|
12
|
+
lower(ast: QueryAst, context: LowererContext<PostgresContract>): Readonly<{
|
|
13
|
+
profileId: string;
|
|
14
|
+
body: Readonly<{
|
|
15
|
+
sql: string;
|
|
16
|
+
params: unknown[];
|
|
17
|
+
}>;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
declare function createPostgresAdapter(options?: PostgresAdapterOptions): Readonly<PostgresAdapterImpl>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { createPostgresAdapter };
|
|
23
|
+
//# sourceMappingURL=adapter.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.mts","names":[],"sources":["../src/core/adapter.ts"],"sourcesContent":[],"mappings":";;;;cA2EM,mBAAA,YAA+B,QAAQ,UAAU,kBAAkB;;EAAnE,SAAA,QAAA,EAAA,UAAoB;EAAmB,SAAA,OAAA,EAMzB,cANyB,CAAA,UAAA,CAAA;EAAU,iBAAA,aAAA;EAAkB,WAAA,CAAA,OAAA,CAAA,EAejD,sBAfiD;EAMrD,mBAAA,CAAA,CAAA,EAkBK,aAlBL,CAkBmB,qBAlBnB,CAAA;EASI,KAAA,CAAA,GAAA,EAaX,QAbW,EAAA,OAAA,EAaQ,cAbR,CAauB,gBAbvB,CAAA,CAAA,EAawC,QAbxC,CAAA;IASe,SAAA,EAAA,MAAA;IAAd,IAAA,UAAA,CAAA;MAIZ,GAAA,EAAA,MAAA;MAAkC,MAAA,EAAA,OAAA,EAAA;IAAf,CAAA,CAAA;;;AA5BK,iBAmbrB,qBAAA,CAnbqB,OAAA,CAAA,EAmbW,sBAnbX,CAAA,EAmbiC,QAnbjC,CAmbiC,mBAnbjC,CAAA"}
|
package/dist/adapter.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { SQL_CHAR_CODEC_ID, SQL_FLOAT_CODEC_ID, SQL_INT_CODEC_ID, SQL_VARCHAR_CODEC_ID } from "@prisma-next/sql-relational-core/ast";
|
|
2
|
+
|
|
3
|
+
//#region src/core/codec-ids.ts
|
|
4
|
+
const PG_TEXT_CODEC_ID = "pg/text@1";
|
|
5
|
+
const PG_ENUM_CODEC_ID = "pg/enum@1";
|
|
6
|
+
const PG_CHAR_CODEC_ID = "pg/char@1";
|
|
7
|
+
const PG_VARCHAR_CODEC_ID = "pg/varchar@1";
|
|
8
|
+
const PG_INT_CODEC_ID = "pg/int@1";
|
|
9
|
+
const PG_INT2_CODEC_ID = "pg/int2@1";
|
|
10
|
+
const PG_INT4_CODEC_ID = "pg/int4@1";
|
|
11
|
+
const PG_INT8_CODEC_ID = "pg/int8@1";
|
|
12
|
+
const PG_FLOAT_CODEC_ID = "pg/float@1";
|
|
13
|
+
const PG_FLOAT4_CODEC_ID = "pg/float4@1";
|
|
14
|
+
const PG_FLOAT8_CODEC_ID = "pg/float8@1";
|
|
15
|
+
const PG_NUMERIC_CODEC_ID = "pg/numeric@1";
|
|
16
|
+
const PG_BOOL_CODEC_ID = "pg/bool@1";
|
|
17
|
+
const PG_BIT_CODEC_ID = "pg/bit@1";
|
|
18
|
+
const PG_VARBIT_CODEC_ID = "pg/varbit@1";
|
|
19
|
+
const PG_TIMESTAMP_CODEC_ID = "pg/timestamp@1";
|
|
20
|
+
const PG_TIMESTAMPTZ_CODEC_ID = "pg/timestamptz@1";
|
|
21
|
+
const PG_TIME_CODEC_ID = "pg/time@1";
|
|
22
|
+
const PG_TIMETZ_CODEC_ID = "pg/timetz@1";
|
|
23
|
+
const PG_INTERVAL_CODEC_ID = "pg/interval@1";
|
|
24
|
+
const PG_JSON_CODEC_ID = "pg/json@1";
|
|
25
|
+
const PG_JSONB_CODEC_ID = "pg/jsonb@1";
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { SQL_CHAR_CODEC_ID as C, SQL_VARCHAR_CODEC_ID as E, PG_VARCHAR_CODEC_ID as S, SQL_INT_CODEC_ID as T, PG_TIMESTAMPTZ_CODEC_ID as _, PG_FLOAT4_CODEC_ID as a, PG_TIME_CODEC_ID as b, PG_INT2_CODEC_ID as c, PG_INTERVAL_CODEC_ID as d, PG_INT_CODEC_ID as f, PG_TEXT_CODEC_ID as g, PG_NUMERIC_CODEC_ID as h, PG_ENUM_CODEC_ID as i, PG_INT4_CODEC_ID as l, PG_JSON_CODEC_ID as m, PG_BOOL_CODEC_ID as n, PG_FLOAT8_CODEC_ID as o, PG_JSONB_CODEC_ID as p, PG_CHAR_CODEC_ID as r, PG_FLOAT_CODEC_ID as s, PG_BIT_CODEC_ID as t, PG_INT8_CODEC_ID as u, PG_TIMESTAMP_CODEC_ID as v, SQL_FLOAT_CODEC_ID as w, PG_VARBIT_CODEC_ID as x, PG_TIMETZ_CODEC_ID as y };
|
|
29
|
+
//# sourceMappingURL=codec-ids-Bsm9c7ns.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec-ids-Bsm9c7ns.mjs","names":[],"sources":["../src/core/codec-ids.ts"],"sourcesContent":["export {\n SQL_CHAR_CODEC_ID,\n SQL_FLOAT_CODEC_ID,\n SQL_INT_CODEC_ID,\n SQL_VARCHAR_CODEC_ID,\n} from '@prisma-next/sql-relational-core/ast';\nexport const PG_TEXT_CODEC_ID = 'pg/text@1' as const;\nexport const PG_ENUM_CODEC_ID = 'pg/enum@1' as const;\nexport const PG_CHAR_CODEC_ID = 'pg/char@1' as const;\nexport const PG_VARCHAR_CODEC_ID = 'pg/varchar@1' as const;\nexport const PG_INT_CODEC_ID = 'pg/int@1' as const;\nexport const PG_INT2_CODEC_ID = 'pg/int2@1' as const;\nexport const PG_INT4_CODEC_ID = 'pg/int4@1' as const;\nexport const PG_INT8_CODEC_ID = 'pg/int8@1' as const;\nexport const PG_FLOAT_CODEC_ID = 'pg/float@1' as const;\nexport const PG_FLOAT4_CODEC_ID = 'pg/float4@1' as const;\nexport const PG_FLOAT8_CODEC_ID = 'pg/float8@1' as const;\nexport const PG_NUMERIC_CODEC_ID = 'pg/numeric@1' as const;\nexport const PG_BOOL_CODEC_ID = 'pg/bool@1' as const;\nexport const PG_BIT_CODEC_ID = 'pg/bit@1' as const;\nexport const PG_VARBIT_CODEC_ID = 'pg/varbit@1' as const;\nexport const PG_TIMESTAMP_CODEC_ID = 'pg/timestamp@1' as const;\nexport const PG_TIMESTAMPTZ_CODEC_ID = 'pg/timestamptz@1' as const;\nexport const PG_TIME_CODEC_ID = 'pg/time@1' as const;\nexport const PG_TIMETZ_CODEC_ID = 'pg/timetz@1' as const;\nexport const PG_INTERVAL_CODEC_ID = 'pg/interval@1' as const;\nexport const PG_JSON_CODEC_ID = 'pg/json@1' as const;\nexport const PG_JSONB_CODEC_ID = 'pg/jsonb@1' as const;\n"],"mappings":";;;AAMA,MAAa,mBAAmB;AAChC,MAAa,mBAAmB;AAChC,MAAa,mBAAmB;AAChC,MAAa,sBAAsB;AACnC,MAAa,kBAAkB;AAC/B,MAAa,mBAAmB;AAChC,MAAa,mBAAmB;AAChC,MAAa,mBAAmB;AAChC,MAAa,oBAAoB;AACjC,MAAa,qBAAqB;AAClC,MAAa,qBAAqB;AAClC,MAAa,sBAAsB;AACnC,MAAa,mBAAmB;AAChC,MAAa,kBAAkB;AAC/B,MAAa,qBAAqB;AAClC,MAAa,wBAAwB;AACrC,MAAa,0BAA0B;AACvC,MAAa,mBAAmB;AAChC,MAAa,qBAAqB;AAClC,MAAa,uBAAuB;AACpC,MAAa,mBAAmB;AAChC,MAAa,oBAAoB"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import * as _prisma_next_sql_relational_core_ast0 from "@prisma-next/sql-relational-core/ast";
|
|
2
|
+
import { Codec } from "@prisma-next/sql-relational-core/ast";
|
|
3
|
+
|
|
4
|
+
//#region src/core/codecs.d.ts
|
|
5
|
+
|
|
6
|
+
type JsonValue = string | number | boolean | null | {
|
|
7
|
+
readonly [key: string]: JsonValue;
|
|
8
|
+
} | readonly JsonValue[];
|
|
9
|
+
declare const codecs: _prisma_next_sql_relational_core_ast0.CodecDefBuilder<{
|
|
10
|
+
char: Codec<"sql/char@1", string, string, Record<string, unknown>, unknown>;
|
|
11
|
+
varchar: Codec<"sql/varchar@1", string, string, Record<string, unknown>, unknown>;
|
|
12
|
+
int: Codec<"sql/int@1", number, number, Record<string, unknown>, unknown>;
|
|
13
|
+
float: Codec<"sql/float@1", number, number, Record<string, unknown>, unknown>;
|
|
14
|
+
text: Codec<"pg/text@1", string, string, Record<string, unknown>, unknown>;
|
|
15
|
+
character: Codec<"pg/char@1", string, string, Record<string, unknown>, unknown>;
|
|
16
|
+
"character varying": Codec<"pg/varchar@1", string, string, Record<string, unknown>, unknown>;
|
|
17
|
+
integer: Codec<"pg/int@1", number, number, Record<string, unknown>, unknown>;
|
|
18
|
+
"double precision": Codec<"pg/float@1", number, number, Record<string, unknown>, unknown>;
|
|
19
|
+
int4: Codec<"pg/int4@1", number, number, Record<string, unknown>, unknown>;
|
|
20
|
+
int2: Codec<"pg/int2@1", number, number, Record<string, unknown>, unknown>;
|
|
21
|
+
int8: Codec<"pg/int8@1", number, number, Record<string, unknown>, unknown>;
|
|
22
|
+
float4: Codec<"pg/float4@1", number, number, Record<string, unknown>, unknown>;
|
|
23
|
+
float8: Codec<"pg/float8@1", number, number, Record<string, unknown>, unknown>;
|
|
24
|
+
numeric: Codec<"pg/numeric@1", string, string, Record<string, unknown>, unknown>;
|
|
25
|
+
timestamp: Codec<"pg/timestamp@1", string | Date, string, Record<string, unknown>, unknown>;
|
|
26
|
+
timestamptz: Codec<"pg/timestamptz@1", string | Date, string, Record<string, unknown>, unknown>;
|
|
27
|
+
time: Codec<"pg/time@1", string, string, Record<string, unknown>, unknown>;
|
|
28
|
+
timetz: Codec<"pg/timetz@1", string, string, Record<string, unknown>, unknown>;
|
|
29
|
+
bool: Codec<"pg/bool@1", boolean, boolean, Record<string, unknown>, unknown>;
|
|
30
|
+
bit: Codec<"pg/bit@1", string, string, Record<string, unknown>, unknown>;
|
|
31
|
+
"bit varying": Codec<"pg/varbit@1", string, string, Record<string, unknown>, unknown>;
|
|
32
|
+
interval: Codec<"pg/interval@1", string, string, Record<string, unknown>, unknown>;
|
|
33
|
+
enum: Codec<"pg/enum@1", string, string, Record<string, unknown>, unknown>;
|
|
34
|
+
json: Codec<"pg/json@1", JsonValue, JsonValue, Record<string, unknown>, unknown>;
|
|
35
|
+
} & Record<"jsonb", Codec<"pg/jsonb@1", JsonValue, JsonValue, Record<string, unknown>, unknown>>>;
|
|
36
|
+
declare const dataTypes: {
|
|
37
|
+
readonly char: "sql/char@1";
|
|
38
|
+
readonly varchar: "sql/varchar@1";
|
|
39
|
+
readonly int: "sql/int@1";
|
|
40
|
+
readonly float: "sql/float@1";
|
|
41
|
+
readonly text: "pg/text@1";
|
|
42
|
+
readonly character: "pg/char@1";
|
|
43
|
+
readonly "character varying": "pg/varchar@1";
|
|
44
|
+
readonly integer: "pg/int@1";
|
|
45
|
+
readonly "double precision": "pg/float@1";
|
|
46
|
+
readonly int4: "pg/int4@1";
|
|
47
|
+
readonly int2: "pg/int2@1";
|
|
48
|
+
readonly int8: "pg/int8@1";
|
|
49
|
+
readonly float4: "pg/float4@1";
|
|
50
|
+
readonly float8: "pg/float8@1";
|
|
51
|
+
readonly numeric: "pg/numeric@1";
|
|
52
|
+
readonly timestamp: "pg/timestamp@1";
|
|
53
|
+
readonly timestamptz: "pg/timestamptz@1";
|
|
54
|
+
readonly time: "pg/time@1";
|
|
55
|
+
readonly timetz: "pg/timetz@1";
|
|
56
|
+
readonly bool: "pg/bool@1";
|
|
57
|
+
readonly bit: "pg/bit@1";
|
|
58
|
+
readonly "bit varying": "pg/varbit@1";
|
|
59
|
+
readonly interval: "pg/interval@1";
|
|
60
|
+
readonly enum: "pg/enum@1";
|
|
61
|
+
readonly json: "pg/json@1";
|
|
62
|
+
readonly jsonb: "pg/jsonb@1";
|
|
63
|
+
};
|
|
64
|
+
type CodecTypes$1 = typeof codecs.CodecTypes;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/exports/codec-types.d.ts
|
|
67
|
+
/**
|
|
68
|
+
* Compile-time view of the Standard Schema protocol.
|
|
69
|
+
* Reads `~standard.types.output` to resolve TypeScript output types for contract.d.ts.
|
|
70
|
+
*
|
|
71
|
+
* This differs from the runtime `StandardSchemaLike` in `standard-schema.ts`, which reads
|
|
72
|
+
* `~standard.jsonSchema.output` for the serializable JSON Schema representation.
|
|
73
|
+
* Both are needed: this one drives compile-time type narrowing, the other drives
|
|
74
|
+
* build-time contract emission.
|
|
75
|
+
*/
|
|
76
|
+
type StandardSchemaLike = {
|
|
77
|
+
readonly '~standard'?: {
|
|
78
|
+
readonly types?: {
|
|
79
|
+
readonly output?: unknown;
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
type ResolveStandardSchemaOutput<P> = P extends {
|
|
84
|
+
readonly schema: infer Schema;
|
|
85
|
+
} ? Schema extends {
|
|
86
|
+
readonly infer: infer Output;
|
|
87
|
+
} ? Output : Schema extends {
|
|
88
|
+
readonly '~standard': {
|
|
89
|
+
readonly types?: {
|
|
90
|
+
readonly output?: infer Output;
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
} ? Output extends undefined ? JsonValue : Output : JsonValue : JsonValue;
|
|
94
|
+
type CodecTypes = CodecTypes$1 & {
|
|
95
|
+
readonly 'pg/json@1': CodecTypes$1['pg/json@1'] & {
|
|
96
|
+
readonly parameterizedOutput: <P extends {
|
|
97
|
+
readonly schema?: StandardSchemaLike;
|
|
98
|
+
}>(params: P) => ResolveStandardSchemaOutput<P>;
|
|
99
|
+
};
|
|
100
|
+
readonly 'pg/jsonb@1': CodecTypes$1['pg/jsonb@1'] & {
|
|
101
|
+
readonly parameterizedOutput: <P extends {
|
|
102
|
+
readonly schema?: StandardSchemaLike;
|
|
103
|
+
}>(params: P) => ResolveStandardSchemaOutput<P>;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
type Branded<T, Shape extends Record<string, unknown>> = T & { readonly [K in keyof Shape]: Shape[K] };
|
|
107
|
+
type BrandedString<Shape extends Record<string, unknown>> = Branded<string, Shape>;
|
|
108
|
+
type Char<N extends number> = BrandedString<{
|
|
109
|
+
__charLength: N;
|
|
110
|
+
}>;
|
|
111
|
+
type Varchar<N extends number> = BrandedString<{
|
|
112
|
+
__varcharLength: N;
|
|
113
|
+
}>;
|
|
114
|
+
type Numeric<P extends number, S extends number | undefined = undefined> = BrandedString<{
|
|
115
|
+
__numericPrecision: P;
|
|
116
|
+
__numericScale: S;
|
|
117
|
+
}>;
|
|
118
|
+
type Bit<N extends number> = BrandedString<{
|
|
119
|
+
__bitLength: N;
|
|
120
|
+
}>;
|
|
121
|
+
type VarBit<N extends number> = BrandedString<{
|
|
122
|
+
__varbitLength: N;
|
|
123
|
+
}>;
|
|
124
|
+
type Timestamp<P extends number | undefined = undefined> = BrandedString<{
|
|
125
|
+
__timestampPrecision: P;
|
|
126
|
+
}>;
|
|
127
|
+
type Timestamptz<P extends number | undefined = undefined> = BrandedString<{
|
|
128
|
+
__timestamptzPrecision: P;
|
|
129
|
+
}>;
|
|
130
|
+
type Time<P extends number | undefined = undefined> = BrandedString<{
|
|
131
|
+
__timePrecision: P;
|
|
132
|
+
}>;
|
|
133
|
+
type Timetz<P extends number | undefined = undefined> = BrandedString<{
|
|
134
|
+
__timetzPrecision: P;
|
|
135
|
+
}>;
|
|
136
|
+
type Interval<P extends number | undefined = undefined> = BrandedString<{
|
|
137
|
+
__intervalPrecision: P;
|
|
138
|
+
}>;
|
|
139
|
+
//#endregion
|
|
140
|
+
export { Bit, Char, CodecTypes, Interval, type JsonValue, Numeric, Time, Timestamp, Timestamptz, Timetz, VarBit, Varchar, dataTypes };
|
|
141
|
+
//# sourceMappingURL=codec-types.d.mts.map
|