-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconnection-from-store.ts
More file actions
83 lines (75 loc) · 2.39 KB
/
connection-from-store.ts
File metadata and controls
83 lines (75 loc) · 2.39 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
81
82
83
import { BuildURI, CoerceURI, runtimeFn, URI } from "@adviser/cement";
import { bs, Database, ensureLogger, SuperThis } from "@fireproof/core";
// export interface StoreOptions {
// readonly data: bs.DataStore;
// readonly meta: bs.MetaStore;
// readonly wal: bs.WALState;
// }
export class ConnectionFromStore extends bs.ConnectionBase {
stores?: {
readonly data: bs.DataStore;
readonly meta: bs.MetaStore;
} = undefined;
// readonly urlData: URI;
// readonly urlMeta: URI;
readonly sthis: SuperThis;
constructor(sthis: SuperThis, url: URI) {
const logger = ensureLogger(sthis, "ConnectionFromStore", {
url: () => url.toString(),
this: 1,
log: 1,
});
super(url, logger);
this.sthis = sthis;
// this.urlData = url;
// this.urlMeta = url;
}
async onConnect(): Promise<void> {
this.logger.Debug().Msg("onConnect-start");
const stores = {
base: this.url,
// data: this.urlData,
// meta: this.urlMeta,
};
const rName = this.url.getParamResult("name");
if (rName.isErr()) {
throw this.logger.Error().Err(rName).Msg("missing Parameter").AsError();
}
const storeRuntime = bs.toStoreRuntime({ stores }, this.sthis);
const loader = {
name: rName.Ok(),
ebOpts: {
logger: this.logger,
store: { stores },
storeRuntime,
},
sthis: this.sthis,
} as bs.Loadable;
this.stores = {
data: await storeRuntime.makeDataStore(loader),
meta: await storeRuntime.makeMetaStore(loader),
};
// await this.stores.data.start();
// await this.stores.meta.start();
this.logger.Debug().Msg("onConnect-done");
return;
}
}
export function connectionFactory(sthis: SuperThis, iurl: CoerceURI): bs.ConnectionBase {
return new ConnectionFromStore(sthis, URI.from(iurl));
}
export function makeKeyBagUrlExtractable(sthis: SuperThis) {
let base = sthis.env.get("FP_KEYBAG_URL");
if (!base) {
if (runtimeFn().isBrowser) {
base = "indexdb://fp-keybag";
} else {
base = "file://./dist/kb-dir-partykit";
}
}
const kbUrl = BuildURI.from(base);
kbUrl.defParam("extractKey", "_deprecated_internal_api");
sthis.env.set("FP_KEYBAG_URL", kbUrl.toString());
sthis.logger.Debug().Url(kbUrl, "keyBagUrl").Msg("Make keybag url extractable");
}
export type ConnectFunction = (db: Database, name?: string, url?: string) => bs.Connection;