monastery 1.40.4 → 1.41.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.
package/changelog.md CHANGED
@@ -2,6 +2,10 @@
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
+ ## [1.41.0](https://github.com/boycce/monastery/compare/1.40.5...1.41.0) (2023-04-16)
6
+
7
+ ### [1.40.5](https://github.com/boycce/monastery/compare/1.40.4...1.40.5) (2023-02-22)
8
+
5
9
  ### [1.40.4](https://github.com/boycce/monastery/compare/1.40.3...1.40.4) (2022-12-21)
6
10
 
7
11
  ### [1.40.3](https://github.com/boycce/monastery/compare/1.40.2...1.40.3) (2022-12-21)
@@ -43,7 +43,7 @@ let user = db.model('user', {
43
43
  })
44
44
  ```
45
45
 
46
- Then when inserting or updating a document you need to set `files` to an obkect containing containing your parsed files, [express-fileupload](https://github.com/richardgirges/express-fileupload) works great with an express setup, e.g.
46
+ Then when inserting or updating a document you need to set `options.files` to an object containing your parsed files, [express-fileupload](https://github.com/richardgirges/express-fileupload) works great with an express setup, e.g.
47
47
 
48
48
  ```js
49
49
  user.update({
@@ -53,7 +53,13 @@ user.update({
53
53
  })
54
54
  ```
55
55
 
56
- When updating, you need to make sure you always pass all of your image objects again back into `data` since any images not found will be removed automatically from your S3 bucket. A nice way to handle image/non-image updates is by appending `?files=true` to your API route calls, e.g.
56
+ ### Updating documents
57
+
58
+ When updating a document with `options.files`, you need to make sure to **always** include previously uploaded file objects in `options.data` otherwise these will be removed automatically from your S3 bucket (via a comparsion check against your previous document).
59
+
60
+ You can reuse the image objects (e.g. `{ bucket, data, uid, ... }`) for other file field values on the same document. You can't however copy image objects across documents and collections.
61
+
62
+ You can skip all file processing by not defining `options.files`. A nice way to sepearte file and non-file updates is by appending ?files=true to your API route calls, e.g.
57
63
 
58
64
  ```js
59
65
  user.update({
@@ -13,6 +13,7 @@ Remove document(s) in a collection and calls model hooks: `beforeRemove`, `afte
13
13
 
14
14
  - `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/v4.4/tutorial/query-documents/), or id
15
15
  - [`sort`] *(string\|object\|array)*: same as the mongodb option, but allows for string parsing e.g. 'name', 'name:1'
16
+ - [`multi`] *(boolean)*: set to false remove only the first document that match the query criteria
16
17
  - [[`any mongodb option`](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#remove)] *(any)*
17
18
 
18
19
  [`callback`] *(function)*: pass instead of return a promise
package/docs/readme.md CHANGED
@@ -100,6 +100,7 @@ Coming soon...
100
100
  - docs: Make the implicit ID query conversion more apparent
101
101
  - Split away from Monk (unless updated)
102
102
  - Add a warning if an invalid model is referenced in jthe schema
103
+ - Remove leading forward slashes from custom image paths (AWS adds this as a seperate folder)
103
104
 
104
105
  ## Versions
105
106
 
package/lib/model.js CHANGED
@@ -131,6 +131,25 @@ Model.prototype._setupFields = function(fields) {
131
131
  * @param {object|array} fields - subsdocument or array
132
132
  */
133
133
  util.forEach(fields, function(field, fieldName) {
134
+ // Preprocess type objects
135
+ if (util.isSchema(field) && typeof field.type == 'object') {
136
+ if (field.schema) {
137
+ this.error(`Field "${fieldName}.schema" on model "${this.name}" is ignored when using `
138
+ + 'object types, simply define the schema fields alongside field.type.')
139
+ delete fields[fieldName]
140
+ return
141
+ }
142
+ if (util.isSchema(field.type)) {
143
+ this.error(`"${fieldName}.type" on model "${this.name}" is a schema object, it must be either a `
144
+ + 'string, subdocument or array')
145
+ delete fields[fieldName]
146
+ return
147
+ }
148
+ let fieldCache = field
149
+ field = fields[fieldName] = field.type
150
+ field.schema = util.omit(fieldCache, ['type'])
151
+ }
152
+
134
153
  // Schema field
135
154
  if (util.isSchema(field)) {
136
155
  // No image schema pre-processing done yet by a plugin
@@ -195,7 +214,13 @@ Model.prototype._setupFields = function(fields) {
195
214
  field.schema.index = index2dsphere
196
215
  delete field.index
197
216
  }
198
- field.schema = { type: 'object', isObject: true, default: objectDefault, nullObject: nullObject, ...field.schema }
217
+ field.schema = {
218
+ type: 'object',
219
+ isObject: true,
220
+ default: objectDefault,
221
+ nullObject: nullObject,
222
+ ...field.schema
223
+ }
199
224
  this._setupFields(field)
200
225
  }
201
226
  }, this)
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "monastery",
3
3
  "description": "⛪ A straight forward MongoDB ODM built around Monk",
4
4
  "author": "Ricky Boyce",
5
- "version": "1.40.4",
5
+ "version": "1.41.0",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
@@ -322,7 +322,8 @@ let plugin = module.exports = {
322
322
  // valid image-object?
323
323
  if (typeof useCount[image.image.uid] == 'undefined') {
324
324
  throw `The passed image object for '${image.dataPath}' does not match any pre-existing
325
- images saved on this document.`
325
+ images saved on this document. Make sure that this image dosen't come from another document or
326
+ collection, and is indeed saved on this document.`
326
327
  // Different image from prexisting image
327
328
  } else if (preExistingImage && preExistingImage.image.uid != image.image.uid) {
328
329
  useCount[preExistingImage.image.uid]--
package/test/model.js CHANGED
@@ -68,6 +68,58 @@ module.exports = function(monastery, opendb) {
68
68
  ))
69
69
  })
70
70
 
71
+ test('model setup with type object', async () => {
72
+ // Setup
73
+ let db = (await opendb(false)).db
74
+ let user = db.model('user', { fields: {
75
+ name: {
76
+ type: { cat: { type: 'string' }}, // subdocument
77
+ minLength: 4,
78
+ },
79
+ name2: {
80
+ type: [{ type: 'string' }], // array
81
+ minLength: 4,
82
+ },
83
+ }})
84
+
85
+ // subdocument object type
86
+ expect(user.fields.name).toEqual({
87
+ cat: { isString: true, type: 'string' },
88
+ schema: { isObject: true, type: 'object', minLength: 4 }
89
+ })
90
+ // array object type
91
+ expect(JSON.stringify(user.fields.name2)).toEqual(JSON.stringify(
92
+ [{ type: 'string', isString: true }]
93
+ ))
94
+ expect(user.fields.name2.schema).toEqual({
95
+ type: 'array',
96
+ isArray: true,
97
+ minLength: 4,
98
+ })
99
+ })
100
+
101
+ test('model setup with invalid schema type object', async () => {
102
+ // Setup
103
+ let db = (await opendb(false, { hideErrors: true })).db // hide debug error
104
+ let user = db.model('user', { fields: {
105
+ // valid subdocument
106
+ name: {
107
+ type: { type: 'string' },
108
+ },
109
+ // schema with invlaid object type
110
+ name2: {
111
+ type: { type: 'string' },
112
+ minLength: 10,
113
+ },
114
+ }})
115
+
116
+ expect(user.fields.name).toEqual({
117
+ type: { isString: true, type: 'string' },
118
+ schema: { isObject: true, type: 'object' },
119
+ })
120
+ expect(user.fields.name2).toEqual(undefined)
121
+ })
122
+
71
123
  test('model setup with default fields', async () => {
72
124
  // Setup
73
125
  let db = (await opendb(false, { defaultObjects: true })).db