MCPcopy Create free account
hub / github.com/dadi/api / createModelChain

Function createModelChain

dadi/lib/fields/reference.js:14–46  ·  view source on GitHub ↗

* Creates an array of Model instances corresponding to a * field query, potentially including dot-notation. * * @param {Model} rootModel - instance of the base/root model * @param {Array } fields - array of node fields * @return {Array }

(rootModel, fields)

Source from the content-addressed store, hash-verified

12 * @return {Array<Model>}
13 */
14function createModelChain(rootModel, fields) {
15 return fields.reduce((chain, field, index) => {
16 if (chain.length === 0) {
17 return chain.concat(rootModel)
18 }
19
20 const nodeModel = chain[chain.length - 1]
21 const referenceField = nodeModel.getField(fields[index - 1].split('@')[0])
22
23 // Validating the node and flagging an error if invalid.
24 if (
25 !referenceField.type ||
26 !referenceField.type.length ||
27 referenceField.type.toLowerCase() !== 'reference'
28 ) {
29 return chain.concat(null)
30 }
31
32 const referenceCollection =
33 field.split('@')[1] ||
34 (referenceField.settings && referenceField.settings.collection)
35
36 // If there isn't a `settings.collection` property, we're
37 // dealing with a self-reference.
38 if (!referenceCollection) {
39 return chain.concat(nodeModel)
40 }
41
42 const referenceModel = rootModel.getForeignModel(referenceCollection)
43
44 return chain.concat(referenceModel)
45 }, [])
46}
47
48module.exports.beforeOutput = function({
49 client,

Callers 2

reference.jsFile · 0.85
processTreeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected