@toa.io/storages.mongodb 1.0.0-alpha.2 → 1.0.0-alpha.4
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 +7 -7
- package/src/record.js +9 -3
- package/src/storage.js +35 -10
- package/test/record.test.js +32 -17
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.4",
|
|
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",
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@toa.io/console": "1.0.0-alpha.
|
|
23
|
-
"@toa.io/conveyor": "1.0.0-alpha.
|
|
24
|
-
"@toa.io/core": "1.0.0-alpha.
|
|
25
|
-
"@toa.io/generic": "1.0.0-alpha.
|
|
26
|
-
"@toa.io/pointer": "1.0.0-alpha.
|
|
22
|
+
"@toa.io/console": "1.0.0-alpha.4",
|
|
23
|
+
"@toa.io/conveyor": "1.0.0-alpha.4",
|
|
24
|
+
"@toa.io/core": "1.0.0-alpha.4",
|
|
25
|
+
"@toa.io/generic": "1.0.0-alpha.4",
|
|
26
|
+
"@toa.io/pointer": "1.0.0-alpha.4",
|
|
27
27
|
"mongodb": "6.3.0",
|
|
28
28
|
"saslprep": "1.0.3"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "80a91c0d9c167484247a91e69a0c0a3c344f90d0"
|
|
31
31
|
}
|
package/src/record.js
CHANGED
|
@@ -5,9 +5,12 @@
|
|
|
5
5
|
* @returns {toa.mongodb.Record}
|
|
6
6
|
*/
|
|
7
7
|
const to = (entity) => {
|
|
8
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
id,
|
|
10
|
+
...rest
|
|
11
|
+
} = entity
|
|
9
12
|
|
|
10
|
-
return /** @type {toa.mongodb.Record} */ { _id: id,
|
|
13
|
+
return /** @type {toa.mongodb.Record} */ { _id: id, ...rest }
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
/**
|
|
@@ -17,7 +20,10 @@ const to = (entity) => {
|
|
|
17
20
|
const from = (record) => {
|
|
18
21
|
if (record === undefined || record === null) return null
|
|
19
22
|
|
|
20
|
-
const {
|
|
23
|
+
const {
|
|
24
|
+
_id,
|
|
25
|
+
...rest
|
|
26
|
+
} = record
|
|
21
27
|
|
|
22
28
|
return { id: _id, ...rest }
|
|
23
29
|
}
|
package/src/storage.js
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
const { Connector } = require('@toa.io/core')
|
|
4
4
|
|
|
5
5
|
const { translate } = require('./translate')
|
|
6
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
to,
|
|
8
|
+
from
|
|
9
|
+
} = require('./record')
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
12
|
* @implements {toa.core.Storage}
|
|
@@ -24,7 +27,10 @@ class Storage extends Connector {
|
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
async get (query) {
|
|
27
|
-
const {
|
|
30
|
+
const {
|
|
31
|
+
criteria,
|
|
32
|
+
options
|
|
33
|
+
} = translate(query)
|
|
28
34
|
|
|
29
35
|
const record = await this.#connection.get(criteria, options)
|
|
30
36
|
|
|
@@ -32,7 +38,10 @@ class Storage extends Connector {
|
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
async find (query) {
|
|
35
|
-
const {
|
|
41
|
+
const {
|
|
42
|
+
criteria,
|
|
43
|
+
options
|
|
44
|
+
} = translate(query)
|
|
36
45
|
const recordset = await this.#connection.find(criteria, options)
|
|
37
46
|
|
|
38
47
|
return recordset.map((item) => from(item))
|
|
@@ -45,28 +54,44 @@ class Storage extends Connector {
|
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
async set (entity) {
|
|
48
|
-
const criteria = {
|
|
57
|
+
const criteria = {
|
|
58
|
+
_id: entity.id,
|
|
59
|
+
_version: entity._version - 1
|
|
60
|
+
}
|
|
49
61
|
const result = await this.#connection.replace(criteria, to(entity))
|
|
50
62
|
|
|
51
63
|
return result !== null
|
|
52
64
|
}
|
|
53
65
|
|
|
54
66
|
async store (entity) {
|
|
55
|
-
if (entity._version ===
|
|
56
|
-
|
|
67
|
+
if (entity._version === 1) {
|
|
68
|
+
return this.add(entity)
|
|
69
|
+
} else {
|
|
70
|
+
return this.set(entity)
|
|
71
|
+
}
|
|
57
72
|
}
|
|
58
73
|
|
|
59
74
|
async upsert (query, changeset, insert) {
|
|
60
|
-
const {
|
|
61
|
-
|
|
75
|
+
const {
|
|
76
|
+
criteria,
|
|
77
|
+
options
|
|
78
|
+
} = translate(query)
|
|
79
|
+
|
|
80
|
+
const update = {
|
|
81
|
+
$set: { ...changeset },
|
|
82
|
+
$inc: { _version: 1 }
|
|
83
|
+
}
|
|
62
84
|
|
|
63
85
|
if (insert !== undefined) {
|
|
64
86
|
delete insert._version
|
|
65
87
|
|
|
66
88
|
options.upsert = true
|
|
67
89
|
|
|
68
|
-
if (criteria._id !== undefined)
|
|
69
|
-
|
|
90
|
+
if (criteria._id !== undefined) {
|
|
91
|
+
insert._id = criteria._id
|
|
92
|
+
} else {
|
|
93
|
+
return null
|
|
94
|
+
} // this shouldn't ever happen
|
|
70
95
|
|
|
71
96
|
if (Object.keys(insert) > 0) update.$setOnInsert = insert
|
|
72
97
|
}
|
package/test/record.test.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
|
|
3
|
+
const {
|
|
4
|
+
to,
|
|
5
|
+
from
|
|
6
|
+
} = require('../src/record')
|
|
5
7
|
|
|
6
8
|
describe('to', () => {
|
|
7
9
|
it('should rename id to _id', () => {
|
|
8
10
|
/** @type {toa.core.storages.Record} */
|
|
9
|
-
const entity = {
|
|
11
|
+
const entity = {
|
|
12
|
+
id: '1',
|
|
13
|
+
_version: 0
|
|
14
|
+
}
|
|
10
15
|
const record = to(entity)
|
|
11
16
|
|
|
12
17
|
expect(record).toMatchObject({ _id: '1' })
|
|
@@ -14,37 +19,47 @@ describe('to', () => {
|
|
|
14
19
|
|
|
15
20
|
it('should not modify argument', () => {
|
|
16
21
|
/** @type {toa.core.storages.Record} */
|
|
17
|
-
const entity = {
|
|
22
|
+
const entity = {
|
|
23
|
+
id: '1',
|
|
24
|
+
_version: 0
|
|
25
|
+
}
|
|
18
26
|
|
|
19
27
|
to(entity)
|
|
20
28
|
|
|
21
|
-
expect(entity).toStrictEqual({
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
/** @type {toa.core.storages.Record} */
|
|
26
|
-
const entity = { id: '1', _version: random() }
|
|
27
|
-
const record = to(entity)
|
|
28
|
-
|
|
29
|
-
expect(record).toMatchObject({ _version: entity._version + 1 })
|
|
29
|
+
expect(entity).toStrictEqual({
|
|
30
|
+
id: '1',
|
|
31
|
+
_version: 0
|
|
32
|
+
})
|
|
30
33
|
})
|
|
31
34
|
})
|
|
32
35
|
|
|
33
36
|
describe('from', () => {
|
|
34
37
|
it('should rename _id to id', () => {
|
|
35
38
|
/** @type {toa.mongodb.Record} */
|
|
36
|
-
const record = {
|
|
39
|
+
const record = {
|
|
40
|
+
_id: '1',
|
|
41
|
+
_version: 0
|
|
42
|
+
}
|
|
37
43
|
const entity = from(record)
|
|
38
44
|
|
|
39
|
-
expect(entity).toStrictEqual({
|
|
45
|
+
expect(entity).toStrictEqual({
|
|
46
|
+
id: '1',
|
|
47
|
+
_version: 0
|
|
48
|
+
})
|
|
40
49
|
})
|
|
41
50
|
|
|
42
51
|
it('should not modify argument', () => {
|
|
43
52
|
/** @type {toa.mongodb.Record} */
|
|
44
|
-
const record = {
|
|
53
|
+
const record = {
|
|
54
|
+
_id: '1',
|
|
55
|
+
_version: 0
|
|
56
|
+
}
|
|
45
57
|
|
|
46
58
|
from(record)
|
|
47
59
|
|
|
48
|
-
expect(record).toStrictEqual({
|
|
60
|
+
expect(record).toStrictEqual({
|
|
61
|
+
_id: '1',
|
|
62
|
+
_version: 0
|
|
63
|
+
})
|
|
49
64
|
})
|
|
50
65
|
})
|