(options?: {
readonly database?: string | undefined
})
| 63 | * @since 4.0.0 |
| 64 | */ |
| 65 | export 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) => |
nothing calls this directly
no test coverage detected