@toa.io/storages.mongodb 1.0.0-alpha.33 → 1.0.0-alpha.331

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.
@@ -0,0 +1,319 @@
1
+ import { describe, it, beforeEach, mock } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { isDeepStrictEqual } from 'node:util'
4
+
5
+ import { Storage } from '../src/storage.js'
6
+
7
+ let collection
8
+ let db
9
+ let session
10
+ let storage
11
+
12
+ beforeEach(async () => {
13
+ collection = {
14
+ collectionName: 'test',
15
+ findOne: mock.fn(async () => null),
16
+ find: mock.fn(() => cursor()),
17
+ updateMany: mock.fn(async () => ({ modifiedCount: 0 })),
18
+ updateOne: mock.fn(async () => ({ upsertedCount: 0, modifiedCount: 0 }))
19
+ }
20
+
21
+ db = { collection: mock.fn(() => null) }
22
+
23
+ session = {
24
+ endSession: async () => undefined,
25
+ abortTransaction: mock.fn(async () => undefined)
26
+ }
27
+
28
+ const instance = { client: { startSession: () => session } }
29
+ const transaction = async (fn) => fn(session)
30
+ const client = { collection, db, instance, transaction, link: () => null }
31
+
32
+ storage = new Storage(client, { schema: { properties: {} } })
33
+
34
+ await storage.open()
35
+ })
36
+
37
+ function cursor(records = []) {
38
+ return {
39
+ hasNext: async () => records.length > 0,
40
+ close: async () => undefined,
41
+ async *[Symbol.asyncIterator]() {
42
+ yield* records
43
+ }
44
+ }
45
+ }
46
+
47
+ describe('open', () => {
48
+ it('should not touch the migrations record where the entity declares none', () => {
49
+ assert.equal(db.collection.mock.callCount(), 0)
50
+ })
51
+ })
52
+
53
+ describe('get', () => {
54
+ it('should filter deleted', async () => {
55
+ await storage.get({})
56
+
57
+ assert.ok(
58
+ collection.findOne.mock.calls.some(
59
+ (call) =>
60
+ call.arguments.length === 2 &&
61
+ isDeepStrictEqual(call.arguments[0], { DELETED: null }) &&
62
+ isDeepStrictEqual(call.arguments[1], {})
63
+ )
64
+ )
65
+ })
66
+
67
+ it('should filter deleted with sort', async () => {
68
+ await storage.get({ options: { sort: [['CREATED', 'desc']] } })
69
+
70
+ assert.ok(
71
+ collection.findOne.mock.calls.some(
72
+ (call) =>
73
+ call.arguments.length === 2 &&
74
+ isDeepStrictEqual(call.arguments[0], { DELETED: null }) &&
75
+ isDeepStrictEqual(call.arguments[1], { sort: [['CREATED', -1]] })
76
+ )
77
+ )
78
+ })
79
+
80
+ it('should not filter deleted if queried by id', async () => {
81
+ const id = 'bcb6780f50e243348cad40ed6b5ef575'
82
+
83
+ await storage.get({ id })
84
+
85
+ assert.ok(
86
+ collection.findOne.mock.calls.some(
87
+ (call) =>
88
+ call.arguments.length === 2 &&
89
+ isDeepStrictEqual(call.arguments[0], { _id: id }) &&
90
+ isDeepStrictEqual(call.arguments[1], {})
91
+ )
92
+ )
93
+ })
94
+
95
+ it('should not filter deleted if requested', async () => {
96
+ await storage.get({ options: { deleted: true } })
97
+
98
+ assert.ok(
99
+ collection.findOne.mock.calls.some(
100
+ (call) =>
101
+ call.arguments.length === 2 &&
102
+ isDeepStrictEqual(call.arguments[0], {}) &&
103
+ isDeepStrictEqual(call.arguments[1], {})
104
+ )
105
+ )
106
+ })
107
+ })
108
+
109
+ describe('stream', () => {
110
+ const read = () => collection.find.mock.calls[0].arguments
111
+
112
+ it('should filter deleted', async () => {
113
+ await storage.stream()
114
+
115
+ assert.deepStrictEqual(read()[0], { DELETED: null })
116
+ })
117
+
118
+ it('should filter deleted with sort', async () => {
119
+ await storage.stream({ options: { sort: [['CREATED', 'desc']] } })
120
+
121
+ assert.deepStrictEqual(read()[0], { DELETED: null })
122
+ assert.deepStrictEqual(read()[1].sort, [['CREATED', -1]])
123
+ })
124
+
125
+ it('should not filter deleted if requested', async () => {
126
+ await storage.stream({ options: { deleted: true } })
127
+
128
+ assert.deepStrictEqual(read()[0], {})
129
+ })
130
+
131
+ it('should read what the majority of the replica set holds', async () => {
132
+ await storage.stream()
133
+
134
+ assert.deepStrictEqual(read()[1].readConcern, { level: 'majority' })
135
+ })
136
+
137
+ it('should end the set with no position where the storage keeps no history', async () => {
138
+ const stream = await storage.stream()
139
+ const parts = await stream.toArray()
140
+
141
+ assert.deepStrictEqual(parts, [{ token: null }])
142
+ })
143
+
144
+ it('should read a window ordered by id', async () => {
145
+ await storage.stream({ options: { limit: 2 } })
146
+
147
+ assert.deepStrictEqual(read()[1].sort, [['_id', 1]])
148
+ assert.equal(read()[1].limit, 2)
149
+ })
150
+
151
+ it('should read a window newest first, and by id where entries share a time', async () => {
152
+ await storage.stream({ options: { limit: 2, sort: [['CREATED', 'desc']] } })
153
+
154
+ assert.deepStrictEqual(read()[1].sort, [
155
+ ['CREATED', -1],
156
+ ['_id', -1]
157
+ ])
158
+ })
159
+
160
+ it('should refuse a window ordered by what an entry changes', async () => {
161
+ await assert.rejects(
162
+ storage.stream({ options: { limit: 2, sort: [['title', 'asc']] } }),
163
+ (error) => error.code === 221
164
+ )
165
+ })
166
+
167
+ it('should end a window it was told to stop at with no window token', async () => {
168
+ const found = [
169
+ { _id: 'a', CREATED: new Date(2), VERSION: 1 },
170
+ { _id: 'b', CREATED: new Date(1), VERSION: 1 }
171
+ ]
172
+
173
+ collection.find.mock.mockImplementationOnce(() => cursor(found))
174
+
175
+ const stream = await storage.stream({ options: { limit: 2, stop: true } })
176
+ const parts = await stream.toArray()
177
+
178
+ assert.deepStrictEqual(parts.at(-1), { token: null })
179
+ })
180
+ })
181
+
182
+ describe('converge', () => {
183
+ const record = { id: 'a1', VERSION: 4, REGION: 1, status: 'paid' }
184
+
185
+ const call = () => collection.updateOne.mock.calls[0].arguments
186
+
187
+ it('should select by id alone, so that what is absent is upserted', async () => {
188
+ await storage.converge(record)
189
+
190
+ const [criteria, , options] = call()
191
+
192
+ assert.deepEqual(criteria, { _id: 'a1' })
193
+ assert.equal(options.upsert, true)
194
+ })
195
+
196
+ it('should supersede a lower version', async () => {
197
+ await storage.converge(record)
198
+
199
+ const [, pipeline] = call()
200
+ const [older] = pipeline[0].$replaceWith.$cond[0].$or
201
+
202
+ assert.deepEqual(older, { $lt: [{ $ifNull: ['$VERSION', 0] }, 4] })
203
+ })
204
+
205
+ it('should supersede an equal version written by an outranked region', async () => {
206
+ await storage.converge(record)
207
+
208
+ const [, pipeline] = call()
209
+ const [, tied] = pipeline[0].$replaceWith.$cond[0].$or
210
+
211
+ assert.deepEqual(tied.$and, [
212
+ { $eq: ['$VERSION', 4] },
213
+ { $gt: [{ $ifNull: ['$REGION', 0] }, 1] }
214
+ ])
215
+ })
216
+
217
+ it('should leave what is stored where it does not', async () => {
218
+ await storage.converge(record)
219
+
220
+ const [, pipeline] = call()
221
+
222
+ assert.equal(pipeline[0].$replaceWith.$cond[2], '$$ROOT')
223
+ })
224
+
225
+ it('should write the record as a literal, so that a `$` value is not a field path', async () => {
226
+ await storage.converge({ ...record, status: '$paid' })
227
+
228
+ const [, pipeline] = call()
229
+ const written = pipeline[0].$replaceWith.$cond[1].$literal
230
+
231
+ assert.equal(written._id, 'a1')
232
+ assert.equal(written.status, '$paid')
233
+ assert.equal(written.id, undefined)
234
+ })
235
+
236
+ it('should answer true where it inserted', async () => {
237
+ collection.updateOne.mock.mockImplementationOnce(async () => ({
238
+ upsertedCount: 1,
239
+ modifiedCount: 0
240
+ }))
241
+
242
+ assert.equal(await storage.converge(record), true)
243
+ })
244
+
245
+ it('should answer true where it superseded', async () => {
246
+ collection.updateOne.mock.mockImplementationOnce(async () => ({
247
+ upsertedCount: 0,
248
+ modifiedCount: 1
249
+ }))
250
+
251
+ assert.equal(await storage.converge(record), true)
252
+ })
253
+
254
+ it('should read a record that lacks a region as the first one', async () => {
255
+ await storage.converge(record)
256
+
257
+ const [, pipeline] = call()
258
+ const [, tied] = pipeline[0].$replaceWith.$cond[0].$or
259
+
260
+ // the migration writes it, and one that reached here without it would win every tie
261
+ assert.deepEqual(tied.$and[1].$gt[0], { $ifNull: ['$REGION', 0] })
262
+ })
263
+
264
+ it('should answer false where it changed nothing, and not throw', async () => {
265
+ assert.equal(await storage.converge(record), false)
266
+ })
267
+ })
268
+
269
+ describe('massStore', () => {
270
+ const stored = { id: 'a1', VERSION: 3, foo: 1 }
271
+ const created = { id: 'b2', VERSION: 1, foo: 1 }
272
+
273
+ const operations = () => collection.bulkWrite.mock.calls[0].arguments[0]
274
+
275
+ beforeEach(() => {
276
+ collection.bulkWrite = mock.fn(async (operations) => ({
277
+ matchedCount: operations.length,
278
+ upsertedCount: 0
279
+ }))
280
+ })
281
+
282
+ it('should replace a record only at the version it was read at', async () => {
283
+ await storage.massStore([stored])
284
+
285
+ assert.deepEqual(operations()[0].replaceOne.filter, { _id: 'a1', VERSION: 2 })
286
+ })
287
+
288
+ it('should create a record only where none but a deleted one holds its id', async () => {
289
+ await storage.massStore([created])
290
+
291
+ const { filter, upsert } = operations()[0].updateOne
292
+
293
+ assert.deepEqual(filter, { _id: 'b2', DELETED: { $ne: null } })
294
+ assert.equal(upsert, true)
295
+ })
296
+
297
+ it('should answer true where every record was written', async () => {
298
+ assert.equal(await storage.massStore([stored, created]), true)
299
+ assert.equal(session.abortTransaction.mock.callCount(), 0)
300
+ })
301
+
302
+ it('should abort and answer false where a record was not matched', async () => {
303
+ collection.bulkWrite = mock.fn(async () => ({ matchedCount: 1, upsertedCount: 0 }))
304
+
305
+ assert.equal(await storage.massStore([stored, { ...stored, id: 'c3' }]), false)
306
+ assert.equal(session.abortTransaction.mock.callCount(), 1)
307
+ })
308
+
309
+ it('should abort and answer false where a record being created exists', async () => {
310
+ collection.bulkWrite = mock.fn(async () => {
311
+ throw Object.assign(new Error('E11000 duplicate key error index: _id_ dup key'), {
312
+ code: 11000
313
+ })
314
+ })
315
+
316
+ assert.equal(await storage.massStore([created]), false)
317
+ assert.equal(session.abortTransaction.mock.callCount(), 1)
318
+ })
319
+ })
@@ -0,0 +1,60 @@
1
+ import { it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { Long, Timestamp } from 'mongodb'
4
+
5
+ import { Streams } from '../src/streams.js'
6
+
7
+ /**
8
+ * A first read takes the position of a change stream, and kills the cursor that took it. The
9
+ * driver gives a cursor id that fits a number as a number, and one that does not as a `Long`.
10
+ */
11
+ function streams(id) {
12
+ const killed = []
13
+
14
+ const db = {
15
+ command: async (command) => {
16
+ if ('killCursors' in command) {
17
+ killed.push(...command.cursors)
18
+
19
+ return {}
20
+ }
21
+
22
+ return { cursor: { id, postBatchResumeToken: { _data: '82' } } }
23
+ }
24
+ }
25
+
26
+ const session = { operationTime: new Timestamp(1n), endSession: async () => undefined }
27
+ const cursor = {
28
+ hasNext: async () => false,
29
+ close: async () => undefined,
30
+ [Symbol.asyncIterator]: async function* () {}
31
+ }
32
+ const collection = { s: { db }, collectionName: 'test', find: () => cursor }
33
+ const client = () => ({ startSession: () => session })
34
+
35
+ return { streams: new Streams(collection, client, (record) => record, true), killed }
36
+ }
37
+
38
+ for (const [name, id] of [
39
+ ['a number', 7],
40
+ ['a Long', Long.fromString('9007199254740993')]
41
+ ])
42
+ it(`should kill the cursor of the position, whose id is ${name}`, async () => {
43
+ const { streams: read, killed } = streams(id)
44
+
45
+ await read.stream({ criteria: {}, options: {} })
46
+
47
+ assert.deepEqual(
48
+ killed.map((one) => Long.fromValue(one).toString()),
49
+ [Long.fromValue(id).toString()]
50
+ )
51
+ assert.ok(killed.every((one) => one instanceof Long))
52
+ })
53
+
54
+ it('should kill no cursor that is closed already', async () => {
55
+ const { streams: read, killed } = streams(0)
56
+
57
+ await read.stream({ criteria: {}, options: {} })
58
+
59
+ assert.deepEqual(killed, [])
60
+ })
@@ -1,33 +1,85 @@
1
- 'use strict'
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const { translate } = require('../src/translate')
4
+ import { translate } from '../src/translate.js'
4
5
 
5
6
  describe('options', () => {
6
7
  it('should translate omit', () => {
7
8
  const options = { omit: 10 }
8
9
  const query = translate({ options })
9
10
 
10
- expect(query.options.skip).toStrictEqual(options.omit)
11
+ assert.deepStrictEqual(query.options.skip, options.omit)
11
12
  })
12
13
 
13
14
  it('should translate limit', () => {
14
15
  const options = { limit: 10 }
15
16
  const query = translate({ options })
16
17
 
17
- expect(query.options.limit).toStrictEqual(options.limit)
18
+ assert.deepStrictEqual(query.options.limit, options.limit)
18
19
  })
19
20
 
20
21
  it('should translate sort', () => {
21
- const options = { sort: [['a', 'asc'], ['b', 'desc'], ['id', 'asc']] }
22
+ const options = {
23
+ sort: [
24
+ ['a', 'asc'],
25
+ ['b', 'desc'],
26
+ ['id', 'asc']
27
+ ]
28
+ }
22
29
  const query = translate({ options })
23
30
 
24
- expect(query.options.sort).toStrictEqual([['a', 1], ['b', -1], ['_id', 1]])
31
+ assert.deepStrictEqual(query.options.sort, [
32
+ ['a', 1],
33
+ ['b', -1],
34
+ ['_id', 1]
35
+ ])
25
36
  })
26
37
 
27
38
  it('should translate projection', () => {
28
39
  const options = { projection: ['a', 'b', 'c', 'id'] }
29
40
  const query = translate({ options })
30
41
 
31
- expect(query.options.projection).toStrictEqual({ a: 1, b: 1, c: 1, _id: 1 })
42
+ assert.deepStrictEqual(query.options.projection, { a: 1, b: 1, c: 1, _id: 1 })
43
+ })
44
+ })
45
+
46
+ describe('dates', () => {
47
+ const comparison = (selector, operator, value) => ({
48
+ type: 'COMPARISON',
49
+ operator,
50
+ left: { type: 'SELECTOR', selector },
51
+ right: { type: 'VALUE', value }
52
+ })
53
+
54
+ it('should compare a date property against a date', () => {
55
+ const criteria = comparison('settled', '<', '2026-09-05T10:00:00.000Z')
56
+ const query = translate({ criteria }, ['settled'])
57
+
58
+ assert.ok(query.criteria.settled.$lt instanceof Date)
59
+ assert.strictEqual(
60
+ query.criteria.settled.$lt.toISOString(),
61
+ '2026-09-05T10:00:00.000Z'
62
+ )
63
+ })
64
+
65
+ it('should convert every value of a list', () => {
66
+ const criteria = comparison('settled', '=in=', ['2026-09-05T10:00:00.000Z'])
67
+ const query = translate({ criteria }, ['settled'])
68
+
69
+ assert.ok(query.criteria.settled.$in[0] instanceof Date)
70
+ })
71
+
72
+ it('should leave a property that is not a date', () => {
73
+ const criteria = comparison('endpoint', '==', 'a.b.c')
74
+ const query = translate({ criteria }, ['settled'])
75
+
76
+ assert.deepStrictEqual(query.criteria, { endpoint: { $eq: 'a.b.c' } })
77
+ })
78
+
79
+ it('should leave every criterion where the entity declares no date', () => {
80
+ const criteria = comparison('settled', '<', '2026-09-05T10:00:00.000Z')
81
+ const query = translate({ criteria })
82
+
83
+ assert.strictEqual(query.criteria.settled.$lt, '2026-09-05T10:00:00.000Z')
32
84
  })
33
85
  })
@@ -6,26 +6,32 @@ import type {
6
6
  FindOneAndReplaceOptions,
7
7
  FindOneAndUpdateOptions,
8
8
  FindOptions,
9
- UpdateFilter,
9
+ UpdateFilter
10
10
  } from 'mongodb'
11
11
 
12
12
  import type { Connector } from '@toa.io/core'
13
- import type { Record } from './record'
13
+ import type { Record } from './record.d.ts'
14
14
 
15
- declare namespace toa.mongodb{
15
+ declare namespace toa.mongodb {
16
+ interface Connection extends Connector {
17
+ get(query: Filter<Record>, options?: FindOptions<Record>): Promise<Record>
16
18
 
17
- interface Connection extends Connector{
18
- get (query: Filter<Record>, options?: FindOptions<Record>): Promise<Record>
19
+ find(query: Filter<Record>, options?: FindOptions<Record>): Promise<Record[]>
19
20
 
20
- find (query: Filter<Record>, options?: FindOptions<Record>): Promise<Record[]>
21
+ add(record: Record): Promise<boolean>
21
22
 
22
- add (record: Record): Promise<boolean>
23
+ addMany(records: Record[]): Promise<boolean>
23
24
 
24
- addMany (records: Record[]): Promise<boolean>
25
+ replace(
26
+ query: Filter<Record>,
27
+ record: UpdateFilter<Record>,
28
+ options?: FindOneAndReplaceOptions
29
+ ): Promise<any>
25
30
 
26
- replace (query: Filter<Record>, record: UpdateFilter<Record>, options?: FindOneAndReplaceOptions): Promise<any>
27
-
28
- update (query: Filter<Record>, update: UpdateFilter<Record>, options?: FindOneAndUpdateOptions): Promise<any>
31
+ update(
32
+ query: Filter<Record>,
33
+ update: UpdateFilter<Record>,
34
+ options?: FindOneAndUpdateOptions
35
+ ): Promise<any>
29
36
  }
30
-
31
37
  }
@@ -1,10 +1,8 @@
1
1
  import type { Pointer as Base } from '@toa.io/pointer/types'
2
2
 
3
3
  declare namespace toa.mongodb {
4
-
5
- interface Pointer extends Base {
6
- db: string
7
- collection: string
8
- }
9
-
4
+ interface Pointer extends Base {
5
+ db: string
6
+ collection: string
7
+ }
10
8
  }
package/types/record.d.ts CHANGED
@@ -1,12 +1,10 @@
1
1
  declare namespace toa.mongodb {
2
+ interface Record {
3
+ _id: string
4
+ VERSION: number
2
5
 
3
- interface Record {
4
- _id: string
5
- _version: number
6
-
7
- [key: string]: any
8
- }
9
-
6
+ [key: string]: any
7
+ }
10
8
  }
11
9
 
12
10
  export type Record = toa.mongodb.Record
package/src/collection.js DELETED
@@ -1,71 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('@toa.io/core')
4
-
5
- class Collection extends Connector {
6
- #client
7
- #collection
8
-
9
- constructor (client) {
10
- super()
11
-
12
- this.#client = client
13
-
14
- this.depends(client)
15
- }
16
-
17
- async open () {
18
- this.#collection = this.#client.collection
19
- }
20
-
21
- /** @hot */
22
- async get (query, options) {
23
- return /** @type {toa.mongodb.Record} */ this.#collection.findOne(query, options)
24
- }
25
-
26
- /** @hot */
27
- async find (query, options) {
28
- return this.#collection.find(query, options).toArray()
29
- }
30
-
31
- async stream (query, options, transform) {
32
- return this.#collection.find(query, options).stream({ transform })
33
- }
34
-
35
- /** @hot */
36
- async add (record) {
37
- return await this.#collection.insertOne(record)
38
- }
39
-
40
- /** @hot */
41
- async replace (query, record, options) {
42
- return await this.#collection.findOneAndReplace(query, record, options)
43
- }
44
-
45
- /** @hot */
46
- async update (query, update, options) {
47
- return this.#collection.findOneAndUpdate(query, update, options)
48
- }
49
-
50
- async index (keys, options) {
51
- return this.#collection.createIndex(keys, options)
52
- }
53
-
54
- async indexes () {
55
- try {
56
- const array = await this.#collection.listIndexes().toArray()
57
-
58
- return array.map(({ name }) => name).filter((name) => name !== '_id_')
59
- } catch {
60
- return []
61
- }
62
- }
63
-
64
- async dropIndexes (names) {
65
- const all = names.map((name) => this.#collection.dropIndex(name))
66
-
67
- return Promise.all(all)
68
- }
69
- }
70
-
71
- exports.Collection = Collection
package/src/deployment.js DELETED
@@ -1,22 +0,0 @@
1
- 'use strict'
2
-
3
- const { createVariables } = require('@toa.io/pointer')
4
-
5
- const deployment = (instances, annotation) => {
6
- const requests = instances.map((instance) => createRequest(instance))
7
- const variables = createVariables(ID, annotation, requests)
8
-
9
- return { variables }
10
- }
11
-
12
- function createRequest (instance) {
13
- return {
14
- group: instance.locator.label,
15
- selectors: [instance.locator.id]
16
- }
17
- }
18
-
19
- const ID = 'mongodb'
20
-
21
- exports.ID = ID
22
- exports.deployment = deployment