Browse Source

fix: base reordering logic

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/3573/head
mertmit 2 years ago
parent
commit
5dd76a4365
  1. 83
      packages/nocodb/src/lib/models/Base.ts

83
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) { static async reorderBases(projectId: string, keepBase?: string, ncMeta = Noco.ncMeta) {
const bases = await this.list({ projectId: projectId }, ncMeta); const bases = await this.list({ projectId: projectId }, ncMeta);
// order list for bases
const orders = [];
const takenOrders = bases.map((base) => base.order);
if (keepBase) { if (keepBase) {
bases.find((base) => { const kpBase = bases.splice(bases.indexOf(bases.find((base) => base.id === keepBase)), 1);
if (base.id === keepBase) { if (kpBase.length) {
orders.push({ id: base.id, order: base.order }); bases.splice(kpBase[0].order - 1, 0, kpBase[0]);
}
});
}
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++;
}
} }
// 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 // update order for bases
for (const [i, o] of Object.entries(orders)) { for (const [i, b] of Object.entries(bases)) {
const fnd = bases.find((b) => b.id === o.id); await ncMeta.metaDelete(null, null, MetaTable.BASES, {
if (fnd && (!fnd.order || fnd.order != parseInt(i) + 1)) { id: b.id,
await ncMeta.metaDelete(null, null, MetaTable.BASES, { });
id: fnd.id,
}); await NocoCache.deepDel(
CacheScope.BASE,
await NocoCache.deepDel( `${CacheScope.BASE}:${b.id}`,
CacheScope.BASE, CacheDelDirection.CHILD_TO_PARENT
`${CacheScope.BASE}:${fnd.id}`, );
CacheDelDirection.CHILD_TO_PARENT
); b.order = parseInt(i) + 1;
fnd.order = parseInt(i) + 1; const { id } = await ncMeta.metaInsert2(
b.project_id,
const { id } = await ncMeta.metaInsert2( null,
fnd.project_id, MetaTable.BASES,
null, b
MetaTable.BASES, );
fnd
); await NocoCache.appendToList(
CacheScope.BASE,
await NocoCache.appendToList( [b.project_id],
CacheScope.BASE, `${CacheScope.BASE}:${id}`
[fnd.project_id], );
`${CacheScope.BASE}:${id}`
); await NocoCache.set(`${CacheScope.BASE}:${id}`, b);
await NocoCache.set(`${CacheScope.BASE}:${id}`, fnd);
}
} }
} }

Loading…
Cancel
Save