-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmod.ts
More file actions
80 lines (76 loc) · 2.56 KB
/
mod.ts
File metadata and controls
80 lines (76 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//@ts-check
/// <reference types="@cloudflare/workers-types" />
import {
ExecFn,
RawFn,
studioMiddleware,
StudioOptions,
QueryableObject,
GetSchemaFn,
QueryableHandler,
} from "queryable-object";
import { getMultiStub, MultiStubConfig } from "multistub";
// Re-export useful features
export * from "multistub";
export * from "migratable-object";
export * from "transferable-object";
export * from "queryable-object";
export * from "./util";
export type SqlStorageValue = ArrayBuffer | string | number | null;
export type Records = { [x: string]: SqlStorageValue };
export interface StudioConfig extends StudioOptions {
pathname?: string;
}
export type DORMClient<T extends Rpc.DurableObjectBranded & QueryableHandler> =
{
/** A stub linked to both your main DO and mirror DO for executing any RPC function on both and retrieving the response only from the first */
stub: DurableObjectStub<T>;
/**
* Middleware to expose exec to be browsable using outerbase
*
* NB: although it's async you can safely insert this as the async part only applies in the /query/raw endpoint
*/
studio: (request: Request) => Promise<Response | undefined>;
// Easier to get
exec: ExecFn;
raw: RawFn;
getSchema: GetSchemaFn;
};
/**
* Creates a client for interacting with DORM
* This is now an async function that initializes storage upfront
*/
export function createClient<
T extends Rpc.DurableObjectBranded & QueryableHandler
>(context: {
doNamespace: DurableObjectNamespace<T>;
ctx: ExecutionContext;
configs: MultiStubConfig[];
studioConfig?: StudioConfig;
}): DORMClient<T> {
const { doNamespace, ctx, configs, studioConfig } = context;
if (!configs || configs.length === 0) {
throw new Error("At least one DO configuration is required");
}
const multistub = getMultiStub<T>(doNamespace, configs, ctx);
async function studio(request: Request): Promise<Response | undefined> {
const url = new URL(request.url);
if (url.pathname === (studioConfig.pathname || "/db")) {
return studioMiddleware(request, multistub.raw, {
basicAuth: studioConfig?.basicAuth,
dangerouslyDisableAuth: studioConfig?.dangerouslyDisableAuth,
});
}
return undefined;
}
// @ts-ignore - Type instantiation is excessively deep and possibly infinite.ts(2589)
// Couldn't figure this out - https://x.com/solinvictvs/status/1671507561143476226
const result: DORMClient<T> = {
stub: multistub,
studio,
raw: multistub.raw,
exec: multistub.exec,
getSchema: multistub.getSchema,
};
return result;
}