Repository navigation
Description
What version of Effect is running?
[email protected] (the code is unchanged on main)
What steps can reproduce the bug?
Stores made with the Persistence service's make({ storeId }) are not kept apart on layerKvs and layerRedis. clear on one store deletes other stores' entries, and some store id / key pairs read each other's entries. layerMemory and layerSql keep stores apart, and the SQL suite pins it: clear on one store leaves another store's entry for the same key intact (isolation between stores, packages/sql/sqlite-node/test/Persistence.test.ts:112).
import { Effect, Exit, Layer, Schema } from "effect"
import { KeyValueStore, Persistable, Persistence } from "effect/persistence"
class GetUser extends Persistable.Class<{ payload: { id: string } }>()("GetUser", {
primaryKey: (p) => p.id,
success: Schema.String
}) {}
const program = Effect.gen(function*() {
const persistence = yield* Persistence.Persistence
const users = yield* persistence.make({ storeId: "users" })
const sessions = yield* persistence.make({ storeId: "sessions" })
yield* users.set(new GetUser({ id: "1" }), Exit.succeed("alice"))
yield* sessions.clear
console.log(yield* users.get(new GetUser({ id: "1" })))
const a = yield* persistence.make({ storeId: "a" })
const ab = yield* persistence.make({ storeId: "ab" })
yield* a.set(new GetUser({ id: "bc" }), Exit.succeed("from a/bc"))
console.log(yield* ab.get(new GetUser({ id: "c" })))
})
Effect.runPromise(program.pipe(
Effect.scoped,
Effect.provide(Persistence.layerKvs.pipe(Layer.provide(KeyValueStore.layerMemory)))
))On layerRedis (NodeRedis, Redis 8.10.2), plain store ids are kept apart, but ids containing : are not:
clearon storecachedeletes the entries of storecache:users.- Store
a/ keyb:cand storea:b/ keycare the same Redis key,a:b:c.
To see it, provide Persistence.layerRedis.pipe(Layer.provide(NodeRedis.layer({ url }))) to the snippet instead, and rename store users → cache:users, sessions → cache, ab → a:b, and key bc → b:c. It prints the same two lines.
What is the expected behavior?
The same as with Persistence.layerMemory or layerSql: users still holds "alice" after sessions.clear, and store ab has no entry c.
What do you see instead?
undefined
{ _id: 'Exit', _tag: 'Success', value: 'from a/bc' }
sessions.clear emptied the whole KeyValueStore, including every other store. Store ab / key c and store a / key bc are the same KeyValueStore key, abc.
Additional information
Cause, at [email protected]:
layerBackingKvsmakes each store withKeyValueStore.prefix(backing, storeId)(Persistence.ts:1058).prefixjoins${prefix}${key}with no separator, and its...selfpasses the backing store'sclearthrough unchanged (KeyValueStore.ts:310-319).layerBackingRediswrites${storeId}:${key}(Persistence.ts:891). ItsclearrunsKEYS ${storeId}:*, thenDELon the matches (Persistence.ts:986-987).
#7004 proposed a fix for the KVS half and was closed unmerged; it did not touch the Redis backing. Draft #8735 changes the Redis clear to escape glob characters, but keeps the storeId:key layout on standalone and Sentinel, so these cases remain. KeyValueStore has no way to list keys, so layerBackingKvs cannot scope clear by prefix alone.
Activity
fix(persistence): isolate stores on KeyValueStore and Redis backings