Skip to content

Commit

Permalink
feat(mongoose): Implement setRelations to set many references
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Apr 13, 2021
1 parent b0c2d2f commit 3dc8a84
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,71 @@ describe('MongooseQueryService', () => {
});
});

describe('#setRelations', () => {
it('set all relations on the entity', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const relationIds = TEST_REFERENCES.slice(3, 6).map((r) => r._id);
const queryResult = await queryService.setRelations('testReferences', entity._id, relationIds);
expect(queryResult).toEqual(
expect.objectContaining({
_id: entity._id,
testReferences: expect.arrayContaining(relationIds),
}),
);

const relations = await queryService.queryRelations(TestReference, 'testReferences', entity, {});
expect(relations.map((r) => r._id)).toEqual(relationIds);
});

it('should remove all relations if the relationIds is empty', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.setRelations('testReferences', entity._id, []);
expect(queryResult).toEqual(
expect.objectContaining({
_id: entity._id,
testReferences: expect.arrayContaining([]),
}),
);

const relations = await queryService.queryRelations(TestReference, 'testReferences', entity, {});
expect(relations.map((r) => r._id)).toEqual([]);
});

describe('with modify options', () => {
it('should throw an error if the entity is not found with the id and provided filter', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
return expect(
queryService.setRelations(
'testReferences',
entity._id,
TEST_REFERENCES.slice(3, 6).map((r) => r._id),
{
filter: { stringType: { eq: TEST_ENTITIES[1].stringType } },
},
),
).rejects.toThrow(`Unable to find TestEntity with id: ${String(entity._id)}`);
});

it('should throw an error if the relations are not found with the relationIds and provided filter', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
return expect(
queryService.setRelations<TestReference>(
'testReferences',
entity._id,
TEST_REFERENCES.slice(3, 6).map((r) => r._id),
{
relationFilter: { referenceName: { like: '%-one' } },
},
),
).rejects.toThrow('Unable to find all testReferences to set on TestEntity');
});
});
});

describe('#setRelation', () => {
it('call select and return the result', async () => {
const entity = TEST_REFERENCES[0];
Expand Down
17 changes: 17 additions & 0 deletions packages/query-mongoose/src/services/reference-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ export abstract class ReferenceQueryService<Entity extends Document> {
return this.getById(id);
}

async setRelations<Relation extends Document>(
relationName: string,
id: string,
relationIds: (string | number)[],
opts?: ModifyRelationOptions<Entity, Relation>,
): Promise<Entity> {
this.checkForReference('AddRelations', relationName, false);
const entity = await this.getById(id, opts);
const refCount = await this.getRefCount(relationName, relationIds, opts?.relationFilter);
if (relationIds.length !== refCount) {
throw new Error(`Unable to find all ${relationName} to set on ${this.Model.modelName}`);
}
await entity.updateOne({ [relationName]: relationIds } as UpdateQuery<Entity>).exec();
// reload the document
return this.getById(id);
}

async setRelation<Relation extends Document>(
relationName: string,
id: string | number,
Expand Down

0 comments on commit 3dc8a84

Please sign in to comment.