-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdefinition.js
More file actions
158 lines (140 loc) · 4.54 KB
/
Copy pathdefinition.js
File metadata and controls
158 lines (140 loc) · 4.54 KB
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
// Copyright IBM Corp. 2015,2019. All Rights Reserved.
// Node module: loopback-workspace
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const g = require('strong-globalize')();
module.exports = function(Definition) {
const loopback = require('loopback');
const clone = require('lodash').clone;
const debug = require('debug')('workspace:definition');
/**
* Base class for LoopBack definitions.
*
* @class Definition
* @inherits WorkspaceEntity
*/
Definition.loadFromFs = function() {
throw new Error(g.f('not implemented in %s', this.modelName));
};
Definition.saveToFs = function(cache, definitionData, cb) {
throw new Error(g.f('not implemented in %s', this.modelName));
};
Definition.toArray = function(obj, embed) {
if (!obj) return [];
if (Array.isArray(obj)) {
return obj;
} else {
return Object.keys(obj).map(function(key) {
return obj[key];
});
}
};
/**
* Get the embeded relations for a `Definition`. Only relations that specify
* a `embed` property will be included.
*
* **Embed Setting**
*
* The following is the two basic types of embeds:
*
* ```js
* "relations": { "things": { "embed": { "as": "array" } } }
* ```
*
* or
*
* ```js
* "relations": { "things": { "embed": { "as": "object", "key": "id" } } }
* ```
*
* **Relations**
*
* Each item in the relations array has the following structure:
*
* ```js
* {
* model: 'DefintionModelName', // eg. ModelDefinition
* as: 'relationPropertyName', // eg. properties
* type: 'hasMany'
* }
* ```
*
* @returns {Array} relations
*/
Definition.getEmbededRelations = function() {
const relations = this.settings.relations;
const results = [];
if (relations) {
Object
.keys(relations)
.forEach(function(name) {
const relation = relations[name];
if (relation.embed) {
results.push({
embed: relation.embed,
model: relation.model,
as: relation.embed.name || name,
type: relation.type,
foreignKey: relation.foreignKey,
});
}
});
}
return results;
};
Definition.addRelatedToCache = function(cache, fileData, facetName, fk) {
const Definition = this;
this.getEmbededRelations().forEach(function(relation) {
const relatedData = fileData[relation.as];
const Entity = loopback.getModel(relation.model);
const properties = Entity.definition.properties;
if (Array.isArray(relatedData)) {
relatedData.forEach(function(config, index) {
config[relation.foreignKey] = fk;
config.facetName = facetName;
if (relation.embed && relation.embed.includeIndex) {
config.index = index;
}
debug('addRelatedToCache %s %j', relation.model, config);
Entity.addToCache(cache, config);
});
} else if (relatedData) {
Object.keys(relatedData).forEach(function(embedId) {
let config = relatedData[embedId];
if (relation.model === 'ModelProperty' && !(config && config.type)) {
if (!config) {
// https://github.com/strongloop/loopback-workspace/issues/223
// {myProp: false} or {myProp: null} is to hide base myProp
config = {
disableInherit: true,
comments: g.f('Flag to not inherit the property from base'),
};
} else {
// expand shorthand notation
config = {type: config};
}
debug('expanded model property %s.%s defined as %j',
fileData.name, embedId, config);
}
config = Entity.getDataFromConfig(config, embedId);
// add extra properties for relations
config[relation.foreignKey] = fk;
config[relation.embed.key] = embedId;
config.facetName = facetName;
debug('addRelatedToCache %s %j', relation.model, config);
Entity.addToCache(cache, config);
});
}
});
};
Definition.addToCache = function(cache, val) {
// Remove data of embedded relations
// see https://github.com/strongloop/loopback-datasource-juggler/issues/242
const data = clone(val);
this.getEmbededRelations().forEach(function(relation) {
delete data[relation.as];
});
return Definition.base.addToCache.call(this, cache, data);
};
};