Skip to content

Commit

Permalink
fix: getRelationship
Browse files Browse the repository at this point in the history
-  getRelationship now returns undefined if the base property is not an ObjectId
- getRelationships return empty array if base property is not an array
  • Loading branch information
Rmannn committed May 4, 2022
1 parent 930bf4b commit 2cdbe63
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-mongo",
"version": "0.14.0",
"version": "0.14.1",
"description": "A NestJS module that provide a simple mongodb orm like",
"keywords": [
"nestjs",
Expand Down
8 changes: 7 additions & 1 deletion src/entity/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,10 @@ export class EntityManager {
);
}

const id: ObjectId = obj[property];
const id: ObjectId | undefined | null = obj[property];
if (!(id instanceof ObjectId)) {
return;
}
const filter: Filter<R> = {};
filter._id = id;
const relationship = await this.findOne<R>(relationMetadata.type, filter, options);
Expand Down Expand Up @@ -423,6 +426,9 @@ export class EntityManager {
}

const value = obj[property];
if (!Array.isArray(value)) {
return [];
}
const relationshipsCursor = await this.find<R>(
relationMetadata.type,
{
Expand Down
17 changes: 17 additions & 0 deletions test/relationship/relationship.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ describe('Relationship', () => {
)
).toHaveLength(2);
});

it('should return undefined for single relationship', async () => {
const entityRelationShip = new EntityRelationship();
entityRelationShip.property = 'test_with_no_parent';

const notFound = await em.getRelationship(entityRelationShip, 'parent');
expect(notFound).toBeUndefined();
});

it('should return undefined for multiple children relationships', async () => {
const entityRelationShip = new EntityRelationship();
entityRelationShip.property = 'test_with_no_children';

const notFound = await em.getRelationships(entityRelationShip, 'children');
expect(notFound).toBeInstanceOf(Array);
expect(notFound).toHaveLength(0);
});
});
});
});
Expand Down

0 comments on commit 2cdbe63

Please sign in to comment.