@toa.io/storages.mongodb 1.0.0-alpha.228 → 1.0.0-alpha.230
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/package.json +2 -2
- package/src/storage.js +8 -0
- package/test/storage.test.js +73 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/storages.mongodb",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.230",
|
|
4
4
|
"description": "Toa MongoDB Storage Connector",
|
|
5
5
|
"author": "temich <tema.gurtovoy@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/toa-io/toa#readme",
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
"openspan": "1.0.0-alpha.173",
|
|
28
28
|
"saslprep": "1.0.3"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "ce845ae0216591ae88cb053fd0f7a7cadb29b7df"
|
|
31
31
|
}
|
package/src/storage.js
CHANGED
|
@@ -35,6 +35,11 @@ class Storage extends Connector {
|
|
|
35
35
|
async get (query) {
|
|
36
36
|
const { criteria, options } = translate(query)
|
|
37
37
|
|
|
38
|
+
// identity lookups must return deleted records, so that callers
|
|
39
|
+
// can tell a deleted entity from a missing one
|
|
40
|
+
if (query?.id === undefined && query?.options?.deleted !== true)
|
|
41
|
+
criteria._deleted = null
|
|
42
|
+
|
|
38
43
|
this.debug('findOne', { criteria, options })
|
|
39
44
|
|
|
40
45
|
const record = await this.#collection.findOne(criteria, options)
|
|
@@ -70,6 +75,9 @@ class Storage extends Connector {
|
|
|
70
75
|
async stream (query = undefined) {
|
|
71
76
|
const { criteria, options } = translate(query)
|
|
72
77
|
|
|
78
|
+
if (query?.options?.deleted !== true)
|
|
79
|
+
criteria._deleted = null
|
|
80
|
+
|
|
73
81
|
this.debug('find (stream)', { criteria, options })
|
|
74
82
|
|
|
75
83
|
return this.#collection.find(criteria, options).stream({ transform: from })
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Storage } = require('../src/storage')
|
|
4
|
+
|
|
5
|
+
let collection
|
|
6
|
+
let storage
|
|
7
|
+
|
|
8
|
+
beforeEach(async () => {
|
|
9
|
+
collection = {
|
|
10
|
+
collectionName: 'test',
|
|
11
|
+
findOne: jest.fn(async () => null),
|
|
12
|
+
find: jest.fn(() => ({ stream: () => null }))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const client = {
|
|
16
|
+
collection,
|
|
17
|
+
link: () => null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
storage = new Storage(client, { schema: { properties: {} } })
|
|
21
|
+
|
|
22
|
+
await storage.open()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
describe('get', () => {
|
|
26
|
+
it('should filter deleted', async () => {
|
|
27
|
+
await storage.get({})
|
|
28
|
+
|
|
29
|
+
expect(collection.findOne).toHaveBeenCalledWith({ _deleted: null }, {})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should filter deleted with sort', async () => {
|
|
33
|
+
await storage.get({ options: { sort: [['_created', 'desc']] } })
|
|
34
|
+
|
|
35
|
+
expect(collection.findOne)
|
|
36
|
+
.toHaveBeenCalledWith({ _deleted: null }, { sort: [['_created', -1]] })
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('should not filter deleted if queried by id', async () => {
|
|
40
|
+
const id = 'bcb6780f50e243348cad40ed6b5ef575'
|
|
41
|
+
|
|
42
|
+
await storage.get({ id })
|
|
43
|
+
|
|
44
|
+
expect(collection.findOne).toHaveBeenCalledWith({ _id: id }, {})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('should not filter deleted if requested', async () => {
|
|
48
|
+
await storage.get({ options: { deleted: true } })
|
|
49
|
+
|
|
50
|
+
expect(collection.findOne).toHaveBeenCalledWith({}, {})
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe('stream', () => {
|
|
55
|
+
it('should filter deleted', async () => {
|
|
56
|
+
await storage.stream()
|
|
57
|
+
|
|
58
|
+
expect(collection.find).toHaveBeenCalledWith({ _deleted: null }, {})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should filter deleted with sort', async () => {
|
|
62
|
+
await storage.stream({ options: { sort: [['_created', 'desc']] } })
|
|
63
|
+
|
|
64
|
+
expect(collection.find)
|
|
65
|
+
.toHaveBeenCalledWith({ _deleted: null }, { sort: [['_created', -1]] })
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('should not filter deleted if requested', async () => {
|
|
69
|
+
await storage.stream({ options: { deleted: true } })
|
|
70
|
+
|
|
71
|
+
expect(collection.find).toHaveBeenCalledWith({}, {})
|
|
72
|
+
})
|
|
73
|
+
})
|