@owlmeans/redis 0.1.18-rc.2 → 0.1.18-rc.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,7 +12,7 @@ Redis service for OwlMeans server contexts — connection management with cluste
12
12
  ## Installation
13
13
 
14
14
  ```bash
15
- bun add @owlmeans/redis
15
+ bun add @owlmeans/redis@^0.1.18-rc.11
16
16
  ```
17
17
 
18
18
  ## Usage
@@ -24,16 +24,20 @@ import { appendRedis, DEFAULT_ALIAS } from '@owlmeans/redis'
24
24
  appendRedis<C, T>(context)
25
25
  ```
26
26
 
27
- Config (`config.json`):
27
+ Config (`config.json`) — `dbs` is a list, and `schema` becomes the key prefix every resource on
28
+ this connection namespaces itself under:
28
29
 
29
30
  ```json
30
31
  {
31
- "dbs": {
32
- "redis": {
32
+ "dbs": [
33
+ {
34
+ "service": "redis",
35
+ "alias": "redis",
33
36
  "host": "localhost",
34
- "port": 6379
37
+ "port": 6379,
38
+ "schema": "app"
35
39
  }
36
- }
40
+ ]
37
41
  }
38
42
  ```
39
43
 
@@ -49,7 +53,21 @@ Registers the Redis service in the context.
49
53
 
50
54
  ### `RedisMeta`
51
55
 
52
- Extends `RedisOptions` (ioredis) — all ioredis connection options are supported in config.
56
+ Extends `RedisOptions` (ioredis) — all ioredis connection options are supported in config. Set
57
+ the database index with `dbIndex` (it accepts a string, so it can come from a file-mounted config
58
+ value) rather than ioredis' own `db`.
59
+
60
+ ## Server Requirements
61
+
62
+ - Redis **6.2 or newer** — `@owlmeans/redis-resource` deletes with `GETDEL`.
63
+ - `notify-keyspace-events` enabled if any resource uses `watch`; keyspace events are emitted on
64
+ **db 0**, so deployments sharing an instance isolate on the key prefix, not on `dbIndex`.
65
+
66
+ ## Tests
67
+
68
+ `tests/` holds the integration specs for the `@owlmeans/redis-resource` contract — this is the
69
+ package that supplies the connection it runs against. Gated on `REDIS_URL` (see `/.env.example`):
70
+ `bun test ./tests`.
53
71
 
54
72
  ## Related Packages
55
73
 
@@ -64,7 +82,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
64
82
  your project's skill store (`.agents/skills/`):
65
83
 
66
84
  ```sh
67
- npx @owlmeans/agent-skills
85
+ npx @owlmeans/agent-skills@^0.1.18-rc.20
68
86
  ```
69
87
 
70
88
  The embedded files are version-matched to this package release. Do not edit them
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 2,
3
3
  "package": "@owlmeans/redis",
4
- "version": "0.1.18-rc.0",
5
- "generatedAt": "2026-08-16T22:20:50.516Z",
4
+ "version": "0.1.18-rc.20",
5
+ "generatedAt": "2026-09-12T14:21:25.470Z",
6
6
  "canonicalRepo": "https://github.com/owlmeans/common",
7
7
  "entries": [
8
8
  {
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: redis
3
- description: How to use @owlmeans/redis — Redis client service factory (makeRedisService) registered on a server context. Auto-invoked when wiring Redis into a server app.
3
+ description: How to use @owlmeans/redis — the Redis connection service (makeRedisService / appendRedis) registered on a server context, its cfg.dbs configuration, the options() seam for consumers that need a connection of their own, and the cluster caveat. Auto-invoked when wiring Redis into a server app.
4
4
  user-invocable: false
5
5
  ---
6
6
  <!-- AUTO-GENERATED — do not edit. Regenerate via sync-agent-meta. -->
@@ -8,26 +8,100 @@ user-invocable: false
8
8
  # @owlmeans/redis
9
9
 
10
10
  **Layer:** Infra
11
- **Install:** `"@owlmeans/redis": "^0.1.18-rc.0"` in `dependencies`
11
+ **Install:** `"@owlmeans/redis": "^0.1.18-rc.20"` in `dependencies`
12
+
13
+ The connection half. The `Resource` contract over those connections is `@owlmeans/redis-resource`;
14
+ queues on them are `@owlmeans/redis-queue`.
12
15
 
13
16
  ## Key Exports
14
17
 
15
18
  | Export | Description |
16
19
  |--------|-------------|
17
- | `makeRedisService()` | Factory for the Redis connection service |
18
- | `Redis` types | Service interface, client handle |
19
- | Constants | `DEFAULT_ALIAS` for the redis service |
20
+ | `appendRedis(context, alias?)` | Register the service — the usual wiring |
21
+ | `makeRedisService(alias?)` | The bare factory, when registering it yourself |
22
+ | `RedisMeta` | What `cfg.dbs[].meta` accepts here: `dbIndex`, `masterNumber`, `slaveNumber`, plus any ioredis option |
23
+ | Constants | `DEFAULT_ALIAS` (`redis`, shared with `@owlmeans/redis-resource`) |
24
+
25
+ The service interface itself (`RedisDbService`, `RedisClient`, `RedisDb`, `RedisConnection`) is
26
+ declared in `@owlmeans/redis-resource`, so a consumer types against the contract package rather
27
+ than against the driver.
20
28
 
21
29
  ## Usage
22
30
 
23
31
  ```typescript
24
- import { makeRedisService } from '@owlmeans/redis'
25
- context.registerService(makeRedisService())
32
+ import { appendRedis } from '@owlmeans/redis'
26
33
 
27
- // Connection settings via cfg.services / cfg.dbs
34
+ appendRedis(context)
35
+ ```
36
+
37
+ ```typescript
38
+ const redis = context.service<RedisDbService>(DEFAULT_ALIAS)
39
+ await redis.ready()
40
+ const client = await redis.client() // the pooled connection
41
+ const { single, cluster, prefix } = redis.options()
28
42
  ```
29
43
 
44
+ Connections are configured through `cfg.dbs` — a LIST of `DbConfig` entries, each naming the
45
+ `service` that owns it and the `alias` it answers to. Every entry for this service is resolved when
46
+ the service initializes, and its client is closed on SIGTERM.
47
+
48
+ | Field | Meaning |
49
+ |---|---|
50
+ | `host` | A string for a single server; an ARRAY makes it a cluster (see below) |
51
+ | `port` | Defaults to 6379 |
52
+ | `secret` | The password |
53
+ | `user` | Sent as ioredis' `username` **only when set** — a bare `requirepass` server rejects AUTH with a username, so leave it absent unless the server has ACL users |
54
+ | `schema` | The key prefix, falling back to the entry's `alias` and then the service's. Every resource on the connection namespaces itself `<prefix>-<resource name>:<id>` |
55
+ | `meta.dbIndex` | The database index (`SELECT n`). Accepts a string, so it can come from a file-mounted config value; never use ioredis' own `db` |
56
+
57
+ **A `dbIndex` other than 0 turns `watch` off.** Redis publishes keyspace events per database, as
58
+ `__keyspace@<db>__:<key>`, and `@owlmeans/redis-resource` subscribes to `__keyspace@0__:` — so on
59
+ any other index a resource's `watch` handler silently never fires. Deployments sharing one instance
60
+ isolate on the key prefix (`schema`), and leave `dbIndex` alone wherever anything watches.
61
+
62
+ ## `options()` versus `client()`
63
+
64
+ `options(alias)` hands out the settings rather than a client, because a consumer that BLOCKS on a
65
+ read holds its connection for the duration and so cannot share the pooled one. Anything doing that
66
+ — BullMQ's workers and event streams, for instance — builds its own client from these settings,
67
+ which keeps the configuration here instead of being re-derived from `cfg.dbs` and drifting. It
68
+ answers `{ single, prefix }` for one host and `{ cluster, prefix }` for several; exactly one of the
69
+ two is present, which is how a consumer that cannot work on a cluster refuses at connection time.
70
+
71
+ ## Server Requirements
72
+
73
+ - Redis **6.2 or newer** — `@owlmeans/redis-resource` deletes with `GETDEL`, which is the whole
74
+ reason `delete`/`take` hand back the record they removed without a preceding read.
75
+ - `notify-keyspace-events` enabled if any resource uses `watch` — redis publishes nothing
76
+ otherwise.
77
+ - **A single server for anything answering criteria.** A resource answers `list`/`count`/`purge`
78
+ and `load(where)` by walking its own prefix with `SCAN`, which reaches one server — so in a
79
+ clustered deployment those calls see a fraction of the keyspace. Keep clustered resources on the
80
+ by-id operations, or put the data in mongo or postgres. Queues refuse a cluster outright.
81
+
82
+ ## A multi-host entry OWNS its cluster
83
+
84
+ A `dbs[]` entry whose `host` is an array of more than one does not merely connect: it asserts the
85
+ topology. It MEETs the nodes it was given, FORGETs the ones it was not, and where the layout does
86
+ not match `meta.masterNumber` / `meta.slaveNumber` it resets nodes and reassigns slot ranges —
87
+ which flushes them. Point such an entry only at a cluster this service is meant to own; for a
88
+ cluster managed elsewhere, or for a single server, give `host` one string. (A one-element array
89
+ collapses to a single connection and is safe.)
90
+
91
+ ## Tests
92
+
93
+ This package's `tests/` hold the integration specs for the `@owlmeans/redis-resource` contract —
94
+ it supplies the connection they run against. Gated on `REDIS_URL` (see `/.env.example`); run them
95
+ with `bun test ./tests`.
96
+
30
97
  ## Depends On
31
98
 
32
- - `@owlmeans/server-context`, `@owlmeans/resource`
99
+ - `@owlmeans/redis-resource` — the service and resource contracts, and `DEFAULT_DB_ALIAS`
100
+ - `@owlmeans/resource` — `createDbService`, `DbConfig`
101
+ - `@owlmeans/server-context` — the config and context this service binds to
33
102
  - `ioredis` (runtime)
103
+
104
+ ## Related
105
+
106
+ - `redis-resource` — records, TTL, pub/sub, keyspace watching and streams over these connections
107
+ - `redis-queue` — BullMQ queues over them
@@ -1 +1 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAwB,MAAM,0BAA0B,CAAA;AAKpF,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAG3E,KAAK,MAAM,GAAG,YAAY,CAAA;AAC1B,UAAU,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;CAAI;AAEzE,eAAO,MAAM,gBAAgB,WAAW,MAAM,KAAmB,cAwEhE,CAAA;AAED,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,WACpE,CAAC,UAAS,MAAM,KACxB,CAMF,CAAA"}
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAwB,MAAM,0BAA0B,CAAA;AAIpF,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAG3E,KAAK,MAAM,GAAG,YAAY,CAAA;AAC1B,UAAU,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;CAAI;AAEzE,eAAO,MAAM,gBAAgB,WAAW,MAAM,KAAmB,cAgEhE,CAAA;AAED,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,WACpE,CAAC,UAAS,MAAM,KACxB,CAMF,CAAA"}
package/build/service.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { DEFAULT_ALIAS } from './consts.js';
2
2
  import { createDbService } from '@owlmeans/resource';
3
- import { assertContext, Layer } from '@owlmeans/context';
4
- import { createClient } from './utils/index.js';
3
+ import { assertContext } from '@owlmeans/context';
4
+ import { createClient, prepareClusterRedisOptions, prepareSingleRedisOptions } from './utils/index.js';
5
5
  export const makeRedisService = (alias = DEFAULT_ALIAS) => {
6
6
  const location = `redis:${alias}`;
7
7
  const service = createDbService(alias, {
@@ -14,21 +14,21 @@ export const makeRedisService = (alias = DEFAULT_ALIAS) => {
14
14
  */
15
15
  return { client: client.duplicate(), prefix: name };
16
16
  },
17
+ options: configAlias => {
18
+ configAlias = service.ensureConfigAlias(configAlias);
19
+ const config = service.config(configAlias);
20
+ const prefix = service.name(configAlias);
21
+ const hosts = Array.isArray(config.host) ? config.host : [config.host];
22
+ return hosts.length > 1
23
+ ? { cluster: prepareClusterRedisOptions(config), prefix }
24
+ : { single: prepareSingleRedisOptions(config, hosts[0]), prefix };
25
+ },
17
26
  initialize: async (configAlias) => {
18
27
  configAlias = service.ensureConfigAlias(configAlias);
19
28
  const config = service.config(configAlias);
20
29
  if (service.clients[configAlias] != null) {
21
30
  return;
22
31
  }
23
- if (service.layers == null) {
24
- service.layers = [Layer.Global];
25
- }
26
- if (config.serviceSensitive && service.layers.includes(Layer.Service)) {
27
- service.layers.push(Layer.Service);
28
- }
29
- if (config.entitySensitive && service.layers.includes(Layer.Entity)) {
30
- service.layers.push(Layer.Entity);
31
- }
32
32
  let client = await createClient(config);
33
33
  // we need to check all hosts for replication consistancy
34
34
  if (service.clients[configAlias] != null) {
@@ -38,12 +38,6 @@ export const makeRedisService = (alias = DEFAULT_ALIAS) => {
38
38
  client.quit();
39
39
  });
40
40
  service.clients[configAlias] = client;
41
- },
42
- reinitializeContext: (context) => {
43
- const _service = makeRedisService(alias);
44
- _service.ctx = context;
45
- _service.layers = service.layers;
46
- return _service;
47
41
  }
48
42
  }, service => async () => {
49
43
  const context = assertContext(service.ctx, location);
@@ -1 +1 @@
1
- {"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAGxD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAK/C,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAK,GAAW,aAAa,EAAkB,EAAE;IAChF,MAAM,QAAQ,GAAG,SAAS,KAAK,EAAE,CAAA;IAEjC,MAAM,OAAO,GAAmB,eAAe,CAC7C,KAAK,EAAE;QACP,EAAE,EAAE,KAAK,EAAC,WAAW,EAAC,EAAE;YACtB,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;YAEhD,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;YAE5C;;;eAGG;YACH,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,SAAS,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAA;QACrD,CAAC;QAED,UAAU,EAAE,KAAK,EAAC,WAAW,EAAC,EAAE;YAC9B,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAA;YACpD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;YAE1C,IAAI,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,IAAI,EAAE,CAAC;gBACzC,OAAM;YACR,CAAC;YAED,I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OAAO,OAAO,CAAA;AAChB,CAAC,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/utils/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAoB5C,eAAO,MAAM,yBAAyB,WAAY,QAAQ,CAAC,SAAS,CAAC,SAAS,MAAM,KAAG,YAWtF,CAAA;AAED,eAAO,MAAM,0BAA0B,WAAY,QAAQ,CAAC,SAAS,CAAC,KAAG;IAAE,KAAK,EAAE,WAAW,EAAE,CAAC;IAAC,OAAO,EAAE,cAAc,CAAA;CAiBvH,CAAA"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/utils/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAoB5C,eAAO,MAAM,yBAAyB,WAAY,QAAQ,CAAC,SAAS,CAAC,SAAS,MAAM,KAAG,YActF,CAAA;AAED,eAAO,MAAM,0BAA0B,WAAY,QAAQ,CAAC,SAAS,CAAC,KAAG;IAAE,KAAK,EAAE,WAAW,EAAE,CAAC;IAAC,OAAO,EAAE,cAAc,CAAA;CAiBvH,CAAA"}
@@ -23,6 +23,9 @@ export const prepareSingleRedisOptions = (config, host) => {
23
23
  return {
24
24
  host: host,
25
25
  port: config.port ?? 6379,
26
+ // Only sent when configured: a bare `requirepass` server rejects AUTH with a username, so an
27
+ // undefined `user` has to stay absent rather than become an empty string.
28
+ ...(config.user != null ? { username: config.user } : {}),
26
29
  password: config.secret,
27
30
  ...normalizeRedisMeta(config.meta)
28
31
  };
@@ -1 +1 @@
1
- {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/utils/config.ts"],"names":[],"mappings":"AAIA,yFAAyF;AACzF,yFAAyF;AACzF,2CAA2C;AAC3C,MAAM,kBAAkB,GAAG,CAAC,IAAgB,EAAgB,EAAE;IAC5D,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;QACjB,OAAO,EAAE,CAAA;IACX,CAAC;IACD,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,IAAI,CAAA;IACjC,IAAI,OAAO,IAAI,IAAI,IAAI,GAAG,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC;QAC3C,OAAO,IAAI,CAAA;IACb,CAAC;IACD,MAAM,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,CAAA;IAC1B,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC;QACrB,MAAM,IAAI,WAAW,CAAC,2BAA2B,OAAO,GAAG,CAAC,CAAA;IAC9D,CAAC;IACD,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,EAAE,CAAA;AACxB,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,MAA2B,EAAE,IAAa,EAAgB,EAAE;IACpG,IAAI,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAW,CAAA;IACpD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,IAAI,WAAW,CAAC,8EAA8E,CAAC,CAAA;IACvG,CAAC;IACD,OAAO;QACL,IAAI,EAAE,IAAI;QACV,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,IAAI;QACzB,QAAQ,EAAE,MAAM,CAAC,MAAM;QACvB,GAAG,kBAAkB,CAAC,MAAM,CAAC,IAAI,CAAC;KAC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E,SAAS,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE;gBAC/B,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;YACzB,CAAC;YACD,mBAAmB,EAAE,KAAK;YAC1B,YAAY,EAAE;gBACZ,qEAAqE;gBACrE,QAAQ,EAAE,MAAM,CAAC,MAAM;gBACvB,GAAG,kBAAkB,CAAC,MAAM,CAAC,IAAI,CAAC;aACnC;SACF;KACF,CAAA;AACH,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/redis",
3
- "version": "0.1.18-rc.2",
3
+ "version": "0.1.18-rc.20",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -23,18 +23,18 @@
23
23
  },
24
24
  "devDependencies": {
25
25
  "@owlmeans/dep-config": "workspace:*",
26
- "@owlmeans/test-integration": "^0.1.18-rc.2",
27
- "@types/bun": "^1.3.14",
26
+ "@owlmeans/test-integration": "^0.1.18-rc.16",
27
+ "@types/bun": "^1.4.0",
28
28
  "@types/node": "^26.1.0",
29
29
  "nodemon": "^3.1.14",
30
30
  "typescript": "^7.0.2"
31
31
  },
32
32
  "dependencies": {
33
33
  "@noble/hashes": "^1.5.0",
34
- "@owlmeans/context": "^0.1.18-rc.2",
35
- "@owlmeans/redis-resource": "^0.1.18-rc.2",
36
- "@owlmeans/resource": "^0.1.18-rc.2",
37
- "@owlmeans/server-context": "^0.1.18-rc.2",
34
+ "@owlmeans/context": "^0.1.18-rc.16",
35
+ "@owlmeans/redis-resource": "^0.1.18-rc.20",
36
+ "@owlmeans/resource": "^0.1.18-rc.17",
37
+ "@owlmeans/server-context": "^0.1.18-rc.20",
38
38
  "@scure/base": "^2.3.0",
39
39
  "ioredis": "^5.4.1"
40
40
  },
package/src/service.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import type { RedisDbService, RedisClient, RedisDb } from '@owlmeans/redis-resource'
2
2
  import { DEFAULT_ALIAS } from './consts.js'
3
3
  import { createDbService } from '@owlmeans/resource'
4
- import { assertContext, Layer } from '@owlmeans/context'
5
- import type { BasicContext } from '@owlmeans/context'
4
+ import { assertContext } from '@owlmeans/context'
6
5
  import type { ServerContext, ServerConfig } from '@owlmeans/server-context'
7
- import { createClient } from './utils/index.js'
6
+ import { createClient, prepareClusterRedisOptions, prepareSingleRedisOptions } from './utils/index.js'
8
7
 
9
8
  type Config = ServerConfig
10
9
  interface Context<C extends Config = Config> extends ServerContext<C> { }
@@ -26,6 +25,18 @@ export const makeRedisService = (alias: string = DEFAULT_ALIAS): RedisDbService
26
25
  return { client: client.duplicate(), prefix: name }
27
26
  },
28
27
 
28
+ options: configAlias => {
29
+ configAlias = service.ensureConfigAlias(configAlias)
30
+ const config = service.config(configAlias)
31
+ const prefix = service.name(configAlias)
32
+
33
+ const hosts = Array.isArray(config.host) ? config.host : [config.host]
34
+
35
+ return hosts.length > 1
36
+ ? { cluster: prepareClusterRedisOptions(config), prefix }
37
+ : { single: prepareSingleRedisOptions(config, hosts[0]), prefix }
38
+ },
39
+
29
40
  initialize: async configAlias => {
30
41
  configAlias = service.ensureConfigAlias(configAlias)
31
42
  const config = service.config(configAlias)
@@ -34,16 +45,6 @@ export const makeRedisService = (alias: string = DEFAULT_ALIAS): RedisDbService
34
45
  return
35
46
  }
36
47
 
37
- if (service.layers == null) {
38
- service.layers = [Layer.Global]
39
- }
40
- if (config.serviceSensitive && service.layers.includes(Layer.Service)) {
41
- service.layers.push(Layer.Service)
42
- }
43
- if (config.entitySensitive && service.layers.includes(Layer.Entity)) {
44
- service.layers.push(Layer.Entity)
45
- }
46
-
47
48
  let client = await createClient(config)
48
49
 
49
50
  // we need to check all hosts for replication consistancy
@@ -57,16 +58,6 @@ export const makeRedisService = (alias: string = DEFAULT_ALIAS): RedisDbService
57
58
  })
58
59
 
59
60
  service.clients[configAlias] = client
60
- },
61
-
62
- reinitializeContext: <T>(context: BasicContext<ServerConfig>) => {
63
- const _service = makeRedisService(alias)
64
-
65
- _service.ctx = context
66
-
67
- _service.layers = service.layers
68
-
69
- return _service as T
70
61
  }
71
62
  }, service => async () => {
72
63
  const context = assertContext<Config, Context>(service.ctx as Context, location)
@@ -28,6 +28,9 @@ export const prepareSingleRedisOptions = (config: DbConfig<RedisMeta>, host?: st
28
28
  return {
29
29
  host: host,
30
30
  port: config.port ?? 6379,
31
+ // Only sent when configured: a bare `requirepass` server rejects AUTH with a username, so an
32
+ // undefined `user` has to stay absent rather than become an empty string.
33
+ ...(config.user != null ? { username: config.user } : {}),
31
34
  password: config.secret,
32
35
  ...normalizeRedisMeta(config.meta)
33
36
  }
package/tests/context.ts CHANGED
@@ -28,11 +28,15 @@ export interface RedisSuite {
28
28
  * One key prefix per suite, dropped by that suite's own `afterAll` — Bun runs every spec
29
29
  * file of a package in one process, so a process-global cleanup queue would let the first
30
30
  * file to finish flush the keys of the files still to come.
31
+ *
32
+ * A spec that walks the namespace — `list`, `count`, `purge` — boots under its own resource
33
+ * alias, because a resource's keys are namespaced by `<schema>-<alias>` and the walk sees every
34
+ * key of the alias it belongs to, including the ones an earlier test in the same file left.
31
35
  */
32
36
  export const makeSuite = (label: string): RedisSuite => {
33
37
  const base = process.env.REDIS_TEST_KEY_PREFIX ?? 'omt'
34
38
  const prefix = randomNamespace(`${base}_${label}`)
35
- const contexts: Array<ServerContext<ServerConfig>> = []
39
+ const resources: Array<RedisResource<TestRecord>> = []
36
40
 
37
41
  const boot = async (alias = 'test-records'): Promise<{
38
42
  context: ServerContext<ServerConfig>
@@ -58,26 +62,42 @@ export const makeSuite = (label: string): RedisSuite => {
58
62
 
59
63
  context.configure()
60
64
  await context.init()
61
- contexts.push(context)
65
+ const booted = context.resource<RedisResource<TestRecord>>(alias)
66
+ resources.push(booted)
62
67
 
63
- return { context, resource: context.resource<RedisResource<TestRecord>>(alias) }
68
+ return { context, resource: booted }
64
69
  }
65
70
 
71
+ /**
72
+ * Every key the suite wrote, matched as `<prefix>*` rather than `<prefix>:*`: a resource
73
+ * namespaces its keys as `<schema>-<alias>:<id>`, so the schema is followed by a dash, not
74
+ * by the separator. SCAN for the same reason the resource uses it — KEYS blocks the server.
75
+ */
66
76
  const teardown = async (): Promise<void> => {
67
77
  if (gate.skip) {
68
78
  return
69
79
  }
70
- for (const context of contexts) {
71
- const resource = context.resource<RedisResource<TestRecord>>('test-records')
80
+ for (const resource of resources) {
72
81
  const client = resource.db?.client
73
82
  if (client == null) continue
74
- const keys = await client.keys(`${prefix}:*`).catch(() => [] as string[])
75
- if (keys.length > 0) {
76
- await client.del(...keys).catch(() => undefined)
83
+ try {
84
+ const keys: string[] = []
85
+ let cursor = '0'
86
+ do {
87
+ const [next, batch] = await client.scan(cursor, 'MATCH', `${prefix}*`, 'COUNT', 500)
88
+ cursor = next
89
+ keys.push(...batch)
90
+ } while (cursor !== '0')
91
+ if (keys.length > 0) {
92
+ await client.del(keys)
93
+ }
94
+ } catch {
95
+ // A connection already gone takes its keys' cleanup with it — the prefix is unique
96
+ // per run, so anything left behind never collides with another suite.
77
97
  }
78
98
  await client.quit().catch(() => undefined)
79
99
  }
80
- contexts.length = 0
100
+ resources.length = 0
81
101
  }
82
102
 
83
103
  return { prefix, boot, teardown }
@@ -0,0 +1,111 @@
1
+ import { afterAll, describe, expect, test } from 'bun:test'
2
+ import { gate, makeSuite } from './context.js'
3
+
4
+ /**
5
+ * The criteria surface.
6
+ *
7
+ * Redis has no index, so anything that is not a bare id is a SCAN of the resource's own namespace
8
+ * evaluated in memory. These assert what that walk owes the caller: criteria reads, an unpaged
9
+ * default, the window that was actually asked for, a total that always counts every match, and a
10
+ * purge that refuses to empty the namespace on an unset filter.
11
+ */
12
+ describe('@owlmeans/redis — resource queries', () => {
13
+ if (gate.skip) {
14
+ test.skip(gate.reason ?? 'redis gate closed', () => { })
15
+ return
16
+ }
17
+
18
+ const suite = makeSuite('query')
19
+
20
+ afterAll(async () => {
21
+ await suite.teardown()
22
+ })
23
+
24
+ /** `rec-0` … `rec-<count-1>`, alternating `even` / `odd`, in a namespace of their own. */
25
+ const seed = async (alias: string, count: number) => {
26
+ const { resource } = await suite.boot(alias)
27
+ for (let index = 0; index < count; index++) {
28
+ await resource.create({ id: `rec-${index}`, value: index % 2 === 0 ? 'even' : 'odd' })
29
+ }
30
+
31
+ return resource
32
+ }
33
+
34
+ test('load takes criteria, not only an id', async () => {
35
+ const resource = await seed('q-load', 0)
36
+ await resource.create({ id: 'one', value: 'needle' })
37
+ await resource.create({ id: 'two', value: 'hay' })
38
+
39
+ expect(await resource.load({ value: 'needle' })).toMatchObject({ id: 'one' })
40
+ expect(await resource.load({ value: 'absent' })).toBeNull()
41
+ })
42
+
43
+ test('get throws when nothing matches the criteria', async () => {
44
+ const resource = await seed('q-get', 0)
45
+
46
+ await expect(resource.get({ value: 'absent' })).rejects.toThrow()
47
+ })
48
+
49
+ test('list returns every match when no size is asked for', async () => {
50
+ const resource = await seed('q-unpaged', 5)
51
+
52
+ const result = await resource.list()
53
+
54
+ expect(result.items).toHaveLength(5)
55
+ expect(result.total).toBe(5)
56
+ })
57
+
58
+ test('list returns the page it was asked for, not the list minus that page', async () => {
59
+ const resource = await seed('q-paged', 5)
60
+
61
+ const result = await resource.list({}, { page: 1, size: 2, sort: ['id'] })
62
+
63
+ expect(result.items.map(item => item.id)).toEqual(['rec-2', 'rec-3'])
64
+ expect(result.total).toBe(5)
65
+ })
66
+
67
+ test('a page without a size is a caller error', async () => {
68
+ const resource = await seed('q-page-only', 1)
69
+
70
+ await expect(resource.list({}, { page: 1 })).rejects.toThrow()
71
+ })
72
+
73
+ test('count answers the match count', async () => {
74
+ const resource = await seed('q-count', 5)
75
+
76
+ expect(await resource.count()).toBe(5)
77
+ expect(await resource.count({ value: 'even' })).toBe(3)
78
+ })
79
+
80
+ test('purge deletes every match and refuses empty criteria', async () => {
81
+ const resource = await seed('q-purge', 5)
82
+
83
+ expect(await resource.purge({ value: 'odd' })).toBe(2)
84
+ expect(await resource.count()).toBe(3)
85
+ await expect(resource.purge({})).rejects.toThrow()
86
+ })
87
+
88
+ test('update replaces the record rather than merging into it', async () => {
89
+ const resource = await seed('q-update', 0)
90
+ await resource.create({ id: 'replaced', value: 'before' })
91
+
92
+ await resource.update({ id: 'replaced' })
93
+
94
+ expect(await resource.load('replaced')).toEqual({ id: 'replaced' })
95
+ })
96
+
97
+ test('update throws for an id that was never created', async () => {
98
+ const resource = await seed('q-missing', 0)
99
+
100
+ await expect(resource.update({ id: 'never-created' })).rejects.toThrow()
101
+ })
102
+
103
+ test('save creates a record that carries no id', async () => {
104
+ const resource = await seed('q-save', 0)
105
+
106
+ const saved = await resource.save({ value: 'fresh' })
107
+
108
+ expect(typeof saved.id).toBe('string')
109
+ expect(await resource.load(saved.id)).toMatchObject({ value: 'fresh' })
110
+ })
111
+ })
@@ -2,13 +2,12 @@ import { afterAll, describe, expect, test } from 'bun:test'
2
2
  import { gate, makeSuite } from './context.js'
3
3
 
4
4
  /**
5
- * `delete` regression coverage.
5
+ * `delete` / `take` coverage.
6
6
  *
7
- * The by-id form silently did nothing: `delete(id, opts)` only loaded the record when `id`
8
- * was an object or `opts` was a field name, so the plain `delete(id)` every caller uses fell
9
- * through to `record == null` and returned before touching redis. It surfaced as
10
- * `RecordExists` on the second email-OTP request for one address inside the code's TTL
11
- * `issueChallenge` upserts by deleting first — and left consumed codes alive in the store.
7
+ * Both drop the key with GETDEL and hand back what was under it; they differ only in what an
8
+ * absent id means `delete` answers `null`, `take` throws. The by-id form carries the whole
9
+ * contract: a `delete(id)` that returns without touching redis leaves consumed OTP codes alive
10
+ * in the store and turns the next `create` under that id into `RecordExists`.
12
11
  */
13
12
  describe('@owlmeans/redis — resource delete', () => {
14
13
  if (gate.skip) {
@@ -22,7 +21,7 @@ describe('@owlmeans/redis — resource delete', () => {
22
21
  await suite.teardown()
23
22
  })
24
23
 
25
- test('removes the record when called by id alone', async () => {
24
+ test('removes the record when called by id', async () => {
26
25
  const { resource } = await suite.boot()
27
26
  await resource.create({ id: 'by-id', value: 'first' })
28
27
 
@@ -46,11 +45,24 @@ describe('@owlmeans/redis — resource delete', () => {
46
45
  expect((await resource.load('otp'))?.value).toBe('second-code')
47
46
  })
48
47
 
49
- test('removes the record when handed the record itself', async () => {
48
+ test('take hands back the record it removed', async () => {
50
49
  const { resource } = await suite.boot()
51
- const record = await resource.create({ id: 'by-record', value: 'x' })
50
+ await resource.create({ id: 'taken', value: 'once' })
52
51
 
53
- expect(await resource.delete(record)).toMatchObject({ id: 'by-record' })
54
- expect(await resource.load('by-record')).toBeNull()
52
+ expect(await resource.take('taken')).toMatchObject({ value: 'once' })
53
+ expect(await resource.load('taken')).toBeNull()
54
+ })
55
+
56
+ test('take throws for an id that is not there', async () => {
57
+ const { resource } = await suite.boot()
58
+
59
+ await expect(resource.take('never-taken')).rejects.toThrow()
60
+ })
61
+
62
+ test('create refuses an id that is already taken', async () => {
63
+ const { resource } = await suite.boot()
64
+ await resource.create({ id: 'occupied', value: 'first' })
65
+
66
+ await expect(resource.create({ id: 'occupied', value: 'second' })).rejects.toThrow()
55
67
  })
56
68
  })
package/build/.gitkeep DELETED
File without changes