Skip to content

Commit

Permalink
update checkrelation functions: now add many type relation top database
Browse files Browse the repository at this point in the history
  • Loading branch information
Amirhosein-hshm committed Apr 29, 2022
1 parent 3cd8e57 commit 18d6f01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/odm/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ export const odm = (schemasObj: ISchema) => {
const inrelationObj =
schemaFns(schemasObj).getSchema(collection).inrelation;

console.log(collection);

const inrelationKeys = Object.keys(inrelationObj);

assert(doc, object(pureInrelSchema));

doc = addOutrelation(collection, doc, foundedSchema);
Expand Down
26 changes: 26 additions & 0 deletions src/odm/utils/checkInsertRelation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ export const checkRelation = (
} else if (
schemaName === obj.schemaName && schemaInrel[key]["type"] === "many"
) {
if (obj.sort.type === "objectId" || obj.sort.type === "date") {
doc[key] && obj.sort.order === "asc"
? doc[key].forEach(async (document: any) => {
await db.collection(schemaInrel[key]["schemaName"]).updateOne({
_id: document._id,
}, {
$push: {
[keyName]: {
$each: [{ ...pureDoc }],
},
},
});
})
: doc[key].forEach(async (document: any) => {
await db.collection(schemaInrel[key]["schemaName"]).updateOne({
_id: document._id,
}, {
$push: {
[keyName]: {
$each: [{ ...pureDoc }],
$position: 0,
},
},
});
});
}
}
});
});
Expand Down

0 comments on commit 18d6f01

Please sign in to comment.