monastery 1.28.3 → 1.30.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 +33 -17
- package/lib/model-validate.js +25 -25
- package/lib/model.js +23 -12
- package/lib/rules.js +26 -31
- 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 +114 -28
- 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 +227 -108
- package/test/virtuals.js +2 -4
package/lib/rules.js
CHANGED
|
@@ -5,7 +5,8 @@ let validator = require('validator')
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
|
|
7
7
|
required: {
|
|
8
|
-
|
|
8
|
+
validateUndefined: true,
|
|
9
|
+
validateEmptyString: true,
|
|
9
10
|
message: 'This field is required.',
|
|
10
11
|
fn: function(x) {
|
|
11
12
|
if (util.isArray(x) && !x.length) return false
|
|
@@ -13,9 +14,10 @@ module.exports = {
|
|
|
13
14
|
}
|
|
14
15
|
},
|
|
15
16
|
|
|
16
|
-
// Type rules below ignore undefined
|
|
17
|
+
// Type rules below ignore undefined (default for custom model rules)
|
|
17
18
|
|
|
18
19
|
'isBoolean': {
|
|
20
|
+
validateEmptyString: true,
|
|
19
21
|
message: 'Value was not a boolean.',
|
|
20
22
|
tryParse: function(x) {
|
|
21
23
|
if (typeof x === 'string' && x === 'true') return true
|
|
@@ -27,12 +29,12 @@ module.exports = {
|
|
|
27
29
|
}
|
|
28
30
|
},
|
|
29
31
|
'isArray': {
|
|
32
|
+
validateEmptyString: true,
|
|
30
33
|
message: 'Value was not an array.',
|
|
31
34
|
tryParse: function(x) {
|
|
32
35
|
if (x === '') return null
|
|
33
36
|
if (!util.isString(x)) return x
|
|
34
|
-
try { var parsed = JSON.parse(x) }
|
|
35
|
-
catch (e) { return x }
|
|
37
|
+
try { var parsed = JSON.parse(x) } catch (e) { return x }
|
|
36
38
|
return Array.isArray(parsed)? parsed : x
|
|
37
39
|
},
|
|
38
40
|
fn: function(x) {
|
|
@@ -40,25 +42,26 @@ module.exports = {
|
|
|
40
42
|
}
|
|
41
43
|
},
|
|
42
44
|
'isDate': {
|
|
45
|
+
validateEmptyString: true,
|
|
43
46
|
message: 'Value was not a unix timestamp.',
|
|
44
47
|
tryParse: function(x) {
|
|
45
|
-
if (util.isString(x) && x.match(/^[
|
|
48
|
+
if (util.isString(x) && x.match(/^[+-]?[0-9]+$/)) return x // keep string nums intact
|
|
46
49
|
return isNaN(parseInt(x))? x : parseInt(x)
|
|
47
50
|
},
|
|
48
51
|
fn: function(x) {
|
|
49
|
-
if (util.isString(x) && x.match(/^[
|
|
52
|
+
if (util.isString(x) && x.match(/^[+-]?[0-9]+$/)) return true
|
|
50
53
|
return typeof x === 'number' && (parseInt(x) === x)
|
|
51
54
|
}
|
|
52
55
|
},
|
|
53
56
|
'isImageObject': {
|
|
57
|
+
validateEmptyString: true,
|
|
54
58
|
message: 'Invalid image value',
|
|
55
59
|
messageLong: 'Image fields need to either be null, undefined, file, or an object containing the following '
|
|
56
60
|
+ 'fields \'{ bucket, date, filename, filesize, path, uid }\'',
|
|
57
61
|
tryParse: function(x) {
|
|
58
62
|
if (x === '') return null
|
|
59
63
|
if (!util.isString(x)) return x
|
|
60
|
-
try { var parsed = JSON.parse(x) }
|
|
61
|
-
catch (e) { return x }
|
|
64
|
+
try { var parsed = JSON.parse(x) } catch (e) { return x }
|
|
62
65
|
return parsed !== null && typeof parsed === 'object' && !(parsed instanceof Array)? parsed : x
|
|
63
66
|
},
|
|
64
67
|
fn: function(x) {
|
|
@@ -67,34 +70,36 @@ module.exports = {
|
|
|
67
70
|
}
|
|
68
71
|
},
|
|
69
72
|
'isInteger': {
|
|
73
|
+
validateEmptyString: true,
|
|
70
74
|
message: 'Value was not an integer.',
|
|
71
75
|
tryParse: function(x) {
|
|
72
|
-
if (util.isString(x) && x.match(/^[
|
|
76
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return x // keep string nums intact
|
|
73
77
|
return isNaN(parseInt(x)) || (!x && x!==0) || x===true? x : parseInt(x)
|
|
74
78
|
},
|
|
75
79
|
fn: function(x) {
|
|
76
|
-
if (util.isString(x) && x.match(/^[
|
|
80
|
+
if (util.isString(x) && x.match(/^[+-]?[0-9]+$/)) return true
|
|
77
81
|
return typeof x === 'number' && (parseInt(x) === x)
|
|
78
82
|
}
|
|
79
83
|
},
|
|
80
84
|
'isNumber': {
|
|
85
|
+
validateEmptyString: true,
|
|
81
86
|
message: 'Value was not a number.',
|
|
82
87
|
tryParse: function(x) {
|
|
83
|
-
if (util.isString(x) && x.match(/^[
|
|
88
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return x // keep string nums intact
|
|
84
89
|
return isNaN(Number(x)) || (!x && x!==0) || x===true? x : Number(x)
|
|
85
90
|
},
|
|
86
91
|
fn: function(x) {
|
|
87
|
-
if (util.isString(x) && x.match(/^[
|
|
92
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return true
|
|
88
93
|
return typeof x === 'number'
|
|
89
94
|
}
|
|
90
95
|
},
|
|
91
96
|
'isObject': {
|
|
97
|
+
validateEmptyString: true,
|
|
92
98
|
message: 'Value was not an object.',
|
|
93
99
|
tryParse: function(x) {
|
|
94
100
|
if (x === '') return null
|
|
95
101
|
if (!util.isString(x)) return x
|
|
96
|
-
try { var parsed = JSON.parse(x) }
|
|
97
|
-
catch (e) { return x }
|
|
102
|
+
try { var parsed = JSON.parse(x) } catch (e) { return x }
|
|
98
103
|
return parsed !== null && typeof parsed === 'object' && !(parsed instanceof Array)? parsed : x
|
|
99
104
|
},
|
|
100
105
|
fn: function(x) {
|
|
@@ -102,18 +107,21 @@ module.exports = {
|
|
|
102
107
|
}
|
|
103
108
|
},
|
|
104
109
|
'isString': {
|
|
110
|
+
validateEmptyString: true,
|
|
105
111
|
message: 'Value was not a string.',
|
|
106
112
|
fn: function(x) {
|
|
107
113
|
return typeof x === 'string'
|
|
108
114
|
}
|
|
109
115
|
},
|
|
110
116
|
'isAny': {
|
|
117
|
+
validateEmptyString: true,
|
|
111
118
|
message: '',
|
|
112
119
|
fn: function(x) {
|
|
113
120
|
return true
|
|
114
121
|
}
|
|
115
122
|
},
|
|
116
123
|
'isId': {
|
|
124
|
+
validateEmptyString: true,
|
|
117
125
|
message: 'Value was not a valid ObjectId.',
|
|
118
126
|
tryParse: function(x) {
|
|
119
127
|
// Try and parse value to a mongodb ObjectId
|
|
@@ -127,9 +135,8 @@ module.exports = {
|
|
|
127
135
|
return util.isObject(x) && ObjectId.isValid(x)/*x.get_inc*/? true : false
|
|
128
136
|
}
|
|
129
137
|
},
|
|
130
|
-
|
|
131
|
-
|
|
132
138
|
'max': {
|
|
139
|
+
validateEmptyString: true,
|
|
133
140
|
message: (x, arg) => 'Value was greater than the configured maximum (' + arg + ')',
|
|
134
141
|
fn: function(x, arg) {
|
|
135
142
|
if (typeof x !== 'number') { throw new Error ('Value was not a number.') }
|
|
@@ -137,6 +144,7 @@ module.exports = {
|
|
|
137
144
|
}
|
|
138
145
|
},
|
|
139
146
|
'min': {
|
|
147
|
+
validateEmptyString: true,
|
|
140
148
|
message: (x, arg) => 'Value was less than the configured minimum (' + arg + ')',
|
|
141
149
|
fn: function(x, arg) {
|
|
142
150
|
if (typeof x !== 'number') { throw new Error ('Value was not a number.') }
|
|
@@ -144,17 +152,17 @@ module.exports = {
|
|
|
144
152
|
}
|
|
145
153
|
},
|
|
146
154
|
'isNotEmptyString': {
|
|
155
|
+
validateEmptyString: true,
|
|
147
156
|
message: 'Value was an empty string.',
|
|
148
157
|
fn: function(x) {
|
|
149
158
|
return x !== ''
|
|
150
159
|
}
|
|
151
160
|
},
|
|
152
161
|
|
|
153
|
-
// Rules below ignore undefined & empty strings
|
|
162
|
+
// Rules below ignore undefined, & empty strings
|
|
154
163
|
// (e.g. an empty email field can be saved that isn't required)
|
|
155
164
|
|
|
156
165
|
'enum': {
|
|
157
|
-
ignoreEmptyString: true,
|
|
158
166
|
message: (x, arg) => 'Invalid enum value',
|
|
159
167
|
fn: function(x, arg) {
|
|
160
168
|
for (let item of arg) {
|
|
@@ -167,57 +175,46 @@ module.exports = {
|
|
|
167
175
|
// fn: function(x, arg) { return !x }
|
|
168
176
|
// },
|
|
169
177
|
'isAfter': {
|
|
170
|
-
ignoreEmptyString: true,
|
|
171
178
|
message: (x, arg) => 'Value was before the configured time (' + arg + ')',
|
|
172
179
|
fn: function(x, arg) { return validator.isAfter(x, arg) }
|
|
173
180
|
},
|
|
174
181
|
'isBefore': {
|
|
175
|
-
ignoreEmptyString: true,
|
|
176
182
|
message: (x, arg) => 'Value was after the configured time (' + arg + ')',
|
|
177
183
|
fn: function(x, arg) { return validator.isBefore(x, arg) }
|
|
178
184
|
},
|
|
179
185
|
'isCreditCard': {
|
|
180
|
-
ignoreEmptyString: true,
|
|
181
186
|
message: 'Value was not a valid credit card.',
|
|
182
187
|
fn: function(x, arg) { return validator.isCreditCard(x, arg) }
|
|
183
188
|
},
|
|
184
189
|
'isEmail': {
|
|
185
|
-
ignoreEmptyString: true,
|
|
186
190
|
message: 'Please enter a valid email address.',
|
|
187
191
|
fn: function(x, arg) { return validator.isEmail(x, arg) }
|
|
188
192
|
},
|
|
189
193
|
'isHexColor': {
|
|
190
|
-
ignoreEmptyString: true,
|
|
191
194
|
message: 'Value was not a valid hex color.',
|
|
192
195
|
fn: function(x, arg) { return validator.isHexColor(x, arg) }
|
|
193
196
|
},
|
|
194
197
|
'isIn': {
|
|
195
|
-
ignoreEmptyString: true,
|
|
196
198
|
message: (x, arg) => 'Value was not in the configured whitelist (' + arg.join(', ') + ')',
|
|
197
199
|
fn: function(x, arg) { return validator.isIn(x, arg) }
|
|
198
200
|
},
|
|
199
201
|
'isIP': {
|
|
200
|
-
ignoreEmptyString: true,
|
|
201
202
|
message: 'Value was not a valid IP address.',
|
|
202
203
|
fn: function(x, arg) { return validator.isIP(x, arg) }
|
|
203
204
|
},
|
|
204
205
|
'isNotIn': {
|
|
205
|
-
ignoreEmptyString: true,
|
|
206
206
|
message: (x, arg) => 'Value was in the configured blacklist (' + arg.join(', ') + ')',
|
|
207
207
|
fn: function(x, arg) { return !validator.isIn(x, arg) }
|
|
208
208
|
},
|
|
209
209
|
'isURL': {
|
|
210
|
-
ignoreEmptyString: true,
|
|
211
210
|
message: 'Value was not a valid URL.',
|
|
212
211
|
fn: function(x, arg) { return validator.isURL(x, arg === true? undefined : arg) }
|
|
213
212
|
},
|
|
214
213
|
'isUUID': {
|
|
215
|
-
ignoreEmptyString: true,
|
|
216
214
|
message: 'Value was not a valid UUID.',
|
|
217
215
|
fn: function(x, arg) { return validator.isUUID(x) }
|
|
218
216
|
},
|
|
219
217
|
'minLength': {
|
|
220
|
-
ignoreEmptyString: true,
|
|
221
218
|
message: function(x, arg) {
|
|
222
219
|
if (typeof x === 'string') return 'Value needs to be at least ' + arg + ' characters long.'
|
|
223
220
|
else return 'Value needs to contain a minimum of ' + arg + ' items.'
|
|
@@ -229,7 +226,6 @@ module.exports = {
|
|
|
229
226
|
}
|
|
230
227
|
},
|
|
231
228
|
'maxLength': {
|
|
232
|
-
ignoreEmptyString: true,
|
|
233
229
|
message: function(x, arg) {
|
|
234
230
|
if (typeof x === 'string') return 'Value was longer than the configured maximum length (' + arg + ')'
|
|
235
231
|
else return 'Value cannot contain more than ' + arg + ' items.'
|
|
@@ -241,7 +237,6 @@ module.exports = {
|
|
|
241
237
|
}
|
|
242
238
|
},
|
|
243
239
|
'regex': {
|
|
244
|
-
ignoreEmptyString: true,
|
|
245
240
|
message: (x, arg) => 'Value did not match the configured regular expression (' + arg + ')',
|
|
246
241
|
fn: function(x, arg) {
|
|
247
242
|
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.30.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
|
}
|