@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/database.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { compileSchema, prepareInsert, } from "./schema.js";
|
|
2
|
+
import { tableName, fieldPath } from "./fields.js";
|
|
3
|
+
import { predicateParser } from "./filters.js";
|
|
4
|
+
import { DatabaseError } from "./error.js";
|
|
5
|
+
import { writeCompiler, rowExpectation } from "./write-expression.js";
|
|
6
|
+
import { insertion, selection, write } from "./database-builder.js";
|
|
7
|
+
import { parseProjection } from "./projection.js";
|
|
8
|
+
import { aggregatePlan } from "./aggregate.js";
|
|
9
|
+
/** Private transaction transport. Applications receive typed databases, never this channel. */
|
|
10
|
+
export function transactionDatabase(schema, writable, request) {
|
|
11
|
+
const tables = new Map(compileSchema(schema).map(table => [table.name, table]));
|
|
12
|
+
function table(name) {
|
|
13
|
+
const value = tables.get(name);
|
|
14
|
+
if (!value)
|
|
15
|
+
throw new TypeError(`Unknown table ${name}`);
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
function encode(name, values) {
|
|
19
|
+
const fields = table(name).fields;
|
|
20
|
+
if (typeof values !== "object" || values === null || Array.isArray(values))
|
|
21
|
+
throw new TypeError("Expected record");
|
|
22
|
+
return Object.fromEntries(Object.entries(values).map(([key, value]) => {
|
|
23
|
+
const field = fields[key];
|
|
24
|
+
if (!field)
|
|
25
|
+
throw new TypeError(`Unknown field ${name}.${key}`);
|
|
26
|
+
const parsed = field.encode(value);
|
|
27
|
+
return [key, typeof parsed === "bigint" ? parsed.toString() : parsed];
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
function projection(name, input) {
|
|
31
|
+
return parseProjection(table(name).fields, input);
|
|
32
|
+
}
|
|
33
|
+
function stored(name, value, selected) {
|
|
34
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
35
|
+
throw new TypeError("Invalid stored row");
|
|
36
|
+
const envelope = value;
|
|
37
|
+
if (typeof envelope.version !== "string" || !/^[1-9][0-9]*$/.test(envelope.version) || BigInt(envelope.version) >= 1n << 63n)
|
|
38
|
+
throw new TypeError("Invalid storage version");
|
|
39
|
+
if (typeof envelope.row !== "object" || envelope.row === null || Array.isArray(envelope.row))
|
|
40
|
+
throw new TypeError("Invalid stored row");
|
|
41
|
+
const fields = table(name).fields;
|
|
42
|
+
const selectedFields = selected ?? Object.keys(fields);
|
|
43
|
+
if (Object.keys(envelope.row).length !== selectedFields.length || selectedFields.some(key => !Object.hasOwn(envelope.row, key)))
|
|
44
|
+
throw new TypeError("Stored row does not match requested projection");
|
|
45
|
+
return Object.fromEntries(selectedFields.map(key => [key, fields[key].decode(envelope.row[key])]));
|
|
46
|
+
}
|
|
47
|
+
function rows(name, result, selected) {
|
|
48
|
+
if (!Array.isArray(result))
|
|
49
|
+
throw new TypeError("Invalid database row result");
|
|
50
|
+
return result.map(value => stored(name, value, selected));
|
|
51
|
+
}
|
|
52
|
+
function filter(name, args) {
|
|
53
|
+
return predicateParser(name, table(name).fields)(args);
|
|
54
|
+
}
|
|
55
|
+
const read = {
|
|
56
|
+
select(input, inputProjection) {
|
|
57
|
+
const name = tableName(input);
|
|
58
|
+
table(name);
|
|
59
|
+
const selected = inputProjection === undefined ? undefined : projection(name, inputProjection);
|
|
60
|
+
const projected = selected ? { projection: selected } : {};
|
|
61
|
+
return selection(args => filter(name, args), async (filters, limit, unique, order) => {
|
|
62
|
+
const result = rows(name, await request({ method: "select", table: name, filters, limit, order, ...projected }), selected);
|
|
63
|
+
if (unique) {
|
|
64
|
+
if (result.length > 1)
|
|
65
|
+
throw new DatabaseError({ code: "not_unique", message: "Expected at most one matching row" });
|
|
66
|
+
return result[0] ?? null;
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}, field => {
|
|
70
|
+
const pathValue = fieldPath(field, name);
|
|
71
|
+
if (typeof pathValue !== "string" && pathValue.length !== 1)
|
|
72
|
+
throw new TypeError("JSON path ordering is not supported");
|
|
73
|
+
const nameValue = typeof pathValue === "string" ? pathValue : pathValue[0];
|
|
74
|
+
if (table(name).fields[nameValue]?.scalar === "json")
|
|
75
|
+
throw new TypeError("JSON ordering is not supported");
|
|
76
|
+
if (!table(name).fields[nameValue])
|
|
77
|
+
throw new TypeError(`Unknown field ${name}.${nameValue}`);
|
|
78
|
+
return nameValue;
|
|
79
|
+
}, async (filters, order, options) => {
|
|
80
|
+
const result = await request({ method: "paginate", table: name, filters, order, ...options, ...projected });
|
|
81
|
+
if (typeof result !== "object" || result === null || Array.isArray(result))
|
|
82
|
+
throw new TypeError("Invalid database page result");
|
|
83
|
+
const page = result;
|
|
84
|
+
if ((page.cursor !== null && typeof page.cursor !== "string") || typeof page.range !== "string")
|
|
85
|
+
throw new TypeError("Invalid database page result");
|
|
86
|
+
return { page: rows(name, page.page, selected), cursor: page.cursor };
|
|
87
|
+
}, (groups, select) => {
|
|
88
|
+
if (selected)
|
|
89
|
+
throw new TypeError("Aggregates select their own output; omit the row projection");
|
|
90
|
+
return aggregatePlan(table(name), groups, select);
|
|
91
|
+
}, async (filters, plan, having, order, limit) => {
|
|
92
|
+
return plan.decode(await request({
|
|
93
|
+
method: "aggregate", table: name, filters, groupBy: plan.groupBy,
|
|
94
|
+
aggregates: plan.aggregates, having, order, limit,
|
|
95
|
+
}));
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
if (!writable)
|
|
100
|
+
return Object.freeze(read);
|
|
101
|
+
return Object.freeze({
|
|
102
|
+
...read,
|
|
103
|
+
insert(tableInput, input) {
|
|
104
|
+
const name = tableName(tableInput);
|
|
105
|
+
table(name);
|
|
106
|
+
const bulk = Array.isArray(input);
|
|
107
|
+
const inputs = bulk ? input : [input];
|
|
108
|
+
const values = inputs.map(value => encode(name, prepareInsert(schema, name, value, false)));
|
|
109
|
+
const compiler = writeCompiler(table(name));
|
|
110
|
+
return insertion(input => projection(name, input), policy => compiler.conflict(policy), rowExpectation, async (selected, conflict, check) => {
|
|
111
|
+
if (bulk && !values.length && !check)
|
|
112
|
+
return [];
|
|
113
|
+
const result = await request({ method: "insert", table: name, values: bulk ? values : values[0], ...(selected ? { projection: selected } : {}), ...(conflict ? { conflict } : {}) }, check);
|
|
114
|
+
return bulk ? rows(name, result, selected) : result === null && conflict?.action.kind === "skip" ? null : stored(name, result, selected);
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
update(input, patch) {
|
|
118
|
+
const name = tableName(input);
|
|
119
|
+
const values = writeCompiler(table(name)).update(patch);
|
|
120
|
+
return write(args => filter(name, args), input => projection(name, input), rowExpectation, async (filters, selected, check) => rows(name, await request({ method: "update", table: name, filters, values, ...(selected ? { projection: selected } : {}) }, check), selected));
|
|
121
|
+
},
|
|
122
|
+
delete(input) {
|
|
123
|
+
const name = tableName(input);
|
|
124
|
+
table(name);
|
|
125
|
+
return write(args => filter(name, args), input => projection(name, input), rowExpectation, async (filters, selected, check) => rows(name, await request({ method: "delete", table: name, filters, ...(selected ? { projection: selected } : {}) }, check), selected));
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { type InvocationLimits } from "./config.ts";
|
|
2
|
+
import type { FunctionKind, FunctionSignature } from "./contracts.ts";
|
|
3
|
+
import { type SchemaInput, type SchemaOutput, type StandardSchema } from "./validation.ts";
|
|
4
|
+
export type { StandardSchema } from "./validation.ts";
|
|
5
|
+
declare const definitionBrand: unique symbol;
|
|
6
|
+
type Stage = {
|
|
7
|
+
readonly input?: StandardSchema;
|
|
8
|
+
readonly handler: (context: unknown, input: unknown) => unknown;
|
|
9
|
+
};
|
|
10
|
+
export interface FunctionDefinition<K extends FunctionKind, I, O, C> extends FunctionSignature {
|
|
11
|
+
readonly kind: K;
|
|
12
|
+
readonly $types?: {
|
|
13
|
+
readonly input: I;
|
|
14
|
+
readonly output: O;
|
|
15
|
+
readonly context: C;
|
|
16
|
+
};
|
|
17
|
+
readonly hasInput: boolean;
|
|
18
|
+
readonly limits?: InvocationLimits;
|
|
19
|
+
readonly stages: readonly Stage[];
|
|
20
|
+
readonly input?: StandardSchema;
|
|
21
|
+
readonly output?: StandardSchema;
|
|
22
|
+
readonly handler: (context: unknown, input: unknown) => unknown;
|
|
23
|
+
readonly [definitionBrand]: true;
|
|
24
|
+
}
|
|
25
|
+
type Combine<A, B> = [A] extends [undefined] ? B : [B] extends [undefined] ? A : A & B;
|
|
26
|
+
type Reserved = "db" | "identity" | "__proto__" | "prototype" | "constructor";
|
|
27
|
+
type Collision<A, B> = Extract<keyof A, keyof B>;
|
|
28
|
+
type DisjointInputs<A, B> = [A] extends [undefined] ? unknown : A extends readonly unknown[] | ((...args: never[]) => unknown) ? {
|
|
29
|
+
readonly combinedInputMustBeAnObject: never;
|
|
30
|
+
} : B extends readonly unknown[] | ((...args: never[]) => unknown) ? {
|
|
31
|
+
readonly combinedInputMustBeAnObject: never;
|
|
32
|
+
} : A extends object ? B extends object ? Collision<A, B> extends never ? unknown : {
|
|
33
|
+
readonly duplicateInputKeys: Collision<A, B>;
|
|
34
|
+
} : {
|
|
35
|
+
readonly combinedInputMustBeAnObject: never;
|
|
36
|
+
} : {
|
|
37
|
+
readonly combinedInputMustBeAnObject: never;
|
|
38
|
+
};
|
|
39
|
+
type ValidExtension<C, E> = [E] extends [void] ? unknown : E extends readonly unknown[] | ((...args: never[]) => unknown) ? {
|
|
40
|
+
readonly extensionMustReturnContextProperties: never;
|
|
41
|
+
} : E extends object ? Extract<keyof E, keyof C | Reserved> extends never ? unknown : {
|
|
42
|
+
readonly duplicateContextKeys: Extract<keyof E, keyof C | Reserved>;
|
|
43
|
+
} : {
|
|
44
|
+
readonly extensionMustReturnContextProperties: never;
|
|
45
|
+
};
|
|
46
|
+
type AddedContext<E> = [E] extends [void] ? unknown : E;
|
|
47
|
+
export interface Declarer<K extends FunctionKind, C, I = undefined, P = undefined> {
|
|
48
|
+
<O>(handler: (context: C, input: P) => O): FunctionDefinition<K, I, Awaited<O>, C>;
|
|
49
|
+
<O>(options: {
|
|
50
|
+
readonly limits?: InvocationLimits;
|
|
51
|
+
readonly handler: (context: C, input: P) => O;
|
|
52
|
+
readonly input?: undefined;
|
|
53
|
+
readonly output?: undefined;
|
|
54
|
+
}): FunctionDefinition<K, I, Awaited<O>, C>;
|
|
55
|
+
<S extends StandardSchema, O>(options: {
|
|
56
|
+
readonly limits?: InvocationLimits;
|
|
57
|
+
readonly input: S;
|
|
58
|
+
readonly output?: undefined;
|
|
59
|
+
readonly handler: (context: C, input: Combine<P, SchemaOutput<S>>) => O;
|
|
60
|
+
} & DisjointInputs<P, SchemaOutput<S>>): FunctionDefinition<K, Combine<I, SchemaInput<S>>, Awaited<O>, C>;
|
|
61
|
+
<S extends StandardSchema, O extends StandardSchema>(options: {
|
|
62
|
+
readonly limits?: InvocationLimits;
|
|
63
|
+
readonly input: S;
|
|
64
|
+
readonly output: O;
|
|
65
|
+
readonly handler: (context: C, input: Combine<P, SchemaOutput<S>>) => SchemaInput<O> | Promise<SchemaInput<O>>;
|
|
66
|
+
} & DisjointInputs<P, SchemaOutput<S>>): FunctionDefinition<K, Combine<I, SchemaInput<S>>, SchemaOutput<O>, C>;
|
|
67
|
+
<O extends StandardSchema>(options: {
|
|
68
|
+
readonly limits?: InvocationLimits;
|
|
69
|
+
readonly input?: undefined;
|
|
70
|
+
readonly output: O;
|
|
71
|
+
readonly handler: (context: C, input: P) => SchemaInput<O> | Promise<SchemaInput<O>>;
|
|
72
|
+
}): FunctionDefinition<K, I, SchemaOutput<O>, C>;
|
|
73
|
+
extend<E>(handler: (context: C, input: P) => E & ValidExtension<C, Awaited<E>>): Declarer<K, C & AddedContext<Awaited<E>>, I, P>;
|
|
74
|
+
extend<E>(options: {
|
|
75
|
+
readonly input?: undefined;
|
|
76
|
+
readonly handler: (context: C, input: P) => E & ValidExtension<C, Awaited<E>>;
|
|
77
|
+
}): Declarer<K, C & AddedContext<Awaited<E>>, I, P>;
|
|
78
|
+
extend<S extends StandardSchema, E>(options: {
|
|
79
|
+
readonly input: S;
|
|
80
|
+
readonly handler: (context: C, input: Combine<P, SchemaOutput<S>>) => E & ValidExtension<C, Awaited<E>>;
|
|
81
|
+
} & DisjointInputs<P, SchemaOutput<S>>): Declarer<K, C & AddedContext<Awaited<E>>, Combine<I, SchemaInput<S>>, Combine<P, SchemaOutput<S>>>;
|
|
82
|
+
}
|
|
83
|
+
/** Internal constructor used by generated schema-bound authoring modules. */
|
|
84
|
+
export declare function defineFunctions<QueryContext, MutationContext>(): {
|
|
85
|
+
query: Declarer<"query", QueryContext, undefined, undefined>;
|
|
86
|
+
mutation: Declarer<"mutation", MutationContext, undefined, undefined>;
|
|
87
|
+
};
|
|
88
|
+
type UntypedDefinition = FunctionDefinition<FunctionKind, unknown, unknown, unknown>;
|
|
89
|
+
export declare function isFunctionDefinition(value: unknown): value is UntypedDefinition;
|
|
90
|
+
/** Validate and execute stages inside the provider's snapshot/transaction; output finishes before commit. */
|
|
91
|
+
export declare function executeDefinition(definition: UntypedDefinition, context: unknown, raw: unknown): Promise<unknown>;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { validateLimits } from "./config.js";
|
|
2
|
+
import { assertStandardSchema, validateSchema } from "./validation.js";
|
|
3
|
+
const definitionBrand = Symbol("Hulla function definition");
|
|
4
|
+
function declarationOptions(value, extension) {
|
|
5
|
+
if (typeof value === "function")
|
|
6
|
+
return { handler: value };
|
|
7
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
8
|
+
throw new TypeError("A function declaration needs an options object or handler");
|
|
9
|
+
const allowed = extension ? ["input", "handler"] : ["input", "output", "handler", "limits"];
|
|
10
|
+
if (Reflect.ownKeys(value).some(key => typeof key !== "string" || !allowed.includes(key)))
|
|
11
|
+
throw new TypeError("Unknown function declaration option");
|
|
12
|
+
const options = value;
|
|
13
|
+
if (typeof options.handler !== "function")
|
|
14
|
+
throw new TypeError("A function declaration needs a handler");
|
|
15
|
+
if (options.input !== undefined)
|
|
16
|
+
assertStandardSchema(options.input);
|
|
17
|
+
if (options.output !== undefined)
|
|
18
|
+
assertStandardSchema(options.output);
|
|
19
|
+
return { ...(options.limits === undefined ? {} : { limits: validateLimits(options.limits) }), handler: options.handler, ...(options.input ? { input: options.input } : {}), ...(options.output ? { output: options.output } : {}) };
|
|
20
|
+
}
|
|
21
|
+
function declarer(kind, stages = []) {
|
|
22
|
+
const declare = (...args) => {
|
|
23
|
+
if (args.length !== 1)
|
|
24
|
+
throw new TypeError("A function declaration takes one options object or handler");
|
|
25
|
+
const options = declarationOptions(args[0], false);
|
|
26
|
+
return Object.freeze({ kind, hasInput: options.input !== undefined || stages.some(stage => stage.input !== undefined), stages, ...options, [definitionBrand]: true });
|
|
27
|
+
};
|
|
28
|
+
const extend = (...args) => {
|
|
29
|
+
if (args.length !== 1)
|
|
30
|
+
throw new TypeError("An extension takes one options object or handler");
|
|
31
|
+
const { input, handler } = declarationOptions(args[0], true);
|
|
32
|
+
return declarer(kind, Object.freeze([...stages, Object.freeze({ handler, ...(input ? { input } : {}) })]));
|
|
33
|
+
};
|
|
34
|
+
return Object.assign(declare, { extend });
|
|
35
|
+
}
|
|
36
|
+
/** Internal constructor used by generated schema-bound authoring modules. */
|
|
37
|
+
export function defineFunctions() {
|
|
38
|
+
return { query: declarer("query"), mutation: declarer("mutation") };
|
|
39
|
+
}
|
|
40
|
+
export function isFunctionDefinition(value) {
|
|
41
|
+
return typeof value === "object" && value !== null && value[definitionBrand] === true;
|
|
42
|
+
}
|
|
43
|
+
const unsafeKeys = new Set(["__proto__", "prototype", "constructor"]);
|
|
44
|
+
function entries(value, label) {
|
|
45
|
+
if (typeof value !== "object" || value === null || Array.isArray(value) || ![Object.prototype, null].includes(Object.getPrototypeOf(value))) {
|
|
46
|
+
throw new TypeError(`${label} must be a plain object`);
|
|
47
|
+
}
|
|
48
|
+
return Reflect.ownKeys(value).map(key => {
|
|
49
|
+
if (typeof key !== "string" || unsafeKeys.has(key))
|
|
50
|
+
throw new TypeError(`Invalid ${label} key`);
|
|
51
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
52
|
+
if (!descriptor.enumerable || !Object.hasOwn(descriptor, "value"))
|
|
53
|
+
throw new TypeError(`${label} properties must be ordinary values`);
|
|
54
|
+
return [key, descriptor.value];
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/** A validator may transform or mutate its own copy, never another stage's submitted input. */
|
|
58
|
+
function copySubmitted(value, ancestors = new Set(), depth = 0) {
|
|
59
|
+
if (depth > 128)
|
|
60
|
+
throw new TypeError("Function input nesting exceeds budget");
|
|
61
|
+
if (value === null || value === undefined || ["string", "number", "bigint", "boolean"].includes(typeof value))
|
|
62
|
+
return value;
|
|
63
|
+
if (typeof value !== "object" || ancestors.has(value))
|
|
64
|
+
throw new TypeError("Function input must contain supported plain data");
|
|
65
|
+
if (value instanceof Date) {
|
|
66
|
+
const time = Date.prototype.getTime.call(value);
|
|
67
|
+
if (!Number.isFinite(time))
|
|
68
|
+
throw new TypeError("Function input dates must be valid");
|
|
69
|
+
return new Date(time);
|
|
70
|
+
}
|
|
71
|
+
const array = Array.isArray(value);
|
|
72
|
+
if (array ? Object.getPrototypeOf(value) !== Array.prototype : ![Object.prototype, null].includes(Object.getPrototypeOf(value)))
|
|
73
|
+
throw new TypeError("Function input must contain supported plain data");
|
|
74
|
+
const result = array ? [] : Object.create(Object.getPrototypeOf(value));
|
|
75
|
+
const keys = Reflect.ownKeys(value);
|
|
76
|
+
if (array && keys.length !== value.length + 1)
|
|
77
|
+
throw new TypeError("Function input arrays must be dense");
|
|
78
|
+
ancestors.add(value);
|
|
79
|
+
try {
|
|
80
|
+
for (const key of keys) {
|
|
81
|
+
if (array && key === "length")
|
|
82
|
+
continue;
|
|
83
|
+
const property = Object.getOwnPropertyDescriptor(value, key);
|
|
84
|
+
if (typeof key !== "string" || !property.enumerable || !Object.hasOwn(property, "value"))
|
|
85
|
+
throw new TypeError("Function input properties must be ordinary values");
|
|
86
|
+
if (array && (!/^(0|[1-9][0-9]*)$/.test(key) || Number(key) >= value.length))
|
|
87
|
+
throw new TypeError("Function input arrays must be dense");
|
|
88
|
+
Object.defineProperty(result, key, { value: copySubmitted(property.value, ancestors, depth + 1), enumerable: true, writable: true, configurable: true });
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
ancestors.delete(value);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Validate and execute stages inside the provider's snapshot/transaction; output finishes before commit. */
|
|
97
|
+
export async function executeDefinition(definition, context, raw) {
|
|
98
|
+
if (!definition.hasInput && raw !== undefined)
|
|
99
|
+
throw new TypeError("This function takes no input");
|
|
100
|
+
let currentContext = Object.create(null);
|
|
101
|
+
for (const [key, value] of entries(context, "Context"))
|
|
102
|
+
currentContext[key] = value;
|
|
103
|
+
currentContext = Object.freeze(currentContext);
|
|
104
|
+
const inputStages = definition.stages.filter(stage => stage.input).length + (definition.input ? 1 : 0);
|
|
105
|
+
const submitted = definition.hasInput ? copySubmitted(raw) : undefined;
|
|
106
|
+
let input;
|
|
107
|
+
const parsedKeys = new Set();
|
|
108
|
+
const validateInput = async (schema) => {
|
|
109
|
+
if (!schema)
|
|
110
|
+
return;
|
|
111
|
+
const parsed = await validateSchema(schema, copySubmitted(submitted), "Input");
|
|
112
|
+
if (inputStages === 1) {
|
|
113
|
+
input = parsed;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const merged = Object.assign(Object.create(null), input);
|
|
117
|
+
for (const [key, value] of entries(parsed, "Parsed input")) {
|
|
118
|
+
if (parsedKeys.has(key))
|
|
119
|
+
throw new TypeError(`Duplicate parsed input key: ${key}`);
|
|
120
|
+
parsedKeys.add(key);
|
|
121
|
+
merged[key] = value;
|
|
122
|
+
}
|
|
123
|
+
input = Object.freeze(merged);
|
|
124
|
+
};
|
|
125
|
+
for (const stage of definition.stages) {
|
|
126
|
+
await validateInput(stage.input);
|
|
127
|
+
const addition = await stage.handler(currentContext, input);
|
|
128
|
+
if (addition === undefined)
|
|
129
|
+
continue;
|
|
130
|
+
const merged = Object.assign(Object.create(null), currentContext);
|
|
131
|
+
for (const [key, value] of entries(addition, "Extension context")) {
|
|
132
|
+
if (key === "db" || key === "identity" || Object.hasOwn(currentContext, key))
|
|
133
|
+
throw new TypeError(`Duplicate or reserved context key: ${key}`);
|
|
134
|
+
merged[key] = value;
|
|
135
|
+
}
|
|
136
|
+
currentContext = Object.freeze(merged);
|
|
137
|
+
}
|
|
138
|
+
await validateInput(definition.input);
|
|
139
|
+
const result = await definition.handler(currentContext, input);
|
|
140
|
+
return definition.output ? validateSchema(definition.output, result, "Output") : result;
|
|
141
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { dirname, resolve } from "node:path";
|
|
10
|
+
import { defineConfig } from "./config.js";
|
|
11
|
+
import { generateProject } from "./project-codegen.js";
|
|
12
|
+
import { startService } from "./service.js";
|
|
13
|
+
let stopping = false;
|
|
14
|
+
let service;
|
|
15
|
+
const stop = () => { stopping = true; void service?.close().catch(() => { }); };
|
|
16
|
+
process.on("SIGINT", stop);
|
|
17
|
+
process.on("SIGTERM", stop);
|
|
18
|
+
try {
|
|
19
|
+
const path = resolve(process.argv[2]);
|
|
20
|
+
const config = defineConfig((await import(__rewriteRelativeImportExtension(Bun.pathToFileURL(path).href))).default);
|
|
21
|
+
const schema = resolve(dirname(path), config.schema ?? "db/schema.ts");
|
|
22
|
+
process.send?.({ watch: [dirname(schema), ...(config.functions ? [resolve(dirname(path), config.functions)] : [])] });
|
|
23
|
+
await generateProject(path);
|
|
24
|
+
if (!stopping) {
|
|
25
|
+
service = await startService(path);
|
|
26
|
+
if (stopping)
|
|
27
|
+
await service.close();
|
|
28
|
+
else {
|
|
29
|
+
console.log(`Database service listening at ${service.url}`);
|
|
30
|
+
await service.closed;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (!stopping) {
|
|
36
|
+
console.error(error instanceof Error ? error.message : "Development startup failed");
|
|
37
|
+
console.error("Waiting for source changes before restarting.");
|
|
38
|
+
process.exitCode = 1;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
process.off("SIGINT", stop);
|
|
43
|
+
process.off("SIGTERM", stop);
|
|
44
|
+
await service?.close().catch(() => { });
|
|
45
|
+
if (process.connected)
|
|
46
|
+
process.disconnect?.();
|
|
47
|
+
}
|
package/dist/dev.d.ts
ADDED
package/dist/dev.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { lstatSync, watch } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { runtimeExportConditions } from "./runtime-conditions.js";
|
|
4
|
+
const ignored = new Set([
|
|
5
|
+
"_generated", "node_modules", ".git", "dist", "build", "target", "coverage",
|
|
6
|
+
".next", ".nuxt", ".output", ".svelte-kit", ".astro", ".solid", ".cache",
|
|
7
|
+
".turbo", ".vite", ".vercel", ".netlify",
|
|
8
|
+
]);
|
|
9
|
+
function contains(parent, child) {
|
|
10
|
+
const path = relative(parent, child);
|
|
11
|
+
return path === "" || (!isAbsolute(path) && path !== ".." && !path.startsWith(`..${sep}`));
|
|
12
|
+
}
|
|
13
|
+
function relevant(directory, path, event) {
|
|
14
|
+
const parts = path.split(/[\\/]/);
|
|
15
|
+
if (parts.some(part => ignored.has(part)))
|
|
16
|
+
return false;
|
|
17
|
+
const name = parts.at(-1);
|
|
18
|
+
if (/\.(?:log|tmp|swp|swo|map|tsbuildinfo)$/.test(name) || name === ".DS_Store")
|
|
19
|
+
return false;
|
|
20
|
+
if (/\.(?:[cm]?[jt]sx?|jsonc?|ya?ml|toml)$/.test(name) || name === ".env" || name.startsWith(".env.")
|
|
21
|
+
|| ["bun.lock", "bun.lockb", "yarn.lock"].includes(name))
|
|
22
|
+
return true;
|
|
23
|
+
if (event !== "rename")
|
|
24
|
+
return false;
|
|
25
|
+
// A newly created/moved directory can introduce declarations at any depth.
|
|
26
|
+
// Deleted paths cannot be classified, so conservatively refresh discovery.
|
|
27
|
+
try {
|
|
28
|
+
return lstatSync(resolve(directory, path)).isDirectory();
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** Development-only supervisor. Each generation gets a fresh module cache. */
|
|
35
|
+
export async function startDevelopment(configPath) {
|
|
36
|
+
if (typeof Bun === "undefined")
|
|
37
|
+
throw new Error("hulla-db dev requires Bun. Use Node or Bun with hulla-db serve for generated applications.");
|
|
38
|
+
const config = resolve(configPath), root = dirname(config);
|
|
39
|
+
const watchers = new Map();
|
|
40
|
+
let child;
|
|
41
|
+
let timer;
|
|
42
|
+
let stopped = false, revision = 0, chain = Promise.resolve();
|
|
43
|
+
let finish;
|
|
44
|
+
const closed = new Promise(resolve => { finish = resolve; });
|
|
45
|
+
async function stopChild() {
|
|
46
|
+
if (!child)
|
|
47
|
+
return;
|
|
48
|
+
const previous = child;
|
|
49
|
+
child = undefined;
|
|
50
|
+
previous.kill("SIGTERM");
|
|
51
|
+
const force = setTimeout(() => previous.kill("SIGKILL"), 5000);
|
|
52
|
+
try {
|
|
53
|
+
await previous.exited;
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
clearTimeout(force);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function schedule() {
|
|
60
|
+
if (stopped)
|
|
61
|
+
return;
|
|
62
|
+
const next = ++revision;
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
timer = setTimeout(() => {
|
|
65
|
+
chain = chain.then(async () => {
|
|
66
|
+
await stopChild();
|
|
67
|
+
if (stopped || next !== revision)
|
|
68
|
+
return;
|
|
69
|
+
launch();
|
|
70
|
+
}).catch(() => { console.error("Development restart failed; waiting for the next source change."); });
|
|
71
|
+
}, 100);
|
|
72
|
+
}
|
|
73
|
+
function watchDirectory(directory) {
|
|
74
|
+
const path = resolve(directory);
|
|
75
|
+
if ([...watchers.keys()].some(parent => contains(parent, path)))
|
|
76
|
+
return;
|
|
77
|
+
try {
|
|
78
|
+
const watcher = watch(path, { recursive: true }, (event, filename) => {
|
|
79
|
+
if (filename === null || relevant(path, String(filename), event))
|
|
80
|
+
schedule();
|
|
81
|
+
});
|
|
82
|
+
watcher.on("error", () => {
|
|
83
|
+
watcher.close();
|
|
84
|
+
watchers.delete(path);
|
|
85
|
+
console.error(`Cannot watch ${path}; restart dev after restoring this directory.`);
|
|
86
|
+
});
|
|
87
|
+
watchers.set(path, watcher);
|
|
88
|
+
for (const [existing, handle] of watchers) {
|
|
89
|
+
if (existing !== path && contains(path, existing)) {
|
|
90
|
+
handle.close();
|
|
91
|
+
watchers.delete(existing);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
console.error(`Cannot watch ${path}; changes there require restarting dev.`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function launch() {
|
|
100
|
+
const runtimeArguments = runtimeExportConditions().map(condition => `--conditions=${condition}`);
|
|
101
|
+
const workerPath = Bun.fileURLToPath(new URL(import.meta.url.endsWith(".ts") ? "./dev-worker.ts" : "./dev-worker.js", import.meta.url));
|
|
102
|
+
try {
|
|
103
|
+
child = Bun.spawn([process.execPath, ...runtimeArguments, workerPath, config], {
|
|
104
|
+
stdin: "ignore", stdout: "inherit", stderr: "inherit",
|
|
105
|
+
ipc(message, sender) {
|
|
106
|
+
if (stopped || child !== sender || !message || typeof message !== "object" || !("watch" in message)
|
|
107
|
+
|| !Array.isArray(message.watch))
|
|
108
|
+
return;
|
|
109
|
+
const roots = [root, ...message.watch.filter((path) => typeof path === "string").map(path => resolve(path))];
|
|
110
|
+
const desired = roots.filter((path, index) => !roots.some((parent, other) => other !== index && contains(parent, path) && (parent !== path || other < index)));
|
|
111
|
+
for (const [path, watcher] of watchers) {
|
|
112
|
+
if (!desired.includes(path)) {
|
|
113
|
+
watcher.close();
|
|
114
|
+
watchers.delete(path);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const path of desired)
|
|
118
|
+
watchDirectory(path);
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
console.error("Cannot start development worker; waiting for the next source change.");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
watchDirectory(root);
|
|
127
|
+
if (!watchers.size)
|
|
128
|
+
throw new Error("Cannot watch the project directory");
|
|
129
|
+
launch();
|
|
130
|
+
let closing;
|
|
131
|
+
return {
|
|
132
|
+
closed,
|
|
133
|
+
close() {
|
|
134
|
+
closing ??= (async () => {
|
|
135
|
+
stopped = true;
|
|
136
|
+
revision++;
|
|
137
|
+
clearTimeout(timer);
|
|
138
|
+
for (const watcher of watchers.values())
|
|
139
|
+
watcher.close();
|
|
140
|
+
watchers.clear();
|
|
141
|
+
await chain;
|
|
142
|
+
await stopChild();
|
|
143
|
+
finish();
|
|
144
|
+
})();
|
|
145
|
+
return closing;
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Error details are explicit JSON data, without class instances or implicit conversions. */
|
|
2
|
+
export type DatabaseErrorDetails = null | string | number | boolean | readonly DatabaseErrorDetails[] | {
|
|
3
|
+
readonly [key: string]: DatabaseErrorDetails;
|
|
4
|
+
};
|
|
5
|
+
export interface DatabaseErrorOptions {
|
|
6
|
+
readonly code: string;
|
|
7
|
+
readonly message: string;
|
|
8
|
+
readonly details?: DatabaseErrorDetails;
|
|
9
|
+
}
|
|
10
|
+
/** An intentional caller-visible failure. Unexpected exceptions stay server-local. */
|
|
11
|
+
export declare class DatabaseError extends Error {
|
|
12
|
+
readonly code: string;
|
|
13
|
+
readonly details?: DatabaseErrorDetails;
|
|
14
|
+
constructor({ code, message, details }: DatabaseErrorOptions);
|
|
15
|
+
/** Only intentionally declared fields cross a serialized error boundary. */
|
|
16
|
+
toJSON(): DatabaseErrorOptions;
|
|
17
|
+
}
|
|
18
|
+
/** Internal provenance: caller-chosen codes cannot control the transport. */
|
|
19
|
+
export declare function applicationError(options: DatabaseErrorOptions): DatabaseError;
|
|
20
|
+
export declare function isApplicationError(error: unknown): boolean;
|