MCPcopy Create free account
hub / github.com/Effect-TS/effect / layerIndexedDb

Function layerIndexedDb

packages/platform/browser/src/BrowserKeyValueStore.ts:65–132  ·  view source on GitHub ↗
(options?: {
  readonly database?: string | undefined
})

Source from the content-addressed store, hash-verified

63 * @since 4.0.0
64 */
65export const layerIndexedDb = (options?: {
66 readonly database?: string | undefined
67}): Layer.Layer<KeyValueStore.KeyValueStore, never, IndexedDb> =>
68 Layer.effect(KeyValueStore.KeyValueStore)(
69 Effect.gen(function*() {
70 const db = yield* Effect.acquireRelease(
71 openDatabase(options?.database ?? "effect_key_value_store"),
72 (db) => Effect.sync(() => db.close())
73 ).pipe(Effect.orDie)
74
75 return KeyValueStore.make({
76 clear: Effect.suspend(() => {
77 return idbWriteRequest(
78 db,
79 { method: "clear", message: "Failed to clear backing store" },
80 (store) => store.clear()
81 )
82 }),
83 get: (key: string) =>
84 Effect.map(
85 Effect.suspend(() => {
86 const store = getKvsEntriesStore(db, "readonly")
87 return idbRequest<{ key: string; value: string } | undefined>({
88 method: "get",
89 message: "Failed to get value from backing store",
90 key
91 }, () => store.get(key))
92 }),
93 (found) => typeof found?.value === "string" ? found.value : undefined
94 ),
95 getUint8Array: (key: string) =>
96 Effect.map(
97 Effect.suspend(() => {
98 const store = getKvsEntriesStore(db, "readonly")
99 return idbRequest<{ key: string; value: Uint8Array } | undefined>({
100 method: "getUint8Array",
101 message: "Failed to get value from backing store",
102 key
103 }, () => store.get(key))
104 }),
105 (found) => found?.value && found.value instanceof Uint8Array ? found.value : undefined
106 ),
107 set: (key: string, value: string | Uint8Array) =>
108 Effect.asVoid(Effect.suspend(() => {
109 return idbWriteRequest(
110 db,
111 { method: "set", message: "Failed to set value in backing store", key },
112 (store) => store.put({ key, value })
113 )
114 })),
115 size: Effect.suspend(() => {
116 const store = getKvsEntriesStore(db, "readonly")
117 return idbRequest<number>(
118 { method: "size", message: "Failed to get backing store size" },
119 () => store.count()
120 )
121 }),
122 remove: (key: string) =>

Callers

nothing calls this directly

Calls 12

openDatabaseFunction · 0.85
idbWriteRequestFunction · 0.85
getKvsEntriesStoreFunction · 0.85
clearMethod · 0.80
countMethod · 0.80
idbRequestFunction · 0.70
pipeMethod · 0.65
closeMethod · 0.65
makeMethod · 0.65
getMethod · 0.65
syncMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected