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

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.154",
3
+ "version": "1.0.0-alpha.158",
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",
@@ -20,12 +20,12 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@toa.io/conveyor": "1.0.0-alpha.93",
23
- "@toa.io/core": "1.0.0-alpha.154",
23
+ "@toa.io/core": "1.0.0-alpha.156",
24
24
  "@toa.io/generic": "1.0.0-alpha.93",
25
25
  "@toa.io/pointer": "1.0.0-alpha.143",
26
26
  "mongodb": "6.7.0",
27
27
  "openspan": "1.0.0-alpha.93",
28
28
  "saslprep": "1.0.3"
29
29
  },
30
- "gitHead": "01832fc47f9906d6a63180d5c2813db86763506a"
30
+ "gitHead": "958edd392a05ed179e174a10a1c132d4e49daa01"
31
31
  }
package/src/storage.js CHANGED
@@ -47,13 +47,25 @@ class Storage extends Connector {
47
47
  }
48
48
 
49
49
  async find (query) {
50
- const { criteria, options } = translate(query)
50
+ const { criteria, options, sample } = translate(query)
51
51
 
52
52
  criteria._deleted = null
53
53
 
54
- this.debug('find', { criteria, options })
54
+ let cursor
55
+
56
+ if (sample === undefined) {
57
+ this.debug('find', { criteria, options })
55
58
 
56
- const recordset = await this.#collection.find(criteria, options).toArray()
59
+ cursor = this.#collection.find(criteria, options)
60
+ } else {
61
+ const pipeline = toPipeline(criteria, options, sample)
62
+
63
+ this.debug('aggregate', { pipeline })
64
+
65
+ cursor = this.#collection.aggregate(pipeline)
66
+ }
67
+
68
+ const recordset = await cursor.toArray()
57
69
 
58
70
  return recordset.map((item) => from(item))
59
71
  }
@@ -255,6 +267,24 @@ class Storage extends Connector {
255
267
  }
256
268
  }
257
269
 
270
+ function toPipeline (criteria, options, sample) {
271
+ const pipeline = []
272
+
273
+ if (criteria !== undefined)
274
+ pipeline.push({ $match: criteria })
275
+
276
+ if (sample !== undefined)
277
+ pipeline.push({ $sample: { size: sample } })
278
+
279
+ if (options?.sort !== undefined)
280
+ pipeline.push({ $sort: options.sort })
281
+
282
+ if (options?.projection !== undefined)
283
+ pipeline.push({ $project: options.projection })
284
+
285
+ return pipeline
286
+ }
287
+
258
288
  const INDEX_TYPES = {
259
289
  'asc': 1,
260
290
  'desc': -1,
package/src/translate.js CHANGED
@@ -9,7 +9,8 @@ const parse = { ...require('./translate/criteria'), ...require('./translate/opti
9
9
  const translate = (query) => {
10
10
  const result = {
11
11
  criteria: query?.criteria === undefined ? {} : parse.criteria(query.criteria),
12
- options: query?.options === undefined ? {} : parse.options(query.options)
12
+ options: query?.options === undefined ? {} : parse.options(query.options),
13
+ sample: query?.options?.sample
13
14
  }
14
15
 
15
16
  if (query?.id !== undefined)