Skip to content

Commit

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

describe('#setRelations', () => {
it('set all relations on the entity', async () => {
const entity = PLAIN_TEST_ENTITIES[0] as TestEntity;
const queryService = moduleRef.get(TestEntityService);
const relationIds = PLAIN_TEST_RELATIONS.slice(3, 6).map((r) => r.testRelationPk);
const queryResult = await queryService.setRelations('testRelations', entity.testEntityPk, relationIds);
expect(queryResult).toEqual(expect.objectContaining(entity));

const relations = await queryService.queryRelations(TestRelation, 'testRelations', entity, {});
expect(relations.map((r) => r.testRelationPk)).toEqual(relationIds);
});

it('should remove all relations if the relationIds is empty', async () => {
const entity = PLAIN_TEST_ENTITIES[0] as TestEntity;
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.setRelations('testRelations', entity.testEntityPk, []);
expect(queryResult).toEqual(expect.objectContaining(entity));

const relations = await queryService.queryRelations(TestRelation, 'testRelations', entity, {});
expect(relations.map((r) => r.testRelationPk)).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 = PLAIN_TEST_ENTITIES[0] as TestEntity;
const queryService = moduleRef.get(TestEntityService);
return expect(
queryService.setRelations(
'testRelations',
entity.testEntityPk,
PLAIN_TEST_RELATIONS.slice(3, 6).map((r) => r.testRelationPk),
{
filter: { stringType: { eq: PLAIN_TEST_ENTITIES[1].stringType } },
},
),
).rejects.toThrow('Unable to find TestEntity with id: test-entity-1');
});

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

describe('#setRelation', () => {
it('call select and return the result', async () => {
const entity = PLAIN_TEST_ENTITIES[0] as TestEntity;
Expand Down
26 changes: 26 additions & 0 deletions packages/query-sequelize/src/services/relation-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,32 @@ export abstract class RelationQueryService<Entity extends Model<Entity, Partial<
return entity;
}

/**
* Set the relations on the entity.
*
* @param id - The id of the entity to set the relation on.
* @param relationName - The name of the relation to query for.
* @param relationIds - The ids of the relation to set on the entity. If the relationIds is empty all relations
* will be removed.
* @param opts - Additional options
*/
async setRelations<Relation>(
relationName: string,
id: string | number,
relationIds: string[] | number[],
opts?: ModifyRelationOptions<Entity, Relation>,
): Promise<Entity> {
const entity = await this.getById(id, opts);
if (relationIds.length) {
const relations = await this.getRelations(relationName, relationIds, opts?.relationFilter);
if (relations.length !== relationIds.length) {
throw new Error(`Unable to find all ${relationName} to set on ${this.model.name}`);
}
}
await entity.$set(relationName as keyof Entity, relationIds);
return entity;
}

/**
* Set the relation on the entity.
*
Expand Down

0 comments on commit b0c2d2f

Please sign in to comment.