@toa.io/storages.mongodb 1.0.0-alpha.12 → 1.0.0-alpha.14
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/client.js +22 -6
- package/src/storage.js +17 -16
- package/src/translate.js +13 -5
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.14",
|
|
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.14",
|
|
23
|
+
"@toa.io/conveyor": "1.0.0-alpha.14",
|
|
24
|
+
"@toa.io/core": "1.0.0-alpha.14",
|
|
25
|
+
"@toa.io/generic": "1.0.0-alpha.14",
|
|
26
|
+
"@toa.io/pointer": "1.0.0-alpha.14",
|
|
27
27
|
"mongodb": "6.3.0",
|
|
28
28
|
"saslprep": "1.0.3"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "8aa52cb97021695885c8dbe64beca26c9665fc8f"
|
|
31
31
|
}
|
package/src/client.js
CHANGED
|
@@ -57,17 +57,17 @@ class Client extends Connector {
|
|
|
57
57
|
*/
|
|
58
58
|
async open () {
|
|
59
59
|
const urls = await this.resolveURLs()
|
|
60
|
+
const db = this.resolveDB()
|
|
61
|
+
const collection = this.locator.lowercase
|
|
60
62
|
|
|
61
|
-
this.key = getKey(urls)
|
|
63
|
+
this.key = getKey(db, urls)
|
|
62
64
|
|
|
63
65
|
INSTANCES[this.key] ??= this.createInstance(urls)
|
|
64
66
|
|
|
65
67
|
this.instance = await INSTANCES[this.key]
|
|
66
68
|
this.instance.count++
|
|
67
69
|
|
|
68
|
-
this.collection = this.instance.client
|
|
69
|
-
.db(this.locator.namespace)
|
|
70
|
-
.collection(this.locator.name)
|
|
70
|
+
this.collection = this.instance.client.db(db).collection(collection)
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
/**
|
|
@@ -116,10 +116,26 @@ class Client extends Connector {
|
|
|
116
116
|
return await resolve(ID, this.locator.id)
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @private
|
|
122
|
+
* @return {string}
|
|
123
|
+
*/
|
|
124
|
+
resolveDB () {
|
|
125
|
+
if (process.env.TOA_CONTEXT !== undefined) {
|
|
126
|
+
return process.env.TOA_CONTEXT
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (process.env.TOA_DEV === '1') {
|
|
130
|
+
return 'toa-dev'
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
throw new Error('Environment variable TOA_CONTEXT is not defined')
|
|
134
|
+
}
|
|
119
135
|
}
|
|
120
136
|
|
|
121
|
-
function getKey (urls) {
|
|
122
|
-
return urls.sort().join(' ')
|
|
137
|
+
function getKey (db, urls) {
|
|
138
|
+
return db + ':' + urls.sort().join(' ')
|
|
123
139
|
}
|
|
124
140
|
|
|
125
141
|
const OPTIONS = {
|
package/src/storage.js
CHANGED
|
@@ -44,6 +44,7 @@ class Storage extends Connector {
|
|
|
44
44
|
criteria,
|
|
45
45
|
options
|
|
46
46
|
} = translate(query)
|
|
47
|
+
|
|
47
48
|
const recordset = await this.#connection.find(criteria, options)
|
|
48
49
|
|
|
49
50
|
return recordset.map((item) => from(item))
|
|
@@ -75,38 +76,38 @@ class Storage extends Connector {
|
|
|
75
76
|
}
|
|
76
77
|
} catch (error) {
|
|
77
78
|
if (error.code === ERR_DUPLICATE_KEY) {
|
|
78
|
-
|
|
79
|
+
|
|
80
|
+
const id = error.keyPattern === undefined
|
|
81
|
+
? error.message.includes(' index: _id_ ') // AWS DocumentDB
|
|
82
|
+
: error.keyPattern._id === 1
|
|
83
|
+
|
|
84
|
+
if (id) {
|
|
85
|
+
return false
|
|
86
|
+
} else {
|
|
87
|
+
throw new exceptions.DuplicateException()
|
|
88
|
+
}
|
|
79
89
|
} else {
|
|
80
90
|
throw error
|
|
81
91
|
}
|
|
82
92
|
}
|
|
83
93
|
}
|
|
84
94
|
|
|
85
|
-
async upsert (query, changeset
|
|
95
|
+
async upsert (query, changeset) {
|
|
86
96
|
const {
|
|
87
97
|
criteria,
|
|
88
98
|
options
|
|
89
99
|
} = translate(query)
|
|
90
100
|
|
|
101
|
+
if (!('_deleted' in changeset) || changeset._deleted === null) {
|
|
102
|
+
delete criteria._deleted
|
|
103
|
+
changeset._deleted = null
|
|
104
|
+
}
|
|
105
|
+
|
|
91
106
|
const update = {
|
|
92
107
|
$set: { ...changeset },
|
|
93
108
|
$inc: { _version: 1 }
|
|
94
109
|
}
|
|
95
110
|
|
|
96
|
-
if (insert !== undefined) {
|
|
97
|
-
delete insert._version
|
|
98
|
-
|
|
99
|
-
options.upsert = true
|
|
100
|
-
|
|
101
|
-
if (criteria._id !== undefined) {
|
|
102
|
-
insert._id = criteria._id
|
|
103
|
-
} else {
|
|
104
|
-
return null
|
|
105
|
-
} // this shouldn't ever happen
|
|
106
|
-
|
|
107
|
-
if (Object.keys(insert) > 0) update.$setOnInsert = insert
|
|
108
|
-
}
|
|
109
|
-
|
|
110
111
|
options.returnDocument = 'after'
|
|
111
112
|
|
|
112
113
|
const result = await this.#connection.update(criteria, update, options)
|
package/src/translate.js
CHANGED
|
@@ -7,12 +7,20 @@ const parse = { ...require('./translate/criteria'), ...require('./translate/opti
|
|
|
7
7
|
* @returns {{criteria: Object, options: Object}}
|
|
8
8
|
*/
|
|
9
9
|
const translate = (query) => {
|
|
10
|
-
const result = {
|
|
10
|
+
const result = {
|
|
11
|
+
criteria: query?.criteria === undefined ? {} : parse.criteria(query.criteria),
|
|
12
|
+
options: query?.options === undefined ? {} : parse.options(query.options)
|
|
13
|
+
}
|
|
11
14
|
|
|
12
|
-
if (query
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
if (query?.id !== undefined) {
|
|
16
|
+
result.criteria._id = query.id
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (query?.version !== undefined) {
|
|
20
|
+
result.criteria._version = query.version
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
result.criteria._deleted = null
|
|
16
24
|
|
|
17
25
|
return result
|
|
18
26
|
}
|