@toa.io/storages.mongodb 1.0.0-alpha.158 → 1.0.0-alpha.161

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/storage.js +18 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/storages.mongodb",
3
- "version": "1.0.0-alpha.158",
3
+ "version": "1.0.0-alpha.161",
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.93",
28
28
  "saslprep": "1.0.3"
29
29
  },
30
- "gitHead": "958edd392a05ed179e174a10a1c132d4e49daa01"
30
+ "gitHead": "0777176febf057c5c351bbbc3de02ffad8c37741"
31
31
  }
package/src/storage.js CHANGED
@@ -180,8 +180,8 @@ class Storage extends Connector {
180
180
 
181
181
  if (this.#entity.unique !== undefined) {
182
182
  for (const [name, fields] of Object.entries(this.#entity.unique)) {
183
- const sparse = this.checkFields(fields)
184
- const unique = await this.uniqueIndex(name, fields, sparse)
183
+ const optional = this.getOptional(fields)
184
+ const unique = await this.uniqueIndex(name, fields, optional)
185
185
 
186
186
  indexes.push(unique)
187
187
  }
@@ -193,9 +193,12 @@ class Storage extends Connector {
193
193
  const fields = Object.fromEntries(Object.entries(declaration)
194
194
  .map(([name, type]) => [name, INDEX_TYPES[type] ?? type]))
195
195
 
196
- const sparse = this.checkFields(Object.keys(fields))
196
+ const optional = this.getOptional(Object.keys(fields))
197
+ const options = { name, sparse: optional.length > 0 }
197
198
 
198
- await this.#collection.createIndex(fields, { name, sparse })
199
+ console.info('Creating index', { fields, options })
200
+
201
+ await this.#collection.createIndex(fields, options)
199
202
  .catch((e) => this.#logs.warn('Index creation failed', { name, fields, error: e }))
200
203
 
201
204
  indexes.push(name)
@@ -205,7 +208,7 @@ class Storage extends Connector {
205
208
  await this.removeObsoleteIndexes(indexes)
206
209
  }
207
210
 
208
- async uniqueIndex (name, properties, sparse = false) {
211
+ async uniqueIndex (name, properties, optional) {
209
212
  const fields = properties.reduce((acc, property) => {
210
213
  acc[property] = 1
211
214
  return acc
@@ -213,7 +216,14 @@ class Storage extends Connector {
213
216
 
214
217
  name = 'unique_' + name
215
218
 
216
- await this.#collection.createIndex(fields, { name, unique: true, sparse })
219
+ const options = { name, unique: true }
220
+
221
+ if (optional.length > 0)
222
+ options.partialFilterExpression = Object.fromEntries(optional.map((field) => [field, { $exists: true }]))
223
+
224
+ console.info('Creating unique index', { name, fields, options })
225
+
226
+ await this.#collection.createIndex(fields, options)
217
227
  .catch((e) => this.#logs.warn('Unique index creation failed', { name, fields, error: e }))
218
228
 
219
229
  return name
@@ -240,7 +250,7 @@ class Storage extends Connector {
240
250
  }
241
251
  }
242
252
 
243
- checkFields (fields) {
253
+ getOptional (fields) {
244
254
  const optional = []
245
255
 
246
256
  for (const field of fields) {
@@ -251,12 +261,7 @@ class Storage extends Connector {
251
261
  optional.push(field)
252
262
  }
253
263
 
254
- if (optional.length > 0) {
255
- this.#logs.info('Index fields are optional, creating sparse index', { fields: optional })
256
-
257
- return true
258
- } else
259
- return false
264
+ return optional
260
265
  }
261
266
 
262
267
  debug (method, attributes) {