From 18d6f01d8cfee6d2d03b5bd680d16a9d3b26ee82 Mon Sep 17 00:00:00 2001 From: amirhosein Date: Fri, 29 Apr 2022 13:33:35 -0500 Subject: [PATCH] update checkrelation functions: now add many type relation top database --- src/odm/mod.ts | 4 ---- src/odm/utils/checkInsertRelation.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/odm/mod.ts b/src/odm/mod.ts index a0176696..d4cc51bf 100644 --- a/src/odm/mod.ts +++ b/src/odm/mod.ts @@ -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); diff --git a/src/odm/utils/checkInsertRelation.ts b/src/odm/utils/checkInsertRelation.ts index e45e2e5c..7d328357 100644 --- a/src/odm/utils/checkInsertRelation.ts +++ b/src/odm/utils/checkInsertRelation.ts @@ -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, + }, + }, + }); + }); + } } }); });