From f933f4c96d9374401315d7e9e221d094ced7c1e9 Mon Sep 17 00:00:00 2001 From: DaddyWarbucks Date: Thu, 14 Mar 2024 13:17:42 -0500 Subject: [PATCH] Update multi patch sort --- src/adapter.ts | 30 ++++++++++++++++-------------- test/index.test.ts | 2 -- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/adapter.ts b/src/adapter.ts index 0ee3949..849c0c7 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -395,21 +395,23 @@ export class SequelizeAdapter< const selected = isPresent(sequelize.attributes); if (instances) { - const sortedInstances: Model[] = []; - const unsortedInstances: Model[] = []; - - current.forEach((item: any) => { - const id = item[this.id]; - // @ts-ignore - const instance = instances.find(instance => instance[this.id] === id); - if (instance) { - sortedInstances.push(instance); - } else { - unsortedInstances.push(item); - } - }); + if (isPresent(params?.query?.$sort)) { + const sortedInstances: Model[] = []; + const unsortedInstances: Model[] = []; + + current.forEach((item: any) => { + const id = item[this.id]; + // @ts-ignore + const instance = instances.find(instance => instance[this.id] === id); + if (instance) { + sortedInstances.push(instance); + } else { + unsortedInstances.push(item); + } + }); - instances = [...sortedInstances, ...unsortedInstances]; + instances = [...sortedInstances, ...unsortedInstances]; + } if (sequelize.raw) { const result = instances.map((instance) => { diff --git a/test/index.test.ts b/test/index.test.ts index 98b815e..e0cc11a 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -487,8 +487,6 @@ describe('Feathers Sequelize Service', () => { // @ts-ignore delete result.updatedAt - console.log(result, { ...current, ...data }) - assert.deepStrictEqual(result, { ...current, ...data }); });