-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(datasource-mongoose): don't return records for null values of fla…
…ttened fields when using asModel on object fields (#853)
- Loading branch information
Guillaume Gautreau
authored
Oct 17, 2023
1 parent
5a86953
commit d4b3f0c
Showing
4 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/datasource-mongoose/src/utils/pipeline/as-model-not-null.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Model, PipelineStage } from 'mongoose'; | ||
|
||
/** | ||
* Generate pipeline to query submodels. | ||
* | ||
* The operations make rotations in the documents so that the root is changed to the submodel | ||
* without loosing the parent (which may be needed later on). | ||
*/ | ||
export default class AsModelNotNullGenerator { | ||
static asModelNotNull( | ||
model: Model<unknown>, | ||
stack: { prefix: string | null; asFields: string[]; asModels: string[] }[], | ||
): PipelineStage[] { | ||
return stack.flatMap(({ prefix, asModels }) => { | ||
return asModels.map( | ||
(asModel): PipelineStage => ({ | ||
$match: { | ||
[[prefix, asModel].filter(Boolean).join('.')]: { $ne: null }, | ||
}, | ||
}), | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters