adapt-authoring-mongodb 3.1.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.
@@ -1,32 +1,17 @@
1
1
  name: Release
2
+
2
3
  on:
3
4
  push:
4
5
  branches:
5
6
  - master
6
7
 
8
+ permissions:
9
+ contents: write
10
+ issues: write
11
+ pull-requests: write
12
+ id-token: write
13
+ packages: write
14
+
7
15
  jobs:
8
16
  release:
9
- name: Release
10
- runs-on: ubuntu-latest
11
- permissions:
12
- contents: write # to be able to publish a GitHub release
13
- issues: write # to be able to comment on released issues
14
- pull-requests: write # to be able to comment on released pull requests
15
- id-token: write # to enable use of OIDC for trusted publishing and npm provenance
16
- steps:
17
- - name: Checkout
18
- uses: actions/checkout@v3
19
- with:
20
- fetch-depth: 0
21
- - name: Setup Node.js
22
- uses: actions/setup-node@v3
23
- with:
24
- node-version: 'lts/*'
25
- - name: Update npm
26
- run: npm install -g npm@latest
27
- - name: Install dependencies
28
- run: npm install
29
- - name: Release
30
- env:
31
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32
- run: npx semantic-release
17
+ uses: adaptlearning/semantic-release-config/.github/workflows/release.yml@master
@@ -12,7 +12,6 @@ import { parseObjectId } from './utils/parseObjectId.js'
12
12
  class MongoDBModule extends AbstractModule {
13
13
  /** @override */
14
14
  async init () {
15
- await this.app.waitForModule('config')
16
15
  /**
17
16
  * Reference to the MongDB client
18
17
  * @type {external:MongoDBMongoClient}
@@ -159,6 +158,24 @@ class MongoDBModule extends AbstractModule {
159
158
  }
160
159
  }
161
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
+
162
179
  /**
163
180
  * Updates an existing object in the database
164
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
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Finds documents with duplicate values for the given index fields.
3
- * @param {import('mongodb').Collection} collection The MongoDB collection to search
3
+ * @param {external:MongoDBCollection} collection The MongoDB collection to search
4
4
  * @param {String|Object} fieldOrSpec The index field spec (string key or object with field:direction pairs)
5
5
  * @returns {Promise<Array<{keyValue: Object, _ids: Array}>>} Array of duplicate groups
6
6
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-mongodb",
3
- "version": "3.1.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",
@@ -8,46 +8,19 @@
8
8
  "main": "index.js",
9
9
  "repository": "github:adapt-security/adapt-authoring-mongodb",
10
10
  "dependencies": {
11
- "adapt-authoring-core": "^2.0.0",
11
+ "adapt-authoring-core": "^3.0.0",
12
12
  "mongodb": "^7.0.0"
13
13
  },
14
14
  "peerDependencies": {
15
- "adapt-authoring-config": "^1.3.0",
16
- "adapt-authoring-core": "^2.0.0",
15
+ "adapt-authoring-core": "^3.0.0",
17
16
  "adapt-authoring-jsonschema": "^1.2.2"
18
17
  },
19
18
  "devDependencies": {
20
- "@semantic-release/git": "^10.0.1",
21
- "conventional-changelog-eslint": "^6.0.0",
22
- "semantic-release": "^25.0.2",
19
+ "@adaptlearning/semantic-release-config": "^1.0.0",
23
20
  "standard": "^17.1.0"
24
21
  },
25
22
  "release": {
26
- "plugins": [
27
- [
28
- "@semantic-release/commit-analyzer",
29
- {
30
- "preset": "eslint"
31
- }
32
- ],
33
- [
34
- "@semantic-release/release-notes-generator",
35
- {
36
- "preset": "eslint"
37
- }
38
- ],
39
- "@semantic-release/npm",
40
- "@semantic-release/github",
41
- [
42
- "@semantic-release/git",
43
- {
44
- "assets": [
45
- "package.json"
46
- ],
47
- "message": "Chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
48
- }
49
- ]
50
- ]
23
+ "extends": "@adaptlearning/semantic-release-config"
51
24
  },
52
25
  "scripts": {
53
26
  "test": "node --test 'tests/**/*.spec.js'"
@@ -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
  })