adapt-authoring-api 1.1.0 → 1.2.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.
@@ -28,5 +28,14 @@
28
28
  "NO_SCHEMA_DEF": {
29
29
  "description": "No json schema has been defined",
30
30
  "statusCode": 404
31
+ },
32
+ "TOO_MANY_RESULTS": {
33
+ "data": {
34
+ "actual": "Number of actual results",
35
+ "expected": "Number of expected results",
36
+ "query": "The query"
37
+ },
38
+ "description": "Too many results were returned",
39
+ "statusCode": 500
31
40
  }
32
41
  }
@@ -566,6 +566,21 @@ class AbstractApiModule extends AbstractModule {
566
566
  return this.cache.get(q, options, mongoOptions)
567
567
  }
568
568
 
569
+ /**
570
+ * Retrieves a single document from the DB. If multiple results are returned from the query, an error is thrown.
571
+ * @param {Object} query Attributes to use to filter DB documents
572
+ * @param {FindOptions} options Function options
573
+ * @param {external:MongoDBFindOptions} mongoOptions Options to be passed to the MongoDB function
574
+ * @return {Promise} Resolves with the single DB document
575
+ */
576
+ async findOne (query, options, mongoOptions) {
577
+ const results = await this.find(query, options, mongoOptions)
578
+ if (results.length > 1) {
579
+ throw this.app.errors.TOO_MANY_RESULTS.setData({ actual: results.length, expected: 1, query })
580
+ }
581
+ return results[0]
582
+ }
583
+
569
584
  /**
570
585
  * Updates an existing document in the DB
571
586
  * @param {Object} query Attributes to use to filter DB documents
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-api",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Abstract module for creating APIs",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-api",
6
6
  "license": "GPL-3.0",