From 5dd76a436524bd1eb369c93a19f1ac8eadd16683 Mon Sep 17 00:00:00 2001 From: mertmit Date: Tue, 1 Nov 2022 12:45:49 +0300 Subject: [PATCH] fix: base reordering logic Signed-off-by: mertmit --- packages/nocodb/src/lib/models/Base.ts | 83 ++++++++++---------------- 1 file changed, 30 insertions(+), 53 deletions(-) diff --git a/packages/nocodb/src/lib/models/Base.ts b/packages/nocodb/src/lib/models/Base.ts index fa32083828..9b206261c3 100644 --- a/packages/nocodb/src/lib/models/Base.ts +++ b/packages/nocodb/src/lib/models/Base.ts @@ -195,65 +195,42 @@ export default class Base implements BaseType { static async reorderBases(projectId: string, keepBase?: string, ncMeta = Noco.ncMeta) { const bases = await this.list({ projectId: projectId }, ncMeta); - - // order list for bases - const orders = []; - const takenOrders = bases.map((base) => base.order); if (keepBase) { - bases.find((base) => { - if (base.id === keepBase) { - orders.push({ id: base.id, order: base.order }); - } - }); - } - - for (const b of bases) { - if (b.id === keepBase) continue; - let tempIndex = b.order; - if (!b.order || orders.find((o) => o.order === tempIndex)) { - tempIndex = 1; - while (takenOrders.includes(tempIndex)) { - tempIndex++; - } + const kpBase = bases.splice(bases.indexOf(bases.find((base) => base.id === keepBase)), 1); + if (kpBase.length) { + bases.splice(kpBase[0].order - 1, 0, kpBase[0]); } - // use index as order if order is not set - orders.push({ id: b.id, order: tempIndex }); } - orders.sort((a, b) => a.order - b.order); - // update order for bases - for (const [i, o] of Object.entries(orders)) { - const fnd = bases.find((b) => b.id === o.id); - if (fnd && (!fnd.order || fnd.order != parseInt(i) + 1)) { - await ncMeta.metaDelete(null, null, MetaTable.BASES, { - id: fnd.id, - }); - - await NocoCache.deepDel( - CacheScope.BASE, - `${CacheScope.BASE}:${fnd.id}`, - CacheDelDirection.CHILD_TO_PARENT - ); - - fnd.order = parseInt(i) + 1; - - const { id } = await ncMeta.metaInsert2( - fnd.project_id, - null, - MetaTable.BASES, - fnd - ); - - await NocoCache.appendToList( - CacheScope.BASE, - [fnd.project_id], - `${CacheScope.BASE}:${id}` - ); - - await NocoCache.set(`${CacheScope.BASE}:${id}`, fnd); - } + for (const [i, b] of Object.entries(bases)) { + await ncMeta.metaDelete(null, null, MetaTable.BASES, { + id: b.id, + }); + + await NocoCache.deepDel( + CacheScope.BASE, + `${CacheScope.BASE}:${b.id}`, + CacheDelDirection.CHILD_TO_PARENT + ); + + b.order = parseInt(i) + 1; + + const { id } = await ncMeta.metaInsert2( + b.project_id, + null, + MetaTable.BASES, + b + ); + + await NocoCache.appendToList( + CacheScope.BASE, + [b.project_id], + `${CacheScope.BASE}:${id}` + ); + + await NocoCache.set(`${CacheScope.BASE}:${id}`, b); } }