Skip to content

Commit

Permalink
Merge pull request #177 from hemedani/main
Browse files Browse the repository at this point in the history
🐛 fixed not found relatedRelation when creating new model
  • Loading branch information
hemedani authored Jan 5, 2025
2 parents 03ad658 + 4c70adf commit 09ed358
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 24 deletions.
98 changes: 97 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 41 additions & 23 deletions src/odm/newModel/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,59 @@ export const newModel = <

const schemas = schemaFns(schemasObj).getSchemas();

pureFields = pureFields._id ? pureFields : {
_id: optional(objectIdValidation),
...pureFields,
};

const mainRelations: Record<string, IMainRelation> = {};
for (const relation in relations) {
mainRelations[relation] = {
const mainRelations = Object.keys(relations).reduce((acc, relation) => {
const mainRelation: IMainRelation = {
schemaName: relations[relation].schemaName,
type: relations[relation].type,
optional: relations[relation].optional,
};
relations[relation].sort &&
(mainRelations[relation].sort = relations[relation].sort);

if (relations[relation].sort) {
mainRelation.sort = relations[relation].sort;
}

for (const relatedRelation in relations[relation].relatedRelations) {
const iteratedRelatedRelation =
relations[relation].relatedRelations[relatedRelation];
schemas[relations[relation].schemaName]
.relatedRelations[relatedRelation] = {
mainRelationName: relation,
mainRelationType: relations[relation].type,
schemaName: name,
type: iteratedRelatedRelation.type,

if (!schemas[relations[relation].schemaName]) {
schemas[relations[relation].schemaName] = {
pure: {},
relations: {},
mainRelations: {},
relatedRelations: {},
options: {},
};
iteratedRelatedRelation.limit && (schemas[relations[relation].schemaName]
.relatedRelations[relatedRelation].limit =
iteratedRelatedRelation.limit);
iteratedRelatedRelation.sort && (schemas[relations[relation].schemaName]
.relatedRelations[relatedRelation].sort = iteratedRelatedRelation.sort);
}

const schema = schemas[relations[relation].schemaName];

schema.relatedRelations[relatedRelation] = {
mainRelationName: relation,
mainRelationType: relations[relation].type,
schemaName: name,
type: iteratedRelatedRelation.type,
};

if (iteratedRelatedRelation.limit) {
schema.relatedRelations[relatedRelation].limit =
iteratedRelatedRelation.limit;
}
if (iteratedRelatedRelation.sort) {
schema.relatedRelations[relatedRelation].sort =
iteratedRelatedRelation.sort;
}
}
}

acc[relation] = mainRelation;
return acc;
}, {} as Record<string, IMainRelation>);

schemas[name] = {
pure: pureFields,
pure: {
_id: optional(objectIdValidation),
...pureFields,
},
relations,
mainRelations,
relatedRelations: {},
Expand Down

0 comments on commit 09ed358

Please sign in to comment.