monastery 1.36.1 → 1.36.2
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 +2 -1
- package/.travis.yml +1 -1
- package/changelog.md +9 -0
- package/docs/{schema/rules.md → definition/field-rules.md} +6 -4
- package/docs/definition/index.md +338 -0
- package/docs/image-plugin.md +16 -16
- package/docs/manager/index.md +8 -8
- package/docs/manager/model.md +4 -3
- package/docs/manager/models.md +1 -1
- package/docs/model/find.md +31 -29
- package/docs/model/findOne.md +3 -4
- package/docs/model/findOneAndUpdate.md +42 -0
- package/docs/model/index.md +2 -2
- package/docs/model/insert.md +22 -20
- package/docs/model/remove.md +3 -3
- package/docs/model/update.md +11 -10
- package/docs/model/validate.md +22 -25
- package/docs/readme.md +5 -4
- package/lib/model-validate.js +1 -1
- package/package.json +1 -1
- package/test/crud.js +23 -23
- package/test/test.js +2 -0
- package/test/validate.js +33 -3
- package/docs/schema/index.md +0 -313
package/docs/schema/index.md
DELETED
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Schema
|
|
3
|
-
nav_order: 4
|
|
4
|
-
has_children: true
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
# Schema
|
|
8
|
-
|
|
9
|
-
Model schema object.
|
|
10
|
-
|
|
11
|
-
### Table of Contents
|
|
12
|
-
|
|
13
|
-
- [Fields](#fields)
|
|
14
|
-
- [Field blacklisting](#field-blacklisting)
|
|
15
|
-
- [Field options](#field-options)
|
|
16
|
-
- [MongoDB indexes](#mongodb-indexes)
|
|
17
|
-
- [Custom validation rules](#custom-validation-rules)
|
|
18
|
-
- [Custom error messages](#custom-error-messages)
|
|
19
|
-
- [Operation hooks](#operation-hooks)
|
|
20
|
-
- [Full example](#full-example)
|
|
21
|
-
|
|
22
|
-
### Fields
|
|
23
|
-
|
|
24
|
-
1. Fields may contain subdocuments, array of values, or an array of subdocuments
|
|
25
|
-
2. Field values need to have the `type` rule defined
|
|
26
|
-
3. Field values can contain [custom](#custom-validation-rules) and [default validation rules](./rules), e.g. `{ minLength: 2 }`
|
|
27
|
-
4. Field values can contain [field options](#field-options).
|
|
28
|
-
|
|
29
|
-
```js
|
|
30
|
-
schema.fields = {
|
|
31
|
-
name: { // value
|
|
32
|
-
type: 'string',
|
|
33
|
-
required: true
|
|
34
|
-
},
|
|
35
|
-
address: { // subdocument
|
|
36
|
-
line1: { type: 'string', required: true },
|
|
37
|
-
city: { type: 'string', minLength: 2 }
|
|
38
|
-
},
|
|
39
|
-
names: [ // array of values
|
|
40
|
-
{ type: 'string' }
|
|
41
|
-
],
|
|
42
|
-
pets: [{ // array of subdocuments
|
|
43
|
-
name: { type: 'string' },
|
|
44
|
-
type: { type: 'string' }
|
|
45
|
-
}]
|
|
46
|
-
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
These fields automatically get assigned and take presidence over any input data when [`manager.timestamps`](./manager) is true (default). You can override the `timestamps` value per operation, e.g. `db.user.update({ ..., timestamps: false})`. These fields use unix timestamps in seconds (by default), but can be configured to use use milliseconds via the manager [`useMilliseconds` ](./manager) option.
|
|
50
|
-
|
|
51
|
-
```js
|
|
52
|
-
schema.fields = {
|
|
53
|
-
createdAt: {
|
|
54
|
-
type: 'date',
|
|
55
|
-
insertOnly: true,
|
|
56
|
-
default: function() { return Math.floor(Date.now() / 1000) }
|
|
57
|
-
},
|
|
58
|
-
updatedAt: {
|
|
59
|
-
type: 'date',
|
|
60
|
-
default: function() { return Math.floor(Date.now() / 1000) }
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### Field blacklisting
|
|
66
|
-
|
|
67
|
-
You are able to provide a list of fields to blacklist per model operation.
|
|
68
|
-
|
|
69
|
-
```js
|
|
70
|
-
// The 'password' field will be removed from the results returned from `model.find`
|
|
71
|
-
schema.findBL = ['password']
|
|
72
|
-
|
|
73
|
-
// The 'password' field will be removed before inserting via `model.insert`
|
|
74
|
-
schema.insertBL = ['password']
|
|
75
|
-
|
|
76
|
-
// The 'password' and 'createdAt' fields will be removed before updating via `model.update`
|
|
77
|
-
schema.updateBL = ['createdAt', 'password']
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
You are also able to blacklist nested fields within subdocuments and arrays of subdocuments.
|
|
81
|
-
|
|
82
|
-
```js
|
|
83
|
-
// Subdocument example: `address.city` will be excluded from the response
|
|
84
|
-
schema.findBL = ['address.city']
|
|
85
|
-
|
|
86
|
-
// Array of subdocuments example: `meta` will be removed from each comment in the array
|
|
87
|
-
schema.findBL = ['comments.meta']
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### Field options
|
|
91
|
-
|
|
92
|
-
Here are some other special field options that can be used alongside validation rules.
|
|
93
|
-
|
|
94
|
-
```js
|
|
95
|
-
let fieldName = {
|
|
96
|
-
|
|
97
|
-
// Enables population, you would save the foreign document _id on this field.
|
|
98
|
-
model: 'pet',
|
|
99
|
-
|
|
100
|
-
// Field will only be allowed to be set on insert when calling model.insert
|
|
101
|
-
insertOnly: true,
|
|
102
|
-
|
|
103
|
-
// Default will always override any passed value (it has some use-cases)
|
|
104
|
-
defaultOverride: true,
|
|
105
|
-
|
|
106
|
-
// Default value
|
|
107
|
-
default: 12,
|
|
108
|
-
|
|
109
|
-
// Default value can be returned from a function. `this` refers to the data object, but be
|
|
110
|
-
// sure to pass any referenced default fields along with insert/update/validate, e.g. `this.age`
|
|
111
|
-
default: function(fieldName, model) { return `I'm ${this.age} years old` },
|
|
112
|
-
|
|
113
|
-
// Monastery will automatically create a mongodb index for this field, see "MongoDB indexes"
|
|
114
|
-
// below for more information
|
|
115
|
-
index: true|1|-1|'text'|'unique'|Object,
|
|
116
|
-
|
|
117
|
-
// The field won't stored, handy for fields that get populated with documents, see ./find for more details
|
|
118
|
-
virtual: true
|
|
119
|
-
}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
### MongoDB indexes
|
|
123
|
-
|
|
124
|
-
You are able to automatically setup MongoDB indexes via the `index` field option.
|
|
125
|
-
|
|
126
|
-
```js
|
|
127
|
-
let fieldName = {
|
|
128
|
-
// This will create an ascending / descending index for this field
|
|
129
|
-
index: true|1|-1,
|
|
130
|
-
|
|
131
|
-
// This will create an ascending unique index which translates:
|
|
132
|
-
// { key: { [fieldName]: 1 }, unique: true }
|
|
133
|
-
index: 'unique',
|
|
134
|
-
|
|
135
|
-
// Text indexes are handled a little differently in which all the fields on the model
|
|
136
|
-
// schema that have a `index: 'text` set are collated into one index, e.g.
|
|
137
|
-
// { key: { [fieldName1]: 'text', [fieldName2]: 'text', .. }}
|
|
138
|
-
index: 'text'
|
|
139
|
-
|
|
140
|
-
// You can also pass an object if you need to use mongodb's index options
|
|
141
|
-
// https://docs.mongodb.com/manual/reference/command/createIndexes/
|
|
142
|
-
// https://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#createIndexes
|
|
143
|
-
index: { type: 1, ...(any mongodb index option) },
|
|
144
|
-
}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
And here's how you would use a 2dsphere index, e.g.
|
|
148
|
-
|
|
149
|
-
```js
|
|
150
|
-
schema.fields = {
|
|
151
|
-
name: {
|
|
152
|
-
type: 'string',
|
|
153
|
-
required: true
|
|
154
|
-
},
|
|
155
|
-
location: {
|
|
156
|
-
index: '2dsphere',
|
|
157
|
-
type: { type: 'string', default: 'Point' },
|
|
158
|
-
coordinates: [{ type: 'number' }] // lng, lat
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Inserting a 2dsphere point
|
|
163
|
-
await db.user.insert({
|
|
164
|
-
data: {
|
|
165
|
-
location: { coordinates: [170.2628528648167, -43.59467883784971] }
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
Since unique indexes by default don't allow mutliple documents with `null`, you use a partial index (less performant), e.g.
|
|
171
|
-
|
|
172
|
-
```js
|
|
173
|
-
|
|
174
|
-
schema.fields = {
|
|
175
|
-
index: {
|
|
176
|
-
name: {
|
|
177
|
-
type: 'string',
|
|
178
|
-
index: {
|
|
179
|
-
type: 'unique',
|
|
180
|
-
partialFilterExpression: {
|
|
181
|
-
email: { $type: 'string' }
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
### Custom validation rules
|
|
189
|
-
|
|
190
|
-
You are able to define custom validation rules to use. (`this` will refer to the data object passed in)
|
|
191
|
-
|
|
192
|
-
```js
|
|
193
|
-
schema.rules = {
|
|
194
|
-
// Basic definition
|
|
195
|
-
isGrandMaster: function(value, ruleArgument, path, model) {
|
|
196
|
-
return (value == 'Martin Luther')? true : false
|
|
197
|
-
},
|
|
198
|
-
// Full definition
|
|
199
|
-
isGrandMaster: {
|
|
200
|
-
message: (value, ruleArgument, path, model) => 'Only grand masters are permitted'
|
|
201
|
-
fn: function(value, ruleArgument, path, model) {
|
|
202
|
-
return (value == 'Martin Luther' || this.age > 100)? true : false
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// And referencing is the same as any other builtin rule
|
|
208
|
-
schema.fields = {
|
|
209
|
-
user: {
|
|
210
|
-
name: {
|
|
211
|
-
type: 'string'
|
|
212
|
-
isGrandMaster: true // true is the ruleArgument
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Additionally, you can define custom messages here
|
|
218
|
-
schema.messages = {
|
|
219
|
-
'user.name': {
|
|
220
|
-
isGrandMaster: 'Only grand masters are permitted'
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
### Custom error messages
|
|
226
|
-
|
|
227
|
-
You are able to define custom error messages for each validation rule.
|
|
228
|
-
|
|
229
|
-
```js
|
|
230
|
-
schema.messages = {
|
|
231
|
-
'name': {
|
|
232
|
-
required: 'Sorry, even a monk cannot be nameless'
|
|
233
|
-
type: 'Sorry, your name needs to be a string'
|
|
234
|
-
},
|
|
235
|
-
'address.city': {
|
|
236
|
-
minLength: (value, ruleArgument, path, model) => {
|
|
237
|
-
return `Is your city of residence really only ${ruleArgument} characters long?`
|
|
238
|
-
}
|
|
239
|
-
},
|
|
240
|
-
// You can assign custom error messages for all subdocument fields in an array
|
|
241
|
-
// e.g. pets = [{ name: { type: 'string' }}]
|
|
242
|
-
'pets.name': {
|
|
243
|
-
required: `Your pet's name needs to be a string.`
|
|
244
|
-
}
|
|
245
|
-
// To target a specific array item
|
|
246
|
-
'pets.0.name': {
|
|
247
|
-
required: `You first pet needs a name`
|
|
248
|
-
}
|
|
249
|
-
// You can also target any rules set on the array or sub arrays
|
|
250
|
-
// e.g.
|
|
251
|
-
// // let arrayWithSchema = (array, schema) => { array.schema = schema; return array }, OR you can use db.arrayWithSchema
|
|
252
|
-
// petGroups = db.arrayWithSchema(
|
|
253
|
-
// [db.arrayWithSchema(
|
|
254
|
-
// [{ name: { type: 'string' }}],
|
|
255
|
-
// { minLength: 1 }
|
|
256
|
-
// )],
|
|
257
|
-
// { minLength: 1 }
|
|
258
|
-
// )
|
|
259
|
-
'petGroups': {
|
|
260
|
-
minLength: `Please add at least one pet pet group.`
|
|
261
|
-
}
|
|
262
|
-
'petGroups.$': {
|
|
263
|
-
minLength: `Please add at least one pet into your pet group.`
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
### Operation hooks
|
|
269
|
-
|
|
270
|
-
You are able provide an array of callbacks to these model operation hooks. If you need to throw an error asynchronously, please pass an error as the first argument to `next()`, e.g. `next(new Error('Your error here'))`. You can also access the operation details via `this` in each callback.
|
|
271
|
-
|
|
272
|
-
```js
|
|
273
|
-
schema.afterFind = [function(data, next) {}]
|
|
274
|
-
schema.afterInsert = [function(data, next) {}]
|
|
275
|
-
schema.afterInsertUpdate = [function(data, next) {}]
|
|
276
|
-
schema.afterUpdate = [function(data, next) {}]
|
|
277
|
-
schema.afterRemove = [function(next) {}]
|
|
278
|
-
schema.beforeInsert = [function(data, next) {}]
|
|
279
|
-
schema.beforeInsertUpdate = [function(data, next) {}]
|
|
280
|
-
schema.beforeUpdate = [function(data, next) {}]
|
|
281
|
-
schema.beforeRemove = [function(next) {}]
|
|
282
|
-
schema.beforeValidate = [function(data, next) {}]
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
### Full example
|
|
286
|
-
|
|
287
|
-
```js
|
|
288
|
-
let schema = {
|
|
289
|
-
fields: {
|
|
290
|
-
email: { type: 'email', required: true, index: 'unique' },
|
|
291
|
-
firstName: { type: 'string', required: true },
|
|
292
|
-
lastName: { type: 'string' }
|
|
293
|
-
},
|
|
294
|
-
|
|
295
|
-
messages: {
|
|
296
|
-
email: { required: 'Please enter an email.' }
|
|
297
|
-
},
|
|
298
|
-
|
|
299
|
-
updateBL: ['email'],
|
|
300
|
-
|
|
301
|
-
beforeValidate: [function (data, next) {
|
|
302
|
-
if (data.firstName) data.firstName = util.ucFirst(data.firstName)
|
|
303
|
-
if (data.lastName) data.lastName = util.ucFirst(data.lastName)
|
|
304
|
-
next()
|
|
305
|
-
}],
|
|
306
|
-
|
|
307
|
-
afterFind: [function(data) {// Synchronous
|
|
308
|
-
data = data || {}
|
|
309
|
-
data.name = data.firstName + ' ' + data.lastName
|
|
310
|
-
}]
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
```
|