Skip to content

Commit

Permalink
set query expressions for nested queries (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarbounakis authored Aug 24, 2024
1 parent 6231278 commit 44f7839
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions OnNestedQueryOptionsListener.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { QueryExpression } = require('@themost/query');
const { QueryExpression, QueryField } = require('@themost/query');
// eslint-disable-next-line no-unused-vars
const { DataEventArgs } = require('./types');
const { instanceOf } = require('./instance-of');
Expand Down Expand Up @@ -106,26 +106,28 @@ class OnNestedQueryOptionsListener {
const nestedModel = context.model(item.$entity.model);
// if model exists
if (nestedModel != null) {
if (item.$entity.$as != null) {
// change view to follow entity alias defined by the current query
// this operation will be used while parsing filter and creating a new query expression
nestedModel.view = item.$entity.$as;
}
return nestedModel.filterAsync(options).then((q) => {
/**
* @typedef {object} QueryExpressionWithPrepared
* @property {*} $prepared
*/
/** @type {QueryExpressionWithPrepared} */
const { query } = q;
if (query && query.$prepared) {
item.$with = {
$and: [
item.$with,
query.$prepared
]
}
}
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();
});
}
Expand Down

0 comments on commit 44f7839

Please sign in to comment.