@toa.io/storages.mongodb 1.0.0-alpha.13 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/storages.mongodb",
3
- "version": "1.0.0-alpha.13",
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.13",
23
- "@toa.io/conveyor": "1.0.0-alpha.13",
24
- "@toa.io/core": "1.0.0-alpha.13",
25
- "@toa.io/generic": "1.0.0-alpha.13",
26
- "@toa.io/pointer": "1.0.0-alpha.13",
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": "f779cc4a98abe96e4e928b918f9521826403536b"
30
+ "gitHead": "8aa52cb97021695885c8dbe64beca26c9665fc8f"
31
31
  }
package/src/client.js CHANGED
@@ -57,7 +57,7 @@ class Client extends Connector {
57
57
  */
58
58
  async open () {
59
59
  const urls = await this.resolveURLs()
60
- const db = process.env.CONTEXT ?? 'toa-dev'
60
+ const db = this.resolveDB()
61
61
  const collection = this.locator.lowercase
62
62
 
63
63
  this.key = getKey(db, urls)
@@ -116,6 +116,22 @@ 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
137
  function getKey (db, urls) {
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,13 +76,16 @@ class Storage extends Connector {
75
76
  }
76
77
  } catch (error) {
77
78
  if (error.code === ERR_DUPLICATE_KEY) {
78
- const keys = error.keyValue ? Object.keys(error.keyValue) : undefined
79
79
 
80
- if (keys === undefined) {
81
- console.error(error)
82
- }
80
+ const id = error.keyPattern === undefined
81
+ ? error.message.includes(' index: _id_ ') // AWS DocumentDB
82
+ : error.keyPattern._id === 1
83
83
 
84
- return new exceptions.DuplicateException(keys)
84
+ if (id) {
85
+ return false
86
+ } else {
87
+ throw new exceptions.DuplicateException()
88
+ }
85
89
  } else {
86
90
  throw error
87
91
  }
@@ -94,6 +98,11 @@ class Storage extends Connector {
94
98
  options
95
99
  } = translate(query)
96
100
 
101
+ if (!('_deleted' in changeset) || changeset._deleted === null) {
102
+ delete criteria._deleted
103
+ changeset._deleted = null
104
+ }
105
+
97
106
  const update = {
98
107
  $set: { ...changeset },
99
108
  $inc: { _version: 1 }
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 = { criteria: {}, options: {} }
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.criteria !== undefined) result.criteria = parse.criteria(query.criteria)
13
- if (query.options !== undefined) result.options = parse.options(query.options)
14
- if (query.id !== undefined) result.criteria._id = query.id
15
- if (query.version !== undefined) result.criteria._version = query.version
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
  }