Skip to content

Commit

Permalink
Fix update/patch with includes
Browse files Browse the repository at this point in the history
  • Loading branch information
DaddyWarbucks committed Mar 14, 2024
1 parent d36f632 commit f75b4f2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ export class SequelizeAdapter<
async _getOrFind (id: NullableId, _params: ServiceParams) {
const params = _params || {} as ServiceParams;
if (id === null) {
return await this._find({
return this._find({
...params,
paginate: false
});
}

return await this._get(id, params);
return this._get(id, params);
}


Expand Down Expand Up @@ -456,7 +456,10 @@ export class SequelizeAdapter<
.catch(errorHandler);

if (isPresent(sequelize.include)) {
return await this._get(id, params);
return this._get(id, {
...params,
query: { $select: params.query?.$select }
});
}

if (sequelize.raw) {
Expand Down Expand Up @@ -507,7 +510,10 @@ export class SequelizeAdapter<
.catch(errorHandler);

if (isPresent(sequelize.include)) {
return await this._get(id, params);
return this._get(id, {
...params,
query: { $select: params.query?.$select }
});
}

if (isPresent(sequelize.attributes)) {
Expand Down
42 changes: 40 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ describe('Feathers Sequelize Service', () => {
await people.create({ name });
const { data } = await people.find({ query: { age: null } }) as Paginated<any>;

console.log(data);

assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].name, name);
assert.strictEqual(data[0].age, null);
Expand Down Expand Up @@ -474,6 +472,26 @@ describe('Feathers Sequelize Service', () => {
expect(result).to.have.property('orders.id');
});

it('patch() includes associations and query', async () => {
const params = { sequelize: { include: Order } };
const data = { name: 'Patched' };

const current = await people.get(kirsten.id, params);

const result = await people.patch(kirsten.id, data, {
query: { name: current.name },
...params
});

delete current.updatedAt;
// @ts-ignore
delete result.updatedAt

console.log(result, { ...current, ...data })

assert.deepStrictEqual(result, { ...current, ...data });
});

it('update() includes associations', async () => {
const params = { sequelize: { include: Order } };
const data = { name: 'Updated' };
Expand All @@ -483,6 +501,26 @@ describe('Feathers Sequelize Service', () => {
expect(result).to.have.property('orders.id');
});

it('update() includes associations and query', async () => {
const params = { sequelize: { include: Order } };
const data = { name: 'Updated' };

const current = await people.get(kirsten.id, params);

const result = await people.update(kirsten.id, {
...current,
...data
}, {
query: { name: current.name },
...params
});

delete current.updatedAt;
delete result.updatedAt

assert.deepStrictEqual(result, { ...current, ...data });
});

it('remove() includes associations', async () => {
const params = { sequelize: { include: Order } };

Expand Down

0 comments on commit f75b4f2

Please sign in to comment.