-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·864 lines (744 loc) · 21.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
'use strict'
const config = require('./../../../config')
const Connection = require('./connection')
const dadiMetadata = require('@dadi/metadata')
const deepMerge = require('deepmerge')
const fields = require('./../fields')
const History = require('./history')
const logger = require('@dadi/logger')
const Search = require('./../search')
const Validator = require('@dadi/api-validator')
/**
* Block with metadata pertaining to an API collection.
*
* @typedef {Object} Metadata
* @property {Number} page - current page
* @property {Number} offset - offset from start of collection
* @property {Number} totalCount - total number of documents
* @property {Number} totalPages - total number of pages
* @property {Number} nextPage - number of next available page
* @property {Number} prevPage - number of previous available page
*/
/**
* @typedef {Object} ResultSet
* @property {Metadata} metadata - object with collection metadata
* @property {Array} results - list of documents
*/
// Pool of initialised models.
let _models = {}
/**
* Creates a new Model instance
* @constructor
* @classdesc
*/
const Model = function (name, schema, connection, settings) {
this.internalProperties = [
'_apiVersion',
'_composed',
'_createdAt',
'_createdBy',
'_history',
'_id',
'_lastModifiedAt',
'_lastModifiedBy',
'_version'
]
this.acl = require('./acl')
// Attach collection name.
this.name = name
// Attach original schema.
if (_models[name] && (!schema || !Object.keys(schema).length)) {
this.schema = _models[name].schema
} else {
this.schema = schema
}
// Attach default settings.
this.settings = Object.assign({}, settings, this.schema.settings)
// Set ACL key
this.aclKey = settings.aclKey || `collection:${settings.database}_${name}`
// Attach display name if supplied.
if (this.settings.displayName) {
this.displayName = this.settings.displayName
}
// Composable reference fields.
if (this.settings.compose) {
this.compose = this.settings.compose
}
// setup search context
this.searchHandler = new Search(this)
if (this.searchHandler.canUse()) {
this.searchHandler.init()
}
// Add any configured indexes.
if (this.settings.index && !Array.isArray(this.settings.index)) {
this.settings.index = [
{
keys: this.settings.index.keys,
options: this.settings.index.options || {}
}
]
}
// Setup history context unless requested not to.
this.storeRevisions = this.settings.storeRevisions !== false
if (this.storeRevisions) {
this.history = new History(this)
// Define the name of the revision collection for this model.
// If no value is specified, use the name of the model with
// the 'History' suffix.
this.revisionCollection = this.settings.revisionCollection
? this.settings.revisionCollection
: this.name + 'History'
}
// Create connection for this model.
if (connection) {
this.connection = connection
} else {
let connectionOptions = {
collection: this.name,
database: this.settings.database,
revisionCollection: this.revisionCollection
}
this.connection = Connection(
connectionOptions,
this.name,
config.get('datastore')
)
}
this.connection.setMaxListeners(35)
if (config.get('env') !== 'test') {
this.connection.once('disconnect', err => {
logger.error({module: 'model'}, err)
})
}
// Save reference to this model in the pool.
_models[name] = this
// Setup validatior.
this.validator = new Validator({
i18nFieldCharacter: config.get('i18n.fieldCharacter'),
internalFieldsPrefix: config.get('internalFieldsPrefix')
})
// Create indexes.
if (this.settings.index) {
this.createIndex()
}
// Compile a list of hooks by field type.
this.hooks = this._compileFieldHooks()
}
/**
* Builds an empty response in the expected format, containing
* a results array and a metadata block.
*
* @param {Object} options
* @return {ResultSet}
*/
Model.prototype._buildEmptyResponse = function (options = {}) {
return {
results: [],
metadata: dadiMetadata(options, 0)
}
}
/**
* Creates an object containing an array of field hooks grouped
* by field type.
*
* @return {Object}
*/
Model.prototype._compileFieldHooks = function () {
let hooks = {}
Object.keys(fields).forEach(key => {
let type = fields[key].type
// Exit if the field doesn't export a `type` property.
if (!type) return
// Ensure `type` is an array.
if (!Array.isArray(type)) {
type = [type]
}
type.forEach(item => {
let sanitisedItem = item.toString().toLowerCase()
hooks[sanitisedItem] = hooks[sanitisedItem] || []
hooks[sanitisedItem].push(fields[key])
})
})
return hooks
}
/**
* Creates a connection error object.
*
* @param {String} message
* @return Error
* @api private
*/
Model.prototype._createValidationError = function (message, data) {
const error = new Error(message || 'Model Validation Failed')
error.statusCode = 400
error.success = false
if (data) {
error.errors = data
}
return error
}
/**
* Formats a result for being sent to the client (formatForInput = false)
* or to be fed into the model (formatForInput = true)
*
* @param {Object} results
* @param {Boolean} formatForInput
* @param {Object} data - data object for hooks
* @return {Promise<ResultSet>}
* @api private
*/
Model.prototype._formatResultSet = function (
results,
formatForInput,
data = {}
) {
const multiple = Array.isArray(results)
const documents = multiple ? results : [results]
const prefixes = {
from: formatForInput
? config.get('internalFieldsPrefix')
: '_',
to: formatForInput
? '_'
: config.get('internalFieldsPrefix')
}
return Promise.all(
documents.map(document => {
if (!document) return null
if (typeof document === 'string') {
return document
}
return Object.keys(document).sort().reduce((result, field) => {
return result.then(newDocument => {
let hookName = formatForInput
? 'beforeSave'
: 'beforeOutput'
// The hook will receive the portion of the document that
// corresponds to the field in question, including any language
// variations.
let subDocument = Object.keys(document)
.reduce((subDocument, rawField) => {
let canonicalField = rawField.split(
config.get('i18n.fieldCharacter')
)[0]
if (canonicalField === field) {
subDocument[rawField] = document[rawField]
}
return subDocument
}, {})
return this.runFieldHooks({
config,
data: Object.assign({}, data, { document }),
input: subDocument,
field,
name: hookName
}).then(subDocument => {
// Doing a shallow merge (i.e. `Object.assign`) isn't enough here,
// because several fields might need to write to the same property
// in the document (e.g. `_composed`). We need a deep merge.
return deepMerge(newDocument, subDocument)
})
})
}, Promise.resolve({})).then(document => {
let internals = this.connection.db.settings.internalProperties || []
return Object.keys(document).sort().reduce((sanitisedDocument, field) => {
const property = field.indexOf(prefixes.from) === 0
? prefixes.to + field.slice(1)
: field
// Stripping null values from the response.
if (document[field] === null) {
return sanitisedDocument
}
// Stripping internal properties (other than `_id`)
if ((field !== '_id') && internals.includes(field)) {
return sanitisedDocument
}
sanitisedDocument[property] = document[field]
return sanitisedDocument
}, {})
})
})
).then(newResultSet => {
return multiple ? newResultSet : newResultSet[0]
})
}
/**
* Merges the value of `compose` in the schema settings
* with a URL override and returns the computed value.
*
* @param {Boolean} override
* @return {Boolean}
*/
Model.prototype._getComposeValue = function (override) {
let rawValue = override !== undefined
? override
: this.settings.compose
if (!rawValue) return 0
switch (rawValue.toString()) {
case 'true': return 1
case 'all': return Infinity
default: return parseInt(rawValue)
}
}
/**
* Returns the name of the id-collection mapping field
* for a given reference field.
*
* @param {String} fieldName - name of the reference field
* @return {String}
*/
Model.prototype._getIdMappingName = function (fieldName) {
return `_ref${fieldName[0].toUpperCase()}${fieldName.slice(1)}`
}
/**
* Attaches the full history of each document and returns
* the modified result set.
*
* @return {ResultSet}
* @api private
*/
Model.prototype._injectHistory = function (data, options) {
return new Promise((resolve, reject) => {
if (data.results.length === 0) {
return resolve(data)
}
data.results.forEach((doc, idx) => {
this.revisions(doc._id, options, (err, history) => {
if (err) logger.error({module: 'model'}, err)
this.formatForOutput(history).then(formattedHistory => {
doc._history = formattedHistory
if (idx === data.results.length - 1) {
return resolve(data)
}
})
})
})
})
}
/**
* Takes two sets of field projection fields, one from a query and the
* other from an ACL rule that will affect the query, and combines them
* into one.
*
* @param {Object} query
* @param {Object} acl
* @return {Object}
*/
Model.prototype._mergeQueryAndAclFields = function (query, acl) {
if (!query || !Object.keys(query).length) {
return acl
}
if (!acl || !Object.keys(acl).length) {
return query
}
const isExclusion = fields => {
return Object.keys(fields).some(field => {
return field !== '_id' && fields[field] === 0
})
}
let result
let queryIsExclusion = isExclusion(query)
let aclIsExclusion = isExclusion(acl)
if (queryIsExclusion) {
if (aclIsExclusion) {
result = Object.assign({}, query)
Object.keys(acl).forEach(field => {
result[field] = acl[field]
})
} else {
result = {}
Object.keys(acl).forEach(field => {
if (query[field] === undefined) {
result[field] = acl[field]
}
})
}
} else {
if (aclIsExclusion) {
result = {}
Object.keys(query).forEach(field => {
if (acl[field] === undefined) {
result[field] = query[field]
}
})
} else {
result = {}
Object.keys(query).forEach(field => {
if (acl[field]) {
result[field] = query[field]
}
})
if (Object.keys(result).length === 0) {
throw new Error('Empty field set')
}
}
}
return result
}
/**
* Transforms a query for execution, running all field hooks.
*
* @param {Object} query
* @return {Promise<Object>} transformed query
*/
Model.prototype._transformQuery = function (query, options) {
let result = Promise.resolve({})
let canonicalQuery = Object.keys(query).reduce((canonical, key) => {
let rootNode = key.split('.')[0].split('@')[0]
canonical[rootNode] = canonical[rootNode] || {}
canonical[rootNode][key] = query[key]
return canonical
}, {})
Object.keys(canonicalQuery).forEach(rootField => {
result = result.then(transformedQuery => {
return this.runFieldHooks({
data: { options },
field: rootField,
input: canonicalQuery[rootField],
name: 'beforeQuery'
}).then(subQuery => {
return Object.assign({}, transformedQuery, subQuery)
})
})
})
return result
}
/**
* Formats a result set before it's fed into the model for insertion/update.
*
* @param {Object} results
* @param {Object} data - data object for hooks
* @return {ResultSet}
* @api public
*/
Model.prototype.formatForInput = function (results, data = {}) {
return this._formatResultSet(results, true, data)
}
/**
* Formats a result set before it's sent to the client.
*
* @param {Object} results
* @param {Object} data - data object for hooks
* @return {ResultSet}
* @api public
*/
Model.prototype.formatForOutput = function (results, data = {}) {
return this._formatResultSet(results, false, data)
}
/**
* Performs a last round of formatting to the query before it's
* delivered to the data adapters
*
* @param {Object} query
* @return An object representing the formatted query
* @api public
*/
Model.prototype.formatQuery = function (query) {
let internalFieldsPrefix = config.get('internalFieldsPrefix')
let newQuery = {}
Object.keys(query).forEach(key => {
if (
internalFieldsPrefix !== '_' &&
key.indexOf(internalFieldsPrefix) === 0
) {
newQuery['_' + key.slice(1)] = query[key]
} else {
newQuery[key] = query[key]
}
})
return newQuery
}
/**
* Returns the field with a given name, if it exists.
*
* @param {String} name
* @return {Object} the field schema
*/
Model.prototype.getField = function (name) {
return this.schema[name]
}
/**
* Returns the lower-cased type of a field if it exists in the
* collection, or `undefined` if it doesn't.
*
* @param {String} field - name of the field
* @param {Object} schema - collection schema
* @return {String} the field type
*/
Model.prototype.getFieldType = function (field) {
if (
!this.getField(field) ||
!this.getField(field).type ||
!this.getField(field).type.length
) {
return undefined
}
return this.getField(field).type.toLowerCase()
}
/**
* Returns a reference to the model of another collection.
*
* @param {String} name - name of the collection
* @return {Model}
*/
Model.prototype.getForeignModel = function (name) {
return _models[name]
}
/**
* Determines whether the given string is a valid key for
* the model
*
* @param {String} key
* @return A Boolean indicating whether the key is valid
* @api public
*/
Model.prototype.isKeyValid = function (key) {
if (key === '_id' || this.schema[key] !== undefined) {
return true
}
// Check for dot-notation, verifying the existence of the
// root node.
let rootNode = key.split('.')[0]
return Boolean(this.schema[rootNode])
}
/**
* Strips all the internal properties from a document.
*
* @param {Object} document
* @return {Object} sanitised document
*/
Model.prototype.removeInternalProperties = function (document) {
return Object.keys(document).reduce((output, field) => {
if (!this.internalProperties.includes(field)) {
output[field] = document[field]
}
return output
}, {})
}
/**
* Runs all hooks with a given name associated with a field,
* returning a Promise with the result.
*
* @param {Object} data - optional data object
* @param {String} field - field name
* @param {Object} input - hook input
* @param {String} name - hook name
* @return {Promise<Object>} input after hooks
*/
Model.prototype.runFieldHooks = function ({
data = {},
field,
input,
name
}) {
let fieldType = this.getFieldType(field)
let queue = Promise.resolve(input)
if (!this.hooks[fieldType]) {
return Promise.resolve(input)
}
this.hooks[fieldType].forEach(hook => {
if (typeof hook[name] === 'function') {
queue = queue.then(query => {
return hook[name].call(
this,
Object.assign({}, data, {
config,
field,
input,
schema: this.getField(field)
})
)
})
}
})
return queue.catch(error => {
let errorData = [
{
field,
message: error.message
}
]
logger.error({module: 'field hooks'}, error)
return Promise.reject(
this._createValidationError('Validation failed', errorData)
)
})
}
/**
* Returns whether the current level of nested reference fields
* should be composed, based on the collection settings, the level
* of nesting and an override value of `compose`, coming from the URL.
*
* @param {Number} options.level
* @param {Boolean} options.composeOverride
* @return {Boolean}
*/
Model.prototype.shouldCompose = function ({
level = 1,
composeOverride = false
}) {
// A value of 'all' enables composition on every level.
if (composeOverride === 'all') return true
// If `compose` is `false`, we disable composition.
if (composeOverride === 'false') return false
let overrideString = composeOverride.toString()
let overrideNumber = parseInt(composeOverride)
// If the value is a number, we compose up to that level.
if (overrideNumber.toString() === overrideString) {
return level <= overrideNumber
}
// If `compose` is `true`, we compose on the first level
// and then rely on the collection-wide settings for the
// remaining levels.
if (overrideString === 'true' && level === 1) {
return true
}
return Boolean(this.settings.compose)
}
/**
* This is a convenience method for determining whether a client has
* access to a client resource, as well as creating or augmenting the
* a series of variables useful for processing a query with ACL.
*
* It receives a client object ({clientId, accessType}), a fields and
* query objects, and an access type (e.g. 'read'). If the client
* does not have permission to access the resource, the resulting
* Promise is rejected with an 'UNAUTHORISED' error; otherwise, the
* Promise is resolved with a {fields, query} object, with the
* augmented fields and query objects.
*
* @param {Object} options.client
* @param {Object} options.fields
* @param {Object} options.query
* @param {Object} options.schema
* @param {String} options.type
* @return {Promise}
*/
Model.prototype.validateAccess = function ({
client,
fields = {},
query = {},
schema = this.schema,
type
}) {
if (!client) {
return Promise.resolve({fields, query, schema})
}
if (this.settings) {
// If the collection has an `authenticate` property and it's set to
// `false`, then access is granted.
if (this.settings.authenticate === false) {
return Promise.resolve({
fields,
query,
schema
})
}
// If the collection has an `authenticate` property and it's an array,
// we must check the type of access that is being attempted against the
// list of HTTP verbs that must be authenticated.
if (Array.isArray(this.settings.authenticate)) {
let authenticatedVerbs = this.settings.authenticate.map(
s => s.toLowerCase()
)
if (
(type === 'create' && !authenticatedVerbs.includes('post')) ||
(type === 'delete' && !authenticatedVerbs.includes('delete')) ||
(type === 'read' && !authenticatedVerbs.includes('get')) ||
(type === 'update' && !authenticatedVerbs.includes('put'))
) {
return Promise.resolve({
fields,
query,
schema
})
}
}
}
return this.acl.access.get(client, this.aclKey).then(access => {
let value = access[type]
if (!value) {
return Promise.reject(
this.acl.createError(client)
)
}
if (value.filter) {
let conflict = Object.keys(value.filter).some(field => {
return (
query[field] !== undefined &&
JSON.stringify(query[field]) !== JSON.stringify(value.filter[field])
)
})
query = conflict
? new Error('EMPTY_RESULT_SET')
: Object.assign({}, query, value.filter)
}
if (value.fields) {
try {
fields = this._mergeQueryAndAclFields(
fields,
value.fields
)
} catch (err) {
return Promise.reject(err)
}
}
let newSchema = this.acl.access.filterFields(access, schema)
return {fields, query, schema: newSchema}
})
}
/**
* Validates a query object and returns an object with `success`
* indicating whether validation has failed or passed, and an
* `errors` array with any resulting validation errors.
*
* @param {Object} query
* @return {Object}
*/
Model.prototype.validateQuery = function (query) {
let response = {
success: true,
errors: []
}
if (!Array.isArray(query) && Object(query) !== query) {
response.success = false
response.errors.push({
message: 'Query must be either a JSON array or a JSON object.'
})
return response
}
Object.keys(query).every(key => {
if (key === '$where') {
response.success = false
response.errors.push({
message: `'$where' is not a valid query operator`
})
}
})
return response
}
Model.prototype.count = require('./collections/count')
Model.prototype.create = require('./collections/create')
Model.prototype.createIndex = require('./collections/createIndex')
Model.prototype.delete = require('./collections/delete')
Model.prototype.find = require('./collections/find')
Model.prototype.get = require('./collections/get')
Model.prototype.getIndexes = require('./collections/getIndexes')
Model.prototype.getRevisions = require('./collections/getRevisions')
Model.prototype.getStats = require('./collections/getStats')
Model.prototype.revisions = require('./collections/getRevisions') // (!) Deprecated in favour of `getRevisions`
Model.prototype.stats = require('./collections/getStats') // (!) Deprecated in favour of `getStats`
Model.prototype.update = require('./collections/update')
Model.prototype.search = require('./search')
module.exports = function (name, schema, connection, settings) {
if (schema) {
return new Model(
name,
schema,
connection,
settings
)
}
return _models[name]
}
module.exports.Model = Model