@reddb-io/sdk 1.45.0 → 1.46.1

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
@@ -358,6 +358,10 @@ await kv.list({ prefix: 'characters:' }) // { items: [{ key, value }] }
358
358
  await kv.delete('characters:hansel') // { affected, deleted }
359
359
  ```
360
360
 
361
+ `await kv.incr('page:views', { by: 1, expireMs: 120_000 })` atomically
362
+ increments the counter, refreshes its TTL and returns the new value.
363
+ [Rate limiter example](https://github.com/reddb-io/reddb/blob/main/docs/getting-started/quickstart-key-value.md#limit-each-user-to-100-requests-per-minute)
364
+
361
365
  A `kv.get` of a missing key returns `null` (never `NOT_FOUND`). `kv.delete`
362
366
  returns the `{ affected, deleted }` envelope; deleting a missing key is not an
363
367
  error and returns `{ affected: 0, deleted: false }`.
package/index.d.ts CHANGED
@@ -288,6 +288,8 @@ export class KvClient {
288
288
  value: unknown,
289
289
  options?: { collection?: string; expireMs?: number; tags?: string[] },
290
290
  ): Promise<QueryResult>
291
+ /** Atomically add a positive integer and return the updated counter. TTL is in milliseconds. */
292
+ incr(key: string, options?: { collection?: string; by?: number; expireMs?: number }): Promise<number | bigint>
291
293
  get(key: string, options?: { collection?: string }): Promise<unknown | null>
292
294
  getMany(keys: string[], options?: { collection?: string }): Promise<Array<unknown | null>>
293
295
  exists(key: string, options?: { collection?: string }): Promise<{ exists: boolean }>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reddb-io/sdk",
3
- "version": "1.45.0",
3
+ "version": "1.46.1",
4
4
  "description": "Official embedded RedDB SDK — launches a local red binary over stdio JSON-RPC. The binary arrives as a per-platform optionalDependency — no install script. Use @reddb-io/client for remote HTTP, gRPC, and RedWire.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -43,11 +43,11 @@
43
43
  "url": "https://github.com/reddb-io/reddb/issues"
44
44
  },
45
45
  "optionalDependencies": {
46
- "@reddb-io/red-linux-x64": "1.45.0",
47
- "@reddb-io/red-linux-x64-musl": "1.45.0",
48
- "@reddb-io/red-linux-arm64": "1.45.0",
49
- "@reddb-io/red-linux-arm64-musl": "1.45.0",
50
- "@reddb-io/red-win32-x64": "1.45.0"
46
+ "@reddb-io/red-linux-x64": "1.46.1",
47
+ "@reddb-io/red-linux-x64-musl": "1.46.1",
48
+ "@reddb-io/red-linux-arm64": "1.46.1",
49
+ "@reddb-io/red-linux-arm64-musl": "1.46.1",
50
+ "@reddb-io/red-win32-x64": "1.46.1"
51
51
  },
52
52
  "scripts": {
53
53
  "test": "node --test test/ask.test.mjs test/cache.test.mjs test/db-helpers.test.mjs test/embedded-only.test.mjs test/helpers.test.mjs test/insert-ids.test.mjs test/kv.test.mjs test/params.test.mjs test/platform-binary.test.mjs test/native-engine.test.mjs test/queue.test.mjs test/redwire.params.test.mjs test/transaction.test.mjs test/url.test.mjs && node test/smoke.test.mjs && node test/conformance.test.mjs && node test/readme-examples.test.mjs"
package/src/index.js CHANGED
@@ -489,6 +489,7 @@ export class RedDB {
489
489
  return new KvClient(client, collection)
490
490
  }, {
491
491
  put: defaultKv.put.bind(defaultKv),
492
+ incr: defaultKv.incr.bind(defaultKv),
492
493
  invalidateTags: defaultKv.invalidateTags.bind(defaultKv),
493
494
  watch: defaultKv.watch.bind(defaultKv),
494
495
  watchPrefix: defaultKv.watchPrefix.bind(defaultKv),
package/src/kv.js CHANGED
@@ -22,6 +22,25 @@ export class KvClient {
22
22
  return this.put(key, value, options)
23
23
  }
24
24
 
25
+ async incr(key, { collection = this.collection, by = 1, expireMs } = {}) {
26
+ if (!Number.isSafeInteger(by) || by < 1) {
27
+ throw new RedDBError('INVALID_ARGUMENT', 'kv.incr by must be a positive safe integer')
28
+ }
29
+ if (expireMs !== undefined && (!Number.isSafeInteger(expireMs) || expireMs < 0)) {
30
+ throw new RedDBError('INVALID_ARGUMENT', 'kv.incr expireMs must be a non-negative safe integer')
31
+ }
32
+ const expire = expireMs === undefined ? '' : ` EXPIRE ${expireMs} ms`
33
+ const result = await this.client.call('query', {
34
+ sql: `KV INCR ${kvPath(collection, key)} BY ${by}${expire}`,
35
+ })
36
+ const value = result?.rows?.[0]?.value
37
+ // Embedded JSON-RPC can return the exact i64 envelope. Other transports
38
+ // may already have decoded it to bigint.
39
+ if (typeof value?.$int === 'string') return BigInt(value.$int)
40
+ if (typeof value === 'bigint' || Number.isSafeInteger(value)) return value
41
+ throw new RedDBError('INVALID_RESPONSE', 'kv.incr expected an integer counter')
42
+ }
43
+
25
44
  async get(key, options = {}) {
26
45
  const collection = options.collection ?? this.collection
27
46
  const result = await this.client.call('query', {