@oridune/epic-odm
Advanced tools
Comparing version 1.0.27 to 1.0.28
{ | ||
"name": "@oridune/epic-odm", | ||
"version": "1.0.27", | ||
"version": "1.0.28", | ||
"description": "Install 1 ODM and code once with any database driver.", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -32,2 +32,47 @@ "use strict"; | ||
const utils_1 = require("../lib/utils"); | ||
// Project Resolver Function | ||
const normalizeProject = (project) => { | ||
project = utils_1.Utils.transposeObjectToDotNotation(project); | ||
if (Object.values(project).includes(1)) | ||
for (const [Key, Value] of Object.entries(project)) | ||
if (!Value) | ||
delete project[Key]; | ||
return project; | ||
}; | ||
// Reference Stages Creator Function | ||
const createReferenceStages = (model, conditions, project) => { | ||
// Get Model Options | ||
const ModelOptions = (0, model_1.getModelOptions)(model); | ||
// Empty Stages Array | ||
let Stages = []; | ||
Object.values(ModelOptions.relations).map((relation) => { | ||
// Get Model Options | ||
const RelationModelOptions = (0, model_1.getModelOptions)(relation.reference()); | ||
const ProjectItem = project[relation.name]; | ||
if (typeof ProjectItem === "object") | ||
Stages = [ | ||
...Stages, | ||
{ | ||
$lookup: { | ||
from: RelationModelOptions.getResolvedName(), | ||
localField: relation.name, | ||
foreignField: "_id", | ||
as: relation.name, | ||
pipeline: createReferenceStages(relation.reference(), conditions, ProjectItem), | ||
}, | ||
}, | ||
...(["OneToOne", "ManyToOne"].includes(relation.type) | ||
? [ | ||
{ | ||
$unwind: { | ||
path: `$${relation.name}`, | ||
preserveNullAndEmptyArrays: true, | ||
}, | ||
}, | ||
] | ||
: []), | ||
]; | ||
}); | ||
return Stages; | ||
}; | ||
class MongoDBDriver extends driver_1.DatabaseDriver { | ||
@@ -161,47 +206,4 @@ constructor(models, uri, Config = {}, Logs = false) { | ||
.collection(ModelOptions.getResolvedName()); | ||
// Project Resolver Function | ||
const normalizeProject = (project) => { | ||
project = utils_1.Utils.transposeObjectToDotNotation(project); | ||
if (Object.values(project).includes(1)) | ||
for (const [Key, Value] of Object.entries(project)) | ||
if (!Value) | ||
delete project[Key]; | ||
return project; | ||
}; | ||
// Reference Stages Creator Function | ||
const createReferenceStages = (model, project) => { | ||
// Get Model Options | ||
const ModelOptions = (0, model_1.getModelOptions)(model); | ||
// Empty Stages Array | ||
let Stages = []; | ||
Object.values(ModelOptions.relations).map((relation) => { | ||
// Get Model Options | ||
const RelationModelOptions = (0, model_1.getModelOptions)(relation.reference()); | ||
const ProjectItem = project[relation.name]; | ||
if (typeof ProjectItem === "object") | ||
Stages = [ | ||
...Stages, | ||
{ | ||
$lookup: { | ||
from: RelationModelOptions.getResolvedName(), | ||
localField: relation.name, | ||
foreignField: "_id", | ||
as: relation.name, | ||
pipeline: createReferenceStages(relation.reference(), ProjectItem), | ||
}, | ||
}, | ||
...(["OneToOne", "ManyToOne"].includes(relation.type) | ||
? [ | ||
{ | ||
$unwind: { | ||
path: `$${relation.name}`, | ||
preserveNullAndEmptyArrays: true, | ||
}, | ||
}, | ||
] | ||
: []), | ||
]; | ||
}); | ||
return Stages; | ||
}; | ||
// Normalize Projection | ||
const Projection = normalizeProject(project.getFieldsList()); | ||
// Create a Cursor | ||
@@ -221,16 +223,28 @@ const Cursor = Collection.aggregate([ | ||
: []), | ||
// Initial Where Conditions Stage | ||
...(aggregate.where instanceof Array | ||
? aggregate.where | ||
: typeof aggregate.where === "object" | ||
? [aggregate.where] | ||
: []).map((condition) => ({ | ||
$match: { | ||
$or: (condition instanceof Array ? condition : [condition]).map((condition) => [ | ||
...Object.keys(ModelOptions.fields), | ||
...Object.keys(ModelOptions.embeds), | ||
].reduce((obj, field) => { | ||
const Value = condition[field]; | ||
return Value === undefined ? obj : Object.assign(Object.assign({}, obj), { [field]: Value }); | ||
}, {})), | ||
}, | ||
})), | ||
// Reference Stage | ||
...createReferenceStages(model, project.getFieldsList()), | ||
// Conditions Stage | ||
...createReferenceStages(model, aggregate.where, project.getFieldsList()), | ||
// Where Conditions Stage | ||
...(aggregate.where instanceof Array | ||
? aggregate.where.map((where) => ({ | ||
$match: { $or: where instanceof Array ? where : [where] }, | ||
})) | ||
? aggregate.where | ||
: typeof aggregate.where === "object" | ||
? [ | ||
{ | ||
$match: { $or: [aggregate.where] }, | ||
}, | ||
] | ||
: []), | ||
? [aggregate.where] | ||
: []).map((condition) => ({ | ||
$match: { $or: condition instanceof Array ? condition : [condition] }, | ||
})), | ||
// Sorting Stage | ||
@@ -253,3 +267,13 @@ ...(aggregate.sort | ||
// Project Stage | ||
{ $project: normalizeProject(project.getFieldsList()) }, | ||
...(Object.values(Projection).includes(0) | ||
? [{ $project: Projection }] | ||
: []), | ||
// Having Conditions Stage | ||
...(aggregate.having instanceof Array | ||
? aggregate.having | ||
: typeof aggregate.having === "object" | ||
? [aggregate.having] | ||
: []).map((condition) => ({ | ||
$match: { $or: condition instanceof Array ? condition : [condition] }, | ||
})), | ||
], { session: connection, allowDiskUse: true }); | ||
@@ -256,0 +280,0 @@ // Add Limit |
@@ -21,2 +21,3 @@ import { BaseModel } from "./base"; | ||
where?: Conditions<M> | Array<Conditions<M>> | Array<Array<Conditions<M>>>; | ||
having?: Conditions<M> | Array<Conditions<M>> | Array<Array<Conditions<M>>>; | ||
group?: {}; | ||
@@ -23,0 +24,0 @@ range?: number | [number, number]; |
97843
1984