adapt-authoring-mongodb 3.2.0 → 3.3.0

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.
@@ -158,6 +158,24 @@ class MongoDBModule extends AbstractModule {
158
158
  }
159
159
  }
160
160
 
161
+ /**
162
+ * Counts the documents matching a query. Normalises ObjectId strings the same way as {@link MongoDBModule#find} so callers get a count consistent with the documents a find would return.
163
+ * @param {String} collectionName The name of the MongoDB collection
164
+ * @param {Object} query
165
+ * @param {external:MongoDBCountDocumentsOptions} options Options to pass to the MongoDB driver
166
+ * @return {Promise} Resolves with the document count
167
+ * @see https://mongodb.github.io/node-mongodb-native/4.2/classes/Collection.html#countDocuments
168
+ */
169
+ async count (collectionName, query, options) {
170
+ convertObjectIds(query)
171
+ try {
172
+ return await this.getCollection(collectionName).countDocuments(query, options)
173
+ } catch (e) {
174
+ this.log('error', `failed to count docs, ${e.message}`)
175
+ throw this.getError(collectionName, 'count', e)
176
+ }
177
+ }
178
+
161
179
  /**
162
180
  * Updates an existing object in the database
163
181
  * @param {String} collectionName The name of the MongoDB collection
package/lib/typedefs.js CHANGED
@@ -7,6 +7,12 @@
7
7
  * @external MongoDBCollection
8
8
  * @see {@link https://mongodb.github.io/node-mongodb-native/4.2/api/classes/Collection.html}
9
9
  */
10
+ /**
11
+ * Options passed to the countDocuments function
12
+ * @memberof mongodb
13
+ * @external MongoDBCountDocumentsOptions
14
+ * @see {@link https://mongodb.github.io/node-mongodb-native/4.2/interfaces/CountDocumentsOptions.html}
15
+ */
10
16
  /**
11
17
  * Options passed to the createIndex function
12
18
  * @memberof mongodb
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-mongodb",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "Module for saving to a MongoDB instance",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-mongodb",
6
6
  "license": "GPL-3.0",
@@ -101,4 +101,26 @@ describe('MongoDBModule', () => {
101
101
  assert.equal(result.code, 'MONGO_ERROR')
102
102
  })
103
103
  })
104
+
105
+ describe('#count()', () => {
106
+ it('should convert ObjectId strings in the query before counting', async () => {
107
+ const { instance } = createInstance()
108
+ const countDocuments = mock.fn(async () => 3)
109
+ instance.getCollection = mock.fn(() => ({ countDocuments }))
110
+ const query = { _id: '507f1f77bcf86cd799439011' }
111
+ const result = await instance.count('courses', query)
112
+ assert.equal(result, 3)
113
+ const [received] = countDocuments.mock.calls[0].arguments
114
+ assert.equal(received._id.constructor.name, 'ObjectId')
115
+ assert.equal(received._id.toString(), '507f1f77bcf86cd799439011')
116
+ })
117
+
118
+ it('should wrap driver errors via getError', async () => {
119
+ const { instance } = createInstance()
120
+ instance.getCollection = mock.fn(() => ({
121
+ countDocuments: mock.fn(async () => { throw new Error('boom') })
122
+ }))
123
+ await assert.rejects(() => instance.count('courses', {}), e => e.code === 'MONGO_ERROR')
124
+ })
125
+ })
104
126
  })