-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOnNestedQueryOptionsListener.js
More file actions
148 lines (143 loc) · 7.08 KB
/
OnNestedQueryOptionsListener.js
File metadata and controls
148 lines (143 loc) · 7.08 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
const { QueryExpression, QueryField } = require('@themost/query');
// eslint-disable-next-line no-unused-vars
const { DataEventArgs } = require('./types');
const { instanceOf } = require('./instance-of');
require('@themost/promise-sequence');
/**
* @implements {BeforeExecuteEventListener}
*/
class OnNestedQueryOptionsListener {
/**
*
* @param {DataEventArgs} event
* @param {function} callback
*/
beforeExecute(event, callback) {
OnNestedQueryOptionsListener.prototype.beforeExecuteAsync(event).then(function () {
return callback();
}).catch(function (err) {
return callback(err);
});
}
beforeExecuteAsync(event) {
const query = event.emitter && event.emitter.query;
const context = event.model.context;
if (query == null) {
return Promise.resolve();
}
// handle only select statements
if (query.$select == null) {
return Promise.resolve();
}
if (Object.prototype.hasOwnProperty.call(query, '$expand')) {
// exit if expand is null or undefined
if (query.$expand == null) {
return Promise.resolve();
}
/**
* @type {Array<{ $entity:{ model:string }}>}
*/
const expand = Array.isArray(query.$expand) ? query.$expand : [query.$expand];
if (expand.length) {
const sources = expand.map(function (item) {
return function () {
// if entity is already a query expression
if (instanceOf(item.$entity, QueryExpression)) {
// do nothing
return Promise.resolve();
}
if (item.$entity && item.$entity.model) {
// get entity alias (which is a field of current model)
let options = null;
if (item.$entity.$as != null) {
// get current model
let currentModel = event.model;
/**
* get attribute by name e.g. products.getAttributes('productionDescription')
* @type {DataField}
*/
let attribute = currentModel.getAttribute(item.$entity.$as);
if (attribute == null) {
if (item.$with) {
// find another join by name
const [key] = Object.keys(item.$with);
if (key) {
// get name e.g. orderedItem.id => orderedItem
const [name] = key.split('.');
if (name) {
// find expand by name (which is join alias)
// e.g. expand.find(x => x.$entity.$as === 'orderedItem')
const findExpand = expand.find((x) => x.$entity.$as === name);
if (findExpand) {
// get model by name
// e.g. context.model('Producn')
currentModel = context.model(findExpand.$entity.model);
if (currentModel) {
// get attribute by name
// e.g. products.getAttributes('productionDescription')
attribute = currentModel.getAttribute(item.$entity.$as);
}
}
}
}
}
if (attribute == null) {
return Promise.resolve();
}
}
const mapping = currentModel.inferMapping(attribute.name);
if (mapping == null) {
return Promise.resolve();
}
options = mapping.options;
if (options == null) {
return Promise.resolve();
}
if (options.$filter == null) {
return Promise.resolve();
}
}
/**
* @type {import('./data-model').DataModel}
*/
const nestedModel = context.model(item.$entity.model);
// if model exists
if (nestedModel != null) {
return nestedModel.filterAsync(options).then((q) => {
/**
* @typedef {object} QueryExpressionWithPrepared
* @property {*} $prepared
*/
/** @type {QueryExpressionWithPrepared} */
const { query } = q;
const collection = item.$entity.name;
Object.assign(item, {
$entity: Object.assign(query, {
$select: {
[collection]: [
new QueryField({
$name: collection.concat('.*')
})
]
},
$alias: item.$entity.$as,
$join: item.$entity.$join,
}),
model: item.$entity.model
})
return Promise.resolve();
});
}
}
return Promise.resolve();
}
});
return Promise.sequence(sources);
}
}
return Promise.resolve();
}
}
module.exports = {
OnNestedQueryOptionsListener
}