@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
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { DatabaseOperation } from "./database.ts";
|
|
2
|
+
import type { CompiledTable } from "./schema.ts";
|
|
3
|
+
import { type FieldReference } from "./fields.ts";
|
|
4
|
+
import { type Expressions, type Predicate } from "./filters.ts";
|
|
5
|
+
declare const aggregateValue: unique symbol;
|
|
6
|
+
export interface AggregateValue<V, Decimal extends boolean = false> {
|
|
7
|
+
readonly [aggregateValue]: {
|
|
8
|
+
readonly value: V;
|
|
9
|
+
readonly decimal: Decimal;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
type Keys<R, V> = {
|
|
13
|
+
[K in Extract<keyof R, string>]: NonNullable<R[K]> extends V ? K : never;
|
|
14
|
+
}[Extract<keyof R, string>];
|
|
15
|
+
type AggregateField<R, V, T extends string> = Keys<R, V> | {
|
|
16
|
+
[K in Keys<R, V>]: FieldReference<R[K], T>;
|
|
17
|
+
}[Keys<R, V>];
|
|
18
|
+
type Value<R, F> = F extends FieldReference<unknown, string, infer V> ? V : F extends keyof R ? R[F] : never;
|
|
19
|
+
type Average<V> = V extends bigint ? string : V extends number ? number : never;
|
|
20
|
+
type BaseField<R, B, V, T extends string> = AggregateField<B, V, T> | {
|
|
21
|
+
[K in Keys<B, V> & keyof R]: FieldReference<R[K], T, B[K]>;
|
|
22
|
+
}[Keys<B, V> & keyof R];
|
|
23
|
+
export interface Aggregates<R, T extends string = string, B = R> {
|
|
24
|
+
count(): AggregateValue<number>;
|
|
25
|
+
sum<F extends BaseField<R, B, number | bigint, T>>(field: F): AggregateValue<NonNullable<Value<B, F>> | null>;
|
|
26
|
+
avg<F extends BaseField<R, B, number | bigint, T>>(field: F): AggregateValue<Average<Value<B, F>> | null, Extract<Value<B, F>, bigint> extends never ? false : true>;
|
|
27
|
+
min<F extends BaseField<R, B, string | number | bigint | Date, T>>(field: F): AggregateValue<NonNullable<Value<B, F>> | null>;
|
|
28
|
+
max<F extends BaseField<R, B, string | number | bigint | Date, T>>(field: F): AggregateValue<NonNullable<Value<B, F>> | null>;
|
|
29
|
+
}
|
|
30
|
+
export type AggregateSelection = Record<string, AggregateValue<unknown, boolean>>;
|
|
31
|
+
export type AggregateResult<A extends AggregateSelection> = {
|
|
32
|
+
[K in keyof A]: A[K] extends AggregateValue<infer V, boolean> ? V : never;
|
|
33
|
+
};
|
|
34
|
+
type DecimalKeys<A extends AggregateSelection> = {
|
|
35
|
+
[K in keyof A]: A[K] extends AggregateValue<unknown, true> ? K : never;
|
|
36
|
+
}[keyof A];
|
|
37
|
+
type TextHelpers = "contains" | "startsWith" | "endsWith" | "like" | "ilike";
|
|
38
|
+
type HavingExpressions<R, T extends string, N extends keyof R, B = R> = Omit<Expressions<R, T, B>, TextHelpers> & Pick<Expressions<Omit<R, N>, T, Omit<B, N>>, TextHelpers>;
|
|
39
|
+
export interface GroupedAggregate<R, T extends string = string, N extends keyof R = never, B = R> extends DatabaseOperation<R[]> {
|
|
40
|
+
having(predicate: (expressions: HavingExpressions<R, T, N, B>) => Predicate): GroupedAggregate<R, T, N, B>;
|
|
41
|
+
orderBy(field: Extract<keyof R, string>, direction?: "asc" | "desc"): GroupedAggregate<R, T, N, B>;
|
|
42
|
+
limit(count: number): DatabaseOperation<R[]>;
|
|
43
|
+
}
|
|
44
|
+
export interface GroupedQuery<R, G extends keyof R, T extends string = string, B = R> {
|
|
45
|
+
aggregate<A extends AggregateSelection>(select: (aggregates: Aggregates<R, T, B>) => A & (Extract<keyof A, G> extends never ? unknown : {
|
|
46
|
+
readonly duplicateGroupKeys: never;
|
|
47
|
+
})): GroupedAggregate<Pick<R, G> & AggregateResult<A>, T, Extract<DecimalKeys<A>, keyof (Pick<R, G> & AggregateResult<A>)>, Pick<B, G & keyof B> & AggregateResult<A>>;
|
|
48
|
+
}
|
|
49
|
+
export type GroupKey<R> = Keys<R, string | number | bigint | boolean | Date>;
|
|
50
|
+
export type AggregateDescriptor = {
|
|
51
|
+
op: "count" | "sum" | "avg" | "min" | "max";
|
|
52
|
+
field?: string;
|
|
53
|
+
};
|
|
54
|
+
export type AggregatePlan = {
|
|
55
|
+
groupBy: string[];
|
|
56
|
+
aggregates: Record<string, AggregateDescriptor>;
|
|
57
|
+
parseHaving(args: readonly unknown[]): Predicate[];
|
|
58
|
+
parseOrder(field: unknown): string;
|
|
59
|
+
decode(result: unknown): Record<string, unknown> | Record<string, unknown>[];
|
|
60
|
+
};
|
|
61
|
+
/** Aggregate outputs have their own representations: sums are not stored int64 fields. */
|
|
62
|
+
export declare function aggregatePlan(table: CompiledTable, groupInputs: readonly unknown[], select: unknown): AggregatePlan;
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { fieldPath } from "./fields.js";
|
|
2
|
+
import { predicateParser } from "./filters.js";
|
|
3
|
+
const aggregateValue = Symbol("Hulla aggregate value");
|
|
4
|
+
function integer(value) {
|
|
5
|
+
if (typeof value !== "string" || !/^(0|-?[1-9][0-9]*)$/.test(value))
|
|
6
|
+
throw new TypeError("Invalid aggregate bigint result");
|
|
7
|
+
return BigInt(value);
|
|
8
|
+
}
|
|
9
|
+
function decimal(value) {
|
|
10
|
+
if (typeof value !== "string" || !/^-?(0|[1-9][0-9]*)(\.[0-9]+)?$/.test(value))
|
|
11
|
+
throw new TypeError("Expected a decimal string");
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function number(value) {
|
|
15
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
16
|
+
throw new TypeError("Invalid aggregate number result");
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
function count(value) {
|
|
20
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0)
|
|
21
|
+
throw new TypeError("Aggregate count must be a nonnegative safe integer");
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
/** Aggregate outputs have their own representations: sums are not stored int64 fields. */
|
|
25
|
+
export function aggregatePlan(table, groupInputs, select) {
|
|
26
|
+
function root(input) {
|
|
27
|
+
const path = fieldPath(input, table.name);
|
|
28
|
+
if (typeof path !== "string" && path.length !== 1)
|
|
29
|
+
throw new TypeError("Aggregates require root columns");
|
|
30
|
+
const name = typeof path === "string" ? path : path[0];
|
|
31
|
+
if (!Object.hasOwn(table.fields, name))
|
|
32
|
+
throw new TypeError(`Unknown field ${table.name}.${name}`);
|
|
33
|
+
if (table.fields[name].scalar === "json")
|
|
34
|
+
throw new TypeError("JSON aggregation and grouping are not supported");
|
|
35
|
+
return name;
|
|
36
|
+
}
|
|
37
|
+
const groupBy = groupInputs.map(root);
|
|
38
|
+
if (new Set(groupBy).size !== groupBy.length)
|
|
39
|
+
throw new TypeError("Duplicate grouping field");
|
|
40
|
+
const values = new WeakMap();
|
|
41
|
+
function expression(op, inputs) {
|
|
42
|
+
if (inputs.length !== (op === "count" ? 0 : 1))
|
|
43
|
+
throw new TypeError(`Invalid ${op} arguments`);
|
|
44
|
+
const descriptor = op === "count" ? { op } : { op, field: root(inputs[0]) };
|
|
45
|
+
const scalar = descriptor.field === undefined ? undefined : table.fields[descriptor.field].scalar;
|
|
46
|
+
if ((op === "sum" || op === "avg") && scalar !== "int64" && scalar !== "float64")
|
|
47
|
+
throw new TypeError(`${op} requires a numeric column`);
|
|
48
|
+
if ((op === "min" || op === "max") && !["string", "int64", "float64", "date", "timestamp", "localTimestamp"].includes(scalar))
|
|
49
|
+
throw new TypeError(`${op} requires a string or numeric column`);
|
|
50
|
+
const token = Object.freeze({});
|
|
51
|
+
values.set(token, descriptor);
|
|
52
|
+
return token;
|
|
53
|
+
}
|
|
54
|
+
if (typeof select !== "function")
|
|
55
|
+
throw new TypeError("Aggregate requires a callback");
|
|
56
|
+
const selected = select(Object.freeze(Object.fromEntries(["count", "sum", "avg", "min", "max"].map(op => [op, (...inputs) => expression(op, inputs)]))));
|
|
57
|
+
if (!selected || typeof selected !== "object" || Array.isArray(selected) || !Object.keys(selected).length)
|
|
58
|
+
throw new TypeError("Aggregate callback must return named aggregates");
|
|
59
|
+
const descriptors = Object.create(null);
|
|
60
|
+
const decoders = Object.create(null);
|
|
61
|
+
for (const name of groupBy) {
|
|
62
|
+
const field = table.fields[name];
|
|
63
|
+
descriptors[name] = field;
|
|
64
|
+
decoders[name] = value => field.decode(value);
|
|
65
|
+
}
|
|
66
|
+
const aggregates = Object.fromEntries(Object.entries(selected).map(([alias, token]) => {
|
|
67
|
+
if (groupBy.includes(alias))
|
|
68
|
+
throw new TypeError(`Aggregate alias ${alias} collides with a grouping field`);
|
|
69
|
+
const descriptor = token && typeof token === "object" ? values.get(token) : undefined;
|
|
70
|
+
if (!descriptor)
|
|
71
|
+
throw new TypeError(`Invalid aggregate ${alias}`);
|
|
72
|
+
const field = descriptor.field === undefined ? undefined : table.fields[descriptor.field];
|
|
73
|
+
let output;
|
|
74
|
+
let decode;
|
|
75
|
+
if (descriptor.op === "count") {
|
|
76
|
+
output = { scalar: "float64", parse: count };
|
|
77
|
+
decode = count;
|
|
78
|
+
}
|
|
79
|
+
else if (descriptor.op === "avg" && field.scalar === "int64") {
|
|
80
|
+
const bigint = field.mode === "bigint";
|
|
81
|
+
output = { scalar: "decimal", parse: bigint ? decimal : value => {
|
|
82
|
+
number(value);
|
|
83
|
+
return String(value);
|
|
84
|
+
} };
|
|
85
|
+
decode = bigint ? decimal : value => number(Number(decimal(value)));
|
|
86
|
+
}
|
|
87
|
+
else if (field.scalar === "int64") {
|
|
88
|
+
const bigint = field.mode === "bigint";
|
|
89
|
+
const operand = (value) => {
|
|
90
|
+
if (bigint ? typeof value !== "bigint" : typeof value !== "number" || !Number.isSafeInteger(value))
|
|
91
|
+
throw new TypeError("Expected safe integer aggregate operand");
|
|
92
|
+
return bigint ? value : BigInt(value);
|
|
93
|
+
};
|
|
94
|
+
const decodeInteger = (value) => {
|
|
95
|
+
const result = integer(value);
|
|
96
|
+
if (descriptor.op !== "sum" && (result < -(1n << 63n) || result >= 1n << 63n))
|
|
97
|
+
throw new TypeError("Invalid aggregate int64");
|
|
98
|
+
if (bigint)
|
|
99
|
+
return result;
|
|
100
|
+
const numeric = Number(result);
|
|
101
|
+
if (!Number.isSafeInteger(numeric))
|
|
102
|
+
throw new TypeError("Aggregate int64 exceeds safe integer range; use mode: bigint");
|
|
103
|
+
return numeric;
|
|
104
|
+
};
|
|
105
|
+
output = { scalar: "int64", parse: operand };
|
|
106
|
+
decode = decodeInteger;
|
|
107
|
+
}
|
|
108
|
+
else if (field.scalar === "float64") {
|
|
109
|
+
output = { scalar: "float64", parse: number };
|
|
110
|
+
decode = number;
|
|
111
|
+
}
|
|
112
|
+
else if (["date", "timestamp", "localTimestamp"].includes(field.scalar) && !field.customCodec) {
|
|
113
|
+
output = field;
|
|
114
|
+
decode = value => field.decode(value);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
output = { scalar: field.scalar, parse: value => field.storage(value) };
|
|
118
|
+
decode = value => field.storage(value);
|
|
119
|
+
}
|
|
120
|
+
descriptors[alias] = output;
|
|
121
|
+
decoders[alias] = value => descriptor.op !== "count" && value === null ? null : decode(value);
|
|
122
|
+
return [alias, descriptor];
|
|
123
|
+
}));
|
|
124
|
+
function row(value) {
|
|
125
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
126
|
+
throw new TypeError("Invalid aggregate result");
|
|
127
|
+
if (Object.keys(value).length !== Object.keys(decoders).length || Object.keys(decoders).some(key => !Object.hasOwn(value, key)))
|
|
128
|
+
throw new TypeError("Aggregate result does not match requested fields");
|
|
129
|
+
return Object.fromEntries(Object.entries(decoders).map(([key, decode]) => [key, decode(value[key])]));
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
groupBy, aggregates,
|
|
133
|
+
parseHaving: predicateParser(table.name, descriptors),
|
|
134
|
+
parseOrder(field) {
|
|
135
|
+
if (typeof field !== "string" || !Object.hasOwn(descriptors, field))
|
|
136
|
+
throw new TypeError("Expected a grouping field or aggregate alias");
|
|
137
|
+
return field;
|
|
138
|
+
},
|
|
139
|
+
decode(result) {
|
|
140
|
+
if (!groupBy.length)
|
|
141
|
+
return row(result);
|
|
142
|
+
if (!Array.isArray(result))
|
|
143
|
+
throw new TypeError("Expected grouped aggregate results");
|
|
144
|
+
return result.map(row);
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Client cache identity only. Credentials are supplied separately by the transport. */
|
|
2
|
+
export type ClientAuthState = null | {
|
|
3
|
+
readonly key: string;
|
|
4
|
+
/** Optional client invalidation deadline, as Unix time in milliseconds. */
|
|
5
|
+
readonly expiresAt?: number;
|
|
6
|
+
};
|
|
7
|
+
export type ClientAuthInput = ClientAuthState | PromiseLike<ClientAuthState>;
|
|
8
|
+
export declare function isAuthPromise(value: ClientAuthInput): value is PromiseLike<ClientAuthState>;
|
|
9
|
+
export declare function authState(value: ClientAuthState): ClientAuthState;
|
|
10
|
+
export declare function sameAuth(a: ClientAuthState, b: ClientAuthState): boolean;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function isAuthPromise(value) {
|
|
2
|
+
return value !== null && (typeof value === "object" || typeof value === "function")
|
|
3
|
+
&& "then" in value && typeof value.then === "function";
|
|
4
|
+
}
|
|
5
|
+
export function authState(value) {
|
|
6
|
+
if (value === null)
|
|
7
|
+
return null;
|
|
8
|
+
if (typeof value !== "object" || Array.isArray(value))
|
|
9
|
+
throw new TypeError("Invalid client auth state");
|
|
10
|
+
if (typeof value.key === "string" && value.key.length > 0
|
|
11
|
+
&& Object.keys(value).every(key => key === "key" || key === "expiresAt")) {
|
|
12
|
+
if (value.expiresAt !== undefined && (!Number.isSafeInteger(value.expiresAt) || value.expiresAt < 0)) {
|
|
13
|
+
throw new TypeError("expiresAt must be a non-negative Unix timestamp in milliseconds");
|
|
14
|
+
}
|
|
15
|
+
return Object.freeze({ key: value.key,
|
|
16
|
+
...(value.expiresAt === undefined ? {} : { expiresAt: value.expiresAt }) });
|
|
17
|
+
}
|
|
18
|
+
throw new TypeError("Auth state accepts only a cache key with optional expiresAt, or null");
|
|
19
|
+
}
|
|
20
|
+
export function sameAuth(a, b) {
|
|
21
|
+
return a === b || (a !== null && b !== null && a.key === b.key);
|
|
22
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** Framework-neutral observation. UI packages own rendering, never a second cache. */
|
|
2
|
+
import type { FunctionReference, FunctionSignature, Input, Output, QueryState, MutationState, MutationHandle, QueryHandle, PageHandle } from "./contracts.ts";
|
|
3
|
+
export type QueryFunction = FunctionReference<FunctionSignature & {
|
|
4
|
+
kind: "query";
|
|
5
|
+
}> & ((...args: never[]) => QueryHandle<unknown>);
|
|
6
|
+
/** Only generated references carrying the direct-page capability qualify. */
|
|
7
|
+
export type PaginatedQueryFunction = QueryFunction & {
|
|
8
|
+
paginate(mapper: (cursor: string | null) => never): PageHandle<unknown>;
|
|
9
|
+
};
|
|
10
|
+
export type PageRow<Q extends PaginatedQueryFunction> = Output<Q> extends {
|
|
11
|
+
readonly page: readonly (infer Row)[];
|
|
12
|
+
} ? Row : never;
|
|
13
|
+
export type PaginationBindingOptions<T> = QueryBindingOptions<readonly T[]>;
|
|
14
|
+
export type PaginationSnapshot<T> = QueryState<readonly T[]> & {
|
|
15
|
+
readonly hasMore: boolean;
|
|
16
|
+
readonly next: (pages?: number) => Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
export type MutationFunction = FunctionReference<FunctionSignature & {
|
|
19
|
+
kind: "mutation";
|
|
20
|
+
}> & ((...args: never[]) => MutationHandle<unknown>);
|
|
21
|
+
export interface QueryBindingOptions<T> {
|
|
22
|
+
/** Authorized SSR data, displayed only until the first live result or failure. */
|
|
23
|
+
readonly initialData?: T;
|
|
24
|
+
/** Same cache key supplied to setAuth on the server-rendered account. Defaults to anonymous. */
|
|
25
|
+
readonly initialAuthKey?: string | null;
|
|
26
|
+
readonly keepPreviousDataOnError?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export type QueryArguments<Q extends QueryFunction> = [Input<Q>] extends [undefined] ? [options?: QueryBindingOptions<NoInfer<Output<Q>>>] : [input: Input<Q>, options?: QueryBindingOptions<NoInfer<Output<Q>>>];
|
|
29
|
+
export type MutationArguments<M extends MutationFunction> = [Input<M>] extends [undefined] ? [] : [input: Input<M>];
|
|
30
|
+
export type MutationBindingState<T> = {
|
|
31
|
+
readonly status: "idle";
|
|
32
|
+
} | MutationState<T>;
|
|
33
|
+
export declare function queryArguments<Q extends QueryFunction>(query: Q, args: QueryArguments<Q>): {
|
|
34
|
+
hasInput: boolean;
|
|
35
|
+
input: Input<Q>;
|
|
36
|
+
options: QueryBindingOptions<Output<Q>>;
|
|
37
|
+
};
|
|
38
|
+
export declare function queryKey<Q extends QueryFunction>(query: Q, input: Input<Q>): string;
|
|
39
|
+
/** Constructing an observer is pure: subscribe starts the live watch, never call(). */
|
|
40
|
+
export declare function createQueryObserver<Q extends QueryFunction>(query: Q, ...args: QueryArguments<Q>): {
|
|
41
|
+
getSnapshot: () => QueryState<Output<Q>>;
|
|
42
|
+
getServerSnapshot: () => QueryState<Output<Q>>;
|
|
43
|
+
subscribe(listener: () => void): () => void;
|
|
44
|
+
};
|
|
45
|
+
/** Observe the core page handle; page ranges and rows stay owned by that handle. */
|
|
46
|
+
export declare function createPaginationObserver<Q extends PaginatedQueryFunction>(query: Q, mapper: (cursor: string | null) => Input<Q>, options?: PaginationBindingOptions<NoInfer<PageRow<Q>>>): {
|
|
47
|
+
subscribe(listener: () => void): () => void;
|
|
48
|
+
getSnapshot: () => PaginationSnapshot<PageRow<Q>>;
|
|
49
|
+
getServerSnapshot: () => PaginationSnapshot<PageRow<Q>>;
|
|
50
|
+
};
|
|
51
|
+
export declare function createMutationObserver<M extends MutationFunction>(mutation: M): {
|
|
52
|
+
getSnapshot: () => MutationBindingState<Output<M>>;
|
|
53
|
+
getServerSnapshot: () => MutationBindingState<Output<M>>;
|
|
54
|
+
subscribe(listener: () => void): () => void;
|
|
55
|
+
invoke(...args: MutationArguments<M>): MutationHandle<Output<M>>;
|
|
56
|
+
};
|
package/dist/bindings.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { bindingInfo } from "./callable.js";
|
|
2
|
+
import { clearsPrevious, querySnapshot } from "./handles.js";
|
|
3
|
+
import { canonical } from "./http/protocol.js";
|
|
4
|
+
import { notification, stagedObserver } from "./notifications.js";
|
|
5
|
+
import { DatabaseError } from "./error.js";
|
|
6
|
+
export function queryArguments(query, args) {
|
|
7
|
+
const { hasInput } = bindingInfo(query);
|
|
8
|
+
return {
|
|
9
|
+
hasInput,
|
|
10
|
+
input: (hasInput ? args[0] : undefined),
|
|
11
|
+
options: ((hasInput ? args[1] : args[0]) ?? {}),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function queryKey(query, input) {
|
|
15
|
+
const { codecs } = bindingInfo(query);
|
|
16
|
+
const encoded = codecs ? codecs.input.encode(input) : input;
|
|
17
|
+
return encoded === undefined ? "undefined" : canonical(encoded);
|
|
18
|
+
}
|
|
19
|
+
function emit(listeners) {
|
|
20
|
+
for (const listener of [...listeners]) {
|
|
21
|
+
try {
|
|
22
|
+
listener();
|
|
23
|
+
}
|
|
24
|
+
catch { /* Observer failures belong to the application. */ }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Shared auth, SSR seed and subscription lifetime for ordinary and paginated queries. */
|
|
28
|
+
function createObservation(query, options, watch) {
|
|
29
|
+
const metadata = bindingInfo(query);
|
|
30
|
+
if (metadata.kind !== "query")
|
|
31
|
+
throw new TypeError("useQuery requires a query");
|
|
32
|
+
if (options.initialAuthKey !== undefined && options.initialAuthKey !== null && typeof options.initialAuthKey !== "string")
|
|
33
|
+
throw new TypeError("initialAuthKey must be a string or null");
|
|
34
|
+
const pending = { status: "pending" };
|
|
35
|
+
const initial = Object.hasOwn(options, "initialData")
|
|
36
|
+
? { status: "pending", data: options.initialData, isStale: true, isOptimistic: false } : pending;
|
|
37
|
+
let state = initial;
|
|
38
|
+
let seed = initial !== pending;
|
|
39
|
+
let hasLiveData = false;
|
|
40
|
+
let runtime;
|
|
41
|
+
let epoch;
|
|
42
|
+
let needsAttach = true;
|
|
43
|
+
let generation = 0;
|
|
44
|
+
let stopWatch;
|
|
45
|
+
let stopScope;
|
|
46
|
+
let stopIdentity;
|
|
47
|
+
const listeners = new Set();
|
|
48
|
+
const publish = (next) => {
|
|
49
|
+
state = next;
|
|
50
|
+
notification(() => emit(listeners));
|
|
51
|
+
};
|
|
52
|
+
const disconnect = () => {
|
|
53
|
+
generation++;
|
|
54
|
+
const stop = stopWatch;
|
|
55
|
+
stopWatch = undefined;
|
|
56
|
+
stop?.();
|
|
57
|
+
stopIdentity?.();
|
|
58
|
+
stopIdentity = undefined;
|
|
59
|
+
};
|
|
60
|
+
const current = () => {
|
|
61
|
+
try {
|
|
62
|
+
return metadata.scope.current();
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const seedMatches = (owner) => {
|
|
69
|
+
const auth = owner.clientAuth;
|
|
70
|
+
return !auth?.pending && !auth?.error && (auth?.key ?? null) === (options.initialAuthKey ?? null);
|
|
71
|
+
};
|
|
72
|
+
const attach = () => {
|
|
73
|
+
const owner = current();
|
|
74
|
+
if (!needsAttach && owner && owner === runtime && owner.identityEpoch === epoch)
|
|
75
|
+
return;
|
|
76
|
+
disconnect();
|
|
77
|
+
needsAttach = false;
|
|
78
|
+
if (runtime) {
|
|
79
|
+
seed = false;
|
|
80
|
+
hasLiveData = false;
|
|
81
|
+
state = pending;
|
|
82
|
+
}
|
|
83
|
+
runtime = owner;
|
|
84
|
+
epoch = owner?.identityEpoch;
|
|
85
|
+
if (!owner) {
|
|
86
|
+
publish({ status: "error", error: new Error("Configure the browser runtime before mounting Hulla bindings") });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (!seedMatches(owner)) {
|
|
90
|
+
seed = false;
|
|
91
|
+
state = pending;
|
|
92
|
+
}
|
|
93
|
+
const auth = owner.clientAuth;
|
|
94
|
+
if (auth?.pending) {
|
|
95
|
+
publish(pending);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (auth?.error) {
|
|
99
|
+
publish({ status: "error", error: auth.error });
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const signal = owner.identitySignal;
|
|
103
|
+
const previousKey = owner.clientAuth?.key ?? null;
|
|
104
|
+
const retired = () => {
|
|
105
|
+
disconnect();
|
|
106
|
+
seed = false;
|
|
107
|
+
hasLiveData = false;
|
|
108
|
+
epoch = owner.identityEpoch;
|
|
109
|
+
needsAttach = true;
|
|
110
|
+
const reason = signal?.reason;
|
|
111
|
+
publish(reason instanceof DatabaseError && reason.code !== "context_changed"
|
|
112
|
+
? { status: "error", error: reason } : pending);
|
|
113
|
+
// Expiry changes the key without setAuth. Reattach only for a local key
|
|
114
|
+
// change; a server authorization error must not create a retry loop.
|
|
115
|
+
if ((owner.clientAuth?.key ?? null) !== previousKey)
|
|
116
|
+
queueMicrotask(() => { if (listeners.size)
|
|
117
|
+
attach(); });
|
|
118
|
+
};
|
|
119
|
+
signal?.addEventListener("abort", retired, { once: true });
|
|
120
|
+
stopIdentity = () => signal?.removeEventListener("abort", retired);
|
|
121
|
+
const active = generation;
|
|
122
|
+
try {
|
|
123
|
+
const stop = watch(owner, stagedObserver(raw => {
|
|
124
|
+
if (active !== generation)
|
|
125
|
+
return;
|
|
126
|
+
let next = raw;
|
|
127
|
+
if (next.status === "pending" && seed)
|
|
128
|
+
next = initial;
|
|
129
|
+
else if (next.status !== "pending") {
|
|
130
|
+
seed = false;
|
|
131
|
+
if (next.status === "error" && hasLiveData && options.keepPreviousDataOnError && !clearsPrevious(next.error) && state.data !== undefined) {
|
|
132
|
+
next = { ...next, data: state.data, isStale: true, isOptimistic: false };
|
|
133
|
+
}
|
|
134
|
+
if (next.status === "success")
|
|
135
|
+
hasLiveData = true;
|
|
136
|
+
}
|
|
137
|
+
publish(next);
|
|
138
|
+
}));
|
|
139
|
+
if (active === generation && listeners.size)
|
|
140
|
+
stopWatch = stop;
|
|
141
|
+
else
|
|
142
|
+
stop();
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
seed = false;
|
|
146
|
+
publish({ status: "error", error });
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const getSnapshot = () => {
|
|
150
|
+
// Also fence a seed before effects run, e.g. hydration after account switch.
|
|
151
|
+
const owner = current();
|
|
152
|
+
if (runtime && runtime.identityEpoch !== epoch) {
|
|
153
|
+
seed = false;
|
|
154
|
+
state = pending;
|
|
155
|
+
}
|
|
156
|
+
if (seed && owner && !seedMatches(owner)) {
|
|
157
|
+
seed = false;
|
|
158
|
+
state = pending;
|
|
159
|
+
}
|
|
160
|
+
return state;
|
|
161
|
+
};
|
|
162
|
+
return {
|
|
163
|
+
getSnapshot,
|
|
164
|
+
// SSR and hydration use the exact same supplied snapshot, never a shared
|
|
165
|
+
// browser cache or an eagerly executed declaration.
|
|
166
|
+
getServerSnapshot: getSnapshot,
|
|
167
|
+
subscribe(listener) {
|
|
168
|
+
listeners.add(listener);
|
|
169
|
+
if (listeners.size === 1) {
|
|
170
|
+
stopScope = metadata.scope.subscribe(attach);
|
|
171
|
+
// A StrictMode cleanup/reconnect needs a new watch, even in the same epoch.
|
|
172
|
+
runtime = undefined;
|
|
173
|
+
attach();
|
|
174
|
+
}
|
|
175
|
+
return () => {
|
|
176
|
+
listeners.delete(listener);
|
|
177
|
+
if (!listeners.size) {
|
|
178
|
+
disconnect();
|
|
179
|
+
stopScope?.();
|
|
180
|
+
stopScope = undefined;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/** Constructing an observer is pure: subscribe starts the live watch, never call(). */
|
|
187
|
+
export function createQueryObserver(query, ...args) {
|
|
188
|
+
const { input, options } = queryArguments(query, args);
|
|
189
|
+
const metadata = bindingInfo(query);
|
|
190
|
+
return createObservation(query, options, (owner, receive) => {
|
|
191
|
+
if (!owner.watch)
|
|
192
|
+
throw new Error("The configured runtime does not support live queries");
|
|
193
|
+
return owner.watch({ name: metadata.name, kind: "query", input,
|
|
194
|
+
...(metadata.codecs ? { codecs: metadata.codecs } : {}) }, stagedObserver(raw => receive(querySnapshot(raw, metadata.codecs))), {});
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/** Observe the core page handle; page ranges and rows stay owned by that handle. */
|
|
198
|
+
export function createPaginationObserver(query, mapper, options = {}) {
|
|
199
|
+
if (typeof query.paginate !== "function")
|
|
200
|
+
throw new TypeError("usePaginatedQuery requires a paginated query");
|
|
201
|
+
if (typeof mapper !== "function")
|
|
202
|
+
throw new TypeError("Pagination requires an input mapper");
|
|
203
|
+
let handle;
|
|
204
|
+
const next = (pages = 1) => handle
|
|
205
|
+
? handle.next(pages)
|
|
206
|
+
: Promise.reject(new Error("Mount the paginated query before loading more pages"));
|
|
207
|
+
const observation = createObservation(query, options, (_owner, receive) => {
|
|
208
|
+
const active = query.paginate(mapper);
|
|
209
|
+
handle = active;
|
|
210
|
+
const stop = active.subscribe(receive);
|
|
211
|
+
return () => { if (handle === active)
|
|
212
|
+
handle = undefined; stop(); active.dispose(); };
|
|
213
|
+
});
|
|
214
|
+
let previous;
|
|
215
|
+
let snapshot;
|
|
216
|
+
const getSnapshot = () => {
|
|
217
|
+
const state = observation.getSnapshot();
|
|
218
|
+
const hasMore = handle?.hasMore ?? false;
|
|
219
|
+
if (previous !== state || snapshot.hasMore !== hasMore) {
|
|
220
|
+
previous = state;
|
|
221
|
+
snapshot = { ...state, hasMore, next };
|
|
222
|
+
}
|
|
223
|
+
return snapshot;
|
|
224
|
+
};
|
|
225
|
+
return { ...observation, getSnapshot, getServerSnapshot: getSnapshot };
|
|
226
|
+
}
|
|
227
|
+
export function createMutationObserver(mutation) {
|
|
228
|
+
if (bindingInfo(mutation).kind !== "mutation")
|
|
229
|
+
throw new TypeError("useMutation requires a mutation");
|
|
230
|
+
const idle = { status: "idle" };
|
|
231
|
+
let state = idle;
|
|
232
|
+
let handle;
|
|
233
|
+
let stop;
|
|
234
|
+
let invocation = 0;
|
|
235
|
+
const listeners = new Set();
|
|
236
|
+
const observe = () => {
|
|
237
|
+
stop?.();
|
|
238
|
+
stop = undefined;
|
|
239
|
+
if (!handle)
|
|
240
|
+
return;
|
|
241
|
+
const id = invocation;
|
|
242
|
+
stop = handle.subscribe(next => {
|
|
243
|
+
if (id !== invocation)
|
|
244
|
+
return;
|
|
245
|
+
state = next;
|
|
246
|
+
emit(listeners);
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
return {
|
|
250
|
+
getSnapshot: () => {
|
|
251
|
+
if (handle && handle.status !== state.status) {
|
|
252
|
+
const status = handle.status;
|
|
253
|
+
state = status === "success" ? { status, data: handle.data }
|
|
254
|
+
: status === "error" ? { status, error: handle.error } : { status };
|
|
255
|
+
}
|
|
256
|
+
return state;
|
|
257
|
+
},
|
|
258
|
+
getServerSnapshot: () => idle,
|
|
259
|
+
subscribe(listener) {
|
|
260
|
+
listeners.add(listener);
|
|
261
|
+
if (listeners.size === 1)
|
|
262
|
+
observe();
|
|
263
|
+
return () => { listeners.delete(listener); if (!listeners.size) {
|
|
264
|
+
stop?.();
|
|
265
|
+
stop = undefined;
|
|
266
|
+
} };
|
|
267
|
+
},
|
|
268
|
+
invoke(...args) {
|
|
269
|
+
invocation++;
|
|
270
|
+
handle = Reflect.apply(mutation, undefined, args);
|
|
271
|
+
state = { status: "pending" };
|
|
272
|
+
if (listeners.size)
|
|
273
|
+
observe();
|
|
274
|
+
return handle;
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type HttpRuntimeOptions } from "./http.ts";
|
|
2
|
+
export interface BrowserOptions extends Pick<HttpRuntimeOptions, "auth" | "headers" | "credentials" | "operationTimeoutMs" | "queryTimeoutMs" | "streamIdleTimeoutMs" | "confirmationTimeoutMs"> {
|
|
3
|
+
readonly url: string;
|
|
4
|
+
}
|
|
5
|
+
/** Generated settings are public data; the server config is never imported here. */
|
|
6
|
+
export declare function createBrowserRuntime(contracts: HttpRuntimeOptions["contracts"], options: Omit<BrowserOptions, "auth" | "headers">, budgets?: Pick<HttpRuntimeOptions, "maxResponseBytes">): {
|
|
7
|
+
runtime: import("./runtime.ts").RuntimeScope;
|
|
8
|
+
setAuth: (state: import("./auth.ts").ClientAuthInput) => void | Promise<void>;
|
|
9
|
+
/** Transport credentials are independent of the cache identity passed to setAuth. */
|
|
10
|
+
setHeaders(value: BrowserOptions["headers"]): void;
|
|
11
|
+
dispose(): Promise<void>;
|
|
12
|
+
};
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { HttpRuntime } from "./http.js";
|
|
2
|
+
import { createRuntimeScope } from "./runtime.js";
|
|
3
|
+
/** Generated settings are public data; the server config is never imported here. */
|
|
4
|
+
export function createBrowserRuntime(contracts, options, budgets) {
|
|
5
|
+
const runtime = createRuntimeScope();
|
|
6
|
+
let adapter;
|
|
7
|
+
let headers;
|
|
8
|
+
const detach = runtime.configure(() => {
|
|
9
|
+
if (!adapter) {
|
|
10
|
+
const url = options.url.startsWith("/") ? new URL(options.url, globalThis.location?.origin).href : options.url;
|
|
11
|
+
adapter = new HttpRuntime({ ...options, ...budgets, baseUrl: url, contracts,
|
|
12
|
+
headers: () => typeof headers === "function" ? headers() : headers ?? {} });
|
|
13
|
+
}
|
|
14
|
+
return adapter;
|
|
15
|
+
});
|
|
16
|
+
let disposal;
|
|
17
|
+
return {
|
|
18
|
+
runtime,
|
|
19
|
+
setAuth: runtime.setAuth.bind(runtime),
|
|
20
|
+
/** Transport credentials are independent of the cache identity passed to setAuth. */
|
|
21
|
+
setHeaders(value) { headers = typeof value === "function" ? value : new Headers(value); },
|
|
22
|
+
dispose() { return disposal ??= (detach(), adapter?.dispose() ?? Promise.resolve()); },
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Page } from "./page.ts";
|
|
2
|
+
import type { FunctionReference, FunctionSignature, Input, MutationHandle, functionSignature, PageHandle, QueryHandle, QueryState, QueryOptions, QuerySubscriptionOptions, ReadonlyResult } from "./contracts.ts";
|
|
3
|
+
import type { RuntimeScope } from "./runtime.ts";
|
|
4
|
+
import type { FunctionCodecs } from "./codec.ts";
|
|
5
|
+
type CallArguments<I, Options> = [I] extends [undefined] ? [options?: Options] : [input: I, options?: Options];
|
|
6
|
+
type SubscribeArguments<I, O> = [I] extends [undefined] ? [listener: (state: QueryState<O>) => void, options?: QuerySubscriptionOptions] : [input: I, listener: (state: QueryState<O>) => void, options?: QuerySubscriptionOptions];
|
|
7
|
+
type ResultOf<D extends FunctionSignature> = NonNullable<D["$types"]>["output"];
|
|
8
|
+
export interface BindingInfo {
|
|
9
|
+
readonly scope: RuntimeScope;
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly kind: "query" | "mutation";
|
|
12
|
+
readonly hasInput: boolean;
|
|
13
|
+
readonly codecs?: FunctionCodecs;
|
|
14
|
+
}
|
|
15
|
+
/** Shared adapter boundary; never executes the callable or imports its handler. */
|
|
16
|
+
export declare function bindingInfo(reference: unknown): BindingInfo;
|
|
17
|
+
type QueryReference = ((...args: never[]) => QueryHandle<unknown>) & FunctionReference;
|
|
18
|
+
type DefinitionOf<Q extends QueryReference> = NonNullable<Q[typeof functionSignature]>;
|
|
19
|
+
type OptimisticSelection<I, QI> = {
|
|
20
|
+
readonly input?: (input: I) => QI;
|
|
21
|
+
readonly match?: (queryInput: QI, input: I) => boolean;
|
|
22
|
+
} & ([QI] extends [undefined] ? unknown : {
|
|
23
|
+
readonly input: (input: I) => QI;
|
|
24
|
+
} | {
|
|
25
|
+
readonly match: (queryInput: QI, input: I) => boolean;
|
|
26
|
+
});
|
|
27
|
+
export type OptimisticTarget<I, Q extends QueryReference> = {
|
|
28
|
+
readonly query: Q;
|
|
29
|
+
readonly update: (current: ReadonlyResult<ResultOf<DefinitionOf<Q>>>, input: I, queryInput: Input<DefinitionOf<Q>>) => ReadonlyResult<ResultOf<DefinitionOf<Q>>>;
|
|
30
|
+
} & OptimisticSelection<I, Input<DefinitionOf<Q>>>;
|
|
31
|
+
export type MutationCallable<D extends FunctionSignature> = ((...args: [Input<D>] extends [undefined] ? [] : [input: Input<D>]) => MutationHandle<ResultOf<D>>) & FunctionReference<D> & {
|
|
32
|
+
withOptimistic<const Q extends readonly [QueryReference, ...QueryReference[]]>(...targets: {
|
|
33
|
+
[K in keyof Q]: OptimisticTarget<Input<D>, Q[K]>;
|
|
34
|
+
}): MutationCallable<D>;
|
|
35
|
+
};
|
|
36
|
+
export type QueryCallable<D extends FunctionSignature> = ((...args: CallArguments<Input<D>, QueryOptions>) => QueryHandle<ResultOf<D>>) & FunctionReference<D> & {
|
|
37
|
+
subscribe(...args: SubscribeArguments<Input<D>, ResultOf<D>>): () => void;
|
|
38
|
+
} & (ResultOf<D> extends Page<infer Row> ? {
|
|
39
|
+
paginate(input: (cursor: string | null) => Input<D>): PageHandle<Row>;
|
|
40
|
+
} : {});
|
|
41
|
+
/** Used by generated modules; input/output types come from a type-only import. */
|
|
42
|
+
export declare function mutationBinding<D extends FunctionSignature & {
|
|
43
|
+
readonly kind: "mutation";
|
|
44
|
+
}>(scope: RuntimeScope, name: string, hasInput: boolean, codecs?: FunctionCodecs): MutationCallable<D>;
|
|
45
|
+
export declare function queryBinding<D extends FunctionSignature & {
|
|
46
|
+
readonly kind: "query";
|
|
47
|
+
}>(scope: RuntimeScope, name: string, hasInput: boolean, codecs?: FunctionCodecs, paginated?: boolean): QueryCallable<D>;
|
|
48
|
+
export {};
|