qubu 0.3.5 → 0.4.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/{canonical-BbnqavJm.mjs → canonical-BXUguqfo.mjs} +23 -23
- package/dist/codegen.d.mts +25 -33
- package/dist/codegen.mjs +38 -45
- package/dist/column-hqKr7-1I.mjs +620 -0
- package/dist/{complete-D5Djh-zo.mjs → complete-WYyVozgK.mjs} +50 -50
- package/dist/{complete-types-B2PO6wQD.d.mts → complete-types-IjEn5VPN.d.mts} +56 -56
- package/dist/core.d.mts +2 -2
- package/dist/core.mjs +4 -4
- package/dist/ddl.d.mts +12 -12
- package/dist/ddl.mjs +13 -14
- package/dist/{dialect-b2-Z6uBF.mjs → dialect-wUKrnPMB.mjs} +2 -2
- package/dist/diff.d.mts +1 -1
- package/dist/diff.mjs +6 -7
- package/dist/{index-7qc6OcIC.d.mts → index-1DpA3mUh.d.mts} +20 -20
- package/dist/{index-CqWnouTK.d.mts → index-CPvfEheG.d.mts} +13 -15
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +148 -120
- package/dist/introspection.d.mts +8 -8
- package/dist/introspection.mjs +29 -30
- package/dist/{json-CUZlv4HT.mjs → json-Db7XRD91.mjs} +2 -2
- package/dist/migration.d.mts +20 -21
- package/dist/migration.mjs +5 -6
- package/dist/{mysql-DqkqXB6A.mjs → mysql-B_cYzzX2.mjs} +237 -7
- package/dist/mysql.d.mts +1 -1
- package/dist/mysql.mjs +3 -3
- package/dist/{on-conflict-BxnxubMb.mjs → on-conflict-hfPW0KmQ.mjs} +4 -4
- package/dist/postgres.d.mts +5 -5
- package/dist/postgres.mjs +6 -6
- package/dist/{registry-BufIskVN.mjs → registry-BRMLYwDp.mjs} +27 -135
- package/dist/{relational-DCZrrNia.mjs → relational-BZ3WDPzC.mjs} +4 -4
- package/dist/schema.d.mts +2 -2
- package/dist/schema.mjs +6 -6
- package/dist/{snapshot-CWPgzxNx.mjs → snapshot-C-W65HEd.mjs} +5 -5
- package/dist/snapshot.d.mts +3 -3
- package/dist/snapshot.mjs +5 -7
- package/dist/{source-DUoJVXmL.mjs → source-BcS2AsIg.mjs} +7 -9
- package/dist/{serialize-PF1cfH2P.mjs → sqlite-Cg0nwYEH.mjs} +326 -12
- package/dist/sqlite.d.mts +2 -2
- package/dist/sqlite.mjs +2 -2
- package/dist/{standard-BTVYKh_F.mjs → standard-DfcZEVOj.mjs} +1 -1
- package/dist/{table-CCUJ60rB.mjs → table-Bp5irMSj.mjs} +5 -7
- package/dist/{types-D8M1yZF4.d.mts → types-BK1COGZe.d.mts} +2661 -2150
- package/dist/{types-Cec0xzo4.mjs → types-JM3FcAnX.mjs} +8 -8
- package/dist/{types-LBt5rclR.d.mts → types-JSZHpUEj.d.mts} +83 -84
- package/dist/{value-BvilP0oz.mjs → value-Bi71Agyf.mjs} +1 -1
- package/dist/vite/ambient.d.ts +311 -390
- package/dist/vite.d.mts +5 -6
- package/dist/vite.mjs +3 -4
- package/docs/dialects-and-execution.md +121 -81
- package/docs/getting-started.md +4 -4
- package/docs/guides/better-auth.md +57 -0
- package/docs/guides/compose-queries.md +24 -67
- package/docs/guides/drizzle.md +23 -23
- package/docs/guides/extensions/dialects.md +4 -4
- package/docs/guides/extensions/sources-and-clauses.md +15 -15
- package/docs/guides/extensions/typed-expressions.md +13 -17
- package/docs/guides/extensions/unsafe-syntax.md +3 -3
- package/docs/guides/json.md +7 -7
- package/docs/guides/mutations.md +14 -21
- package/docs/guides/select/conditions.md +6 -8
- package/docs/guides/select/grouping-and-windows.md +4 -15
- package/docs/guides/select/ordering-and-pagination.md +6 -19
- package/docs/guides/select/overview.md +12 -26
- package/docs/guides/sql-templates.md +20 -24
- package/docs/guides/vite-plugin.md +9 -13
- package/docs/index.md +5 -7
- package/docs/query-model/fragments.md +4 -15
- package/docs/query-model/result-shapes.md +11 -14
- package/docs/query-model/source-scope.md +31 -49
- package/docs/reference/mysql-snapshot.md +2 -5
- package/docs/reference/postgres-snapshot.md +3 -3
- package/docs/reference/sqlite-snapshot.md +2 -5
- package/docs/reference/supported-surface.md +33 -33
- package/docs/schema/catalog-model.md +2 -5
- package/docs/schema/code-generation.md +18 -18
- package/docs/schema/columns-and-writes.md +17 -25
- package/docs/schema/constraints-and-indexes.md +36 -52
- package/docs/schema/ddl-emission.md +6 -6
- package/docs/schema/diff.md +5 -5
- package/docs/schema/introspection.md +7 -7
- package/docs/schema/migration-plans.md +9 -9
- package/docs/schema/snapshots.md +3 -3
- package/docs/schema/storage-and-schema-sql.md +11 -11
- package/docs/schema/tables-and-names.md +11 -14
- package/docs/sql-semantic-types.md +10 -10
- package/docs/troubleshooting.md +10 -10
- package/package.json +21 -33
- package/skills/qubu/agents/openai.yaml +3 -3
- package/dist/column-CXMxx8Hq.mjs +0 -118
- package/dist/column-CYMbKbOy.mjs +0 -290
- package/dist/drizzle-mysql.d.mts +0 -24
- package/dist/drizzle-mysql.mjs +0 -72
- package/dist/drizzle-postgres.d.mts +0 -24
- package/dist/drizzle-postgres.mjs +0 -73
- package/dist/drizzle-sqlite.d.mts +0 -50
- package/dist/drizzle-sqlite.mjs +0 -108
- package/dist/drizzle.d.mts +0 -13
- package/dist/drizzle.mjs +0 -2
- package/dist/errors-BGCoLe_r.mjs +0 -14
- package/dist/naming-QVCOnSj2.mjs +0 -20
- package/dist/postgres-DEBBeh52.mjs +0 -235
- package/dist/runtime-Cn_Xgzta.mjs +0 -193
- package/dist/sqlite-BU6DBxef.mjs +0 -320
- package/dist/types-Ctlxz1ip.d.mts +0 -45
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
import { a as assertDialectCapability } from "./json-Db7XRD91.mjs";
|
|
2
|
+
//#region src/result.ts
|
|
3
|
+
const resultValueMetadata = Symbol("qubu.result-value-metadata");
|
|
4
|
+
function resultValue(type, decoder) {
|
|
5
|
+
return type === void 0 && decoder === void 0 ? void 0 : Object.freeze({
|
|
6
|
+
...type === void 0 ? {} : { type },
|
|
7
|
+
...decoder === void 0 ? {} : { decoder }
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function resultValueOf(value) {
|
|
11
|
+
if (typeof value !== "object" || value === null) return;
|
|
12
|
+
return value[resultValueMetadata];
|
|
13
|
+
}
|
|
14
|
+
function attachResultValue(value, metadata) {
|
|
15
|
+
if (metadata === void 0) return value;
|
|
16
|
+
return Object.freeze({
|
|
17
|
+
...value,
|
|
18
|
+
[resultValueMetadata]: metadata
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function createResultShape(fields) {
|
|
22
|
+
return Object.freeze({ fields: Object.freeze(fields.map((field) => Object.freeze({ ...field }))) });
|
|
23
|
+
}
|
|
24
|
+
function resultShapeValue(shape, name) {
|
|
25
|
+
const field = shape.fields.find((candidate) => candidate.name === name);
|
|
26
|
+
return field === void 0 ? void 0 : resultValue(field.type, field.decoder);
|
|
27
|
+
}
|
|
28
|
+
/** Decode SQLite/MySQL-style boolean values while accepting native booleans. */
|
|
29
|
+
const booleanResultDecoder = (value) => {
|
|
30
|
+
if (typeof value === "boolean") return value;
|
|
31
|
+
if (value === 0 || value === 0n) return false;
|
|
32
|
+
if (value === 1 || value === 1n) return true;
|
|
33
|
+
throw new TypeError("Expected a boolean or numeric 0/1 result");
|
|
34
|
+
};
|
|
35
|
+
/** Decode a SQL DATE string as a JavaScript Date at UTC midnight. */
|
|
36
|
+
const dateResultDecoder = (value) => {
|
|
37
|
+
if (value instanceof Date && !Number.isNaN(value.getTime())) return value;
|
|
38
|
+
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/u.test(value)) throw new TypeError("Expected a valid Date or YYYY-MM-DD result");
|
|
39
|
+
const decoded = /* @__PURE__ */ new Date(`${value}T00:00:00.000Z`);
|
|
40
|
+
if (Number.isNaN(decoded.getTime()) || decoded.toISOString().slice(0, 10) !== value) throw new TypeError("Expected a valid calendar date result");
|
|
41
|
+
return decoded;
|
|
42
|
+
};
|
|
43
|
+
/** Decode a SQL/ISO timestamp string while accepting driver-created Dates. */
|
|
44
|
+
const timestampResultDecoder = (value) => {
|
|
45
|
+
if (value instanceof Date && !Number.isNaN(value.getTime())) return value;
|
|
46
|
+
if (typeof value !== "string") throw new TypeError("Expected a valid Date or timestamp string result");
|
|
47
|
+
const normalized = value.includes("T") ? value : value.replace(" ", "T");
|
|
48
|
+
const decoded = new Date(/(?:Z|[+-]\d{2}:?\d{2})$/u.test(normalized) ? normalized : `${normalized}Z`);
|
|
49
|
+
if (Number.isNaN(decoded.getTime())) throw new TypeError("Expected a valid timestamp result");
|
|
50
|
+
return decoded;
|
|
51
|
+
};
|
|
52
|
+
/** Decode a serialized JSON result. Use only when the adapter returns JSON text. */
|
|
53
|
+
const jsonTextResultDecoder = (value) => {
|
|
54
|
+
if (typeof value !== "string") return value;
|
|
55
|
+
return JSON.parse(value);
|
|
56
|
+
};
|
|
57
|
+
/** Error raised when a projected driver value cannot be decoded. */
|
|
58
|
+
var ResultDecodingError = class extends TypeError {
|
|
59
|
+
name = "ResultDecodingError";
|
|
60
|
+
field;
|
|
61
|
+
rowIndex;
|
|
62
|
+
resultType;
|
|
63
|
+
constructor(field, rowIndex, resultType) {
|
|
64
|
+
super(`Could not decode result field "${field}" in row ${rowIndex}${resultType === void 0 ? "" : ` as ${resultType}`}`);
|
|
65
|
+
this.field = field;
|
|
66
|
+
this.rowIndex = rowIndex;
|
|
67
|
+
this.resultType = resultType;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
function decodeResultRow(row, shape, decoders, dialect, rowIndex) {
|
|
71
|
+
let decoded;
|
|
72
|
+
for (const field of shape.fields) {
|
|
73
|
+
const decoder = field.decoder ?? (field.type ? decoders?.[field.type] : void 0);
|
|
74
|
+
if (decoder === void 0 || row[field.name] === null) continue;
|
|
75
|
+
try {
|
|
76
|
+
decoded ??= { ...row };
|
|
77
|
+
decoded[field.name] = decoder(row[field.name], {
|
|
78
|
+
dialect,
|
|
79
|
+
field: field.name,
|
|
80
|
+
rowIndex
|
|
81
|
+
});
|
|
82
|
+
} catch {
|
|
83
|
+
throw new ResultDecodingError(field.name, rowIndex, field.type);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return decoded ?? row;
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/core/fragment.ts
|
|
90
|
+
function fragment(render) {
|
|
91
|
+
return Object.freeze({ render });
|
|
92
|
+
}
|
|
93
|
+
function sequence(parts, separator = " ") {
|
|
94
|
+
return fragment((context) => {
|
|
95
|
+
let first = true;
|
|
96
|
+
for (const part of parts) {
|
|
97
|
+
if (!first) context.append(separator);
|
|
98
|
+
context.render(part);
|
|
99
|
+
first = false;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function parenthesize(part) {
|
|
104
|
+
return fragment((context) => {
|
|
105
|
+
context.append("(");
|
|
106
|
+
context.render(part);
|
|
107
|
+
context.append(")");
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function isFragment(value) {
|
|
111
|
+
return typeof value === "object" && value !== null && "render" in value && typeof value.render === "function";
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region src/expressions/types.ts
|
|
115
|
+
/** Runtime/type-level proof that an expression is safe to use in schema SQL. */
|
|
116
|
+
const schemaExpressionBrand = Symbol("qubu.schema-expression");
|
|
117
|
+
/** Add a concrete dialect requirement without dropping expression metadata. */
|
|
118
|
+
function withDialectCapability(expression, capability) {
|
|
119
|
+
const wrapped = makeExpression(expression.expressionKind, (context) => {
|
|
120
|
+
assertDialectCapability(context.dialect, capability);
|
|
121
|
+
context.render(expression);
|
|
122
|
+
}, expression.expressionCategory, resultValueOf(expression));
|
|
123
|
+
return isSchemaExpression(expression) ? markSchemaExpression(wrapped) : wrapped;
|
|
124
|
+
}
|
|
125
|
+
function makeExpression(expressionKind, render, expressionCategory, result) {
|
|
126
|
+
return attachResultValue(Object.freeze({
|
|
127
|
+
expressionKind,
|
|
128
|
+
...expressionCategory ? { expressionCategory } : {},
|
|
129
|
+
...fragment(render)
|
|
130
|
+
}), result);
|
|
131
|
+
}
|
|
132
|
+
/** Mark a query-only expression category without changing its SQL renderer. */
|
|
133
|
+
function markExpressionCategory(expression, category) {
|
|
134
|
+
return Object.freeze({
|
|
135
|
+
...expression,
|
|
136
|
+
expressionCategory: category
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Mark a built-in or explicitly audited renderer as schema-deterministic. Prefer
|
|
141
|
+
* {@link defineSchemaExpression} for application extensions because it supplies a restricted schema
|
|
142
|
+
* rendering context.
|
|
143
|
+
*/
|
|
144
|
+
function makeSchemaExpression(expressionKind, render, result) {
|
|
145
|
+
const expression = makeExpression(expressionKind, render, void 0, result);
|
|
146
|
+
return Object.freeze({
|
|
147
|
+
...expression,
|
|
148
|
+
[schemaExpressionBrand]: true
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/** Add the schema-determinism brand to an explicitly audited expression. */
|
|
152
|
+
function markSchemaExpression(expression) {
|
|
153
|
+
if (isSchemaExpression(expression)) return expression;
|
|
154
|
+
return Object.freeze({
|
|
155
|
+
...expression,
|
|
156
|
+
[schemaExpressionBrand]: true
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/** Test the runtime brand used by schema rendering and validation. */
|
|
160
|
+
function isSchemaExpression(value) {
|
|
161
|
+
return typeof value === "object" && value !== null && schemaExpressionBrand in value && value[schemaExpressionBrand] === true;
|
|
162
|
+
}
|
|
163
|
+
function isExpression(value) {
|
|
164
|
+
return typeof value === "object" && value !== null && "expressionKind" in value && "render" in value && typeof value.render === "function";
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/core/naming.ts
|
|
168
|
+
/** Convert an application-facing field name to a SQL-facing identifier. */
|
|
169
|
+
function snakeCaseIdentifier(name) {
|
|
170
|
+
return name.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[-\s]+/g, "_").toLowerCase();
|
|
171
|
+
}
|
|
172
|
+
function resolveSqlNames(fields) {
|
|
173
|
+
const sqlNames = {};
|
|
174
|
+
const fieldsBySqlName = /* @__PURE__ */ new Map();
|
|
175
|
+
for (const field of fields) {
|
|
176
|
+
const sqlName = field.sqlName ?? snakeCaseIdentifier(field.fieldName);
|
|
177
|
+
if (sqlName.length === 0) throw new Error(`SQL name for field "${field.fieldName}" cannot be empty`);
|
|
178
|
+
const existingField = fieldsBySqlName.get(sqlName);
|
|
179
|
+
if (existingField) throw new Error(`Fields "${existingField}" and "${field.fieldName}" both resolve to SQL name "${sqlName}"`);
|
|
180
|
+
fieldsBySqlName.set(sqlName, field.fieldName);
|
|
181
|
+
sqlNames[field.fieldName] = sqlName;
|
|
182
|
+
}
|
|
183
|
+
return Object.freeze(sqlNames);
|
|
184
|
+
}
|
|
185
|
+
//#endregion
|
|
186
|
+
//#region src/schema/metadata.ts
|
|
187
|
+
/** Error raised when relational metadata cannot be represented safely. */
|
|
188
|
+
var SchemaMetadataValidationError = class extends TypeError {
|
|
189
|
+
name = "SchemaMetadataValidationError";
|
|
190
|
+
diagnostics;
|
|
191
|
+
issues;
|
|
192
|
+
constructor(diagnostics) {
|
|
193
|
+
const frozenDiagnostics = Object.freeze(diagnostics.map((diagnostic) => Object.freeze({
|
|
194
|
+
...diagnostic,
|
|
195
|
+
path: Object.freeze([...diagnostic.path]),
|
|
196
|
+
relatedPaths: diagnostic.relatedPaths ? Object.freeze(diagnostic.relatedPaths.map((path) => Object.freeze([...path]))) : void 0
|
|
197
|
+
})));
|
|
198
|
+
super(frozenDiagnostics.map((diagnostic) => diagnostic.message).join("\n"));
|
|
199
|
+
this.diagnostics = frozenDiagnostics;
|
|
200
|
+
this.issues = frozenDiagnostics;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
/** Generate the version-one physical name for a relational object ID. */
|
|
204
|
+
function generatedSchemaObjectName(id) {
|
|
205
|
+
return snakeCaseIdentifier(id);
|
|
206
|
+
}
|
|
207
|
+
/** Deep-freeze plain metadata values without introducing executable nodes. */
|
|
208
|
+
function freezeSchemaMetadata(value) {
|
|
209
|
+
if (Array.isArray(value)) return Object.freeze(value.map((item) => freezeSchemaMetadata(item)));
|
|
210
|
+
if (typeof value !== "object" || value === null) return value;
|
|
211
|
+
const frozen = Object.fromEntries(Object.entries(value).map(([key, nested]) => [key, freezeSchemaMetadata(nested)]));
|
|
212
|
+
return Object.freeze(frozen);
|
|
213
|
+
}
|
|
214
|
+
/** Check the portable identifier subset used for generated metadata names. */
|
|
215
|
+
function isValidSchemaObjectName(name) {
|
|
216
|
+
return name.length > 0 && name === name.trim() && !/[.\\/\u0000-\u001f\u007f"']/u.test(name);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Attach a record key and resolved physical name without changing the enumerable shape of legacy
|
|
220
|
+
* constraint/index values. The properties are still ordinary read-only runtime metadata and are
|
|
221
|
+
* available to snapshot traversals.
|
|
222
|
+
*/
|
|
223
|
+
function materializeSchemaObjectIdentity(value, id, physicalName) {
|
|
224
|
+
const materialized = { ...value };
|
|
225
|
+
Object.defineProperties(materialized, {
|
|
226
|
+
id: {
|
|
227
|
+
configurable: false,
|
|
228
|
+
enumerable: false,
|
|
229
|
+
value: id,
|
|
230
|
+
writable: false
|
|
231
|
+
},
|
|
232
|
+
physicalName: {
|
|
233
|
+
configurable: false,
|
|
234
|
+
enumerable: false,
|
|
235
|
+
value: physicalName ?? generatedSchemaObjectName(id),
|
|
236
|
+
writable: false
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
return Object.freeze(materialized);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Resolve and validate names for one metadata record. The caller decides whether constraint and
|
|
243
|
+
* index names share a database namespace.
|
|
244
|
+
*/
|
|
245
|
+
function materializeSchemaObjectRecord(record, kind) {
|
|
246
|
+
const diagnostics = [];
|
|
247
|
+
const names = /* @__PURE__ */ new Map();
|
|
248
|
+
const result = {};
|
|
249
|
+
for (const [id, value] of Object.entries(record)) {
|
|
250
|
+
const physicalName = value.physicalName ?? generatedSchemaObjectName(id);
|
|
251
|
+
const path = [kind === "constraint" ? "constraints" : "indexes", id];
|
|
252
|
+
if (!isValidSchemaObjectName(physicalName)) diagnostics.push({
|
|
253
|
+
code: "invalid-physical-name",
|
|
254
|
+
message: `The ${kind} "${id}" has invalid physical name "${physicalName}"`,
|
|
255
|
+
path: [...path, "physicalName"]
|
|
256
|
+
});
|
|
257
|
+
const previousId = names.get(physicalName);
|
|
258
|
+
if (previousId !== void 0) diagnostics.push({
|
|
259
|
+
code: "duplicate-physical-name",
|
|
260
|
+
message: `The ${kind}s "${previousId}" and "${id}" both use physical name "${physicalName}"`,
|
|
261
|
+
path: [...path, "physicalName"],
|
|
262
|
+
relatedPaths: [[
|
|
263
|
+
kind === "constraint" ? "constraints" : "indexes",
|
|
264
|
+
previousId,
|
|
265
|
+
"physicalName"
|
|
266
|
+
]]
|
|
267
|
+
});
|
|
268
|
+
else names.set(physicalName, id);
|
|
269
|
+
result[id] = materializeSchemaObjectIdentity(value, id, physicalName);
|
|
270
|
+
}
|
|
271
|
+
if (diagnostics.length > 0) throw new SchemaMetadataValidationError(diagnostics);
|
|
272
|
+
return Object.freeze(result);
|
|
273
|
+
}
|
|
274
|
+
/** Return a diagnostic when a dialect extension is used by another adapter. */
|
|
275
|
+
function dialectMismatchDiagnostic(extension, dialect, path) {
|
|
276
|
+
if (extension.dialect === dialect) return;
|
|
277
|
+
return {
|
|
278
|
+
code: "dialect-mismatch",
|
|
279
|
+
message: `Dialect extension belongs to "${extension.dialect}" but the active schema dialect is "${dialect}"`,
|
|
280
|
+
path,
|
|
281
|
+
dialect
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/** Throw a structured error for one or more dialect capability findings. */
|
|
285
|
+
function assertSchemaDialectSupport(diagnostics) {
|
|
286
|
+
if (diagnostics.length > 0) throw new SchemaMetadataValidationError(diagnostics);
|
|
287
|
+
}
|
|
288
|
+
//#endregion
|
|
289
|
+
//#region src/schema/column-behavior.ts
|
|
290
|
+
/** A column behavior error with a stable code and optional property path. */
|
|
291
|
+
var ColumnBehaviorError = class extends TypeError {
|
|
292
|
+
code;
|
|
293
|
+
path;
|
|
294
|
+
constructor(code, message, path) {
|
|
295
|
+
super(message);
|
|
296
|
+
this.name = "ColumnBehaviorError";
|
|
297
|
+
this.code = code;
|
|
298
|
+
this.path = path;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
/** Build a canonical literal node from a supported JavaScript scalar. */
|
|
302
|
+
function canonicalLiteral(value) {
|
|
303
|
+
if (value === null) return Object.freeze({ kind: "null" });
|
|
304
|
+
if (typeof value === "boolean") return Object.freeze({
|
|
305
|
+
kind: "boolean",
|
|
306
|
+
value
|
|
307
|
+
});
|
|
308
|
+
if (typeof value === "string") return Object.freeze({
|
|
309
|
+
kind: "string",
|
|
310
|
+
value
|
|
311
|
+
});
|
|
312
|
+
if (typeof value === "bigint") return Object.freeze({
|
|
313
|
+
kind: "bigint",
|
|
314
|
+
value: String(value)
|
|
315
|
+
});
|
|
316
|
+
if (!Number.isFinite(value)) throw new ColumnBehaviorError("invalid-default", "Default literal numbers must be finite", "default.value");
|
|
317
|
+
return Object.freeze({
|
|
318
|
+
kind: "number",
|
|
319
|
+
value: Object.is(value, -0) ? "0" : String(value)
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
function literalDefault(value) {
|
|
323
|
+
return Object.freeze({
|
|
324
|
+
kind: "literal",
|
|
325
|
+
value: canonicalLiteral(value)
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/** Normalize a public default input into its canonical snapshot descriptor. */
|
|
329
|
+
function normalizeDefault(value) {
|
|
330
|
+
if (isSchemaExpression(value)) return Object.freeze({
|
|
331
|
+
kind: "expression",
|
|
332
|
+
expression: value
|
|
333
|
+
});
|
|
334
|
+
if (isExternalDefaultDescriptor(value)) return externalDefault();
|
|
335
|
+
return literalDefault(value);
|
|
336
|
+
}
|
|
337
|
+
/** Mark a legacy or externally managed database default explicitly. */
|
|
338
|
+
function externalDefault() {
|
|
339
|
+
return Object.freeze({ kind: "external" });
|
|
340
|
+
}
|
|
341
|
+
/** Create an immutable generated-column descriptor. */
|
|
342
|
+
function generatedColumn(expression, mode) {
|
|
343
|
+
assertSchemaExpression(expression, "generatedColumn.expression", "invalid-generated-column");
|
|
344
|
+
const resolvedMode = typeof mode === "string" ? mode : mode?.mode ?? "stored";
|
|
345
|
+
if (resolvedMode !== "stored" && resolvedMode !== "virtual") throw new ColumnBehaviorError("invalid-generated-column", `Generated-column mode must be "stored" or "virtual", received "${String(resolvedMode)}"`, "generatedColumn.mode");
|
|
346
|
+
return Object.freeze({
|
|
347
|
+
kind: "expression",
|
|
348
|
+
expression,
|
|
349
|
+
mode: resolvedMode
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
/** Mark a legacy or externally managed generated column explicitly. */
|
|
353
|
+
function externalGeneratedColumn() {
|
|
354
|
+
return Object.freeze({ kind: "external" });
|
|
355
|
+
}
|
|
356
|
+
/** Describe a database identity column without inventing a generated SQL expression. */
|
|
357
|
+
function identityColumn(generation, options) {
|
|
358
|
+
const resolvedGeneration = generation ?? "by-default";
|
|
359
|
+
if (resolvedGeneration !== "always" && resolvedGeneration !== "by-default") throw new ColumnBehaviorError("invalid-identity", `Identity generation must be "always" or "by-default", received "${String(resolvedGeneration)}"`, "identity.generation");
|
|
360
|
+
return Object.freeze({
|
|
361
|
+
kind: "identity",
|
|
362
|
+
generation: resolvedGeneration,
|
|
363
|
+
...options?.dialect === void 0 ? {} : { dialect: freezeSchemaMetadata(options.dialect) }
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
/** Normalize complete and legacy column behavior into immutable metadata. */
|
|
367
|
+
function resolveColumnBehavior(options) {
|
|
368
|
+
const hasDefaultFlag = options.hasDefault === true;
|
|
369
|
+
const generatedFlag = options.generated === true;
|
|
370
|
+
const defaultDescriptor = options.default === void 0 ? void 0 : normalizeDefault(options.default);
|
|
371
|
+
const generatedDescriptor = options.generatedColumn;
|
|
372
|
+
const identityDescriptor = options.identity;
|
|
373
|
+
const onUpdateExpression = options.onUpdate;
|
|
374
|
+
if (onUpdateExpression !== void 0 && !isSchemaExpression(onUpdateExpression)) throw new ColumnBehaviorError("invalid-on-update", "Column onUpdate metadata must carry the deterministic schema-expression brand", "onUpdate");
|
|
375
|
+
if (defaultDescriptor !== void 0) {
|
|
376
|
+
assertDefaultDescriptor(defaultDescriptor);
|
|
377
|
+
if (options.hasDefault === false) throw new ColumnBehaviorError("default-flag-conflict", "A complete default descriptor cannot be combined with hasDefault: false", "hasDefault");
|
|
378
|
+
if (generatedDescriptor !== void 0 || identityDescriptor !== void 0) throw new ColumnBehaviorError("default-generated-conflict", "A column cannot declare a complete default together with generated-column or identity metadata", "default");
|
|
379
|
+
if (generatedFlag) throw new ColumnBehaviorError("default-generated-conflict", "A complete default descriptor cannot be combined with generated: true", "default");
|
|
380
|
+
}
|
|
381
|
+
if (generatedDescriptor !== void 0) {
|
|
382
|
+
assertGeneratedColumnDescriptor(generatedDescriptor);
|
|
383
|
+
if (options.generated === false) throw new ColumnBehaviorError("generated-flag-conflict", "A complete generated-column descriptor cannot be combined with generated: false", "generated");
|
|
384
|
+
if (identityDescriptor !== void 0) throw new ColumnBehaviorError("identity-generated-conflict", "A column cannot declare both generated-column and identity metadata", "generatedColumn");
|
|
385
|
+
if (hasDefaultFlag) throw new ColumnBehaviorError("default-generated-conflict", "A complete generated-column descriptor cannot be combined with hasDefault: true", "generatedColumn");
|
|
386
|
+
}
|
|
387
|
+
if (identityDescriptor !== void 0) {
|
|
388
|
+
assertIdentityDescriptor(identityDescriptor);
|
|
389
|
+
if (options.generated === false) throw new ColumnBehaviorError("generated-flag-conflict", "Identity metadata cannot be combined with generated: false", "generated");
|
|
390
|
+
if (hasDefaultFlag) throw new ColumnBehaviorError("identity-generated-conflict", "Identity metadata cannot be combined with hasDefault: true", "identity");
|
|
391
|
+
}
|
|
392
|
+
const normalizedDefault = defaultDescriptor === void 0 ? void 0 : freezeDefaultDescriptor(defaultDescriptor);
|
|
393
|
+
const normalizedGenerated = generatedDescriptor === void 0 ? void 0 : freezeGeneratedColumnDescriptor(generatedDescriptor);
|
|
394
|
+
const normalizedIdentity = identityDescriptor === void 0 ? void 0 : freezeIdentityDescriptor(identityDescriptor);
|
|
395
|
+
return Object.freeze({
|
|
396
|
+
hasDefault: hasDefaultFlag || normalizedDefault !== void 0,
|
|
397
|
+
generated: generatedFlag || normalizedGenerated !== void 0 || normalizedIdentity !== void 0,
|
|
398
|
+
default: normalizedDefault ?? (hasDefaultFlag ? externalDefault() : void 0),
|
|
399
|
+
generatedColumn: normalizedGenerated ?? (generatedFlag && normalizedIdentity === void 0 ? externalGeneratedColumn() : void 0),
|
|
400
|
+
identity: normalizedIdentity,
|
|
401
|
+
onUpdate: onUpdateExpression
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
function freezeDefaultDescriptor(value) {
|
|
405
|
+
if (value.kind === "external") return externalDefault();
|
|
406
|
+
if (value.kind === "expression") return Object.freeze({
|
|
407
|
+
kind: "expression",
|
|
408
|
+
expression: value.expression
|
|
409
|
+
});
|
|
410
|
+
return Object.freeze({
|
|
411
|
+
kind: "literal",
|
|
412
|
+
value: Object.freeze({ ...value.value })
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
function isExternalDefaultDescriptor(value) {
|
|
416
|
+
return typeof value === "object" && value !== null && value.kind === "external";
|
|
417
|
+
}
|
|
418
|
+
function freezeGeneratedColumnDescriptor(value) {
|
|
419
|
+
if (value.kind === "external") return externalGeneratedColumn();
|
|
420
|
+
return Object.freeze({
|
|
421
|
+
kind: "expression",
|
|
422
|
+
expression: value.expression,
|
|
423
|
+
mode: value.mode
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
function freezeIdentityDescriptor(value) {
|
|
427
|
+
return Object.freeze({
|
|
428
|
+
kind: "identity",
|
|
429
|
+
generation: value.generation,
|
|
430
|
+
...value.dialect === void 0 ? {} : { dialect: freezeSchemaMetadata(value.dialect) }
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
function assertSchemaExpression(expression, path, code = "invalid-default") {
|
|
434
|
+
if (!isSchemaExpression(expression)) throw new ColumnBehaviorError(code, "Schema behavior expressions must carry the deterministic schema-expression brand", path);
|
|
435
|
+
}
|
|
436
|
+
function assertDefaultDescriptor(value) {
|
|
437
|
+
if (!value || typeof value !== "object") throw new ColumnBehaviorError("invalid-default", "Column default metadata must be an object", "default");
|
|
438
|
+
if (value.kind === "external") return;
|
|
439
|
+
if (value.kind === "expression") {
|
|
440
|
+
assertSchemaExpression(value.expression, "default.expression");
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (value.kind === "literal") {
|
|
444
|
+
assertCanonicalLiteral(value.value);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
throw new ColumnBehaviorError("invalid-default", "Unknown column default descriptor kind", "default.kind");
|
|
448
|
+
}
|
|
449
|
+
function assertGeneratedColumnDescriptor(value) {
|
|
450
|
+
if (!value || typeof value !== "object") throw new ColumnBehaviorError("invalid-generated-column", "Generated-column metadata must be an object", "generatedColumn");
|
|
451
|
+
if (value.kind === "external") return;
|
|
452
|
+
if (value.kind === "expression") {
|
|
453
|
+
assertSchemaExpression(value.expression, "generatedColumn.expression", "invalid-generated-column");
|
|
454
|
+
if (value.mode !== "stored" && value.mode !== "virtual") throw new ColumnBehaviorError("invalid-generated-column", "Generated-column mode must be \"stored\" or \"virtual\"", "generatedColumn.mode");
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
throw new ColumnBehaviorError("invalid-generated-column", "Unknown generated-column descriptor kind", "generatedColumn.kind");
|
|
458
|
+
}
|
|
459
|
+
function assertIdentityDescriptor(value) {
|
|
460
|
+
if (!value || typeof value !== "object" || value.kind !== "identity" || value.generation !== "always" && value.generation !== "by-default") throw new ColumnBehaviorError("invalid-identity", "Identity metadata must use generation \"always\" or \"by-default\"", "identity.generation");
|
|
461
|
+
if (value.dialect !== void 0) {
|
|
462
|
+
if (typeof value.dialect !== "object" || value.dialect === null || typeof value.dialect.dialect !== "string" || value.dialect.dialect.length === 0) throw new ColumnBehaviorError("invalid-identity", "Identity dialect metadata must contain a non-empty dialect tag", "identity.dialect");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function assertCanonicalLiteral(value) {
|
|
466
|
+
if (!value || typeof value !== "object") throw new ColumnBehaviorError("invalid-default", "Literal defaults must contain a canonical literal node", "default.value");
|
|
467
|
+
switch (value.kind) {
|
|
468
|
+
case "null": return;
|
|
469
|
+
case "boolean":
|
|
470
|
+
if (typeof value.value === "boolean") return;
|
|
471
|
+
break;
|
|
472
|
+
case "string":
|
|
473
|
+
if (typeof value.value === "string") return;
|
|
474
|
+
break;
|
|
475
|
+
case "number":
|
|
476
|
+
if (typeof value.value === "string" && value.value.length > 0) return;
|
|
477
|
+
break;
|
|
478
|
+
case "bigint": if (/^-?(0|[1-9][0-9]*)$/.test(value.value)) return;
|
|
479
|
+
}
|
|
480
|
+
throw new ColumnBehaviorError("invalid-default", "Literal defaults must contain a valid canonical literal node", "default.value");
|
|
481
|
+
}
|
|
482
|
+
//#endregion
|
|
483
|
+
//#region src/schema/column.ts
|
|
484
|
+
/** Create an immutable portable physical storage descriptor. */
|
|
485
|
+
function portableStorage(type) {
|
|
486
|
+
return Object.freeze({
|
|
487
|
+
kind: "portable",
|
|
488
|
+
type
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
function nativeStorage(dialectOrOptions, type) {
|
|
492
|
+
const dialect = typeof dialectOrOptions === "string" ? dialectOrOptions : dialectOrOptions.dialect;
|
|
493
|
+
const declaration = typeof dialectOrOptions === "string" ? type : "type" in dialectOrOptions ? dialectOrOptions.type : dialectOrOptions.declaration;
|
|
494
|
+
return Object.freeze({
|
|
495
|
+
kind: "native",
|
|
496
|
+
dialect,
|
|
497
|
+
type: declaration
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
function narrowColumnType() {
|
|
501
|
+
return this;
|
|
502
|
+
}
|
|
503
|
+
function withPortableCast(definition, type) {
|
|
504
|
+
return Object.freeze({
|
|
505
|
+
...definition,
|
|
506
|
+
castTarget: Object.freeze({
|
|
507
|
+
kind: "portable-cast",
|
|
508
|
+
type
|
|
509
|
+
})
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
function withPortableStorage(definition, type) {
|
|
513
|
+
return Object.freeze({
|
|
514
|
+
...definition,
|
|
515
|
+
storage: portableStorage(type)
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
function column(options) {
|
|
519
|
+
return Object.freeze({
|
|
520
|
+
definitionKind: "column",
|
|
521
|
+
nullable: options?.nullable === true,
|
|
522
|
+
...resolveColumnBehavior(options ?? {}),
|
|
523
|
+
sqlName: options?.sqlName,
|
|
524
|
+
storage: options?.storage ? Object.freeze({ ...options.storage }) : void 0,
|
|
525
|
+
...options?.decode === void 0 ? {} : { resultDecoder: options.decode },
|
|
526
|
+
$type: narrowColumnType,
|
|
527
|
+
castTarget: options?.castType ? Object.freeze({
|
|
528
|
+
kind: "named-cast",
|
|
529
|
+
typeName: options.castType
|
|
530
|
+
}) : void 0
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
/** Resolve the runtime result metadata carried by a column definition. */
|
|
534
|
+
function columnResultValue(definition) {
|
|
535
|
+
const storage = definition.storage;
|
|
536
|
+
return resultValue(storage?.kind === "portable" && (storage.type === "boolean" || storage.type === "date" || storage.type === "timestamp" || storage.type === "json") ? storage.type : void 0, definition.resultDecoder);
|
|
537
|
+
}
|
|
538
|
+
function nativeColumn(storageOrDialect, typeOrOptions, maybeOptions) {
|
|
539
|
+
const storage = typeof storageOrDialect === "string" ? nativeStorage(storageOrDialect, typeOrOptions) : storageOrDialect;
|
|
540
|
+
return column({
|
|
541
|
+
...typeof storageOrDialect === "string" ? maybeOptions : typeOrOptions,
|
|
542
|
+
storage
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
function nullable(definition) {
|
|
546
|
+
return Object.freeze({
|
|
547
|
+
...definition,
|
|
548
|
+
nullable: true
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
function integer(options) {
|
|
552
|
+
return withPortableStorage(withPortableCast(column(options), "integer"), "integer");
|
|
553
|
+
}
|
|
554
|
+
function numeric(options) {
|
|
555
|
+
return withPortableStorage(withPortableCast(column(options), "decimal"), "numeric");
|
|
556
|
+
}
|
|
557
|
+
function text(options) {
|
|
558
|
+
return withPortableStorage(withPortableCast(column(options), "text"), "text");
|
|
559
|
+
}
|
|
560
|
+
function boolean(options) {
|
|
561
|
+
return withPortableStorage(withPortableCast(column(options), "boolean"), "boolean");
|
|
562
|
+
}
|
|
563
|
+
function date(options) {
|
|
564
|
+
return withPortableStorage(withPortableCast(column(options), "date"), "date");
|
|
565
|
+
}
|
|
566
|
+
function timestamp(options) {
|
|
567
|
+
return withPortableStorage(withPortableCast(column(options), "timestamp"), "timestamp");
|
|
568
|
+
}
|
|
569
|
+
function uuid(options) {
|
|
570
|
+
return withPortableStorage(withPortableCast(column(options), "uuid"), "uuid");
|
|
571
|
+
}
|
|
572
|
+
function json(options) {
|
|
573
|
+
return withPortableStorage(withPortableCast(column(options), "json"), "json");
|
|
574
|
+
}
|
|
575
|
+
function bigint(options) {
|
|
576
|
+
return withPortableStorage(withPortableCast(column(options), "bigint"), "bigint");
|
|
577
|
+
}
|
|
578
|
+
function binary(options) {
|
|
579
|
+
return withPortableStorage(withPortableCast(column(options), "binary"), "binary");
|
|
580
|
+
}
|
|
581
|
+
//#endregion
|
|
582
|
+
//#region src/core/primitives/identifier.ts
|
|
583
|
+
function identifier(name) {
|
|
584
|
+
return fragment((context) => context.append(context.dialect.quoteIdentifier(name)));
|
|
585
|
+
}
|
|
586
|
+
function qualifiedIdentifier(...parts) {
|
|
587
|
+
return fragment((context) => {
|
|
588
|
+
parts.forEach((part, index) => {
|
|
589
|
+
if (index > 0) context.append(".");
|
|
590
|
+
context.append(context.dialect.quoteIdentifier(part));
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
//#endregion
|
|
595
|
+
//#region src/expressions/column.ts
|
|
596
|
+
function createColumnReference(columnName, sourceReference, fieldName, result) {
|
|
597
|
+
const expression = makeSchemaExpression("column", (context) => {
|
|
598
|
+
if (context.renderColumnReference) {
|
|
599
|
+
context.renderColumnReference(columnName);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
context.render(sourceReference);
|
|
603
|
+
context.append(".");
|
|
604
|
+
context.render(identifier(columnName));
|
|
605
|
+
}, result);
|
|
606
|
+
return Object.freeze({
|
|
607
|
+
...expression,
|
|
608
|
+
fieldName,
|
|
609
|
+
columnName
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
function isColumnReference(value) {
|
|
613
|
+
return typeof value === "object" && value !== null && "expressionKind" in value && value.expressionKind === "column";
|
|
614
|
+
}
|
|
615
|
+
/** Turn an expression into a fragment that renders it without changing it. */
|
|
616
|
+
function expressionFragment(expression) {
|
|
617
|
+
return fragment((context) => context.render(expression));
|
|
618
|
+
}
|
|
619
|
+
//#endregion
|
|
620
|
+
export { dateResultDecoder as $, assertSchemaDialectSupport as A, isSchemaExpression as B, canonicalLiteral as C, identityColumn as D, generatedColumn as E, materializeSchemaObjectIdentity as F, withDialectCapability as G, makeSchemaExpression as H, materializeSchemaObjectRecord as I, parenthesize as J, fragment as K, resolveSqlNames as L, freezeSchemaMetadata as M, generatedSchemaObjectName as N, resolveColumnBehavior as O, isValidSchemaObjectName as P, createResultShape as Q, snakeCaseIdentifier as R, ColumnBehaviorError as S, externalGeneratedColumn as T, markExpressionCategory as U, makeExpression as V, markSchemaExpression as W, ResultDecodingError as X, sequence as Y, booleanResultDecoder as Z, numeric as _, qualifiedIdentifier as a, timestampResultDecoder as at, timestamp as b, boolean as c, date as d, decodeResultRow as et, integer as f, nullable as g, nativeStorage as h, identifier as i, resultValueOf as it, dialectMismatchDiagnostic as j, SchemaMetadataValidationError as k, column as l, nativeColumn as m, expressionFragment as n, resultShapeValue as nt, bigint as o, json as p, isFragment as q, isColumnReference as r, resultValue as rt, binary as s, createColumnReference as t, jsonTextResultDecoder as tt, columnResultValue as u, portableStorage as v, externalDefault as w, uuid as x, text as y, isExpression as z };
|