monastery 3.5.7 → 3.5.8

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/changelog.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [3.5.8](https://github.com/boycce/monastery/compare/3.5.6...3.5.8) (2025-12-23)
6
+
5
7
  ### [3.5.7](https://github.com/boycce/monastery/compare/3.5.6...3.5.7) (2025-09-16)
6
8
 
7
9
  ### [3.5.6](https://github.com/boycce/monastery/compare/3.5.5...3.5.6) (2025-06-15)
@@ -304,7 +304,7 @@ You are able to define custom error messages for each field rule.
304
304
 
305
305
  ### Operation hooks
306
306
 
307
- You are able provide an array of promises to the following model operation hooks. `this` is the operation details.
307
+ You are able provide an array of promises to the following model operation hooks.
308
308
 
309
309
  ```js
310
310
  {
@@ -318,7 +318,7 @@ You are able provide an array of promises to the following model operation hooks
318
318
  beforeInsertUpdate: [await function(data) {}],
319
319
  beforeUpdate: [await function(data) {}],
320
320
  beforeRemove: [await function() {}],
321
- beforeValidate: [await function(data) {}],
321
+ beforeValidate: [await function(data) {}], // runs prior to beforeInsert/Update
322
322
 
323
323
  // You can also return an object which will be passed to the next hook in the chain, or if it's the last hook, returned as the result:
324
324
  afterFind: [await function(data) {
@@ -328,6 +328,20 @@ You are able provide an array of promises to the following model operation hooks
328
328
  }
329
329
  ```
330
330
 
331
+ The context (`this`) inside each hook will contain extra information relating to the operation, for an example, a basic update operation may contain the following fields:
332
+ ```js
333
+ {
334
+ query: { _id: new ObjectId("68d66a33ae584af64197c971") },
335
+ data: { name: 'John Doe' },
336
+ limit: 1,
337
+ sort: { createdAt: -1 },
338
+ type: 'update',
339
+ model: {...}, // Model reference,
340
+ skipValidation: [],
341
+ // ...
342
+ }
343
+ ```
344
+
331
345
  ### Methods
332
346
 
333
347
  You are able define reusable methods in the definition which is handy for storing related functions.
@@ -5,7 +5,7 @@ parent: Model
5
5
 
6
6
  # `model.insert`
7
7
 
8
- Validate and insert document(s) in a collection and calls model hooks: `beforeInsert`, `afterInsert`
8
+ Validate and insert document(s) in a collection and calls model hooks: `beforeValidate`, `beforeInsert`, `afterInsert`
9
9
 
10
10
  ### Arguments
11
11
 
@@ -5,14 +5,14 @@ parent: Model
5
5
 
6
6
  # `model.update`
7
7
 
8
- Update document(s) in a collection and calls model hooks: `beforeUpdate`, `afterUpdate`. By default this method method updates a single document. Set the `multi` mongodb option to update all documents that match the query criteria.
8
+ Update document(s) in a collection and calls model hooks: `beforeValidate`, `beforeUpdate`, `afterUpdate`. By default this method method updates a single document. Set the `multi` mongodb option to update all documents that match the query criteria.
9
9
 
10
10
  ### Arguments
11
11
 
12
12
  `options` *(object)*
13
13
 
14
14
  - `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/v5.0/tutorial/query-documents/), or id
15
- - [`data`](#data) *(object)* - data that's validated against the model fields (always wrapped in `{ $set: .. }`)
15
+ - [`data`](#data) *(object)* - data that's validated against the model fields, which eventually gets wrapped in `{ $set: .. }`
16
16
  - [[`blacklist`](#blacklisting)]*(array\|string\|false)*: augment `definition.updateBL`. `false` will remove all blacklisting
17
17
  - [`project`] *(string\|array\|object)*: project these fields, ignores blacklisting
18
18
  - [`skipValidation`] *(string\|array\|boolean)*: skip validation for these fields, or pass `true` to skip all fields and validation hooks
@@ -42,7 +42,7 @@ await user.update({ query: { name: 'foo' }, data: { name: 'bar' }})
42
42
 
43
43
  ### Data
44
44
 
45
- Data that's validated against the model fields (always wrapped in `{ $set: .. }`). Key names can be in dot or bracket notation which is handy for HTML FormData.
45
+ Data that's validated against the model fields, which eventually gets wrapped in `{ $set: .. }`. Key names can be in dot or bracket notation which is handy for HTML FormData.
46
46
 
47
47
  You can also pass `options.$set` or any other mongodb update operation instead of `options.data`, which bypasses validation, e.g.
48
48
 
package/lib/rules.js CHANGED
@@ -83,7 +83,7 @@ module.exports = {
83
83
  },
84
84
  fn: function(x) {
85
85
  if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return true
86
- return typeof x === 'number' && (parseInt(x) === x)
86
+ return typeof x === 'number' && (parseInt(x + '') === x)
87
87
  },
88
88
  },
89
89
  isNumber: {
package/lib/util.js CHANGED
@@ -71,22 +71,28 @@ module.exports = {
71
71
  forceArray: function(value) {
72
72
  return this.isArray(value)? value : [value]
73
73
  },
74
+
75
+ /**
76
+ * @typedef {string} HexString
77
+ * @description A hex string of 24 characters
78
+ */
74
79
 
75
80
  /**
76
81
  * Casts to ObjectId
77
- * @param {string|ObjectId} str - string = hex string
78
- * @return {ObjectId}
82
+ * @param {HexString|ObjectId} [value] - empty string = new ObjectId('')
79
83
  */
80
- id: function(str) {
81
- if (str == null) return new ObjectId()
82
- return typeof str === 'string' ? ObjectId.createFromHexString(str) : str
84
+ id: function(value) {
85
+ if (value === undefined) return new ObjectId()
86
+ else if (typeof value === 'string') return ObjectId.createFromHexString(value)
87
+ else if (value instanceof ObjectId) return value
88
+ else throw new Error(`Monastery: invalid value "${value}" passed to id(value?: HexString|ObjectId).`)
83
89
  },
84
90
 
85
91
  inArray: (array, key, value) => {
86
92
  /**
87
93
  * Property match inside an array of objects
88
94
  * (For a string/number value check just use [].includes(x))
89
- * @param {string} <key> - optional to match across on a colleciton of objects
95
+ * @param {string} [key] - optional to match across on a colleciton of objects
90
96
  * @param {any} value
91
97
  */
92
98
  if (!array || typeof key == 'undefined') return false
@@ -361,7 +367,7 @@ module.exports = {
361
367
  /**
362
368
  * Runs functions in series
363
369
  *
364
- * @param {function(err, result)[]} tasks - array of functions
370
+ * @param {((err: any, result: any) => any)[]} tasks - array of functions
365
371
  * @param {string} hookName - e.g. 'afterFind'
366
372
  * @param {any} data - data to pass to the first function
367
373
  *
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "monastery",
3
3
  "description": "⛪ A simple, straightforward MongoDB ODM",
4
4
  "author": "Ricky Boyce",
5
- "version": "3.5.7",
5
+ "version": "3.5.8",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
@@ -6,7 +6,7 @@ declare class Manager {
6
6
  arrayWithSchema(array: any, schema: any): any;
7
7
  close(): Promise<any>;
8
8
  _state: string;
9
- command(...args: any[]): any;
9
+ rawCommand(...args: any[]): any;
10
10
  connectionString(uri: any, databaseName: any): any;
11
11
  get(name: any, options: any): any;
12
12
  id(str: any): import("bson").ObjectId;
@@ -1 +1 @@
1
- {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../lib/manager.js"],"names":[],"mappings":";AAYA,gEAuFC;;IAvFD,8CAuFC;IAED,sCAEC;IAED,8CAGC;IAED,sBAoBC;IAfqB,eAAuB;IAiB7C,6BAKC;IAED,mDA2BC;IAED,kCAYC;IAED,sCAEC;IAED,0BAEC;IAED,8CAsCC;IAED,+BAiBC;IAED,8BAuBC;IAED,sBAsBC;IAfG,QAA0B;IAiB9B,gFAEC;IAED,oBAAuB;IAEvB,2EAA8B;IAE9B,uBAEC"}
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../lib/manager.js"],"names":[],"mappings":";AAYA,gEA4FC;;IA5FD,8CA4FC;IAED,sCAEC;IAED,8CAGC;IAED,sBAoBC;IAfqB,eAAuB;IAiB7C,gCAKC;IAED,mDA2BC;IAED,kCAYC;IAED,sCAEC;IAED,0BAEC;IAED,8CAsCC;IAED,+BAiBC;IAED,8BAuBC;IAED,sBAsBC;IAfG,QAA0B;IAiB9B,gFAEC;IAED,oBAAuB;IAEvB,2EAA8B;IAE9B,uBAEC"}