@hulla/db 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +263 -2
- package/THIRD_PARTY_NOTICES.md +850 -0
- package/dist/aggregate.d.ts +63 -0
- package/dist/aggregate.js +147 -0
- package/dist/auth.d.ts +10 -0
- package/dist/auth.js +22 -0
- package/dist/bindings.d.ts +56 -0
- package/dist/bindings.js +277 -0
- package/dist/browser.d.ts +12 -0
- package/dist/browser.js +24 -0
- package/dist/callable.d.ts +48 -0
- package/dist/callable.js +142 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +65 -0
- package/dist/codec.d.ts +32 -0
- package/dist/codec.js +129 -0
- package/dist/codegen.d.ts +19 -0
- package/dist/codegen.js +275 -0
- package/dist/config.d.ts +70 -0
- package/dist/config.js +100 -0
- package/dist/contracts.d.ts +109 -0
- package/dist/contracts.js +1 -0
- package/dist/database-builder.d.ts +19 -0
- package/dist/database-builder.js +183 -0
- package/dist/database.d.ts +69 -0
- package/dist/database.js +128 -0
- package/dist/declarations.d.ts +91 -0
- package/dist/declarations.js +141 -0
- package/dist/dev-worker.d.ts +1 -0
- package/dist/dev-worker.js +47 -0
- package/dist/dev.d.ts +5 -0
- package/dist/dev.js +148 -0
- package/dist/error.d.ts +20 -0
- package/dist/error.js +82 -0
- package/dist/fields.d.ts +36 -0
- package/dist/fields.js +63 -0
- package/dist/filters.d.ts +93 -0
- package/dist/filters.js +220 -0
- package/dist/function-shapes.d.ts +7 -0
- package/dist/function-shapes.js +150 -0
- package/dist/handles.d.ts +27 -0
- package/dist/handles.js +387 -0
- package/dist/http/protocol.d.ts +64 -0
- package/dist/http/protocol.js +77 -0
- package/dist/http/sse.d.ts +7 -0
- package/dist/http/sse.js +70 -0
- package/dist/http.d.ts +44 -0
- package/dist/http.js +911 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1 -0
- package/dist/internal/client.d.ts +6 -0
- package/dist/internal/client.js +5 -0
- package/dist/json-value.d.ts +21 -0
- package/dist/json-value.js +79 -0
- package/dist/names.d.ts +2 -0
- package/dist/names.js +10 -0
- package/dist/native.d.ts +3 -0
- package/dist/native.js +42 -0
- package/dist/notifications.d.ts +8 -0
- package/dist/notifications.js +70 -0
- package/dist/optimistic.d.ts +46 -0
- package/dist/optimistic.js +140 -0
- package/dist/page.d.ts +11 -0
- package/dist/page.js +1 -0
- package/dist/pagination.d.ts +17 -0
- package/dist/pagination.js +262 -0
- package/dist/project-codegen.d.ts +2 -0
- package/dist/project-codegen.js +68 -0
- package/dist/projection.d.ts +19 -0
- package/dist/projection.js +31 -0
- package/dist/runtime-conditions.d.ts +2 -0
- package/dist/runtime-conditions.js +10 -0
- package/dist/runtime.d.ts +13 -0
- package/dist/runtime.js +47 -0
- package/dist/schema-codegen.d.ts +16 -0
- package/dist/schema-codegen.js +212 -0
- package/dist/schema-contract.d.ts +4 -0
- package/dist/schema-contract.js +42 -0
- package/dist/schema.d.ts +356 -0
- package/dist/schema.js +720 -0
- package/dist/server-database.d.ts +14 -0
- package/dist/server-database.js +386 -0
- package/dist/server.d.ts +7 -0
- package/dist/server.js +1 -0
- package/dist/service.d.ts +13 -0
- package/dist/service.js +206 -0
- package/dist/sql-check.d.ts +28 -0
- package/dist/sql-check.js +59 -0
- package/dist/validation.d.ts +43 -0
- package/dist/validation.js +21 -0
- package/dist/worker-codegen.d.ts +3 -0
- package/dist/worker-codegen.js +44 -0
- package/dist/worker.d.ts +9 -0
- package/dist/worker.js +92 -0
- package/dist/write-expression.d.ts +72 -0
- package/dist/write-expression.js +122 -0
- package/dist/zod.d.ts +28 -0
- package/dist/zod.js +102 -0
- package/licenses/allocator-api2-0.2.21/LICENSE-APACHE +176 -0
- package/licenses/allocator-api2-0.2.21/LICENSE-MIT +23 -0
- package/licenses/async-trait-0.1.92/LICENSE-APACHE +176 -0
- package/licenses/async-trait-0.1.92/LICENSE-MIT +23 -0
- package/licenses/atomic-waker-1.1.2/LICENSE-APACHE +201 -0
- package/licenses/atomic-waker-1.1.2/LICENSE-MIT +23 -0
- package/licenses/atomic-waker-1.1.2/LICENSE-THIRD-PARTY +45 -0
- package/licenses/axum-0.8.9/LICENSE +25 -0
- package/licenses/axum-core-0.5.6/LICENSE +27 -0
- package/licenses/base64-0.22.1/LICENSE-APACHE +201 -0
- package/licenses/base64-0.22.1/LICENSE-MIT +21 -0
- package/licenses/bitflags-2.13.2/LICENSE-APACHE +201 -0
- package/licenses/bitflags-2.13.2/LICENSE-MIT +25 -0
- package/licenses/block-buffer-0.12.1/LICENSE-APACHE +201 -0
- package/licenses/block-buffer-0.12.1/LICENSE-MIT +25 -0
- package/licenses/byteorder-1.5.0/COPYING +3 -0
- package/licenses/byteorder-1.5.0/LICENSE-MIT +21 -0
- package/licenses/bytes-1.12.1/LICENSE +25 -0
- package/licenses/cc-1.6.0/LICENSE-APACHE +201 -0
- package/licenses/cc-1.6.0/LICENSE-MIT +25 -0
- package/licenses/cfg-if-1.0.5/LICENSE-APACHE +201 -0
- package/licenses/cfg-if-1.0.5/LICENSE-MIT +25 -0
- package/licenses/chacha20-0.10.2/LICENSE-APACHE +201 -0
- package/licenses/chacha20-0.10.2/LICENSE-MIT +25 -0
- package/licenses/cmov-0.5.4/LICENSE-APACHE +202 -0
- package/licenses/cmov-0.5.4/LICENSE-MIT +25 -0
- package/licenses/const-oid-0.10.2/LICENSE-APACHE +201 -0
- package/licenses/const-oid-0.10.2/LICENSE-MIT +25 -0
- package/licenses/const-oid-0.9.6/LICENSE-APACHE +201 -0
- package/licenses/const-oid-0.9.6/LICENSE-MIT +25 -0
- package/licenses/cpufeatures-0.3.1/LICENSE-APACHE +201 -0
- package/licenses/cpufeatures-0.3.1/LICENSE-MIT +25 -0
- package/licenses/crypto-common-0.2.2/LICENSE-APACHE +201 -0
- package/licenses/crypto-common-0.2.2/LICENSE-MIT +25 -0
- package/licenses/ctutils-0.4.2/LICENSE-APACHE +202 -0
- package/licenses/ctutils-0.4.2/LICENSE-MIT +25 -0
- package/licenses/der-0.7.10/LICENSE-APACHE +201 -0
- package/licenses/der-0.7.10/LICENSE-MIT +25 -0
- package/licenses/der_derive-0.7.3/LICENSE-APACHE +201 -0
- package/licenses/der_derive-0.7.3/LICENSE-MIT +25 -0
- package/licenses/deranged-0.5.8/LICENSE-Apache +202 -0
- package/licenses/deranged-0.5.8/LICENSE-MIT +19 -0
- package/licenses/digest-0.11.3/LICENSE-APACHE +201 -0
- package/licenses/digest-0.11.3/LICENSE-MIT +26 -0
- package/licenses/equivalent-1.0.3/LICENSE-APACHE +201 -0
- package/licenses/equivalent-1.0.3/LICENSE-MIT +25 -0
- package/licenses/errno-0.3.14/LICENSE-APACHE +201 -0
- package/licenses/errno-0.3.14/LICENSE-MIT +25 -0
- package/licenses/fallible-iterator-0.2.0/LICENSE-APACHE +202 -0
- package/licenses/fallible-iterator-0.2.0/LICENSE-MIT +19 -0
- package/licenses/find-msvc-tools-0.1.14/LICENSE-APACHE +201 -0
- package/licenses/find-msvc-tools-0.1.14/LICENSE-MIT +25 -0
- package/licenses/flagset-0.4.7/LICENSE +202 -0
- package/licenses/foldhash-0.2.0/LICENSE +19 -0
- package/licenses/futures-channel-0.3.34/LICENSE-APACHE +202 -0
- package/licenses/futures-channel-0.3.34/LICENSE-MIT +26 -0
- package/licenses/futures-core-0.3.34/LICENSE-APACHE +202 -0
- package/licenses/futures-core-0.3.34/LICENSE-MIT +26 -0
- package/licenses/futures-sink-0.3.34/LICENSE-APACHE +202 -0
- package/licenses/futures-sink-0.3.34/LICENSE-MIT +26 -0
- package/licenses/futures-task-0.3.34/LICENSE-APACHE +202 -0
- package/licenses/futures-task-0.3.34/LICENSE-MIT +26 -0
- package/licenses/futures-util-0.3.34/LICENSE-APACHE +202 -0
- package/licenses/futures-util-0.3.34/LICENSE-MIT +26 -0
- package/licenses/getrandom-0.2.17/LICENSE-APACHE +201 -0
- package/licenses/getrandom-0.2.17/LICENSE-MIT +26 -0
- package/licenses/getrandom-0.4.3/LICENSE-APACHE +201 -0
- package/licenses/getrandom-0.4.3/LICENSE-MIT +26 -0
- package/licenses/hashbrown-0.17.1/LICENSE-APACHE +201 -0
- package/licenses/hashbrown-0.17.1/LICENSE-MIT +25 -0
- package/licenses/hmac-0.13.0/LICENSE-APACHE +201 -0
- package/licenses/hmac-0.13.0/LICENSE-MIT +25 -0
- package/licenses/http-1.5.0/LICENSE-APACHE +201 -0
- package/licenses/http-1.5.0/LICENSE-MIT +25 -0
- package/licenses/http-body-1.1.0/LICENSE +25 -0
- package/licenses/http-body-util-0.1.5/LICENSE +25 -0
- package/licenses/httparse-1.10.1/LICENSE-APACHE +201 -0
- package/licenses/httparse-1.10.1/LICENSE-MIT +20 -0
- package/licenses/httpdate-1.0.3/LICENSE-APACHE +201 -0
- package/licenses/httpdate-1.0.3/LICENSE-MIT +19 -0
- package/licenses/hybrid-array-0.4.15/LICENSE-APACHE +201 -0
- package/licenses/hybrid-array-0.4.15/LICENSE-MIT +25 -0
- package/licenses/hyper-1.11.1/LICENSE +19 -0
- package/licenses/hyper-util-0.1.21/LICENSE +19 -0
- package/licenses/itoa-1.0.18/LICENSE-APACHE +176 -0
- package/licenses/itoa-1.0.18/LICENSE-MIT +23 -0
- package/licenses/libc-0.2.189/LICENSE-APACHE +176 -0
- package/licenses/libc-0.2.189/LICENSE-MIT +25 -0
- package/licenses/lock_api-0.4.14/LICENSE-APACHE +201 -0
- package/licenses/lock_api-0.4.14/LICENSE-MIT +25 -0
- package/licenses/log-0.4.34/LICENSE-APACHE +201 -0
- package/licenses/log-0.4.34/LICENSE-MIT +25 -0
- package/licenses/matchit-0.8.4/LICENSE +21 -0
- package/licenses/matchit-0.8.4/LICENSE.httprouter +29 -0
- package/licenses/md-5-0.11.0/LICENSE-APACHE +201 -0
- package/licenses/md-5-0.11.0/LICENSE-MIT +28 -0
- package/licenses/memchr-2.8.3/COPYING +3 -0
- package/licenses/memchr-2.8.3/LICENSE-MIT +21 -0
- package/licenses/mime-0.3.17/LICENSE-APACHE +201 -0
- package/licenses/mime-0.3.17/LICENSE-MIT +20 -0
- package/licenses/mio-1.2.3/LICENSE +19 -0
- package/licenses/num-conv-0.2.2/LICENSE-Apache +177 -0
- package/licenses/num-conv-0.2.2/LICENSE-MIT +19 -0
- package/licenses/objc2-core-foundation-0.3.2/LICENSE.md +34 -0
- package/licenses/objc2-system-configuration-0.3.2/LICENSE.md +34 -0
- package/licenses/once_cell-1.21.4/LICENSE-APACHE +201 -0
- package/licenses/once_cell-1.21.4/LICENSE-MIT +23 -0
- package/licenses/parking_lot-0.12.5/LICENSE-APACHE +201 -0
- package/licenses/parking_lot-0.12.5/LICENSE-MIT +25 -0
- package/licenses/parking_lot_core-0.9.12/LICENSE-APACHE +201 -0
- package/licenses/parking_lot_core-0.9.12/LICENSE-MIT +25 -0
- package/licenses/percent-encoding-2.3.2/LICENSE-APACHE +201 -0
- package/licenses/percent-encoding-2.3.2/LICENSE-MIT +25 -0
- package/licenses/phf-0.13.1/LICENSE +20 -0
- package/licenses/phf_shared-0.13.1/LICENSE +20 -0
- package/licenses/pin-project-lite-0.2.17/LICENSE-APACHE +177 -0
- package/licenses/pin-project-lite-0.2.17/LICENSE-MIT +23 -0
- package/licenses/postgres-protocol-0.6.12/LICENSE-APACHE +201 -0
- package/licenses/postgres-protocol-0.6.12/LICENSE-MIT +22 -0
- package/licenses/postgres-types-0.2.14/LICENSE-APACHE +201 -0
- package/licenses/postgres-types-0.2.14/LICENSE-MIT +22 -0
- package/licenses/powerfmt-0.2.1/LICENSE-Apache +202 -0
- package/licenses/powerfmt-0.2.1/LICENSE-MIT +19 -0
- package/licenses/proc-macro2-1.0.107/LICENSE-APACHE +176 -0
- package/licenses/proc-macro2-1.0.107/LICENSE-MIT +23 -0
- package/licenses/quote-1.0.47/LICENSE-APACHE +176 -0
- package/licenses/quote-1.0.47/LICENSE-MIT +23 -0
- package/licenses/rand-0.10.3/COPYRIGHT +12 -0
- package/licenses/rand-0.10.3/LICENSE-APACHE +176 -0
- package/licenses/rand-0.10.3/LICENSE-MIT +26 -0
- package/licenses/rand_core-0.10.1/COPYRIGHT +9 -0
- package/licenses/rand_core-0.10.1/LICENSE-APACHE +187 -0
- package/licenses/rand_core-0.10.1/LICENSE-MIT +25 -0
- package/licenses/ring-0.17.14/LICENSE +9 -0
- package/licenses/ring-0.17.14/LICENSE-BoringSSL +272 -0
- package/licenses/ring-0.17.14/LICENSE-other-bits +13 -0
- package/licenses/ring-0.17.14/src/polyfill/once_cell/LICENSE-APACHE +201 -0
- package/licenses/ring-0.17.14/src/polyfill/once_cell/LICENSE-MIT +23 -0
- package/licenses/ring-0.17.14/third_party/fiat/LICENSE +15 -0
- package/licenses/rquickjs-0.14.0/LICENSE +22 -0
- package/licenses/rquickjs-core-0.14.0/LICENSE +22 -0
- package/licenses/rquickjs-sys-0.14.0/quickjs/LICENSE +24 -0
- package/licenses/rquickjs-sys-0.14.0/vendor/wasi-libc/NOTICE.md +26 -0
- package/licenses/rustls-0.23.46/LICENSE-APACHE +201 -0
- package/licenses/rustls-0.23.46/LICENSE-ISC +15 -0
- package/licenses/rustls-0.23.46/LICENSE-MIT +25 -0
- package/licenses/rustls-pki-types-1.15.1/LICENSE-APACHE +201 -0
- package/licenses/rustls-pki-types-1.15.1/LICENSE-MIT +25 -0
- package/licenses/rustls-webpki-0.103.15/LICENSE +19 -0
- package/licenses/scopeguard-1.2.0/LICENSE-APACHE +201 -0
- package/licenses/scopeguard-1.2.0/LICENSE-MIT +25 -0
- package/licenses/serde-1.0.229/LICENSE-APACHE +176 -0
- package/licenses/serde-1.0.229/LICENSE-MIT +23 -0
- package/licenses/serde_core-1.0.229/LICENSE-APACHE +176 -0
- package/licenses/serde_core-1.0.229/LICENSE-MIT +23 -0
- package/licenses/serde_derive-1.0.229/LICENSE-APACHE +176 -0
- package/licenses/serde_derive-1.0.229/LICENSE-MIT +23 -0
- package/licenses/serde_json-1.0.151/LICENSE-APACHE +176 -0
- package/licenses/serde_json-1.0.151/LICENSE-MIT +23 -0
- package/licenses/serde_path_to_error-0.1.20/LICENSE-APACHE +176 -0
- package/licenses/serde_path_to_error-0.1.20/LICENSE-MIT +23 -0
- package/licenses/sha2-0.11.0/LICENSE-APACHE +201 -0
- package/licenses/sha2-0.11.0/LICENSE-MIT +28 -0
- package/licenses/shlex-2.0.1/LICENSE-APACHE +13 -0
- package/licenses/shlex-2.0.1/LICENSE-MIT +21 -0
- package/licenses/signal-hook-registry-1.4.8/LICENSE-APACHE +201 -0
- package/licenses/signal-hook-registry-1.4.8/LICENSE-MIT +25 -0
- package/licenses/siphasher-1.0.4/COPYING +7 -0
- package/licenses/siphasher-1.0.4/LICENSE-APACHE +202 -0
- package/licenses/siphasher-1.0.4/LICENSE-MIT +17 -0
- package/licenses/slab-0.4.12/LICENSE +25 -0
- package/licenses/smallvec-1.16.2/LICENSE-APACHE +201 -0
- package/licenses/smallvec-1.16.2/LICENSE-MIT +25 -0
- package/licenses/socket2-0.6.5/LICENSE-APACHE +201 -0
- package/licenses/socket2-0.6.5/LICENSE-MIT +25 -0
- package/licenses/spki-0.7.3/LICENSE-APACHE +201 -0
- package/licenses/spki-0.7.3/LICENSE-MIT +25 -0
- package/licenses/stringprep-0.1.5/LICENSE-APACHE +202 -0
- package/licenses/stringprep-0.1.5/LICENSE-MIT +19 -0
- package/licenses/subtle-2.6.1/LICENSE +29 -0
- package/licenses/syn-2.0.119/LICENSE-APACHE +176 -0
- package/licenses/syn-2.0.119/LICENSE-MIT +23 -0
- package/licenses/syn-3.0.6/LICENSE-APACHE +176 -0
- package/licenses/syn-3.0.6/LICENSE-MIT +23 -0
- package/licenses/sync_wrapper-1.0.2/LICENSE +177 -0
- package/licenses/time-0.3.55/LICENSE-Apache +177 -0
- package/licenses/time-0.3.55/LICENSE-MIT +19 -0
- package/licenses/time-core-0.1.9/LICENSE-Apache +177 -0
- package/licenses/time-core-0.1.9/LICENSE-MIT +19 -0
- package/licenses/time-macros-0.2.32/LICENSE-Apache +177 -0
- package/licenses/time-macros-0.2.32/LICENSE-MIT +19 -0
- package/licenses/tinyvec-1.13.3/LICENSE-APACHE.md +202 -0
- package/licenses/tinyvec-1.13.3/LICENSE-MIT.md +5 -0
- package/licenses/tinyvec-1.13.3/LICENSE-ZLIB.md +11 -0
- package/licenses/tokio-1.53.1/LICENSE +21 -0
- package/licenses/tokio-macros-2.7.2/LICENSE +22 -0
- package/licenses/tokio-postgres-0.7.18/LICENSE-APACHE +201 -0
- package/licenses/tokio-postgres-0.7.18/LICENSE-MIT +22 -0
- package/licenses/tokio-postgres-rustls-0.14.0/LICENSE +21 -0
- package/licenses/tokio-rustls-0.26.6/LICENSE-APACHE +201 -0
- package/licenses/tokio-rustls-0.26.6/LICENSE-MIT +25 -0
- package/licenses/tokio-util-0.7.19/LICENSE +21 -0
- package/licenses/tower-0.5.3/LICENSE +25 -0
- package/licenses/tower-layer-0.3.3/LICENSE +25 -0
- package/licenses/tower-service-0.3.3/LICENSE +25 -0
- package/licenses/typenum-1.20.1/LICENSE +1 -0
- package/licenses/typenum-1.20.1/LICENSE-APACHE +201 -0
- package/licenses/typenum-1.20.1/LICENSE-MIT +21 -0
- package/licenses/unicode-bidi-0.3.18/COPYRIGHT +8 -0
- package/licenses/unicode-bidi-0.3.18/LICENSE-APACHE +201 -0
- package/licenses/unicode-bidi-0.3.18/LICENSE-MIT +25 -0
- package/licenses/unicode-ident-1.0.26/LICENSE-APACHE +176 -0
- package/licenses/unicode-ident-1.0.26/LICENSE-MIT +23 -0
- package/licenses/unicode-ident-1.0.26/LICENSE-UNICODE +39 -0
- package/licenses/unicode-normalization-0.1.25/COPYRIGHT +7 -0
- package/licenses/unicode-normalization-0.1.25/LICENSE-APACHE +201 -0
- package/licenses/unicode-normalization-0.1.25/LICENSE-MIT +25 -0
- package/licenses/unicode-properties-0.1.4/COPYRIGHT +7 -0
- package/licenses/unicode-properties-0.1.4/LICENSE-APACHE +201 -0
- package/licenses/unicode-properties-0.1.4/LICENSE-MIT +25 -0
- package/licenses/untrusted-0.9.0/LICENSE.txt +13 -0
- package/licenses/uuid-1.26.1/LICENSE-APACHE +201 -0
- package/licenses/uuid-1.26.1/LICENSE-MIT +26 -0
- package/licenses/webpki-roots-1.0.9/LICENSE +61 -0
- package/licenses/whoami-2.1.3/LICENSE_APACHE +176 -0
- package/licenses/whoami-2.1.3/LICENSE_BOOST +23 -0
- package/licenses/whoami-2.1.3/LICENSE_MIT +19 -0
- package/licenses/x509-cert-0.2.5/LICENSE-APACHE +201 -0
- package/licenses/x509-cert-0.2.5/LICENSE-MIT +25 -0
- package/licenses/zeroize-1.9.1/LICENSE-APACHE +202 -0
- package/licenses/zeroize-1.9.1/LICENSE-MIT +25 -0
- package/licenses/zmij-1.0.23/LICENSE-MIT +23 -0
- package/native/darwin-arm64/hulladb-bridge +0 -0
- package/native/darwin-arm64/hulladb-js +0 -0
- package/native/darwin-arm64/hulladb-service +0 -0
- package/native/linux-x64/hulladb-bridge +0 -0
- package/native/linux-x64/hulladb-js +0 -0
- package/native/linux-x64/hulladb-service +0 -0
- package/package.json +90 -4
package/dist/schema.js
ADDED
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
import { validateJson } from "./json-value.js";
|
|
2
|
+
import { compileChecks } from "./sql-check.js";
|
|
3
|
+
export { sql } from "./sql-check.js";
|
|
4
|
+
export { createTables } from "./fields.js";
|
|
5
|
+
const fieldBrand = Symbol("Hulla schema field");
|
|
6
|
+
const representations = new WeakMap();
|
|
7
|
+
function synchronous(value) {
|
|
8
|
+
if (value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function") {
|
|
9
|
+
Promise.resolve(value).catch(() => { });
|
|
10
|
+
throw new TypeError("Field codecs must be synchronous");
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
/** Immutable application validator with an explicit storage representation. */
|
|
15
|
+
class Field {
|
|
16
|
+
[fieldBrand] = true;
|
|
17
|
+
scalar;
|
|
18
|
+
allowsNull;
|
|
19
|
+
hasDefault;
|
|
20
|
+
defaultValue;
|
|
21
|
+
target;
|
|
22
|
+
jsonShape;
|
|
23
|
+
precision;
|
|
24
|
+
mode;
|
|
25
|
+
customCodec;
|
|
26
|
+
constraints;
|
|
27
|
+
onDelete;
|
|
28
|
+
constructor(scalar, allowsNull, hasDefault, defaultValue, target, jsonShape, representation, options = {}) {
|
|
29
|
+
this.scalar = scalar;
|
|
30
|
+
this.allowsNull = allowsNull;
|
|
31
|
+
this.hasDefault = hasDefault;
|
|
32
|
+
this.defaultValue = defaultValue;
|
|
33
|
+
this.target = target;
|
|
34
|
+
if (jsonShape)
|
|
35
|
+
this.jsonShape = Object.freeze(jsonShape);
|
|
36
|
+
if (options.precision !== undefined)
|
|
37
|
+
this.precision = options.precision;
|
|
38
|
+
if (options.mode !== undefined)
|
|
39
|
+
this.mode = options.mode;
|
|
40
|
+
this.customCodec = options.customCodec ?? false;
|
|
41
|
+
if (options.constraints)
|
|
42
|
+
this.constraints = Object.freeze({ ...options.constraints, ...(options.constraints.oneOf ? { oneOf: Object.freeze([...options.constraints.oneOf]) } : {}) });
|
|
43
|
+
if (options.onDelete)
|
|
44
|
+
this.onDelete = options.onDelete;
|
|
45
|
+
representations.set(this, representation ?? { encode: value => value, decode: value => value });
|
|
46
|
+
Object.freeze(this);
|
|
47
|
+
}
|
|
48
|
+
copy(allowsNull, hasDefault, defaultValue, target, representation = representations.get(this), customCodec = this.customCodec, constraints = this.constraints) {
|
|
49
|
+
return new Field(this.scalar, allowsNull, hasDefault, defaultValue, target, this.jsonShape, representation, { ...(this.precision === undefined ? {} : { precision: this.precision }), ...(this.mode === undefined ? {} : { mode: this.mode }), ...(this.onDelete === undefined ? {} : { onDelete: this.onDelete }), ...(constraints === undefined ? {} : { constraints }), customCodec });
|
|
50
|
+
}
|
|
51
|
+
optional() { return jsonValidator(this).optional(); }
|
|
52
|
+
nullable() { return this.copy(true, this.hasDefault, this.defaultValue, this.target); }
|
|
53
|
+
default(value) { return this.copy(this.allowsNull, true, this.target === undefined ? this.parse(value) : value, this.target); }
|
|
54
|
+
min(value) { return this.bound("min", value); }
|
|
55
|
+
max(value) { return this.bound("max", value); }
|
|
56
|
+
bound(kind, value) {
|
|
57
|
+
if (this.target !== undefined)
|
|
58
|
+
throw new TypeError("v.id inherits its target primary key constraints; configure the target primary key or a named SQL check");
|
|
59
|
+
if (!["string", "float64", "int64"].includes(this.scalar))
|
|
60
|
+
throw new TypeError("Bounds require a string or numeric field");
|
|
61
|
+
if (this.scalar === "int64") {
|
|
62
|
+
if (typeof value === "number" && !Number.isSafeInteger(value))
|
|
63
|
+
throw new TypeError("int64 bounds require safe integers or bigint");
|
|
64
|
+
value = this.storage(typeof value === "number" ? BigInt(value) : value);
|
|
65
|
+
}
|
|
66
|
+
else if (typeof value !== "number" || !Number.isFinite(value) || (this.scalar === "string" && (!Number.isSafeInteger(value) || value < 0)))
|
|
67
|
+
throw new TypeError("Invalid field bound");
|
|
68
|
+
const previous = this.constraints?.[kind];
|
|
69
|
+
// Chaining refines constraints; it never silently weakens an earlier check.
|
|
70
|
+
if (previous !== undefined)
|
|
71
|
+
value = kind === "min" ? (value > previous ? value : previous) : (value < previous ? value : previous);
|
|
72
|
+
return this.constrained({ ...this.constraints, [kind]: value });
|
|
73
|
+
}
|
|
74
|
+
oneOf(values) {
|
|
75
|
+
if (this.target !== undefined)
|
|
76
|
+
throw new TypeError("v.id inherits its target primary key constraints; configure the target primary key or a named SQL check");
|
|
77
|
+
if (this.scalar === "json" || !Array.isArray(values) || !values.length)
|
|
78
|
+
throw new TypeError("oneOf requires a nonempty scalar value list");
|
|
79
|
+
const stored = [...new Set(values.map(value => this.encode(value)))];
|
|
80
|
+
if (stored.some(value => value === null || !["string", "number", "bigint", "boolean"].includes(typeof value)))
|
|
81
|
+
throw new TypeError("oneOf values must be non-null scalars");
|
|
82
|
+
return this.constrained({ ...this.constraints, oneOf: stored });
|
|
83
|
+
}
|
|
84
|
+
constrained(constraints) {
|
|
85
|
+
if (constraints.min !== undefined && constraints.max !== undefined && constraints.min > constraints.max)
|
|
86
|
+
throw new TypeError("Contradictory field bounds");
|
|
87
|
+
const field = this.copy(this.allowsNull, this.hasDefault, this.defaultValue, this.target, representations.get(this), this.customCodec, constraints);
|
|
88
|
+
if (constraints.oneOf)
|
|
89
|
+
for (const value of constraints.oneOf)
|
|
90
|
+
field.checkConstraints(value);
|
|
91
|
+
if (field.hasDefault)
|
|
92
|
+
field.parse(field.defaultValue);
|
|
93
|
+
return field;
|
|
94
|
+
}
|
|
95
|
+
checkConstraints(value) {
|
|
96
|
+
if (value === null || !this.constraints)
|
|
97
|
+
return;
|
|
98
|
+
const measured = this.scalar === "string" ? [...value].length : value;
|
|
99
|
+
if (this.constraints.min !== undefined && measured < this.constraints.min)
|
|
100
|
+
throw new TypeError("Value is below field minimum");
|
|
101
|
+
if (this.constraints.max !== undefined && measured > this.constraints.max)
|
|
102
|
+
throw new TypeError("Value exceeds field maximum");
|
|
103
|
+
if (this.constraints.oneOf && !this.constraints.oneOf.includes(value))
|
|
104
|
+
throw new TypeError("Value is not in field oneOf values");
|
|
105
|
+
}
|
|
106
|
+
codec(codec) {
|
|
107
|
+
if (this.target !== undefined)
|
|
108
|
+
throw new TypeError("v.id inherits its target primary key codec; configure the codec on the primary key");
|
|
109
|
+
if (!codec || typeof codec.encode !== "function" || typeof codec.decode !== "function")
|
|
110
|
+
throw new TypeError("Expected encode and decode codec functions");
|
|
111
|
+
const prior = representations.get(this);
|
|
112
|
+
const representation = { encode: (value) => prior.encode(synchronous(codec.encode(value))), decode: (value) => synchronous(codec.decode(prior.decode(value))) };
|
|
113
|
+
const defaultValue = this.hasDefault && this.defaultValue !== null ? synchronous(codec.decode(this.defaultValue)) : this.defaultValue;
|
|
114
|
+
return this.copy(this.allowsNull, this.hasDefault, defaultValue, this.target, representation, true);
|
|
115
|
+
}
|
|
116
|
+
/** Validate and encode an application value to the database scalar (int64 uses bigint). */
|
|
117
|
+
encode(value) {
|
|
118
|
+
if (value === null) {
|
|
119
|
+
if (this.allowsNull)
|
|
120
|
+
return null;
|
|
121
|
+
throw new TypeError("Null requires a nullable field");
|
|
122
|
+
}
|
|
123
|
+
const stored = this.storage(representations.get(this).encode(value));
|
|
124
|
+
this.checkConstraints(stored);
|
|
125
|
+
return stored;
|
|
126
|
+
}
|
|
127
|
+
/** Predicates and arithmetic operands validate representation, not stored-row constraints. */
|
|
128
|
+
encodeOperand(value) {
|
|
129
|
+
if (value === null) {
|
|
130
|
+
if (this.allowsNull)
|
|
131
|
+
return null;
|
|
132
|
+
throw new TypeError("Null requires a nullable field");
|
|
133
|
+
}
|
|
134
|
+
return this.storage(representations.get(this).encode(value));
|
|
135
|
+
}
|
|
136
|
+
/** Validate and decode a database wire value. */
|
|
137
|
+
decode(value) {
|
|
138
|
+
if (value === null) {
|
|
139
|
+
if (this.allowsNull)
|
|
140
|
+
return null;
|
|
141
|
+
throw new TypeError("Null requires a nullable field");
|
|
142
|
+
}
|
|
143
|
+
if (this.scalar === "int64") {
|
|
144
|
+
if (typeof value !== "string" || !/^(0|-?[1-9][0-9]*)$/.test(value))
|
|
145
|
+
throw new TypeError("Invalid stored int64");
|
|
146
|
+
value = BigInt(value);
|
|
147
|
+
}
|
|
148
|
+
const stored = this.storage(value);
|
|
149
|
+
this.checkConstraints(stored);
|
|
150
|
+
return synchronous(representations.get(this).decode(stored));
|
|
151
|
+
}
|
|
152
|
+
/** Storage-level validation is also used by SQL arithmetic operands. */
|
|
153
|
+
storage(value) {
|
|
154
|
+
if (value === null && this.allowsNull)
|
|
155
|
+
return null;
|
|
156
|
+
if (this.scalar === "json") {
|
|
157
|
+
if (value === null)
|
|
158
|
+
throw new TypeError("Root null requires nullable JSON field");
|
|
159
|
+
return validateJson(value, this.jsonShape);
|
|
160
|
+
}
|
|
161
|
+
if (this.scalar === "date")
|
|
162
|
+
return dateString(value);
|
|
163
|
+
if (this.scalar === "timestamp" || this.scalar === "localTimestamp")
|
|
164
|
+
return timestampString(value, this.scalar === "timestamp", this.precision ?? 3);
|
|
165
|
+
const valid = this.scalar === "float64" ? typeof value === "number" && Number.isFinite(value)
|
|
166
|
+
: this.scalar === "string" ? typeof value === "string" && !/[\uD800-\uDFFF]/u.test(value)
|
|
167
|
+
: this.scalar === "boolean" ? typeof value === "boolean"
|
|
168
|
+
: this.scalar === "int64" ? typeof value === "bigint" && value >= -(1n << 63n) && value < 1n << 63n
|
|
169
|
+
: typeof value === "string" && /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(value);
|
|
170
|
+
if (!valid)
|
|
171
|
+
throw new TypeError(`Expected a ${this.scalar}${this.allowsNull ? " or null" : ""}`);
|
|
172
|
+
return this.scalar === "uuid" ? value.toLowerCase() : value;
|
|
173
|
+
}
|
|
174
|
+
encodeElement(value) { return encodeJsonElement(this.jsonShape, value); }
|
|
175
|
+
nested(parts) {
|
|
176
|
+
let shape = this.jsonShape;
|
|
177
|
+
for (const part of parts) {
|
|
178
|
+
if (shape?.kind === "nullable")
|
|
179
|
+
shape = shape.inner;
|
|
180
|
+
if (shape?.kind === "any")
|
|
181
|
+
return { scalar: "json", jsonShape: shape, parse: value => validateJson(value), encode: value => validateJson(value), encodeOperand: value => validateJson(value), encodeElement: value => validateJson(value) };
|
|
182
|
+
if (shape?.kind !== "object" || !Object.hasOwn(shape.fields, part))
|
|
183
|
+
return undefined;
|
|
184
|
+
shape = shape.fields[part].shape;
|
|
185
|
+
}
|
|
186
|
+
if (!shape)
|
|
187
|
+
return undefined;
|
|
188
|
+
const validator = runtimeJsonValidators.get(shape);
|
|
189
|
+
const scalar = shape.kind === "nullable" ? shape.inner : shape;
|
|
190
|
+
return { scalar: scalar.kind === "number" ? "float64" : scalar.kind === "string" || scalar.kind === "boolean" ? scalar.kind : "json", jsonShape: shape, parse: value => validator ? validator.parse(value) : validateJson(value, shape), encode: value => validator ? validator.encode(value) : validateJson(value, shape), encodeOperand: value => validator ? validator.encode(value) : validateJson(value, shape), encodeElement: value => encodeJsonElement(shape, value) };
|
|
191
|
+
}
|
|
192
|
+
parse(value) {
|
|
193
|
+
const stored = this.encode(value);
|
|
194
|
+
if (value === null || this.customCodec)
|
|
195
|
+
return value;
|
|
196
|
+
return representations.get(this).decode(stored);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function dateString(value) {
|
|
200
|
+
if (typeof value !== "string" || !/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(value) || value.startsWith("0000"))
|
|
201
|
+
throw new TypeError("Expected YYYY-MM-DD calendar date (years 0001..9999)");
|
|
202
|
+
const time = new Date(value + "T00:00:00.000Z");
|
|
203
|
+
if (!Number.isFinite(time.getTime()) || time.toISOString().slice(0, 10) !== value)
|
|
204
|
+
throw new TypeError("Invalid calendar date");
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
function validDate(value) {
|
|
208
|
+
if (!(value instanceof Date) || !Number.isFinite(value.getTime()) || value.getUTCFullYear() < 1 || value.getUTCFullYear() > 9999)
|
|
209
|
+
throw new TypeError("Expected a valid Date (years 0001..9999)");
|
|
210
|
+
return value;
|
|
211
|
+
}
|
|
212
|
+
function timestampString(value, timezone, precision) {
|
|
213
|
+
if (typeof value !== "string")
|
|
214
|
+
throw new TypeError("Expected timestamp string");
|
|
215
|
+
const pattern = timezone ? /^([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]+))?(Z|[+-][0-9]{2}:[0-9]{2})$/ : /^([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]+))?$/;
|
|
216
|
+
const match = pattern.exec(value);
|
|
217
|
+
if (!match)
|
|
218
|
+
throw new TypeError("Expected ISO timestamp with explicit timezone" + (timezone ? "" : "-free clock components"));
|
|
219
|
+
dateString(match[1]);
|
|
220
|
+
if (Number(match[2]) > 23 || Number(match[3]) > 59 || Number(match[4]) > 59)
|
|
221
|
+
throw new TypeError("Invalid timestamp clock components");
|
|
222
|
+
const fraction = match[5] ?? "";
|
|
223
|
+
if (fraction.length > 9 || fraction.slice(precision).replaceAll("0", ""))
|
|
224
|
+
throw new TypeError("Timestamp exceeds configured precision");
|
|
225
|
+
const timezonePart = match[6] ?? "Z";
|
|
226
|
+
if (timezonePart !== "Z" && (Number(timezonePart.slice(1, 3)) > 23 || Number(timezonePart.slice(4)) > 59))
|
|
227
|
+
throw new TypeError("Invalid timestamp offset");
|
|
228
|
+
const instant = new Date(`${match[1]}T${match[2]}:${match[3]}:${match[4]}.${fraction.slice(0, 3).padEnd(3, "0")}${timezonePart}`);
|
|
229
|
+
validDate(instant);
|
|
230
|
+
const clock = timezone ? instant.toISOString().slice(0, 19) : `${match[1]}T${match[2]}:${match[3]}:${match[4]}`;
|
|
231
|
+
return clock + (precision ? "." + fraction.slice(0, precision).padEnd(precision, "0") : "") + (timezone ? "Z" : "");
|
|
232
|
+
}
|
|
233
|
+
function timestamp(options = {}) {
|
|
234
|
+
const precision = options.precision ?? 3;
|
|
235
|
+
const mode = options.mode ?? (options.withTimezone === false ? "string" : "date");
|
|
236
|
+
if (!Number.isInteger(precision) || precision < 0 || precision > 6 || !["string", "date"].includes(mode) || (mode === "date" && precision > 3))
|
|
237
|
+
throw new TypeError("Timestamp Date mode requires precision 0..3; use string mode for precision 4..6");
|
|
238
|
+
const timezone = options.withTimezone !== false;
|
|
239
|
+
if (options.withTimezone !== undefined && typeof options.withTimezone !== "boolean")
|
|
240
|
+
throw new TypeError("withTimezone must be boolean");
|
|
241
|
+
return new Field(timezone ? "timestamp" : "localTimestamp", false, false, undefined, undefined, undefined, { encode: value => timestampString(mode === "date" ? validDate(value).toISOString().replace(timezone ? /$/ : /Z$/, "") : value, timezone, precision), decode: value => mode === "date" ? new Date(String(value) + (timezone ? "" : "Z")) : value }, { precision, mode });
|
|
242
|
+
}
|
|
243
|
+
function int64(options = {}) {
|
|
244
|
+
const mode = options.mode ?? "number";
|
|
245
|
+
if (!["number", "bigint"].includes(mode))
|
|
246
|
+
throw new TypeError("Invalid int64 representation mode");
|
|
247
|
+
return new Field("int64", false, false, undefined, undefined, undefined, {
|
|
248
|
+
encode: value => { if (mode === "bigint")
|
|
249
|
+
return value; if (typeof value !== "number" || !Number.isSafeInteger(value))
|
|
250
|
+
throw new TypeError("int64 number mode requires a safe integer; use mode: bigint for lossless values"); return BigInt(value); },
|
|
251
|
+
decode: value => { if (mode === "bigint")
|
|
252
|
+
return value; const number = Number(value); if (!Number.isSafeInteger(number))
|
|
253
|
+
throw new TypeError("Stored int64 exceeds safe integer range; use mode: bigint"); return number; },
|
|
254
|
+
}, { mode });
|
|
255
|
+
}
|
|
256
|
+
function date(options = {}) {
|
|
257
|
+
const mode = options.mode ?? "string";
|
|
258
|
+
if (!["string", "date"].includes(mode))
|
|
259
|
+
throw new TypeError("Invalid date representation mode");
|
|
260
|
+
return new Field("date", false, false, undefined, undefined, undefined, { encode: value => mode === "date" ? validDate(value).toISOString().slice(0, 10) : value, decode: value => mode === "date" ? new Date(value + "T00:00:00.000Z") : value }, { mode });
|
|
261
|
+
}
|
|
262
|
+
const runtimeJsonValidators = new WeakMap();
|
|
263
|
+
class JsonValidator {
|
|
264
|
+
shape;
|
|
265
|
+
isOptional;
|
|
266
|
+
customCodec;
|
|
267
|
+
encode;
|
|
268
|
+
decode;
|
|
269
|
+
constructor(shape, isOptional = false, encode = (value, path) => validateJson(value, shape, path), decode = (value, path) => validateJson(value, shape, path), customCodec = false) {
|
|
270
|
+
this.shape = Object.freeze(shape);
|
|
271
|
+
runtimeJsonValidators.set(this.shape, this);
|
|
272
|
+
this.isOptional = isOptional;
|
|
273
|
+
this.encode = encode;
|
|
274
|
+
this.decode = decode;
|
|
275
|
+
this.customCodec = customCodec;
|
|
276
|
+
Object.freeze(this);
|
|
277
|
+
}
|
|
278
|
+
nullable() { return new JsonValidator({ kind: "nullable", inner: this.shape }, this.isOptional, (value, path) => value === null ? null : this.encode(value, path), (value, path) => value === null ? null : this.decode(value, path), this.customCodec); }
|
|
279
|
+
optional() { return new JsonValidator(this.shape, true, this.encode, this.decode, this.customCodec); }
|
|
280
|
+
parse(value) { return this.decode(this.encode(value)); }
|
|
281
|
+
}
|
|
282
|
+
/** Internal adapter bridge; the immutable shape is the runtime validator identity. */
|
|
283
|
+
export function jsonValidatorForShape(shape) { return runtimeJsonValidators.get(shape); }
|
|
284
|
+
function encodeJsonElement(shape, value) {
|
|
285
|
+
if (shape?.kind === "nullable")
|
|
286
|
+
shape = shape.inner;
|
|
287
|
+
if (shape?.kind !== "array")
|
|
288
|
+
throw new TypeError("Expected an array JSON shape");
|
|
289
|
+
const validator = runtimeJsonValidators.get(shape.item);
|
|
290
|
+
return validateJson(validator ? validator.encode(value) : value, shape.item);
|
|
291
|
+
}
|
|
292
|
+
function jsonValidator(input) {
|
|
293
|
+
if (input instanceof JsonValidator)
|
|
294
|
+
return input;
|
|
295
|
+
if (!(input instanceof Field) || !["string", "boolean", "float64"].includes(input.scalar) || input.hasDefault || input.target !== undefined)
|
|
296
|
+
throw new TypeError("JSON shapes require JSON-compatible validators; use an explicit string/number codec for Date, bigint or other application values");
|
|
297
|
+
if (input.constraints)
|
|
298
|
+
throw new TypeError("Field constraints require a database column; use a SQL check or function input validation for JSON members");
|
|
299
|
+
const scalar = { kind: input.scalar === "float64" ? "number" : input.scalar };
|
|
300
|
+
const shape = input.allowsNull ? { kind: "nullable", inner: scalar } : scalar;
|
|
301
|
+
return new JsonValidator(shape, false, value => input.encode(value), value => input.decode(value), input.customCodec);
|
|
302
|
+
}
|
|
303
|
+
function objectShape(fields) {
|
|
304
|
+
plain(fields, "JSON shape");
|
|
305
|
+
if (Object.keys(fields).some(key => !key))
|
|
306
|
+
throw new TypeError("JSON shape keys must be nonempty");
|
|
307
|
+
const children = Object.fromEntries(Object.entries(fields).map(([key, input]) => [key, jsonValidator(input)]));
|
|
308
|
+
const shape = { kind: "object", fields: Object.freeze(Object.fromEntries(Object.entries(children).map(([key, input]) => [key, Object.freeze({ shape: input.shape, optional: input.isOptional })]))) };
|
|
309
|
+
function map(value, direction, path = "$") {
|
|
310
|
+
plain(value, "JSON object");
|
|
311
|
+
if (Object.getOwnPropertySymbols(value).length)
|
|
312
|
+
throw new TypeError("Symbol properties are not JSON");
|
|
313
|
+
const result = Object.create(null);
|
|
314
|
+
for (const key of Object.getOwnPropertyNames(value)) {
|
|
315
|
+
const property = Object.getOwnPropertyDescriptor(value, key);
|
|
316
|
+
if (!property.enumerable || !("value" in property))
|
|
317
|
+
throw new TypeError("JSON accessors/nonenumerable properties are unsupported");
|
|
318
|
+
if (!Object.hasOwn(children, key))
|
|
319
|
+
throw new TypeError(`${path}.${key}: Unknown property`);
|
|
320
|
+
result[key] = children[key][direction](property.value, `${path}.${key}`);
|
|
321
|
+
}
|
|
322
|
+
for (const [key, child] of Object.entries(children))
|
|
323
|
+
if (!child.isOptional && !Object.hasOwn(result, key))
|
|
324
|
+
throw new TypeError(`${path}.${key}: Missing property`);
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
return new JsonValidator(shape, false, (value, path) => validateJson(map(value, "encode", path), shape, path), (value, path) => map(validateJson(value, shape, path), "decode", path));
|
|
328
|
+
}
|
|
329
|
+
function arrayShape(item) {
|
|
330
|
+
const child = jsonValidator(item);
|
|
331
|
+
const shape = { kind: "array", item: child.shape };
|
|
332
|
+
function encode(value, path = "$") {
|
|
333
|
+
if (!Array.isArray(value) || Object.getOwnPropertySymbols(value).length || Object.getOwnPropertyNames(value).length !== value.length + 1)
|
|
334
|
+
throw new TypeError("Expected JSON array");
|
|
335
|
+
return validateJson(Array.from({ length: value.length }, (_, i) => {
|
|
336
|
+
const property = Object.getOwnPropertyDescriptor(value, String(i));
|
|
337
|
+
if (!property || !("value" in property))
|
|
338
|
+
throw new TypeError("Sparse arrays and accessors are not JSON");
|
|
339
|
+
return child.encode(property.value, `${path}[${i}]`);
|
|
340
|
+
}), shape);
|
|
341
|
+
}
|
|
342
|
+
return new JsonValidator(shape, false, encode, (value, path = "$") => validateJson(value, shape, path).map((item, i) => child.decode(item, `${path}[${i}]`)));
|
|
343
|
+
}
|
|
344
|
+
function jsonField(input) {
|
|
345
|
+
const validator = input === undefined ? new JsonValidator({ kind: "any" }) : input instanceof Field || input instanceof JsonValidator ? jsonValidator(input) : objectShape(input);
|
|
346
|
+
return new Field("json", false, false, undefined, undefined, validator.shape, { encode: validator.encode, decode: validator.decode });
|
|
347
|
+
}
|
|
348
|
+
function tableKeyField(table) {
|
|
349
|
+
return table.fields[table.options.primaryKey ?? "id"] ?? v.uuid();
|
|
350
|
+
}
|
|
351
|
+
function id(target, options = {}) {
|
|
352
|
+
if ((typeof target !== "string" || !target) && (typeof target !== "object" || target?.[tableBrand] !== true))
|
|
353
|
+
throw new TypeError("v.id requires a table name or table declaration");
|
|
354
|
+
const onDelete = options.onDelete;
|
|
355
|
+
plain(options, "v.id options");
|
|
356
|
+
if (Object.keys(options).some(key => key !== "onDelete") || (onDelete !== undefined && !["restrict", "cascade", "setNull"].includes(onDelete)))
|
|
357
|
+
throw new TypeError("Invalid reference deletion policy");
|
|
358
|
+
const key = typeof target === "string" ? v.uuid() : tableKeyField(target);
|
|
359
|
+
return new Field(key.scalar, false, false, undefined, target, undefined, representations.get(key), { ...(key.mode === undefined ? {} : { mode: key.mode }), customCodec: key.customCodec, onDelete: onDelete ?? "restrict" });
|
|
360
|
+
}
|
|
361
|
+
export const v = {
|
|
362
|
+
id,
|
|
363
|
+
json: jsonField,
|
|
364
|
+
object: objectShape,
|
|
365
|
+
array: arrayShape,
|
|
366
|
+
number: () => new Field("float64", false, false, undefined, undefined),
|
|
367
|
+
string: () => new Field("string", false, false, undefined, undefined),
|
|
368
|
+
boolean: () => new Field("boolean", false, false, undefined, undefined),
|
|
369
|
+
int64,
|
|
370
|
+
date,
|
|
371
|
+
timestamp,
|
|
372
|
+
uuid: () => new Field("uuid", false, false, undefined, undefined),
|
|
373
|
+
};
|
|
374
|
+
const tableBrand = Symbol("Hulla schema table");
|
|
375
|
+
function withIndexes(fields, options, indexes) {
|
|
376
|
+
return Object.freeze({ fields, options, indexes: Object.freeze(indexes), [tableBrand]: true,
|
|
377
|
+
index(selected, indexOptions = {}) {
|
|
378
|
+
plain(indexOptions, "table.index");
|
|
379
|
+
if (Object.keys(indexOptions).some(key => key !== "name") || (indexOptions.name !== undefined && (typeof indexOptions.name !== "string" || !indexOptions.name)))
|
|
380
|
+
throw new TypeError("Invalid index options");
|
|
381
|
+
if (!Array.isArray(selected) || !selected.length || new Set(selected).size !== selected.length)
|
|
382
|
+
throw new TypeError("Index requires distinct fields");
|
|
383
|
+
for (const name of selected)
|
|
384
|
+
if (!Object.hasOwn(fields, name) && !(name === "id" && options.primaryKey === undefined))
|
|
385
|
+
throw new TypeError(`Unknown index field ${name}`);
|
|
386
|
+
const declaration = Object.freeze({ ...indexOptions, fields: Object.freeze([...selected]) });
|
|
387
|
+
return withIndexes(fields, options, [...indexes, declaration]);
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
export function defineTable(fields, options = {}) {
|
|
392
|
+
plain(fields, "table.fields");
|
|
393
|
+
plain(options, "table.options");
|
|
394
|
+
if (options.unique !== undefined) {
|
|
395
|
+
plain(options.unique, "table.unique");
|
|
396
|
+
for (const [name, constraint] of Object.entries(options.unique)) {
|
|
397
|
+
plain(constraint, `table.unique.${name}`);
|
|
398
|
+
if (!Array.isArray(constraint.fields))
|
|
399
|
+
fail(`table.unique.${name}`, "Expected a field list");
|
|
400
|
+
if (Object.keys(constraint).some((key) => !["fields", "nulls"].includes(key)))
|
|
401
|
+
fail(`table.unique.${name}`, "Unknown unique option");
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const unique = options.unique &&
|
|
405
|
+
Object.freeze(Object.fromEntries(Object.entries(options.unique).map(([name, value]) => [
|
|
406
|
+
name,
|
|
407
|
+
Object.freeze({ ...value, fields: Object.freeze([...value.fields]) }),
|
|
408
|
+
])));
|
|
409
|
+
const keyGeneration = typeof options.keyGeneration === "object" && options.keyGeneration !== null
|
|
410
|
+
? Object.freeze({ ...options.keyGeneration })
|
|
411
|
+
: options.keyGeneration;
|
|
412
|
+
const columns = Object.fromEntries(Object.entries(fields).map(([name, field]) => {
|
|
413
|
+
if (!(field instanceof JsonValidator))
|
|
414
|
+
return [name, field];
|
|
415
|
+
if (field.isOptional)
|
|
416
|
+
throw new TypeError("Table columns cannot be optional; use nullable JSON columns");
|
|
417
|
+
const column = jsonField(field);
|
|
418
|
+
return [name, field.shape.kind === "nullable" ? column.nullable() : column];
|
|
419
|
+
}));
|
|
420
|
+
return withIndexes(Object.freeze(columns), Object.freeze({
|
|
421
|
+
...options,
|
|
422
|
+
...(keyGeneration === undefined ? {} : { keyGeneration }),
|
|
423
|
+
...(unique ? { unique } : {}),
|
|
424
|
+
}), []);
|
|
425
|
+
}
|
|
426
|
+
const compiledSchemas = new WeakMap();
|
|
427
|
+
const compiledDefinitions = new WeakMap();
|
|
428
|
+
export function compiledTableForDefinition(table) { return compiledDefinitions.get(table); }
|
|
429
|
+
/** Validate authoring metadata immediately; storage enforcement belongs to the adapter. */
|
|
430
|
+
export function defineSchema(tables, options = {}) {
|
|
431
|
+
plain(tables, "schema.tables");
|
|
432
|
+
plain(options, "schema.options");
|
|
433
|
+
if (Object.keys(options).some((key) => key !== "defaultUuidGeneration"))
|
|
434
|
+
fail("schema.options", "Unknown schema option");
|
|
435
|
+
const schema = Object.freeze({
|
|
436
|
+
tables: Object.freeze({ ...tables }),
|
|
437
|
+
defaultUuidGeneration: options.defaultUuidGeneration ?? "v7",
|
|
438
|
+
});
|
|
439
|
+
compiledSchemas.set(schema, compileSchema(schema));
|
|
440
|
+
return schema;
|
|
441
|
+
}
|
|
442
|
+
function fail(path, message) {
|
|
443
|
+
throw new TypeError(`${path}: ${message}`);
|
|
444
|
+
}
|
|
445
|
+
function plain(value, path) {
|
|
446
|
+
if (typeof value !== "object" ||
|
|
447
|
+
value === null ||
|
|
448
|
+
Array.isArray(value) ||
|
|
449
|
+
![Object.prototype, null].includes(Object.getPrototypeOf(value)))
|
|
450
|
+
fail(path, "Expected a plain object");
|
|
451
|
+
}
|
|
452
|
+
/** Normalize metadata with the same key/default/reference rules as hulladb_schema. */
|
|
453
|
+
export function compileSchema(schema) {
|
|
454
|
+
const cached = compiledSchemas.get(schema);
|
|
455
|
+
if (cached)
|
|
456
|
+
return cached;
|
|
457
|
+
plain(schema, "schema");
|
|
458
|
+
plain(schema.tables, "schema.tables");
|
|
459
|
+
if (!["v4", "v7"].includes(schema.defaultUuidGeneration))
|
|
460
|
+
fail("schema", "Invalid UUID generation");
|
|
461
|
+
const tables = Object.entries(schema.tables).map(([name, table]) => {
|
|
462
|
+
if (!name || table?.[tableBrand] !== true)
|
|
463
|
+
fail(name || "schema", "Expected a named table declaration");
|
|
464
|
+
const options = table.options;
|
|
465
|
+
for (const key of Object.keys(options))
|
|
466
|
+
if (![
|
|
467
|
+
"primaryKey",
|
|
468
|
+
"keyGeneration",
|
|
469
|
+
"insertKeyPolicy",
|
|
470
|
+
"unique",
|
|
471
|
+
"checks",
|
|
472
|
+
].includes(key))
|
|
473
|
+
fail(name, `Unknown table option ${key}`);
|
|
474
|
+
const fields = Object.assign(Object.create(null), table.fields);
|
|
475
|
+
for (const [key, field] of Object.entries(fields)) {
|
|
476
|
+
if (!key || field?.[fieldBrand] !== true)
|
|
477
|
+
fail(`${name}.${key}`, "Expected a named scalar field");
|
|
478
|
+
if (!["string", "boolean", "int64", "uuid", "float64", "json", "date", "timestamp", "localTimestamp"].includes(field.scalar))
|
|
479
|
+
fail(`${name}.${key}`, "Unknown scalar");
|
|
480
|
+
if (typeof field.allowsNull !== "boolean" ||
|
|
481
|
+
typeof field.hasDefault !== "boolean")
|
|
482
|
+
fail(`${name}.${key}`, "Invalid field descriptor");
|
|
483
|
+
if (field.hasDefault && field.target === undefined)
|
|
484
|
+
field.parse(field.defaultValue);
|
|
485
|
+
}
|
|
486
|
+
const primaryKey = options.primaryKey ?? "id";
|
|
487
|
+
if (typeof primaryKey !== "string" || !primaryKey)
|
|
488
|
+
fail(name, "Expected a single primary key name");
|
|
489
|
+
if (!Object.hasOwn(fields, primaryKey)) {
|
|
490
|
+
if (primaryKey !== "id")
|
|
491
|
+
fail(`${name}.${primaryKey}`, "Missing primary key field");
|
|
492
|
+
fields.id = v.uuid();
|
|
493
|
+
}
|
|
494
|
+
const key = fields[primaryKey];
|
|
495
|
+
if (key.allowsNull || key.hasDefault || !["string", "int64", "uuid"].includes(key.scalar))
|
|
496
|
+
fail(`${name}.${primaryKey}`, "Primary keys must be non-null string, int64 or UUID without a literal default");
|
|
497
|
+
const keyGeneration = options.keyGeneration ??
|
|
498
|
+
(key.target !== undefined ? "none" : key.scalar === "uuid"
|
|
499
|
+
? schema.defaultUuidGeneration === "v4"
|
|
500
|
+
? "uuidV4"
|
|
501
|
+
: "uuidV7"
|
|
502
|
+
: "none");
|
|
503
|
+
if (typeof keyGeneration === "object") {
|
|
504
|
+
plain(keyGeneration, name);
|
|
505
|
+
if (typeof keyGeneration.handler !== "string" ||
|
|
506
|
+
!keyGeneration.handler ||
|
|
507
|
+
!["string", "boolean", "int64", "uuid"].includes(keyGeneration.outputType) ||
|
|
508
|
+
Object.keys(keyGeneration).some((key) => !["handler", "outputType"].includes(key)))
|
|
509
|
+
fail(name, "Invalid custom generator");
|
|
510
|
+
}
|
|
511
|
+
else if (!["none", "uuidV4", "uuidV7"].includes(keyGeneration))
|
|
512
|
+
fail(name, "Invalid key generation");
|
|
513
|
+
const outputType = typeof keyGeneration === "object"
|
|
514
|
+
? keyGeneration.outputType
|
|
515
|
+
: keyGeneration === "none"
|
|
516
|
+
? undefined
|
|
517
|
+
: "uuid";
|
|
518
|
+
if (outputType !== undefined && key.target === undefined && outputType !== key.scalar)
|
|
519
|
+
fail(name, "Key generator output type does not match primary key");
|
|
520
|
+
const insertKeyPolicy = options.insertKeyPolicy ?? "allowExplicit";
|
|
521
|
+
if (!["allowExplicit", "generatedOnly"].includes(insertKeyPolicy))
|
|
522
|
+
fail(name, "Invalid insert key policy");
|
|
523
|
+
if (insertKeyPolicy === "generatedOnly" && keyGeneration === "none")
|
|
524
|
+
fail(name, "Generated-only keys need a generator");
|
|
525
|
+
const unique = Object.create(null);
|
|
526
|
+
const signatures = new Set();
|
|
527
|
+
for (const [constraint, declaration] of Object.entries(options.unique ?? {})) {
|
|
528
|
+
const selected = declaration.fields;
|
|
529
|
+
if (!constraint ||
|
|
530
|
+
!Array.isArray(selected) ||
|
|
531
|
+
!selected.length ||
|
|
532
|
+
new Set(selected).size !== selected.length)
|
|
533
|
+
fail(`${name}.${constraint}`, "Unique constraint needs distinct fields");
|
|
534
|
+
for (const field of selected)
|
|
535
|
+
if (!Object.hasOwn(fields, field))
|
|
536
|
+
fail(`${name}.${constraint}`, `Unknown unique field ${field}`);
|
|
537
|
+
const signature = JSON.stringify([...selected].sort());
|
|
538
|
+
if (signatures.has(signature))
|
|
539
|
+
fail(`${name}.${constraint}`, "Duplicate unique field set");
|
|
540
|
+
signatures.add(signature);
|
|
541
|
+
if (selected.some((field) => fields[field].allowsNull) &&
|
|
542
|
+
declaration.nulls === undefined)
|
|
543
|
+
fail(`${name}.${constraint}`, "Nullable unique fields require an explicit null policy");
|
|
544
|
+
const nulls = declaration.nulls ?? "notDistinct";
|
|
545
|
+
if (!["distinct", "notDistinct"].includes(nulls))
|
|
546
|
+
fail(`${name}.${constraint}`, "Invalid unique null policy");
|
|
547
|
+
unique[constraint] = Object.freeze({
|
|
548
|
+
fields: Object.freeze([...selected]),
|
|
549
|
+
nulls,
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
const indexNames = new Set();
|
|
553
|
+
const indexFields = new Set();
|
|
554
|
+
const indexes = table.indexes.map(index => {
|
|
555
|
+
for (const field of index.fields)
|
|
556
|
+
if (!Object.hasOwn(fields, field))
|
|
557
|
+
fail(`${name}.index`, `Unknown index field ${field}`);
|
|
558
|
+
const indexName = index.name ?? defaultIndexName(name, index.fields);
|
|
559
|
+
const signature = JSON.stringify(index.fields);
|
|
560
|
+
if (indexNames.has(indexName) || indexFields.has(signature))
|
|
561
|
+
fail(`${name}.index`, "Duplicate index name or field set");
|
|
562
|
+
if (utf8Length(indexName) > 63)
|
|
563
|
+
fail(`${name}.index`, "Index names must fit PostgreSQL's 63-byte identifier limit");
|
|
564
|
+
indexNames.add(indexName);
|
|
565
|
+
indexFields.add(signature);
|
|
566
|
+
return Object.freeze({ name: indexName, fields: index.fields });
|
|
567
|
+
});
|
|
568
|
+
return Object.freeze({
|
|
569
|
+
name,
|
|
570
|
+
fields: Object.freeze(fields),
|
|
571
|
+
primaryKey,
|
|
572
|
+
keyGeneration: typeof keyGeneration === "object"
|
|
573
|
+
? Object.freeze({ ...keyGeneration })
|
|
574
|
+
: keyGeneration,
|
|
575
|
+
insertKeyPolicy,
|
|
576
|
+
unique: Object.freeze(unique),
|
|
577
|
+
indexes: Object.freeze(indexes),
|
|
578
|
+
checks: compileChecks(options.checks, Object.keys(fields), primaryKey),
|
|
579
|
+
});
|
|
580
|
+
});
|
|
581
|
+
const byName = new Map(tables.map((table) => [table.name, table]));
|
|
582
|
+
const definitions = new Map();
|
|
583
|
+
for (const [name, declaration] of Object.entries(schema.tables))
|
|
584
|
+
definitions.set(declaration, [...definitions.get(declaration) ?? [], name]);
|
|
585
|
+
const resolvedFields = new Map();
|
|
586
|
+
const resolvingFields = new Set();
|
|
587
|
+
const resolveField = (table, name) => {
|
|
588
|
+
const field = table.fields[name];
|
|
589
|
+
const path = JSON.stringify([table.name, name]);
|
|
590
|
+
if (field.target === undefined)
|
|
591
|
+
return field;
|
|
592
|
+
if (resolvedFields.has(path))
|
|
593
|
+
return resolvedFields.get(path);
|
|
594
|
+
if (resolvingFields.has(path))
|
|
595
|
+
fail(`${table.name}.${name}`, "Cyclic primary-key references cannot resolve a storage scalar");
|
|
596
|
+
resolvingFields.add(path);
|
|
597
|
+
const names = typeof field.target === "string" ? [field.target] : definitions.get(field.target);
|
|
598
|
+
if (!names || names.length !== 1)
|
|
599
|
+
fail(`${table.name}.${name}`, "Reference table must be registered exactly once in defineSchema");
|
|
600
|
+
const targetName = names[0];
|
|
601
|
+
const target = byName.get(targetName);
|
|
602
|
+
if (!target)
|
|
603
|
+
fail(`${table.name}.${name}`, `Missing reference target ${targetName}`);
|
|
604
|
+
const key = resolveField(target, target.primaryKey);
|
|
605
|
+
if (field.onDelete === "setNull" && !field.allowsNull)
|
|
606
|
+
fail(`${table.name}.${name}`, "setNull references require a nullable field");
|
|
607
|
+
const resolvedField = new Field(key.scalar, field.allowsNull, field.hasDefault, field.defaultValue, targetName, undefined, representations.get(key), { ...(key.mode === undefined ? {} : { mode: key.mode }), customCodec: key.customCodec, onDelete: field.onDelete ?? "restrict", ...(field.constraints === undefined ? {} : { constraints: field.constraints }) });
|
|
608
|
+
if (resolvedField.hasDefault)
|
|
609
|
+
resolvedField.parse(resolvedField.defaultValue);
|
|
610
|
+
resolvingFields.delete(path);
|
|
611
|
+
resolvedFields.set(path, resolvedField);
|
|
612
|
+
return resolvedField;
|
|
613
|
+
};
|
|
614
|
+
const resolved = tables.map(table => {
|
|
615
|
+
const fields = Object.fromEntries(Object.keys(table.fields).map(name => [name, resolveField(table, name)]));
|
|
616
|
+
const key = fields[table.primaryKey];
|
|
617
|
+
const outputType = typeof table.keyGeneration === "object" ? table.keyGeneration.outputType : table.keyGeneration === "none" ? undefined : "uuid";
|
|
618
|
+
if (outputType !== undefined && outputType !== key.scalar)
|
|
619
|
+
fail(`${table.name}.${table.primaryKey}`, "Key generator output type does not match primary key");
|
|
620
|
+
const result = Object.freeze({ ...table, fields: Object.freeze(fields) });
|
|
621
|
+
return result;
|
|
622
|
+
});
|
|
623
|
+
const allIndexNames = new Set();
|
|
624
|
+
for (const table of resolved)
|
|
625
|
+
for (const index of table.indexes) {
|
|
626
|
+
if (allIndexNames.has(index.name))
|
|
627
|
+
fail(`${table.name}.index`, `Index name ${index.name} is already used by another table`);
|
|
628
|
+
allIndexNames.add(index.name);
|
|
629
|
+
}
|
|
630
|
+
for (const table of resolved) {
|
|
631
|
+
const prior = compiledDefinitions.get(schema.tables[table.name]);
|
|
632
|
+
if (prior && !sameTableBinding(prior, table))
|
|
633
|
+
fail(table.name, "Table declaration was already bound to an incompatible schema; create a separate declaration for each binding");
|
|
634
|
+
}
|
|
635
|
+
for (const table of resolved)
|
|
636
|
+
compiledDefinitions.set(schema.tables[table.name], table);
|
|
637
|
+
return Object.freeze(resolved);
|
|
638
|
+
}
|
|
639
|
+
function sameValue(left, right) {
|
|
640
|
+
if (Object.is(left, right))
|
|
641
|
+
return true;
|
|
642
|
+
if (typeof left !== "object" || left === null || typeof right !== "object" || right === null || Array.isArray(left) !== Array.isArray(right))
|
|
643
|
+
return false;
|
|
644
|
+
const keys = Object.keys(left);
|
|
645
|
+
return keys.length === Object.keys(right).length && keys.every(key => Object.hasOwn(right, key) && sameValue(left[key], right[key]));
|
|
646
|
+
}
|
|
647
|
+
function sameTableBinding(left, right) {
|
|
648
|
+
return left.primaryKey === right.primaryKey && sameValue(left.keyGeneration, right.keyGeneration) && left.insertKeyPolicy === right.insertKeyPolicy && Object.keys(left.fields).length === Object.keys(right.fields).length && Object.entries(left.fields).every(([name, field]) => {
|
|
649
|
+
const other = right.fields[name];
|
|
650
|
+
return other !== undefined && field.scalar === other.scalar && field.mode === other.mode && field.precision === other.precision && field.allowsNull === other.allowsNull && field.hasDefault === other.hasDefault && field.target === other.target && field.onDelete === other.onDelete && sameValue(field.constraints, other.constraints) && field.customCodec === other.customCodec && (!field.customCodec || representations.get(field) === representations.get(other)) && (!field.hasDefault || sameValue(field.encode(field.defaultValue), other.encode(other.defaultValue)));
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
function defaultIndexName(table, fields) {
|
|
654
|
+
const full = `${table}_${fields.join("_")}_idx`;
|
|
655
|
+
if (utf8Length(full) <= 63)
|
|
656
|
+
return full;
|
|
657
|
+
let hash = 2166136261;
|
|
658
|
+
for (const character of JSON.stringify([table, fields]))
|
|
659
|
+
hash = Math.imul(hash ^ character.codePointAt(0), 16777619) >>> 0;
|
|
660
|
+
return `${full.replace(/[^a-zA-Z0-9_]/g, "_").slice(0, 40)}_${hash.toString(16).padStart(8, "0")}_idx`;
|
|
661
|
+
}
|
|
662
|
+
// Schema evaluation also runs in the restricted JS realm, without Web encoders.
|
|
663
|
+
function utf8Length(value) {
|
|
664
|
+
let bytes = 0;
|
|
665
|
+
for (const character of value) {
|
|
666
|
+
const point = character.codePointAt(0);
|
|
667
|
+
bytes += point <= 0x7f ? 1 : point <= 0x7ff ? 2 : point <= 0xffff ? 3 : 4;
|
|
668
|
+
}
|
|
669
|
+
return bytes;
|
|
670
|
+
}
|
|
671
|
+
function parseRecord(schema, name, input, kind, fillDefaults = true) {
|
|
672
|
+
const table = compileSchema(schema).find((table) => table.name === name);
|
|
673
|
+
if (!table)
|
|
674
|
+
fail(name, "Unknown table");
|
|
675
|
+
plain(input, name);
|
|
676
|
+
const result = Object.create(null);
|
|
677
|
+
for (const key of Object.keys(input)) {
|
|
678
|
+
if (!Object.hasOwn(table.fields, key))
|
|
679
|
+
fail(`${name}.${key}`, "Unknown field");
|
|
680
|
+
if (key === table.primaryKey &&
|
|
681
|
+
(kind === "patch" ||
|
|
682
|
+
(kind === "insert" && table.insertKeyPolicy === "generatedOnly")))
|
|
683
|
+
fail(`${name}.${key}`, "Primary key assignment forbidden");
|
|
684
|
+
}
|
|
685
|
+
for (const [key, field] of Object.entries(table.fields)) {
|
|
686
|
+
if (Object.hasOwn(input, key)) {
|
|
687
|
+
if (input[key] === undefined && (kind === "patch" || (kind === "insert" && (field.hasDefault || (key === table.primaryKey && table.keyGeneration !== "none")))))
|
|
688
|
+
continue;
|
|
689
|
+
try {
|
|
690
|
+
result[key] = field.parse(input[key]);
|
|
691
|
+
}
|
|
692
|
+
catch (error) {
|
|
693
|
+
fail(`${name}.${key}`, error.message);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
else if (kind === "patch")
|
|
697
|
+
continue;
|
|
698
|
+
else if (kind === "insert" && field.hasDefault) {
|
|
699
|
+
if (fillDefaults)
|
|
700
|
+
result[key] = field.parse(field.defaultValue);
|
|
701
|
+
}
|
|
702
|
+
else if (kind === "insert" &&
|
|
703
|
+
key === table.primaryKey &&
|
|
704
|
+
table.keyGeneration !== "none")
|
|
705
|
+
continue;
|
|
706
|
+
else
|
|
707
|
+
fail(`${name}.${key}`, "Missing field");
|
|
708
|
+
}
|
|
709
|
+
return result;
|
|
710
|
+
}
|
|
711
|
+
export function parseRow(schema, table, input) {
|
|
712
|
+
return parseRecord(schema, table, input, "row");
|
|
713
|
+
}
|
|
714
|
+
/** Fill literal defaults; an omitted generated key remains pending runtime execution. */
|
|
715
|
+
export function prepareInsert(schema, table, input, fillDefaults = true) {
|
|
716
|
+
return parseRecord(schema, table, input, "insert", fillDefaults);
|
|
717
|
+
}
|
|
718
|
+
export function parsePatch(schema, table, input) {
|
|
719
|
+
return parseRecord(schema, table, input, "patch");
|
|
720
|
+
}
|