@toa.io/storages.mongodb 1.0.0-alpha.313 → 1.0.0-alpha.315

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/CHANGELOG.md CHANGED
@@ -3,6 +3,24 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.0.0-alpha.315](https://github.com/toa-io/toa/compare/v1.0.0-alpha.314...v1.0.0-alpha.315) (2026-09-23)
7
+
8
+ **Note:** Version bump only for package @toa.io/storages.mongodb
9
+
10
+
11
+
12
+
13
+
14
+ # [1.0.0-alpha.314](https://github.com/toa-io/toa/compare/v1.0.0-alpha.313...v1.0.0-alpha.314) (2026-09-19)
15
+
16
+ * feat(core)!: let an operation state how long its calls are remembered ([af177c2](https://github.com/toa-io/toa/commit/af177c2ab3d00d475840b5ef9e612f04e82b72bd))
17
+
18
+ ### BREAKING CHANGES
19
+
20
+ * `inbox.retention` under 600 is refused. See
21
+ migrations/313.md.
22
+
23
+
6
24
  # [1.0.0-alpha.313](https://github.com/toa-io/toa/compare/v1.0.0-alpha.312...v1.0.0-alpha.313) (2026-09-18)
7
25
 
8
26
  **Note:** Version bump only for package @toa.io/storages.mongodb
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/storages.mongodb",
3
- "version": "1.0.0-alpha.313",
3
+ "version": "1.0.0-alpha.315",
4
4
  "type": "module",
5
5
  "description": "Toa MongoDB Storage Connector",
6
6
  "author": "temich <tema.gurtovoy@gmail.com>",
@@ -21,12 +21,12 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@toa.io/conveyor": "1.0.0-alpha.311",
24
- "@toa.io/core": "1.0.0-alpha.312",
25
- "@toa.io/definitions": "1.0.0-alpha.313",
24
+ "@toa.io/core": "1.0.0-alpha.315",
25
+ "@toa.io/definitions": "1.0.0-alpha.315",
26
26
  "@toa.io/generic": "1.0.0-alpha.311",
27
27
  "@toa.io/pointer": "1.0.0-alpha.311",
28
28
  "mongodb": "7.6.0",
29
29
  "openspan": "1.0.0-alpha.305"
30
30
  },
31
- "gitHead": "7da3b2627ceeefd1fa19d5c8851be88d928523b6"
31
+ "gitHead": "d88ed286bf44bf95b5f3d9db6e7d4621261dbb99"
32
32
  }
package/src/inbox.js CHANGED
@@ -39,12 +39,18 @@ export class Inbox {
39
39
  * whole mechanism: the call has been made, this transaction changes nothing, and the caller
40
40
  * is answered from what is already there.
41
41
  *
42
- * @param {{ id: string, reply: object }} call
42
+ * The record carries the moment it expires rather than leaving that to the index, because the
43
+ * window is the operation's where it states one, and an index has one window for all of them.
44
+ *
45
+ * @param {{ id: string, reply: object, retention?: number }} call
43
46
  * @param {import('mongodb').ClientSession} session
44
47
  */
45
48
  async insert(call, session) {
49
+ const at = new Date()
50
+ const expires = new Date(at.getTime() + (call.retention ?? this.#retention) * 1000)
51
+
46
52
  await this.#collection.insertOne(
47
- { _id: call.id, reply: call.reply ?? {}, at: new Date() },
53
+ { _id: call.id, reply: call.reply ?? {}, at, expires },
48
54
  { session }
49
55
  )
50
56
  }
@@ -56,18 +62,44 @@ export class Inbox {
56
62
  */
57
63
  async index() {
58
64
  const desired = {
59
- // how long a call is remembered is how long a duplicate of it is caught
60
- inbox_at: {
61
- fields: { at: 1 },
62
- options: { name: 'inbox_at', expireAfterSeconds: this.#retention }
65
+ // a record is reaped at the moment it names, which is how each keeps a window of its own
66
+ inbox_expires: {
67
+ fields: { expires: 1 },
68
+ options: { name: 'inbox_expires', expireAfterSeconds: 0 }
63
69
  }
64
70
  }
65
71
 
72
+ await this.#backfill()
73
+
66
74
  for (const { fields, options } of Object.values(desired))
67
75
  await index(this.#collection, fields, options)
68
76
 
69
77
  await prune(this.#collection, Object.keys(desired))
70
78
  }
79
+
80
+ /**
81
+ * A record written before a record named its own expiry was reaped by an index on `at`, which
82
+ * this replaces. One with no `expires` would be kept forever under the new index, so it is
83
+ * given the one it had: the deployment's window after it was written. Done before the old
84
+ * index is pruned, so that nothing is left unreaped in between, and on every boot, because a
85
+ * process of the previous release may still be writing them during a rolling deploy. After
86
+ * that it finds nothing.
87
+ */
88
+ async #backfill() {
89
+ await this.#collection.updateMany({ expires: { $exists: false } }, [
90
+ {
91
+ $set: {
92
+ expires: {
93
+ $dateAdd: {
94
+ startDate: { $ifNull: ['$at', '$$NOW'] },
95
+ unit: 'second',
96
+ amount: this.#retention
97
+ }
98
+ }
99
+ }
100
+ }
101
+ ])
102
+ }
71
103
  }
72
104
 
73
105
  function retention() {
@@ -77,7 +109,7 @@ function retention() {
77
109
  }
78
110
 
79
111
  /**
80
- * Seconds a call is remembered. An hour, and not the outbox's day: what a duplicate arrives
112
+ * Seconds a call of an operation that states no window of its own is remembered. An hour, and not the outbox's day: what a duplicate arrives
81
113
  * within is the broker's redelivery, the five attempts `comq` makes of a message, and whatever
82
114
  * a client retries on — minutes. Every call of an operation that declares `once` is a document
83
115
  * here for this long.
package/src/storage.js CHANGED
@@ -411,7 +411,7 @@ export class Storage extends Connector {
411
411
  call.reply?.output === undefined ? { ...call.reply, output: state } : call.reply
412
412
 
413
413
  try {
414
- await this.#inbox.insert({ id: call.id, reply }, session)
414
+ await this.#inbox.insert({ ...call, reply }, session)
415
415
  } catch (error) {
416
416
  if (error?.code !== ERR_DUPLICATE_KEY) throw error
417
417