-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata-field-query-resolver.js
More file actions
192 lines (187 loc) · 9.18 KB
/
data-field-query-resolver.js
File metadata and controls
192 lines (187 loc) · 9.18 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
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
const {Args, DataError} = require('@themost/common');
const {hasOwnProperty} = require('./has-own-property');
const {QueryEntity, QueryExpression, QueryField} = require('@themost/query');
class DataFieldQueryResolver {
/**
* @param {import("./data-model").DataModel} target
*/
constructor(target) {
this.target = target;
}
/**
*
* @param {string} value
* @returns {string}
*/
formatName(value) {
if (/^\$/.test(value)) {
return value.replace(/(\$?(\w+)?)/g, '$2').replace(/\.(\w+)/g, '.$1')
}
return value;
}
nameReplacer(key, value) {
if (typeof value === 'string') {
if (/^\$(\w+)$/.test(value)) {
const baseModel = this.target.base();
const name = value.replace(/^\$/, '');
let field = null;
let collection = null;
// try to find if field belongs to base model
if (baseModel) {
field = baseModel.getAttribute(name);
collection = baseModel.viewAdapter;
}
if (field == null) {
collection = this.target.sourceAdapter;
field = this.target.getAttribute(name);
}
if (field) {
return {
$name: collection + '.' + name
}
}
throw new DataError('An expression contains an attribute that cannot be found', null, this.target.name, name);
} else if (/^\$((\w+)(\.(\w+)){1,})$/.test(value)) {
return {
$name: value.replace(/^\$/, '')
}
}
}
return value;
}
/**
* @param {import("./types").DataField} field
* @returns {{$select?: import("@themost/query").QueryField, $expand?: import("@themost/query").QueryEntity[]}|null}
*/
resolve(field) {
Args.check(field != null, new DataError('E_FIELD','Field may not be null', null, this.target.name));
if (Array.isArray(field.query) === false) {
return {
select: null,
expand: []
};
}
let expand = [];
let select = null;
const self = this;
// get base model
const baseModel = this.target.base();
for (const stage of field.query) {
if (stage.$lookup) {
// get from model
const from = stage.$lookup.from;
const fromModel = this.target.context.model(from);
if (stage.$lookup.pipeline && stage.$lookup.pipeline.length) {
stage.$lookup.pipeline.forEach(function(pipelineStage) {
if (pipelineStage.$match && pipelineStage.$match.$expr) {
const q = new QueryExpression().select('*').from(self.target.sourceAdapter);
// get expression as string
const exprString = JSON.stringify(pipelineStage.$match.$expr, function(key, value) {
if (typeof value === 'string') {
if (/\$\$(\w+)/g.test(value)) {
let localField = /\$\$(\w+)/.exec(value)[1];
let localFieldAttribute = self.target.getAttribute(localField);
if (localFieldAttribute && localFieldAttribute.model === self.target.name) {
return {
$name: self.target.sourceAdapter + '.' + localField
}
}
if (baseModel) {
localFieldAttribute = baseModel.getAttribute(localField);
if (localFieldAttribute) {
return {
$name: baseModel.viewAdapter + '.' + localField
}
}
}
throw new DataError('E_FIELD', 'Data field cannot be found', null, self.target.name, localField);
}
}
return self.nameReplacer(key, value);
});
const joinCollection = new QueryEntity(fromModel.viewAdapter).as(stage.$lookup.as).left();
Object.defineProperty(joinCollection, 'model', {
configurable: true,
enumerable: false,
writable: true,
value: fromModel.name
});
const joinExpression = Object.assign(new QueryExpression(), {
$where: JSON.parse(exprString)
});
q.join(joinCollection).with(joinExpression);
const appendExpand = [].concat(q.$expand);
expand.push.apply(expand, appendExpand);
}
});
} else {
let localField = this.formatName(stage.$lookup.localField);
if (/\./g.test(localField) === false) {
// get local field expression
let localFieldAttribute = this.target.getAttribute(localField);
if (localFieldAttribute && localFieldAttribute.model === this.target.name) {
localField = `${this.target.sourceAdapter}.${localField}`;
} else {
// get base model
const baseModel = this.target.base();
if (baseModel) {
localFieldAttribute = baseModel.getAttribute(localField);
if (localFieldAttribute) {
localField = `${baseModel.viewAdapter}.${localField}`;
}
}
}
}
const foreignField = this.formatName(stage.$lookup.foreignField);
const q = new QueryExpression().select('*').from(this.target.sourceAdapter);
Args.check(fromModel != null, new DataError('E_MODEL', 'Data model cannot be found', null, from));
const joinCollection = new QueryEntity(fromModel.viewAdapter).as(stage.$lookup.as).left();
Object.defineProperty(joinCollection, 'model', {
configurable: true,
enumerable: false,
writable: true,
value: fromModel.name
});
q.join(joinCollection).with(
new QueryExpression().where(new QueryField(localField))
.equal(new QueryField(foreignField).from(stage.$lookup.as))
);
const appendExpand = [].concat(q.$expand);
expand.push.apply(expand, appendExpand);
}
}
const name = field.property || field.name;
if (stage.$project) {
Args.check(hasOwnProperty(stage.$project, name), new DataError('E_QUERY', 'Field projection expression is missing.', null, this.target.name, field.name));
const expr = Object.getOwnPropertyDescriptor(stage.$project, name).value;
if (typeof expr === 'string') {
select = new QueryField(this.formatName(expr)).as(name)
} else {
const expr1 = Object.defineProperty({}, name, {
configurable: true,
enumerable: true,
writable: true,
value: expr
});
// Important note: Field references e.g. $customer.email
// are not supported by @themost/query@Formatter
// and should be replaced by name references e.g. { "$name": "customer.email" }
// A workaround is being used here is a regular expression replacer which
// will try to replace "$customer.email" with { "$name": "customer.email" }
// but this operation is definitely a feature request for @themost/query
const finalExpr = JSON.parse(JSON.stringify(expr1, function(key, value) {
return self.nameReplacer(key, value);
}));
select = Object.assign(new QueryField(), finalExpr);
}
}
}
return {
$select: select,
$expand: expand
}
}
}
module.exports = {
DataFieldQueryResolver
}