terrascale 0.3.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 +202 -0
- package/README.md +143 -0
- package/package.json +159 -0
- package/sdk-current-contract.json +27 -0
- package/sdk-route-manifest.json +67 -0
- package/src/admin.js +9 -0
- package/src/better-auth.js +14 -0
- package/src/config.js +121 -0
- package/src/database-codec.js +845 -0
- package/src/database-types.js +237 -0
- package/src/database-view.js +422 -0
- package/src/database.js +420 -0
- package/src/discovery.js +374 -0
- package/src/http.js +887 -0
- package/src/index.js +79 -0
- package/src/local/authentication.js +47 -0
- package/src/local/better-auth.js +517 -0
- package/src/local/cli.js +51 -0
- package/src/local/context.js +23 -0
- package/src/local/environment.js +109 -0
- package/src/local/index.js +204 -0
- package/src/local/router.js +999 -0
- package/src/local/server.js +664 -0
- package/src/local/store.js +530 -0
- package/src/local/test-environment.js +74 -0
- package/src/management-contracts.js +72 -0
- package/src/management.js +12 -0
- package/src/native-origin.js +75 -0
- package/src/postgres.js +494 -0
- package/src/react/core.js +743 -0
- package/src/react/index.js +99 -0
- package/src/result.js +251 -0
- package/src/schema.js +366 -0
- package/src/sql.js +996 -0
- package/src/svelte/index.js +129 -0
- package/src/tanstack/index.js +511 -0
- package/src/ts-auth-discovery.js +190 -0
- package/src/ts-auth.js +3497 -0
- package/types/admin.d.ts +6 -0
- package/types/better-auth.d.ts +8 -0
- package/types/config.d.ts +58 -0
- package/types/database-codec.d.ts +111 -0
- package/types/database-types.d.ts +213 -0
- package/types/database-view.d.ts +183 -0
- package/types/database.d.ts +98 -0
- package/types/discovery.d.ts +114 -0
- package/types/http.d.ts +46 -0
- package/types/index.d.ts +52 -0
- package/types/local/authentication.d.ts +11 -0
- package/types/local/better-auth.d.ts +33 -0
- package/types/local/cli.d.ts +2 -0
- package/types/local/context.d.ts +14 -0
- package/types/local/environment.d.ts +23 -0
- package/types/local/index.d.ts +94 -0
- package/types/local/router.d.ts +66 -0
- package/types/local/server.d.ts +54 -0
- package/types/local/store.d.ts +106 -0
- package/types/local/test-environment.d.ts +25 -0
- package/types/management-contracts.d.ts +44 -0
- package/types/management.d.ts +6 -0
- package/types/native-origin.d.ts +23 -0
- package/types/postgres.d.ts +123 -0
- package/types/react/core.d.ts +366 -0
- package/types/react/index.d.ts +54 -0
- package/types/result.d.ts +161 -0
- package/types/schema.d.ts +145 -0
- package/types/sql.d.ts +288 -0
- package/types/svelte/index.d.ts +81 -0
- package/types/tanstack/index.d.ts +165 -0
- package/types/ts-auth-discovery.d.ts +11 -0
- package/types/ts-auth.d.ts +1826 -0
package/types/admin.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production Better Auth persistence is required but blocked by security authority
|
|
3
|
+
* and catalog/credential admission. Local development uses the explicit ./local
|
|
4
|
+
* SQLite factory pinned to Better Auth 1.7.2.
|
|
5
|
+
* @returns {never}
|
|
6
|
+
*/
|
|
7
|
+
export function requireTerraScaleBetterAuth(): never;
|
|
8
|
+
export { terraScaleReleaseBlockers, TerraScaleCapabilityUnavailableError } from "./management-contracts.js";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared fetch signature; native database and hosted identity credentials stay distinct.
|
|
3
|
+
* @typedef {(input: RequestInfo | URL, init?: RequestInit) => Promise<Response>} FetchLike
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {string | (() => Promise<string | undefined> | string | undefined)} AccessTokenProvider
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* `requestTimeoutMs`: one deadline covers discovery, credentials, requests, bodies and retry delays.
|
|
10
|
+
* @typedef {{
|
|
11
|
+
* readonly apiBaseUrl: string;
|
|
12
|
+
* readonly accessToken?: AccessTokenProvider;
|
|
13
|
+
* readonly fetch?: FetchLike;
|
|
14
|
+
* readonly maxRetryAttempts?: number | undefined;
|
|
15
|
+
* readonly retryDelayMs?: number | undefined;
|
|
16
|
+
* readonly requestTimeoutMs?: number | undefined;
|
|
17
|
+
* }} TsAuthRuntimeConfig
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {{
|
|
21
|
+
* readonly apiBaseUrl: string;
|
|
22
|
+
* readonly maxRetryAttempts: number;
|
|
23
|
+
* readonly retryDelayMs: number;
|
|
24
|
+
* readonly requestTimeoutMs: number;
|
|
25
|
+
* }} ResolvedTsAuthRuntimeConfig
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* @param {TsAuthRuntimeConfig} config
|
|
29
|
+
* @returns {ResolvedTsAuthRuntimeConfig}
|
|
30
|
+
*/
|
|
31
|
+
export function resolveTsAuthRuntimeConfig(config: TsAuthRuntimeConfig): ResolvedTsAuthRuntimeConfig;
|
|
32
|
+
/**
|
|
33
|
+
* @param {AccessTokenProvider | undefined} provider
|
|
34
|
+
* @returns {Promise<string | undefined>}
|
|
35
|
+
*/
|
|
36
|
+
export function resolveAccessToken(provider: AccessTokenProvider | undefined): Promise<string | undefined>;
|
|
37
|
+
/**
|
|
38
|
+
* Shared fetch signature; native database and hosted identity credentials stay distinct.
|
|
39
|
+
*/
|
|
40
|
+
export type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
41
|
+
export type AccessTokenProvider = string | (() => Promise<string | undefined> | string | undefined);
|
|
42
|
+
/**
|
|
43
|
+
* `requestTimeoutMs`: one deadline covers discovery, credentials, requests, bodies and retry delays.
|
|
44
|
+
*/
|
|
45
|
+
export type TsAuthRuntimeConfig = {
|
|
46
|
+
readonly apiBaseUrl: string;
|
|
47
|
+
readonly accessToken?: AccessTokenProvider;
|
|
48
|
+
readonly fetch?: FetchLike;
|
|
49
|
+
readonly maxRetryAttempts?: number | undefined;
|
|
50
|
+
readonly retryDelayMs?: number | undefined;
|
|
51
|
+
readonly requestTimeoutMs?: number | undefined;
|
|
52
|
+
};
|
|
53
|
+
export type ResolvedTsAuthRuntimeConfig = {
|
|
54
|
+
readonly apiBaseUrl: string;
|
|
55
|
+
readonly maxRetryAttempts: number;
|
|
56
|
+
readonly retryDelayMs: number;
|
|
57
|
+
readonly requestTimeoutMs: number;
|
|
58
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {unknown} value
|
|
3
|
+
* @param {string} [name]
|
|
4
|
+
* @returns {string}
|
|
5
|
+
*/
|
|
6
|
+
export function requireDatabaseUuid(value: unknown, name?: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* @param {unknown} value
|
|
9
|
+
* @param {string} [name]
|
|
10
|
+
* @param {boolean} [positive]
|
|
11
|
+
* @returns {bigint}
|
|
12
|
+
*/
|
|
13
|
+
export function requireDatabaseU64(value: unknown, name?: string, positive?: boolean): bigint;
|
|
14
|
+
/**
|
|
15
|
+
* Typed value encoding preserves numeric field IDs, arrays, bytes, and explicit null.
|
|
16
|
+
* @param {DatabaseValue} value
|
|
17
|
+
* @returns {JsonObject}
|
|
18
|
+
*/
|
|
19
|
+
export function encodeDatabaseValue(value: DatabaseValue): JsonObject;
|
|
20
|
+
/**
|
|
21
|
+
* @param {unknown} value
|
|
22
|
+
* @returns {DatabaseValue}
|
|
23
|
+
*/
|
|
24
|
+
export function decodeDatabaseValue(value: unknown): DatabaseValue;
|
|
25
|
+
/**
|
|
26
|
+
* @param {unknown} raw
|
|
27
|
+
* @returns {DatabasePoint}
|
|
28
|
+
*/
|
|
29
|
+
export function decodeDatabasePoint(raw: unknown): DatabasePoint;
|
|
30
|
+
/**
|
|
31
|
+
* @param {DatabaseQuery} query
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
export function encodeDatabaseQuery(query: DatabaseQuery): string;
|
|
35
|
+
/**
|
|
36
|
+
* @param {unknown} raw
|
|
37
|
+
* @returns {DatabaseQueryPage}
|
|
38
|
+
*/
|
|
39
|
+
export function decodeDatabaseQueryPage(raw: unknown): DatabaseQueryPage;
|
|
40
|
+
/**
|
|
41
|
+
* @param {DatabaseSubscriptionRequest} request
|
|
42
|
+
* @returns {string}
|
|
43
|
+
*/
|
|
44
|
+
export function encodeDatabaseSubscription(request: DatabaseSubscriptionRequest): string;
|
|
45
|
+
/**
|
|
46
|
+
* @param {unknown} raw
|
|
47
|
+
* @returns {DatabaseSubscriptionResponse}
|
|
48
|
+
*/
|
|
49
|
+
export function decodeDatabaseSubscription(raw: unknown): DatabaseSubscriptionResponse;
|
|
50
|
+
/**
|
|
51
|
+
* @param {DatabaseAuthority} authority
|
|
52
|
+
* @returns {JsonObject}
|
|
53
|
+
*/
|
|
54
|
+
export function encodeDatabaseAuthority(authority: DatabaseAuthority): JsonObject;
|
|
55
|
+
/**
|
|
56
|
+
* @param {unknown} raw
|
|
57
|
+
* @returns {DatabaseAuthority}
|
|
58
|
+
*/
|
|
59
|
+
export function decodeDatabaseAuthority(raw: unknown): DatabaseAuthority;
|
|
60
|
+
/**
|
|
61
|
+
* @param {unknown} raw
|
|
62
|
+
* @returns {DatabaseCommitReceipt}
|
|
63
|
+
*/
|
|
64
|
+
export function decodeDatabaseCommitReceipt(raw: unknown): DatabaseCommitReceipt;
|
|
65
|
+
/**
|
|
66
|
+
* @param {DatabaseTransactionIdentity} identity
|
|
67
|
+
* @returns {string}
|
|
68
|
+
*/
|
|
69
|
+
export function encodeDatabaseStatus(identity: DatabaseTransactionIdentity): string;
|
|
70
|
+
/**
|
|
71
|
+
* @param {unknown} raw
|
|
72
|
+
* @returns {DatabaseTransactionStatus}
|
|
73
|
+
*/
|
|
74
|
+
export function decodeDatabaseStatus(raw: unknown): DatabaseTransactionStatus;
|
|
75
|
+
/**
|
|
76
|
+
* Reject duplicate JSON members and unbounded syntax before typed response decoding.
|
|
77
|
+
* @param {Uint8Array} bytes
|
|
78
|
+
* @returns {unknown}
|
|
79
|
+
*/
|
|
80
|
+
export function parseDatabaseJson(bytes: Uint8Array): unknown;
|
|
81
|
+
/**
|
|
82
|
+
* Build and snapshot the exact logical effect before awaiting WebCrypto.
|
|
83
|
+
* @param {DatabaseScope} scope
|
|
84
|
+
* @param {DatabaseCommit} commit
|
|
85
|
+
* @returns {Promise<{ readonly body: string; readonly effect_digest: string }>}
|
|
86
|
+
*/
|
|
87
|
+
export function encodeDatabaseCommit(scope: DatabaseScope, commit: DatabaseCommit): Promise<{
|
|
88
|
+
readonly body: string;
|
|
89
|
+
readonly effect_digest: string;
|
|
90
|
+
}>;
|
|
91
|
+
export type Budget = {
|
|
92
|
+
nodes: number;
|
|
93
|
+
};
|
|
94
|
+
export type Json = null | boolean | number | string | JsonArray | JsonObject;
|
|
95
|
+
export type JsonArray = readonly Json[];
|
|
96
|
+
export type JsonObject = {
|
|
97
|
+
readonly [key: string]: Json;
|
|
98
|
+
};
|
|
99
|
+
export type RecordValue = Record<string, unknown>;
|
|
100
|
+
import type { DatabaseValue } from "./database-types.js";
|
|
101
|
+
import type { DatabasePoint } from "./database-types.js";
|
|
102
|
+
import type { DatabaseQuery } from "./database-types.js";
|
|
103
|
+
import type { DatabaseQueryPage } from "./database-types.js";
|
|
104
|
+
import type { DatabaseSubscriptionRequest } from "./database-types.js";
|
|
105
|
+
import type { DatabaseSubscriptionResponse } from "./database-types.js";
|
|
106
|
+
import type { DatabaseAuthority } from "./database-types.js";
|
|
107
|
+
import type { DatabaseCommitReceipt } from "./database-types.js";
|
|
108
|
+
import type { DatabaseTransactionIdentity } from "./database-types.js";
|
|
109
|
+
import type { DatabaseTransactionStatus } from "./database-types.js";
|
|
110
|
+
import type { DatabaseScope } from "./database-types.js";
|
|
111
|
+
import type { DatabaseCommit } from "./database-types.js";
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The current TerraBase document protocol uses canonical UUID entity IDs.
|
|
3
|
+
*/
|
|
4
|
+
export type CanonicalUuid = string;
|
|
5
|
+
/**
|
|
6
|
+
* All 64-bit quantities stay bigint in JavaScript and decimal strings on the wire.
|
|
7
|
+
*/
|
|
8
|
+
export type DatabaseScope = {
|
|
9
|
+
readonly tenant: CanonicalUuid;
|
|
10
|
+
readonly database: CanonicalUuid;
|
|
11
|
+
readonly shard: bigint;
|
|
12
|
+
};
|
|
13
|
+
export type CollectionBinding = {
|
|
14
|
+
readonly collection: CanonicalUuid;
|
|
15
|
+
readonly schema_generation: bigint;
|
|
16
|
+
readonly schema_digest: string;
|
|
17
|
+
};
|
|
18
|
+
export type DatabaseValue = ({
|
|
19
|
+
readonly type: "null";
|
|
20
|
+
} | {
|
|
21
|
+
readonly type: "bool";
|
|
22
|
+
readonly value: boolean;
|
|
23
|
+
} | {
|
|
24
|
+
readonly type: "i64";
|
|
25
|
+
readonly value: bigint;
|
|
26
|
+
} | {
|
|
27
|
+
readonly type: "utf8";
|
|
28
|
+
readonly value: string;
|
|
29
|
+
} | {
|
|
30
|
+
readonly type: "bytes";
|
|
31
|
+
readonly value: Uint8Array;
|
|
32
|
+
} | {
|
|
33
|
+
readonly type: "map";
|
|
34
|
+
readonly fields: readonly DatabaseField[];
|
|
35
|
+
} | {
|
|
36
|
+
readonly type: "array";
|
|
37
|
+
readonly items: readonly DatabaseValue[];
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* Missing fields are omitted; an explicit null remains a typed null value.
|
|
41
|
+
*/
|
|
42
|
+
export type DatabaseField = {
|
|
43
|
+
readonly id: number;
|
|
44
|
+
readonly value: DatabaseValue;
|
|
45
|
+
};
|
|
46
|
+
export type DatabaseObserved = {
|
|
47
|
+
readonly replica: CanonicalUuid;
|
|
48
|
+
readonly incarnation: string;
|
|
49
|
+
readonly sequence: bigint;
|
|
50
|
+
};
|
|
51
|
+
export type DatabaseEvent = DatabaseObserved & {
|
|
52
|
+
readonly database: CanonicalUuid;
|
|
53
|
+
readonly shard: bigint;
|
|
54
|
+
};
|
|
55
|
+
export type DatabaseAuthority = ({
|
|
56
|
+
readonly mode: "leaderless";
|
|
57
|
+
} | {
|
|
58
|
+
readonly mode: "single_writer";
|
|
59
|
+
readonly generation: bigint;
|
|
60
|
+
readonly writer: {
|
|
61
|
+
readonly replica: CanonicalUuid;
|
|
62
|
+
readonly incarnation: string;
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
export type DatabaseCondition = ({
|
|
66
|
+
readonly kind: "none";
|
|
67
|
+
} | {
|
|
68
|
+
readonly kind: "absent";
|
|
69
|
+
} | {
|
|
70
|
+
readonly kind: "version";
|
|
71
|
+
readonly version: string;
|
|
72
|
+
});
|
|
73
|
+
export type DatabaseWriteValue = ({
|
|
74
|
+
readonly kind: "live";
|
|
75
|
+
readonly fields: readonly DatabaseField[];
|
|
76
|
+
} | {
|
|
77
|
+
readonly kind: "tombstone";
|
|
78
|
+
});
|
|
79
|
+
export type DatabaseWrite = CollectionBinding & {
|
|
80
|
+
readonly document: CanonicalUuid;
|
|
81
|
+
readonly condition: DatabaseCondition;
|
|
82
|
+
readonly observed: readonly DatabaseObserved[];
|
|
83
|
+
readonly value: DatabaseWriteValue;
|
|
84
|
+
};
|
|
85
|
+
export type DatabaseTransactionId = {
|
|
86
|
+
readonly expires_at_unix_ms: bigint;
|
|
87
|
+
readonly nonce: string;
|
|
88
|
+
};
|
|
89
|
+
export type DatabaseCommit = {
|
|
90
|
+
readonly transaction_id: DatabaseTransactionId;
|
|
91
|
+
readonly authority: DatabaseAuthority;
|
|
92
|
+
readonly writes: readonly DatabaseWrite[];
|
|
93
|
+
};
|
|
94
|
+
export type DatabaseSibling = Omit<CollectionBinding, "collection"> & {
|
|
95
|
+
readonly event: DatabaseEvent;
|
|
96
|
+
} & DatabaseWriteValue;
|
|
97
|
+
/**
|
|
98
|
+
* `state_version`: only a unique live or deleted state carries an atomic condition token.
|
|
99
|
+
*/
|
|
100
|
+
export type DatabasePoint = {
|
|
101
|
+
readonly version: 1;
|
|
102
|
+
readonly state: "absent" | "deleted" | "value" | "conflict";
|
|
103
|
+
readonly state_version: string | null;
|
|
104
|
+
readonly context: readonly DatabaseObserved[];
|
|
105
|
+
readonly siblings: readonly DatabaseSibling[];
|
|
106
|
+
};
|
|
107
|
+
export type DatabaseQueryValue = Extract<DatabaseValue, {
|
|
108
|
+
readonly type: "bool" | "i64" | "utf8" | "bytes";
|
|
109
|
+
}>;
|
|
110
|
+
export type DatabaseQueryPredicate = ({
|
|
111
|
+
readonly op: "equal" | "less_than" | "less_than_or_equal" | "greater_than" | "greater_than_or_equal";
|
|
112
|
+
readonly path: readonly number[];
|
|
113
|
+
readonly value: DatabaseQueryValue;
|
|
114
|
+
} | {
|
|
115
|
+
readonly op: "is_null" | "is_missing";
|
|
116
|
+
readonly path: readonly number[];
|
|
117
|
+
});
|
|
118
|
+
export type DatabaseQuery = {
|
|
119
|
+
readonly predicate: DatabaseQueryPredicate;
|
|
120
|
+
readonly limit: number;
|
|
121
|
+
readonly cursor?: string;
|
|
122
|
+
};
|
|
123
|
+
export type DatabaseQueryRow = {
|
|
124
|
+
readonly document: CanonicalUuid;
|
|
125
|
+
readonly point: DatabasePoint;
|
|
126
|
+
};
|
|
127
|
+
export type DatabaseQueryPage = {
|
|
128
|
+
readonly version: 1;
|
|
129
|
+
readonly rows: readonly DatabaseQueryRow[];
|
|
130
|
+
readonly next_cursor?: string;
|
|
131
|
+
};
|
|
132
|
+
export type DatabaseCommitReceipt = {
|
|
133
|
+
readonly version: 1;
|
|
134
|
+
readonly durability: "local_durable" | "object_durable";
|
|
135
|
+
readonly transaction_id: DatabaseTransactionId;
|
|
136
|
+
readonly event: DatabaseEvent;
|
|
137
|
+
readonly commit_sequence: bigint;
|
|
138
|
+
readonly effect_digest: string;
|
|
139
|
+
readonly authority: DatabaseAuthority;
|
|
140
|
+
};
|
|
141
|
+
export type DatabaseTransactionIdentity = {
|
|
142
|
+
readonly transaction_id: DatabaseTransactionId;
|
|
143
|
+
readonly authority: DatabaseAuthority;
|
|
144
|
+
readonly effect_digest: string;
|
|
145
|
+
};
|
|
146
|
+
export type DatabaseTransactionStatus = DatabaseTransactionIdentity & {
|
|
147
|
+
readonly version: 1;
|
|
148
|
+
} & ({
|
|
149
|
+
readonly state: "committed";
|
|
150
|
+
readonly outcome: {
|
|
151
|
+
readonly receipt: DatabaseCommitReceipt;
|
|
152
|
+
};
|
|
153
|
+
} | {
|
|
154
|
+
readonly state: "rejected";
|
|
155
|
+
readonly outcome: {
|
|
156
|
+
readonly error: {
|
|
157
|
+
readonly code: "precondition_failed";
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
} | {
|
|
161
|
+
readonly state: "pending" | "unknown" | "expired";
|
|
162
|
+
readonly outcome: null;
|
|
163
|
+
});
|
|
164
|
+
/**
|
|
165
|
+
* An uncertain request needs status resolution; it is never an aborted transaction.
|
|
166
|
+
*/
|
|
167
|
+
export type DatabaseResult<T> = ({
|
|
168
|
+
readonly ok: true;
|
|
169
|
+
readonly status: number;
|
|
170
|
+
readonly value: T;
|
|
171
|
+
} | {
|
|
172
|
+
readonly ok: false;
|
|
173
|
+
readonly error: {
|
|
174
|
+
readonly code: string;
|
|
175
|
+
readonly status?: number;
|
|
176
|
+
readonly mutation_outcome?: "rejected" | "uncertain" | "unknown";
|
|
177
|
+
readonly transaction?: DatabaseTransactionIdentity;
|
|
178
|
+
};
|
|
179
|
+
});
|
|
180
|
+
export type DatabaseSubscriptionRequest = ({
|
|
181
|
+
readonly phase: "snapshot";
|
|
182
|
+
readonly predicate: DatabaseQueryPredicate;
|
|
183
|
+
readonly limit: number;
|
|
184
|
+
readonly snapshot_cursor?: string;
|
|
185
|
+
} | {
|
|
186
|
+
readonly phase: "poll";
|
|
187
|
+
readonly predicate: DatabaseQueryPredicate;
|
|
188
|
+
readonly poll_cursor: string;
|
|
189
|
+
});
|
|
190
|
+
export type DatabaseSubscriptionEffect = {
|
|
191
|
+
readonly document: CanonicalUuid;
|
|
192
|
+
readonly point: DatabasePoint | null;
|
|
193
|
+
};
|
|
194
|
+
export type DatabaseSubscriptionGroup = {
|
|
195
|
+
readonly sequence: bigint;
|
|
196
|
+
readonly effects: readonly DatabaseSubscriptionEffect[];
|
|
197
|
+
};
|
|
198
|
+
export type DatabaseSubscriptionResponse = ({
|
|
199
|
+
readonly version: 1;
|
|
200
|
+
readonly phase: "snapshot";
|
|
201
|
+
readonly rows: readonly DatabaseQueryRow[];
|
|
202
|
+
readonly next_snapshot_cursor: string;
|
|
203
|
+
} | {
|
|
204
|
+
readonly version: 1;
|
|
205
|
+
readonly phase: "snapshot";
|
|
206
|
+
readonly rows: readonly DatabaseQueryRow[];
|
|
207
|
+
readonly poll_cursor: string;
|
|
208
|
+
} | {
|
|
209
|
+
readonly version: 1;
|
|
210
|
+
readonly phase: "poll";
|
|
211
|
+
readonly groups: readonly DatabaseSubscriptionGroup[];
|
|
212
|
+
readonly poll_cursor: string;
|
|
213
|
+
});
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native snapshot/poll source shared by React, Svelte and TanStack.
|
|
3
|
+
* @param {TerraBaseClient} client
|
|
4
|
+
* @param {DatabaseQueryOptions} options
|
|
5
|
+
* @returns {DatabaseQuerySource}
|
|
6
|
+
*/
|
|
7
|
+
export function createDatabaseQuery(client: TerraBaseClient, options: DatabaseQueryOptions): DatabaseQuerySource;
|
|
8
|
+
/**
|
|
9
|
+
* @import { TerraBaseClient } from "./database.js"
|
|
10
|
+
* @import {
|
|
11
|
+
* DatabaseField, DatabasePoint, DatabaseQueryPredicate, DatabaseQueryRow,
|
|
12
|
+
* DatabaseResult, DatabaseScope, DatabaseSubscriptionRequest,
|
|
13
|
+
* DatabaseSubscriptionResponse, DatabaseValue,
|
|
14
|
+
* } from "./database-types.js"
|
|
15
|
+
* @import { ReactiveReset, ReactiveSubscriptionContext } from "./react/core.js"
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* - `limit`: snapshot page size, between 1 and 128.
|
|
19
|
+
* - `maximumRows`: client memory bound, including all snapshot pages and later inserts.
|
|
20
|
+
* - `retryLimit`: bounded retries of read transport failures with the identical cursor.
|
|
21
|
+
* @typedef {{
|
|
22
|
+
* readonly collection: string;
|
|
23
|
+
* readonly predicate: DatabaseQueryPredicate;
|
|
24
|
+
* readonly limit?: number;
|
|
25
|
+
* readonly maximumRows?: number;
|
|
26
|
+
* readonly pollIntervalMs?: number;
|
|
27
|
+
* readonly retryLimit?: number;
|
|
28
|
+
* readonly retryDelayMs?: number;
|
|
29
|
+
* }} DatabaseQueryOptions
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {{
|
|
33
|
+
* readonly scope: DatabaseScope;
|
|
34
|
+
* readonly collection: string;
|
|
35
|
+
* readonly predicate: DatabaseQueryPredicate;
|
|
36
|
+
* readonly limit: number;
|
|
37
|
+
* }} DatabaseViewBinding
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* Local generation and server position of one complete authorized cut.
|
|
41
|
+
* @typedef {{
|
|
42
|
+
* readonly generation: number;
|
|
43
|
+
* readonly poll_cursor: string;
|
|
44
|
+
* readonly sequence?: bigint;
|
|
45
|
+
* }} DatabaseViewPosition
|
|
46
|
+
*/
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {{
|
|
49
|
+
* readonly rows: readonly DatabaseQueryRow[];
|
|
50
|
+
* readonly position: DatabaseViewPosition;
|
|
51
|
+
* }} DatabaseViewSnapshot
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* `next`: an async consumer can acknowledge publication before the next poll.
|
|
55
|
+
* @typedef {{
|
|
56
|
+
* readonly next: (rows: readonly DatabaseQueryRow[], position: DatabaseViewPosition) => void | Promise<void>;
|
|
57
|
+
* readonly error: (error: unknown) => void;
|
|
58
|
+
* readonly reset: (reset: ReactiveReset<readonly DatabaseQueryRow[]>) => void;
|
|
59
|
+
* }} DatabaseQueryCallbacks
|
|
60
|
+
*/
|
|
61
|
+
/**
|
|
62
|
+
* @typedef {{
|
|
63
|
+
* readonly key?: string | readonly unknown[];
|
|
64
|
+
* readonly binding: DatabaseViewBinding;
|
|
65
|
+
* readonly subscribe: (
|
|
66
|
+
* observer: DatabaseQueryCallbacks,
|
|
67
|
+
* context: ReactiveSubscriptionContext,
|
|
68
|
+
* ) => () => void;
|
|
69
|
+
* }} DatabaseQuerySource
|
|
70
|
+
*/
|
|
71
|
+
export class DatabaseSubscriptionError extends Error {
|
|
72
|
+
/**
|
|
73
|
+
* @param {string} code
|
|
74
|
+
* @param {Extract<DatabaseResult<never>, { readonly ok: false }>["error"]} [detail]
|
|
75
|
+
*/
|
|
76
|
+
constructor(code: string, detail?: Extract<DatabaseResult<never>, {
|
|
77
|
+
readonly ok: false;
|
|
78
|
+
}>["error"]);
|
|
79
|
+
/**
|
|
80
|
+
* @override
|
|
81
|
+
* @readonly
|
|
82
|
+
* @type {"DatabaseSubscriptionError"}
|
|
83
|
+
*/
|
|
84
|
+
override readonly name: "DatabaseSubscriptionError";
|
|
85
|
+
/**
|
|
86
|
+
* @readonly
|
|
87
|
+
* @type {string}
|
|
88
|
+
*/
|
|
89
|
+
readonly code: string;
|
|
90
|
+
/**
|
|
91
|
+
* @readonly
|
|
92
|
+
* @type {Extract<DatabaseResult<never>, { readonly ok: false }>["error"] | undefined}
|
|
93
|
+
*/
|
|
94
|
+
readonly detail: Extract<DatabaseResult<never>, {
|
|
95
|
+
readonly ok: false;
|
|
96
|
+
}>["error"] | undefined;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Pure native result reducer. A paged snapshot stays private until its
|
|
100
|
+
* terminal poll cursor. Poll groups are staged in order and published as one
|
|
101
|
+
* complete response; invalid input cannot partially replace the prior cut.
|
|
102
|
+
*/
|
|
103
|
+
export class DatabaseSubscriptionView {
|
|
104
|
+
/**
|
|
105
|
+
* @param {DatabaseScope} scope
|
|
106
|
+
* @param {Pick<DatabaseQueryOptions, "collection" | "predicate" | "limit" | "maximumRows">} options
|
|
107
|
+
*/
|
|
108
|
+
constructor(scope: DatabaseScope, options: Pick<DatabaseQueryOptions, "collection" | "predicate" | "limit" | "maximumRows">);
|
|
109
|
+
/**
|
|
110
|
+
* @readonly
|
|
111
|
+
* @type {DatabaseViewBinding}
|
|
112
|
+
*/
|
|
113
|
+
readonly binding: DatabaseViewBinding;
|
|
114
|
+
/** @returns {DatabaseViewSnapshot | undefined} */
|
|
115
|
+
getSnapshot(): DatabaseViewSnapshot | undefined;
|
|
116
|
+
/** @returns {DatabaseSubscriptionRequest} */
|
|
117
|
+
getRequest(): DatabaseSubscriptionRequest;
|
|
118
|
+
/**
|
|
119
|
+
* Discard every opaque position; the next request starts a fresh cut.
|
|
120
|
+
* @returns {void}
|
|
121
|
+
*/
|
|
122
|
+
restart(): void;
|
|
123
|
+
/**
|
|
124
|
+
* @param {DatabaseSubscriptionResponse} response
|
|
125
|
+
* @returns {DatabaseViewSnapshot | undefined}
|
|
126
|
+
*/
|
|
127
|
+
apply(response: DatabaseSubscriptionResponse): DatabaseViewSnapshot | undefined;
|
|
128
|
+
#private;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* - `limit`: snapshot page size, between 1 and 128.
|
|
132
|
+
* - `maximumRows`: client memory bound, including all snapshot pages and later inserts.
|
|
133
|
+
* - `retryLimit`: bounded retries of read transport failures with the identical cursor.
|
|
134
|
+
*/
|
|
135
|
+
export type DatabaseQueryOptions = {
|
|
136
|
+
readonly collection: string;
|
|
137
|
+
readonly predicate: DatabaseQueryPredicate;
|
|
138
|
+
readonly limit?: number;
|
|
139
|
+
readonly maximumRows?: number;
|
|
140
|
+
readonly pollIntervalMs?: number;
|
|
141
|
+
readonly retryLimit?: number;
|
|
142
|
+
readonly retryDelayMs?: number;
|
|
143
|
+
};
|
|
144
|
+
export type DatabaseViewBinding = {
|
|
145
|
+
readonly scope: DatabaseScope;
|
|
146
|
+
readonly collection: string;
|
|
147
|
+
readonly predicate: DatabaseQueryPredicate;
|
|
148
|
+
readonly limit: number;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Local generation and server position of one complete authorized cut.
|
|
152
|
+
*/
|
|
153
|
+
export type DatabaseViewPosition = {
|
|
154
|
+
readonly generation: number;
|
|
155
|
+
readonly poll_cursor: string;
|
|
156
|
+
readonly sequence?: bigint;
|
|
157
|
+
};
|
|
158
|
+
export type DatabaseViewSnapshot = {
|
|
159
|
+
readonly rows: readonly DatabaseQueryRow[];
|
|
160
|
+
readonly position: DatabaseViewPosition;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* `next`: an async consumer can acknowledge publication before the next poll.
|
|
164
|
+
*/
|
|
165
|
+
export type DatabaseQueryCallbacks = {
|
|
166
|
+
readonly next: (rows: readonly DatabaseQueryRow[], position: DatabaseViewPosition) => void | Promise<void>;
|
|
167
|
+
readonly error: (error: unknown) => void;
|
|
168
|
+
readonly reset: (reset: ReactiveReset<readonly DatabaseQueryRow[]>) => void;
|
|
169
|
+
};
|
|
170
|
+
export type DatabaseQuerySource = {
|
|
171
|
+
readonly key?: string | readonly unknown[];
|
|
172
|
+
readonly binding: DatabaseViewBinding;
|
|
173
|
+
readonly subscribe: (observer: DatabaseQueryCallbacks, context: ReactiveSubscriptionContext) => () => void;
|
|
174
|
+
};
|
|
175
|
+
import type { TerraBaseClient } from "./database.js";
|
|
176
|
+
import type { DatabaseResult } from "./database-types.js";
|
|
177
|
+
import type { DatabaseSubscriptionRequest } from "./database-types.js";
|
|
178
|
+
import type { DatabaseSubscriptionResponse } from "./database-types.js";
|
|
179
|
+
import type { DatabaseScope } from "./database-types.js";
|
|
180
|
+
import type { DatabaseQueryPredicate } from "./database-types.js";
|
|
181
|
+
import type { DatabaseQueryRow } from "./database-types.js";
|
|
182
|
+
import type { ReactiveReset } from "./react/core.js";
|
|
183
|
+
import type { ReactiveSubscriptionContext } from "./react/core.js";
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {TerraBaseClientConfig} config
|
|
3
|
+
* @returns {TerraBaseClient}
|
|
4
|
+
*/
|
|
5
|
+
export function createTerraBaseClient(config: TerraBaseClientConfig): TerraBaseClient;
|
|
6
|
+
/** Current scoped document client. All mutations are server commits in one declared shard. */
|
|
7
|
+
export class TerraBaseClient {
|
|
8
|
+
/** @param {TerraBaseClientConfig} config */
|
|
9
|
+
constructor(config: TerraBaseClientConfig);
|
|
10
|
+
/** @returns {DatabaseScope} */
|
|
11
|
+
get scope(): DatabaseScope;
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} collection
|
|
14
|
+
* @param {string} document
|
|
15
|
+
* @param {DatabaseRequestOptions} [options]
|
|
16
|
+
* @returns {Promise<DatabaseResult<DatabasePoint>>}
|
|
17
|
+
*/
|
|
18
|
+
read(collection: string, document: string, options?: DatabaseRequestOptions): Promise<DatabaseResult<DatabasePoint>>;
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} collection
|
|
21
|
+
* @param {DatabaseQuery} query
|
|
22
|
+
* @param {DatabaseRequestOptions} [options]
|
|
23
|
+
* @returns {Promise<DatabaseResult<DatabaseQueryPage>>}
|
|
24
|
+
*/
|
|
25
|
+
query(collection: string, query: DatabaseQuery, options?: DatabaseRequestOptions): Promise<DatabaseResult<DatabaseQueryPage>>;
|
|
26
|
+
/**
|
|
27
|
+
* @param {string} collection
|
|
28
|
+
* @param {DatabaseSubscriptionRequest} request
|
|
29
|
+
* @param {DatabaseRequestOptions} [options]
|
|
30
|
+
* @returns {Promise<DatabaseResult<DatabaseSubscriptionResponse>>}
|
|
31
|
+
*/
|
|
32
|
+
subscription(collection: string, request: DatabaseSubscriptionRequest, options?: DatabaseRequestOptions): Promise<DatabaseResult<DatabaseSubscriptionResponse>>;
|
|
33
|
+
/**
|
|
34
|
+
* Prepare once and retain this identity for exact retry and durable status resolution.
|
|
35
|
+
* @param {DatabaseCommit} commit
|
|
36
|
+
* @returns {Promise<PreparedDatabaseCommit>}
|
|
37
|
+
*/
|
|
38
|
+
prepareCommit(commit: DatabaseCommit): Promise<PreparedDatabaseCommit>;
|
|
39
|
+
/**
|
|
40
|
+
* @param {PreparedDatabaseCommit} prepared
|
|
41
|
+
* @param {DatabaseRequestOptions} [options]
|
|
42
|
+
* @returns {Promise<DatabaseResult<DatabaseCommitReceipt>>}
|
|
43
|
+
*/
|
|
44
|
+
commit(prepared: PreparedDatabaseCommit, options?: DatabaseRequestOptions): Promise<DatabaseResult<DatabaseCommitReceipt>>;
|
|
45
|
+
/**
|
|
46
|
+
* Unknown, pending and expired outcomes remain unresolved; an absent document proves no abort.
|
|
47
|
+
* @param {DatabaseTransactionIdentity} identity
|
|
48
|
+
* @param {DatabaseRequestOptions} [options]
|
|
49
|
+
* @returns {Promise<DatabaseResult<DatabaseTransactionStatus>>}
|
|
50
|
+
*/
|
|
51
|
+
transactionStatus(identity: DatabaseTransactionIdentity, options?: DatabaseRequestOptions): Promise<DatabaseResult<DatabaseTransactionStatus>>;
|
|
52
|
+
#private;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* - `apiKey`: native service key used for authenticated Atlas discovery and the selected regional listener.
|
|
56
|
+
* - `endpoint`: a provisioned direct origin. Omit to use authenticated native Atlas discovery.
|
|
57
|
+
* - `discoveryTimeoutMs`: bounds the canonical Atlas request within the complete operation deadline.
|
|
58
|
+
*/
|
|
59
|
+
export type TerraBaseClientConfig = DatabaseScope & {
|
|
60
|
+
readonly apiKey: string | (() => string | Promise<string>);
|
|
61
|
+
readonly endpoint?: string;
|
|
62
|
+
readonly discoveryTimeoutMs?: number;
|
|
63
|
+
readonly requestTimeoutMs?: number;
|
|
64
|
+
readonly fetch?: FetchLike;
|
|
65
|
+
};
|
|
66
|
+
export type DatabaseRequestOptions = {
|
|
67
|
+
readonly signal?: AbortSignal;
|
|
68
|
+
};
|
|
69
|
+
export type PreparedDatabaseCommit = DatabaseTransactionIdentity & {
|
|
70
|
+
readonly [preparedBrand]: true;
|
|
71
|
+
};
|
|
72
|
+
import type { DatabaseScope } from "./database-types.js";
|
|
73
|
+
import type { DatabasePoint } from "./database-types.js";
|
|
74
|
+
import type { DatabaseResult } from "./database-types.js";
|
|
75
|
+
import type { DatabaseQuery } from "./database-types.js";
|
|
76
|
+
import type { DatabaseQueryPage } from "./database-types.js";
|
|
77
|
+
import type { DatabaseSubscriptionRequest } from "./database-types.js";
|
|
78
|
+
import type { DatabaseSubscriptionResponse } from "./database-types.js";
|
|
79
|
+
import type { DatabaseCommit } from "./database-types.js";
|
|
80
|
+
import type { DatabaseCommitReceipt } from "./database-types.js";
|
|
81
|
+
import type { DatabaseTransactionIdentity } from "./database-types.js";
|
|
82
|
+
import type { DatabaseTransactionStatus } from "./database-types.js";
|
|
83
|
+
import type { FetchLike } from "./config.js";
|
|
84
|
+
/**
|
|
85
|
+
* - `apiKey`: native service key used for authenticated Atlas discovery and the selected regional listener.
|
|
86
|
+
* - `endpoint`: a provisioned direct origin. Omit to use authenticated native Atlas discovery.
|
|
87
|
+
* - `discoveryTimeoutMs`: bounds the canonical Atlas request within the complete operation deadline.
|
|
88
|
+
* @typedef {DatabaseScope & {
|
|
89
|
+
* readonly apiKey: string | (() => string | Promise<string>);
|
|
90
|
+
* readonly endpoint?: string;
|
|
91
|
+
* readonly discoveryTimeoutMs?: number;
|
|
92
|
+
* readonly requestTimeoutMs?: number;
|
|
93
|
+
* readonly fetch?: FetchLike;
|
|
94
|
+
* }} TerraBaseClientConfig
|
|
95
|
+
*/
|
|
96
|
+
/** @typedef {{ readonly signal?: AbortSignal }} DatabaseRequestOptions */
|
|
97
|
+
declare const preparedBrand: unique symbol;
|
|
98
|
+
export {};
|