monastery 1.28.2 → 1.29.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/.eslintrc.json +30 -4
- package/docs/Gemfile +8 -18
- package/docs/_config.yml +1 -4
- package/docs/errors.md +0 -0
- package/docs/image-plugin.md +0 -0
- package/docs/manager/model.md +0 -0
- package/docs/manager/models.md +0 -0
- package/docs/model/findOne.md +0 -0
- package/docs/model/index.md +0 -0
- package/docs/model/remove.md +0 -0
- package/docs/readme.md +1 -1
- package/docs/rules.md +0 -0
- package/lib/index.js +0 -1
- package/lib/model-crud.js +32 -16
- package/lib/model-validate.js +20 -24
- package/lib/model.js +13 -4
- package/lib/rules.js +38 -16
- package/lib/util.js +13 -7
- package/package.json +17 -16
- package/plugins/images/index.js +4 -4
- package/test/assets/bad.svg +0 -0
- package/test/assets/image.ico +0 -0
- package/test/assets/image.webp +0 -0
- package/test/assets/lion1.png +0 -0
- package/test/assets/lion2.jpg +0 -0
- package/test/assets/logo.png +0 -0
- package/test/assets/logo2.png +0 -0
- package/test/blacklisting.js +4 -8
- package/test/crud.js +28 -31
- package/test/model.js +22 -25
- package/test/monk.js +4 -4
- package/test/plugin-images.js +400 -390
- package/test/populate.js +15 -18
- package/test/util.js +8 -8
- package/test/validate.js +126 -50
- package/test/virtuals.js +2 -4
package/lib/rules.js
CHANGED
|
@@ -5,7 +5,7 @@ let validator = require('validator')
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
|
|
7
7
|
required: {
|
|
8
|
-
|
|
8
|
+
ignoreUndefined: false,
|
|
9
9
|
message: 'This field is required.',
|
|
10
10
|
fn: function(x) {
|
|
11
11
|
if (util.isArray(x) && !x.length) return false
|
|
@@ -13,9 +13,10 @@ module.exports = {
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
|
|
16
|
-
// Type rules below ignore undefined
|
|
16
|
+
// Type rules below ignore undefined (default for custom model rules)
|
|
17
17
|
|
|
18
18
|
'isBoolean': {
|
|
19
|
+
ignoreUndefined: true,
|
|
19
20
|
message: 'Value was not a boolean.',
|
|
20
21
|
tryParse: function(x) {
|
|
21
22
|
if (typeof x === 'string' && x === 'true') return true
|
|
@@ -27,12 +28,12 @@ module.exports = {
|
|
|
27
28
|
}
|
|
28
29
|
},
|
|
29
30
|
'isArray': {
|
|
31
|
+
ignoreUndefined: true,
|
|
30
32
|
message: 'Value was not an array.',
|
|
31
33
|
tryParse: function(x) {
|
|
32
34
|
if (x === '') return null
|
|
33
35
|
if (!util.isString(x)) return x
|
|
34
|
-
try { var parsed = JSON.parse(x) }
|
|
35
|
-
catch (e) { return x }
|
|
36
|
+
try { var parsed = JSON.parse(x) } catch (e) { return x }
|
|
36
37
|
return Array.isArray(parsed)? parsed : x
|
|
37
38
|
},
|
|
38
39
|
fn: function(x) {
|
|
@@ -40,25 +41,26 @@ module.exports = {
|
|
|
40
41
|
}
|
|
41
42
|
},
|
|
42
43
|
'isDate': {
|
|
44
|
+
ignoreUndefined: true,
|
|
43
45
|
message: 'Value was not a unix timestamp.',
|
|
44
46
|
tryParse: function(x) {
|
|
45
|
-
if (util.isString(x) && x.match(/^[
|
|
47
|
+
if (util.isString(x) && x.match(/^[+-]?[0-9]+$/)) return x // keep string nums intact
|
|
46
48
|
return isNaN(parseInt(x))? x : parseInt(x)
|
|
47
49
|
},
|
|
48
50
|
fn: function(x) {
|
|
49
|
-
if (util.isString(x) && x.match(/^[
|
|
51
|
+
if (util.isString(x) && x.match(/^[+-]?[0-9]+$/)) return true
|
|
50
52
|
return typeof x === 'number' && (parseInt(x) === x)
|
|
51
53
|
}
|
|
52
54
|
},
|
|
53
55
|
'isImageObject': {
|
|
56
|
+
ignoreUndefined: true,
|
|
54
57
|
message: 'Invalid image value',
|
|
55
58
|
messageLong: 'Image fields need to either be null, undefined, file, or an object containing the following '
|
|
56
59
|
+ 'fields \'{ bucket, date, filename, filesize, path, uid }\'',
|
|
57
60
|
tryParse: function(x) {
|
|
58
61
|
if (x === '') return null
|
|
59
62
|
if (!util.isString(x)) return x
|
|
60
|
-
try { var parsed = JSON.parse(x) }
|
|
61
|
-
catch (e) { return x }
|
|
63
|
+
try { var parsed = JSON.parse(x) } catch (e) { return x }
|
|
62
64
|
return parsed !== null && typeof parsed === 'object' && !(parsed instanceof Array)? parsed : x
|
|
63
65
|
},
|
|
64
66
|
fn: function(x) {
|
|
@@ -67,34 +69,36 @@ module.exports = {
|
|
|
67
69
|
}
|
|
68
70
|
},
|
|
69
71
|
'isInteger': {
|
|
72
|
+
ignoreUndefined: true,
|
|
70
73
|
message: 'Value was not an integer.',
|
|
71
74
|
tryParse: function(x) {
|
|
72
|
-
if (util.isString(x) && x.match(/^[
|
|
75
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return x // keep string nums intact
|
|
73
76
|
return isNaN(parseInt(x)) || (!x && x!==0) || x===true? x : parseInt(x)
|
|
74
77
|
},
|
|
75
78
|
fn: function(x) {
|
|
76
|
-
if (util.isString(x) && x.match(/^[
|
|
79
|
+
if (util.isString(x) && x.match(/^[+-]?[0-9]+$/)) return true
|
|
77
80
|
return typeof x === 'number' && (parseInt(x) === x)
|
|
78
81
|
}
|
|
79
82
|
},
|
|
80
83
|
'isNumber': {
|
|
84
|
+
ignoreUndefined: true,
|
|
81
85
|
message: 'Value was not a number.',
|
|
82
86
|
tryParse: function(x) {
|
|
83
|
-
if (util.isString(x) && x.match(/^[
|
|
87
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return x // keep string nums intact
|
|
84
88
|
return isNaN(Number(x)) || (!x && x!==0) || x===true? x : Number(x)
|
|
85
89
|
},
|
|
86
90
|
fn: function(x) {
|
|
87
|
-
if (util.isString(x) && x.match(/^[
|
|
91
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return true
|
|
88
92
|
return typeof x === 'number'
|
|
89
93
|
}
|
|
90
94
|
},
|
|
91
95
|
'isObject': {
|
|
96
|
+
ignoreUndefined: true,
|
|
92
97
|
message: 'Value was not an object.',
|
|
93
98
|
tryParse: function(x) {
|
|
94
99
|
if (x === '') return null
|
|
95
100
|
if (!util.isString(x)) return x
|
|
96
|
-
try { var parsed = JSON.parse(x) }
|
|
97
|
-
catch (e) { return x }
|
|
101
|
+
try { var parsed = JSON.parse(x) } catch (e) { return x }
|
|
98
102
|
return parsed !== null && typeof parsed === 'object' && !(parsed instanceof Array)? parsed : x
|
|
99
103
|
},
|
|
100
104
|
fn: function(x) {
|
|
@@ -102,18 +106,21 @@ module.exports = {
|
|
|
102
106
|
}
|
|
103
107
|
},
|
|
104
108
|
'isString': {
|
|
109
|
+
ignoreUndefined: true,
|
|
105
110
|
message: 'Value was not a string.',
|
|
106
111
|
fn: function(x) {
|
|
107
112
|
return typeof x === 'string'
|
|
108
113
|
}
|
|
109
114
|
},
|
|
110
115
|
'isAny': {
|
|
116
|
+
ignoreUndefined: true,
|
|
111
117
|
message: '',
|
|
112
118
|
fn: function(x) {
|
|
113
119
|
return true
|
|
114
120
|
}
|
|
115
121
|
},
|
|
116
122
|
'isId': {
|
|
123
|
+
ignoreUndefined: true,
|
|
117
124
|
message: 'Value was not a valid ObjectId.',
|
|
118
125
|
tryParse: function(x) {
|
|
119
126
|
// Try and parse value to a mongodb ObjectId
|
|
@@ -127,9 +134,8 @@ module.exports = {
|
|
|
127
134
|
return util.isObject(x) && ObjectId.isValid(x)/*x.get_inc*/? true : false
|
|
128
135
|
}
|
|
129
136
|
},
|
|
130
|
-
|
|
131
|
-
|
|
132
137
|
'max': {
|
|
138
|
+
ignoreUndefined: true,
|
|
133
139
|
message: (x, arg) => 'Value was greater than the configured maximum (' + arg + ')',
|
|
134
140
|
fn: function(x, arg) {
|
|
135
141
|
if (typeof x !== 'number') { throw new Error ('Value was not a number.') }
|
|
@@ -137,6 +143,7 @@ module.exports = {
|
|
|
137
143
|
}
|
|
138
144
|
},
|
|
139
145
|
'min': {
|
|
146
|
+
ignoreUndefined: true,
|
|
140
147
|
message: (x, arg) => 'Value was less than the configured minimum (' + arg + ')',
|
|
141
148
|
fn: function(x, arg) {
|
|
142
149
|
if (typeof x !== 'number') { throw new Error ('Value was not a number.') }
|
|
@@ -144,6 +151,7 @@ module.exports = {
|
|
|
144
151
|
}
|
|
145
152
|
},
|
|
146
153
|
'isNotEmptyString': {
|
|
154
|
+
ignoreUndefined: true,
|
|
147
155
|
message: 'Value was an empty string.',
|
|
148
156
|
fn: function(x) {
|
|
149
157
|
return x !== ''
|
|
@@ -155,6 +163,7 @@ module.exports = {
|
|
|
155
163
|
|
|
156
164
|
'enum': {
|
|
157
165
|
ignoreEmptyString: true,
|
|
166
|
+
ignoreUndefined: true,
|
|
158
167
|
message: (x, arg) => 'Invalid enum value',
|
|
159
168
|
fn: function(x, arg) {
|
|
160
169
|
for (let item of arg) {
|
|
@@ -168,56 +177,67 @@ module.exports = {
|
|
|
168
177
|
// },
|
|
169
178
|
'isAfter': {
|
|
170
179
|
ignoreEmptyString: true,
|
|
180
|
+
ignoreUndefined: true,
|
|
171
181
|
message: (x, arg) => 'Value was before the configured time (' + arg + ')',
|
|
172
182
|
fn: function(x, arg) { return validator.isAfter(x, arg) }
|
|
173
183
|
},
|
|
174
184
|
'isBefore': {
|
|
175
185
|
ignoreEmptyString: true,
|
|
186
|
+
ignoreUndefined: true,
|
|
176
187
|
message: (x, arg) => 'Value was after the configured time (' + arg + ')',
|
|
177
188
|
fn: function(x, arg) { return validator.isBefore(x, arg) }
|
|
178
189
|
},
|
|
179
190
|
'isCreditCard': {
|
|
180
191
|
ignoreEmptyString: true,
|
|
192
|
+
ignoreUndefined: true,
|
|
181
193
|
message: 'Value was not a valid credit card.',
|
|
182
194
|
fn: function(x, arg) { return validator.isCreditCard(x, arg) }
|
|
183
195
|
},
|
|
184
196
|
'isEmail': {
|
|
185
197
|
ignoreEmptyString: true,
|
|
198
|
+
ignoreUndefined: true,
|
|
186
199
|
message: 'Please enter a valid email address.',
|
|
187
200
|
fn: function(x, arg) { return validator.isEmail(x, arg) }
|
|
188
201
|
},
|
|
189
202
|
'isHexColor': {
|
|
190
203
|
ignoreEmptyString: true,
|
|
204
|
+
ignoreUndefined: true,
|
|
191
205
|
message: 'Value was not a valid hex color.',
|
|
192
206
|
fn: function(x, arg) { return validator.isHexColor(x, arg) }
|
|
193
207
|
},
|
|
194
208
|
'isIn': {
|
|
195
209
|
ignoreEmptyString: true,
|
|
210
|
+
ignoreUndefined: true,
|
|
196
211
|
message: (x, arg) => 'Value was not in the configured whitelist (' + arg.join(', ') + ')',
|
|
197
212
|
fn: function(x, arg) { return validator.isIn(x, arg) }
|
|
198
213
|
},
|
|
199
214
|
'isIP': {
|
|
200
215
|
ignoreEmptyString: true,
|
|
216
|
+
ignoreUndefined: true,
|
|
201
217
|
message: 'Value was not a valid IP address.',
|
|
202
218
|
fn: function(x, arg) { return validator.isIP(x, arg) }
|
|
203
219
|
},
|
|
204
220
|
'isNotIn': {
|
|
205
221
|
ignoreEmptyString: true,
|
|
222
|
+
ignoreUndefined: true,
|
|
206
223
|
message: (x, arg) => 'Value was in the configured blacklist (' + arg.join(', ') + ')',
|
|
207
224
|
fn: function(x, arg) { return !validator.isIn(x, arg) }
|
|
208
225
|
},
|
|
209
226
|
'isURL': {
|
|
210
227
|
ignoreEmptyString: true,
|
|
228
|
+
ignoreUndefined: true,
|
|
211
229
|
message: 'Value was not a valid URL.',
|
|
212
230
|
fn: function(x, arg) { return validator.isURL(x, arg === true? undefined : arg) }
|
|
213
231
|
},
|
|
214
232
|
'isUUID': {
|
|
215
233
|
ignoreEmptyString: true,
|
|
234
|
+
ignoreUndefined: true,
|
|
216
235
|
message: 'Value was not a valid UUID.',
|
|
217
236
|
fn: function(x, arg) { return validator.isUUID(x) }
|
|
218
237
|
},
|
|
219
238
|
'minLength': {
|
|
220
239
|
ignoreEmptyString: true,
|
|
240
|
+
ignoreUndefined: true,
|
|
221
241
|
message: function(x, arg) {
|
|
222
242
|
if (typeof x === 'string') return 'Value needs to be at least ' + arg + ' characters long.'
|
|
223
243
|
else return 'Value needs to contain a minimum of ' + arg + ' items.'
|
|
@@ -230,6 +250,7 @@ module.exports = {
|
|
|
230
250
|
},
|
|
231
251
|
'maxLength': {
|
|
232
252
|
ignoreEmptyString: true,
|
|
253
|
+
ignoreUndefined: true,
|
|
233
254
|
message: function(x, arg) {
|
|
234
255
|
if (typeof x === 'string') return 'Value was longer than the configured maximum length (' + arg + ')'
|
|
235
256
|
else return 'Value cannot contain more than ' + arg + ' items.'
|
|
@@ -242,6 +263,7 @@ module.exports = {
|
|
|
242
263
|
},
|
|
243
264
|
'regex': {
|
|
244
265
|
ignoreEmptyString: true,
|
|
266
|
+
ignoreUndefined: true,
|
|
245
267
|
message: (x, arg) => 'Value did not match the configured regular expression (' + arg + ')',
|
|
246
268
|
fn: function(x, arg) {
|
|
247
269
|
if (util.isRegex(arg)) return validator.matches(x, arg)
|
package/lib/util.js
CHANGED
|
@@ -9,7 +9,7 @@ module.exports = {
|
|
|
9
9
|
for (let key in obj) {
|
|
10
10
|
let v = obj[key]
|
|
11
11
|
if (this.isId(v)) obj2[key] = v.toString()
|
|
12
|
-
else obj2[key] = (typeof v ===
|
|
12
|
+
else obj2[key] = (typeof v === 'object')? this.deepCopy(v) : v
|
|
13
13
|
}
|
|
14
14
|
return obj2
|
|
15
15
|
},
|
|
@@ -165,7 +165,7 @@ module.exports = {
|
|
|
165
165
|
* @param {object}
|
|
166
166
|
*/
|
|
167
167
|
for (let key in obj) {
|
|
168
|
-
if (key.indexOf(
|
|
168
|
+
if (key.indexOf('.') !== -1) recurse(key, obj[key], obj)
|
|
169
169
|
}
|
|
170
170
|
return obj
|
|
171
171
|
function recurse(str, val, obj) {
|
|
@@ -199,8 +199,12 @@ module.exports = {
|
|
|
199
199
|
*/
|
|
200
200
|
return new Promise(res => {
|
|
201
201
|
for (let key in obj) {
|
|
202
|
-
if (key.match(/\[\]\[/i))
|
|
203
|
-
|
|
202
|
+
if (key.match(/\[\]\[/i)) {
|
|
203
|
+
throw `Array items in bracket notation need array indexes "${key}", e.g. users[0][name]`
|
|
204
|
+
}
|
|
205
|
+
if (key.indexOf('[') !== -1) {
|
|
206
|
+
setup(key)
|
|
207
|
+
}
|
|
204
208
|
}
|
|
205
209
|
res(obj)
|
|
206
210
|
})
|
|
@@ -241,7 +245,7 @@ module.exports = {
|
|
|
241
245
|
removeUndefined: (variable) => {
|
|
242
246
|
// takes an array or object
|
|
243
247
|
if (Array.isArray(variable)) {
|
|
244
|
-
for (let
|
|
248
|
+
for (let i=variable.length; i--;) {
|
|
245
249
|
if (variable[i] === undefined) variable.splice(i, 1)
|
|
246
250
|
}
|
|
247
251
|
} else {
|
|
@@ -308,7 +312,7 @@ module.exports = {
|
|
|
308
312
|
target[chunks[i]] = value
|
|
309
313
|
}
|
|
310
314
|
} else {
|
|
311
|
-
let isArray = chunks[i+1].match(/^[0-9
|
|
315
|
+
let isArray = chunks[i+1].match(/^[0-9$]+$/)
|
|
312
316
|
let parentCopy = target[chunks[i]] || (isArray? [] : {})
|
|
313
317
|
if (ignoreEmptyArrays && isArray && !parentCopy.length) break
|
|
314
318
|
target = target[chunks[i]] = parentCopy
|
|
@@ -316,7 +320,9 @@ module.exports = {
|
|
|
316
320
|
if (chunks[i+1] == '$') {
|
|
317
321
|
for (let m=0, n=target.length; m<n; m++) {
|
|
318
322
|
let newPath = chunks.slice(i+2).join('.')
|
|
319
|
-
if (newPath)
|
|
323
|
+
if (newPath) {
|
|
324
|
+
this.setDeepValue(target[m], newPath, value, onlyUndefined, onlyUndefinedNull, ignoreEmptyArrays)
|
|
325
|
+
}
|
|
320
326
|
}
|
|
321
327
|
break
|
|
322
328
|
}
|
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.
|
|
5
|
+
"version": "1.29.0",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|
|
@@ -19,27 +19,28 @@
|
|
|
19
19
|
"scripts": {
|
|
20
20
|
"minor": "standard-version --release-as minor && npm publish",
|
|
21
21
|
"patch": "standard-version && npm publish",
|
|
22
|
-
"test": "jest",
|
|
22
|
+
"test": "npm run lint && jest",
|
|
23
23
|
"test-one-example": "jest -t images",
|
|
24
|
-
"dev": "DEBUG=-monastery:info jest --watchAll --runInBand --verbose=false",
|
|
24
|
+
"dev": "npm run lint & DEBUG=-monastery:info jest --watchAll --runInBand --verbose=false",
|
|
25
|
+
"lint": "eslint ./lib ./plugins ./test",
|
|
25
26
|
"docs": "cd docs && bundle exec jekyll serve --livereload --livereload-port 4001"
|
|
26
27
|
},
|
|
27
28
|
"dependencies": {
|
|
28
|
-
"aws-sdk": "
|
|
29
|
-
"debug": "
|
|
30
|
-
"file-type": "
|
|
31
|
-
"monk": "
|
|
32
|
-
"nanoid": "
|
|
33
|
-
"validator": "
|
|
29
|
+
"aws-sdk": "2.1062.0",
|
|
30
|
+
"debug": "4.1.1",
|
|
31
|
+
"file-type": "15.0.0",
|
|
32
|
+
"monk": "7.3.4",
|
|
33
|
+
"nanoid": "3.2.0",
|
|
34
|
+
"validator": "13.7.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"body-parser": "
|
|
37
|
-
"eslint": "
|
|
38
|
-
"express": "
|
|
39
|
-
"express-fileupload": "
|
|
40
|
-
"jest": "
|
|
41
|
-
"standard-version": "
|
|
42
|
-
"supertest": "
|
|
37
|
+
"body-parser": "1.19.0",
|
|
38
|
+
"eslint": "8.7.0",
|
|
39
|
+
"express": "4.17.1",
|
|
40
|
+
"express-fileupload": "1.2.0",
|
|
41
|
+
"jest": "27.4.7",
|
|
42
|
+
"standard-version": "9.3.2",
|
|
43
|
+
"supertest": "4.0.2"
|
|
43
44
|
},
|
|
44
45
|
"standard-version": {
|
|
45
46
|
"releaseCommitMessageFormat": "{{currentTag}}",
|
package/plugins/images/index.js
CHANGED
|
@@ -155,8 +155,8 @@ let plugin = module.exports = {
|
|
|
155
155
|
if (test) return [prunedData]
|
|
156
156
|
return model._update(
|
|
157
157
|
idquery,
|
|
158
|
-
{
|
|
159
|
-
{
|
|
158
|
+
{ '$set': prunedData },
|
|
159
|
+
{ 'multi': options.multi || options.create }
|
|
160
160
|
)
|
|
161
161
|
|
|
162
162
|
// If errors, remove inserted documents to prevent double ups when the user resaves.
|
|
@@ -246,7 +246,7 @@ let plugin = module.exports = {
|
|
|
246
246
|
// pre-existing image, push to unused
|
|
247
247
|
return plugin._findValidImages(options.files || {}, options.model).then(files => {
|
|
248
248
|
for (let filesArray of files) {
|
|
249
|
-
if (pre = preExistingImages.find(o => o.dataPath == filesArray.inputPath)) {
|
|
249
|
+
if ((pre = preExistingImages.find(o => o.dataPath == filesArray.inputPath))) {
|
|
250
250
|
useCount[pre.image.uid]--
|
|
251
251
|
}
|
|
252
252
|
}
|
|
@@ -380,7 +380,7 @@ let plugin = module.exports = {
|
|
|
380
380
|
let list = []
|
|
381
381
|
util.forEach(fields, (field, fieldName) => {
|
|
382
382
|
let path2 = `${path}.${fieldName}`.replace(/^\./, '')
|
|
383
|
-
let schema = field.schema || {}
|
|
383
|
+
// let schema = field.schema || {}
|
|
384
384
|
|
|
385
385
|
// Subdocument field
|
|
386
386
|
if (util.isSubdocument(field)) {//schema.isObject
|
package/test/assets/bad.svg
CHANGED
|
File without changes
|
package/test/assets/image.ico
CHANGED
|
File without changes
|
package/test/assets/image.webp
CHANGED
|
File without changes
|
package/test/assets/lion1.png
CHANGED
|
File without changes
|
package/test/assets/lion2.jpg
CHANGED
|
File without changes
|
package/test/assets/logo.png
CHANGED
|
File without changes
|
package/test/assets/logo2.png
CHANGED
|
File without changes
|
package/test/blacklisting.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module.exports = function(monastery, opendb) {
|
|
2
2
|
|
|
3
|
-
test('Find blacklisting', async (
|
|
3
|
+
test('Find blacklisting', async () => {
|
|
4
4
|
// Setup
|
|
5
5
|
let db = (await opendb(null)).db
|
|
6
6
|
let bird = db.model('bird', {
|
|
@@ -150,10 +150,9 @@ module.exports = function(monastery, opendb) {
|
|
|
150
150
|
})
|
|
151
151
|
|
|
152
152
|
db.close()
|
|
153
|
-
done()
|
|
154
153
|
})
|
|
155
154
|
|
|
156
|
-
test('Find blacklisting (default fields)', async (
|
|
155
|
+
test('Find blacklisting (default fields)', async () => {
|
|
157
156
|
// Setup
|
|
158
157
|
let db = (await opendb(null)).db
|
|
159
158
|
let user = db.model('user', {
|
|
@@ -222,10 +221,9 @@ module.exports = function(monastery, opendb) {
|
|
|
222
221
|
})
|
|
223
222
|
|
|
224
223
|
db.close()
|
|
225
|
-
done()
|
|
226
224
|
})
|
|
227
225
|
|
|
228
|
-
test('Find blacklisting (populate)', async (
|
|
226
|
+
test('Find blacklisting (populate)', async () => {
|
|
229
227
|
// Setup
|
|
230
228
|
let db = (await opendb(null)).db
|
|
231
229
|
let bird = db.model('bird', {
|
|
@@ -305,10 +303,9 @@ module.exports = function(monastery, opendb) {
|
|
|
305
303
|
})
|
|
306
304
|
|
|
307
305
|
db.close()
|
|
308
|
-
done()
|
|
309
306
|
})
|
|
310
307
|
|
|
311
|
-
test('Insert/update blacklisting (validate)', async (
|
|
308
|
+
test('Insert/update blacklisting (validate)', async () => {
|
|
312
309
|
// Setup
|
|
313
310
|
let db = (await opendb(null)).db
|
|
314
311
|
let user = db.model('user', {
|
|
@@ -406,7 +403,6 @@ module.exports = function(monastery, opendb) {
|
|
|
406
403
|
}
|
|
407
404
|
})
|
|
408
405
|
db.close()
|
|
409
|
-
done()
|
|
410
406
|
})
|
|
411
407
|
|
|
412
408
|
}
|
package/test/crud.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
let util = require('../lib/util')
|
|
1
|
+
// let util = require('../lib/util')
|
|
2
2
|
|
|
3
3
|
module.exports = function(monastery, opendb) {
|
|
4
4
|
|
|
5
|
-
test('Basic operator calls', async (
|
|
5
|
+
test('Basic operator calls', async () => {
|
|
6
6
|
let db = (await opendb(null)).db
|
|
7
7
|
let user = db.model('user', {
|
|
8
8
|
fields: { name: { type: 'string' }},
|
|
@@ -53,13 +53,14 @@ module.exports = function(monastery, opendb) {
|
|
|
53
53
|
expect(find6).toEqual({ _id: inserted2[0]._id, name: 'Martin Luther1' })
|
|
54
54
|
|
|
55
55
|
// Missing parameters
|
|
56
|
-
await expect(user.find()).rejects.toThrow(
|
|
57
|
-
await expect(user.find(undefined)).rejects.toThrow(
|
|
58
|
-
await expect(user.find({})).rejects.toThrow(
|
|
59
|
-
await expect(user.find({ query: null })).rejects.toThrow(
|
|
60
|
-
await expect(user.find({ query: undefined })).rejects.toThrow(
|
|
61
|
-
await expect(user.find({ query: { _id: undefined }}))
|
|
62
|
-
|
|
56
|
+
await expect(user.find()).rejects.toThrow('Please pass an object or MongoId to options.query')
|
|
57
|
+
await expect(user.find(undefined)).rejects.toThrow('Please pass an object or MongoId to options.query')
|
|
58
|
+
await expect(user.find({})).rejects.toThrow('Please pass an object or MongoId to options.query')
|
|
59
|
+
await expect(user.find({ query: null })).rejects.toThrow('Please pass an object or MongoId to options.query')
|
|
60
|
+
await expect(user.find({ query: undefined })).rejects.toThrow('Please pass an object or MongoId to options.query')
|
|
61
|
+
await expect(user.find({ query: { _id: undefined }}))
|
|
62
|
+
.rejects.toThrow('Please pass an object or MongoId to options.query')
|
|
63
|
+
await expect(user.find(1)).rejects.toThrow('Please pass an object or MongoId to options.query')
|
|
63
64
|
|
|
64
65
|
// Bad MongoID
|
|
65
66
|
await expect(user.find({ query: '' })).resolves.toEqual(null)
|
|
@@ -89,10 +90,10 @@ module.exports = function(monastery, opendb) {
|
|
|
89
90
|
|
|
90
91
|
// Update (no/empty data object)
|
|
91
92
|
await expect(user.update({ query: inserted._id, data: {}}))
|
|
92
|
-
.rejects.toThrow(
|
|
93
|
+
.rejects.toThrow('No valid data passed to user.update()')
|
|
93
94
|
|
|
94
95
|
await expect(user.update({ query: inserted._id }))
|
|
95
|
-
.rejects.toThrow(
|
|
96
|
+
.rejects.toThrow('No valid data passed to user.update()')
|
|
96
97
|
|
|
97
98
|
// Update (no/empty data object, but has update operators
|
|
98
99
|
await expect(user.update({ query: inserted._id, $set: { name: 'bruce' }}))
|
|
@@ -114,7 +115,7 @@ module.exports = function(monastery, opendb) {
|
|
|
114
115
|
expect(beforeValidateHookCalled).toEqual(false)
|
|
115
116
|
|
|
116
117
|
// Update multiple
|
|
117
|
-
|
|
118
|
+
await user.update({
|
|
118
119
|
query: { _id: { $in: [inserted2[0]._id, inserted2[1]._id] }},
|
|
119
120
|
data: { name: 'Martin Luther3' },
|
|
120
121
|
multi: true
|
|
@@ -137,10 +138,9 @@ module.exports = function(monastery, opendb) {
|
|
|
137
138
|
expect(remove.result).toEqual({ n: 1, ok: 1 })
|
|
138
139
|
|
|
139
140
|
db.close()
|
|
140
|
-
done()
|
|
141
141
|
})
|
|
142
142
|
|
|
143
|
-
test('Insert defaults', async (
|
|
143
|
+
test('Insert defaults', async () => {
|
|
144
144
|
let db = (await opendb(null, { defaultObjects: true, serverSelectionTimeoutMS: 2000 })).db
|
|
145
145
|
let db2 = (await opendb(null, { useMilliseconds: true, serverSelectionTimeoutMS: 2000 })).db
|
|
146
146
|
let user = db.model('user', { fields: {
|
|
@@ -192,10 +192,9 @@ module.exports = function(monastery, opendb) {
|
|
|
192
192
|
|
|
193
193
|
db.close()
|
|
194
194
|
db2.close()
|
|
195
|
-
done()
|
|
196
195
|
})
|
|
197
196
|
|
|
198
|
-
test('update defaults', async (
|
|
197
|
+
test('update defaults', async () => {
|
|
199
198
|
let db = (await opendb(null, { useMilliseconds: true, serverSelectionTimeoutMS: 2000 })).db
|
|
200
199
|
let user = db.model('user', {
|
|
201
200
|
fields: {
|
|
@@ -236,7 +235,7 @@ module.exports = function(monastery, opendb) {
|
|
|
236
235
|
query: inserted._id,
|
|
237
236
|
data: {},
|
|
238
237
|
timestamps: false
|
|
239
|
-
})).rejects.toThrow(
|
|
238
|
+
})).rejects.toThrow('No valid data passed to user.update()')
|
|
240
239
|
|
|
241
240
|
// UpdatedAt override (wont work)
|
|
242
241
|
let updated4 = await user.update({
|
|
@@ -254,12 +253,11 @@ module.exports = function(monastery, opendb) {
|
|
|
254
253
|
expect(updated5.updatedAt).toEqual(1)
|
|
255
254
|
|
|
256
255
|
db.close()
|
|
257
|
-
done()
|
|
258
256
|
})
|
|
259
257
|
|
|
260
|
-
test('Insert with id casting', async (
|
|
258
|
+
test('Insert with id casting', async () => {
|
|
261
259
|
let db = (await opendb(null)).db
|
|
262
|
-
|
|
260
|
+
db.model('company', { fields: {
|
|
263
261
|
name: { type: 'string' }
|
|
264
262
|
}})
|
|
265
263
|
let user = db.model('user', { fields: {
|
|
@@ -279,10 +277,9 @@ module.exports = function(monastery, opendb) {
|
|
|
279
277
|
})
|
|
280
278
|
|
|
281
279
|
db.close()
|
|
282
|
-
done()
|
|
283
280
|
})
|
|
284
281
|
|
|
285
|
-
test('Find default field population', async (
|
|
282
|
+
test('Find default field population', async () => {
|
|
286
283
|
let db = (await opendb(null)).db
|
|
287
284
|
let user = db.model('user', {
|
|
288
285
|
fields: {
|
|
@@ -308,7 +305,7 @@ module.exports = function(monastery, opendb) {
|
|
|
308
305
|
],
|
|
309
306
|
pet: { dog: inserted._id }
|
|
310
307
|
}})
|
|
311
|
-
|
|
308
|
+
await dog.update({
|
|
312
309
|
query: inserted._id,
|
|
313
310
|
data: { user: inserted2._id }
|
|
314
311
|
})
|
|
@@ -370,10 +367,9 @@ module.exports = function(monastery, opendb) {
|
|
|
370
367
|
})
|
|
371
368
|
|
|
372
369
|
db.close()
|
|
373
|
-
done()
|
|
374
370
|
})
|
|
375
371
|
|
|
376
|
-
test('Hooks', async (
|
|
372
|
+
test('Hooks', async () => {
|
|
377
373
|
let db = (await opendb(null)).db
|
|
378
374
|
let user = db.model('user', {
|
|
379
375
|
fields: {
|
|
@@ -412,8 +408,8 @@ module.exports = function(monastery, opendb) {
|
|
|
412
408
|
let userDoc = await user.insert({ data: { first: 'Martin', last: 'Luther' }})
|
|
413
409
|
|
|
414
410
|
// Catch insert (a)synchronous errors thrown in function or through `next(err)`
|
|
415
|
-
await expect(user.insert({ data: { first: '' } })).rejects.toThrow(
|
|
416
|
-
await expect(user.insert({ data: { first: 'Martin' } })).rejects.toThrow(
|
|
411
|
+
await expect(user.insert({ data: { first: '' } })).rejects.toThrow('beforeInsert error 1..')
|
|
412
|
+
await expect(user.insert({ data: { first: 'Martin' } })).rejects.toThrow('beforeInsert error 2..')
|
|
417
413
|
await expect(user.insert({ data: { first: 'Martin', last: 'Luther' } })).resolves.toEqual({
|
|
418
414
|
_id: expect.any(Object),
|
|
419
415
|
first: 'Martin',
|
|
@@ -421,15 +417,17 @@ module.exports = function(monastery, opendb) {
|
|
|
421
417
|
})
|
|
422
418
|
|
|
423
419
|
// Catch update (a)synchronous errors thrown in function or through `next(err)`
|
|
424
|
-
await expect(user.update({ query: userDoc._id, data: { first: '' } }))
|
|
425
|
-
|
|
420
|
+
await expect(user.update({ query: userDoc._id, data: { first: '' } }))
|
|
421
|
+
.rejects.toThrow('beforeUpdate error 1..')
|
|
422
|
+
await expect(user.update({ query: userDoc._id, data: { first: 'Martin' } }))
|
|
423
|
+
.rejects.toThrow('beforeUpdate error 2..')
|
|
426
424
|
await expect(user.update({ query: userDoc._id, data: { first: 'Martin', last: 'Luther' } })).resolves.toEqual({
|
|
427
425
|
first: 'Martin',
|
|
428
426
|
last: 'Luther'
|
|
429
427
|
})
|
|
430
428
|
|
|
431
429
|
// Catch remove synchronous errors through `next(err)`
|
|
432
|
-
await expect(user.remove({ query: userDoc._id })).rejects.toThrow(
|
|
430
|
+
await expect(user.remove({ query: userDoc._id })).rejects.toThrow('beforeRemove error..')
|
|
433
431
|
|
|
434
432
|
// After find continues series
|
|
435
433
|
await expect(user.find({ query: userDoc._id })).resolves.toEqual({
|
|
@@ -439,7 +437,6 @@ module.exports = function(monastery, opendb) {
|
|
|
439
437
|
})
|
|
440
438
|
|
|
441
439
|
db.close()
|
|
442
|
-
done()
|
|
443
440
|
})
|
|
444
441
|
|
|
445
442
|
}
|