Effect CommunityEC
Effect Community3y ago
33 replies
hagag

Implementing a KeyValueStore as a Config Provider

How can I implement a KeyValueStore as a config provider?
This is where I'm at so far:
export class ConfigSchema extends S.Class<ConfigSchema>()({
  subdomain: S.string,
  token: S.string,
}) {}

const ConfigStoreLayerSchema = KeyValueStore.layerSchema(ConfigSchema);
const ConfigStore = ConfigStoreLayerSchema.tag;
const ConfigStoreLive = ConfigStoreLayerSchema.layer.pipe(Layer.use(layerLocalStorage));

export interface Config {
  get: () => Effect.Effect<never, PlatformError.PlatformError | ParseResult.ParseError, Option.Option<ConfigSchema>>;
  set: (value: ConfigSchema) => Effect.Effect<never, PlatformError.PlatformError | ParseResult.ParseError, void>;
}

export const Config = Context.Tag<Config>();
export const ConfigLive = Layer.effect(
  Config,
  Effect.map(ConfigStore, (store) => Config.of({ get: () => store.get("config"), set: (v) => store.set("config", v) }))
).pipe(Layer.use(ConfigStoreLive));
Was this page helpful?