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

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.156",
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": "687ebdf64b8fbc177e3a802440ecd6c96d6b068f"
31
31
  }
package/src/storage.js CHANGED
@@ -47,13 +47,26 @@ class Storage extends Connector {
47
47
  }
48
48
 
49
49
  async find (query) {
50
- const { criteria, options } = translate(query)
50
+ debugger
51
+ const { criteria, options, sample } = translate(query)
51
52
 
52
53
  criteria._deleted = null
53
54
 
54
- this.debug('find', { criteria, options })
55
+ let cursor
56
+
57
+ if (sample === undefined) {
58
+ this.debug('find', { criteria, options })
55
59
 
56
- const recordset = await this.#collection.find(criteria, options).toArray()
60
+ cursor = this.#collection.find(criteria, options)
61
+ } else {
62
+ const pipeline = toPipeline(criteria, options, sample)
63
+
64
+ this.debug('aggregate', { pipeline })
65
+
66
+ cursor = this.#collection.aggregate(pipeline)
67
+ }
68
+
69
+ const recordset = await cursor.toArray()
57
70
 
58
71
  return recordset.map((item) => from(item))
59
72
  }
@@ -255,6 +268,24 @@ class Storage extends Connector {
255
268
  }
256
269
  }
257
270
 
271
+ function toPipeline (criteria, options, sample) {
272
+ const pipeline = []
273
+
274
+ if (criteria !== undefined)
275
+ pipeline.push({ $match: criteria })
276
+
277
+ if (sample !== undefined)
278
+ pipeline.push({ $sample: { size: sample } })
279
+
280
+ if (options?.sort !== undefined)
281
+ pipeline.push({ $sort: options.sort })
282
+
283
+ if (options?.projection !== undefined)
284
+ pipeline.push({ $project: options.projection })
285
+
286
+ return pipeline
287
+ }
288
+
258
289
  const INDEX_TYPES = {
259
290
  'asc': 1,
260
291
  '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)