Browse Source

Merge pull request #8759 from nocodb/nc-fix/cache-list-order

Add option to sort cache list data
pull/8767/head
Pranav C 2 weeks ago committed by GitHub
parent
commit
cdceec6edd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      packages/nc-gui/composables/useViewFilters.ts
  2. 56
      packages/nocodb/src/cache/CacheMgr.ts
  3. 7
      packages/nocodb/src/cache/NocoCache.ts
  4. 53
      packages/nocodb/src/models/Filter.ts
  5. 20
      packages/nocodb/src/models/HookFilter.ts

6
packages/nc-gui/composables/useViewFilters.ts

@ -502,9 +502,11 @@ export function useViewFilters(
} else { } else {
try { try {
await $api.dbTableFilter.delete(filter.id) await $api.dbTableFilter.delete(filter.id)
if (!isWebhook && !isLink) reloadData?.() if (!isWebhook && !isLink) reloadData?.()
filters.value.splice(i, 1)
// find item index by using id and remove it from array since item index may change
const itemIndex = filters.value.findIndex((f) => f.id === filter.id)
if (itemIndex > -1) filters.value.splice(itemIndex)
} catch (e: any) { } catch (e: any) {
console.log(e) console.log(e)
message.error(await extractSdkResponseErrorMsg(e)) message.error(await extractSdkResponseErrorMsg(e))

56
packages/nocodb/src/cache/CacheMgr.ts vendored

@ -251,6 +251,11 @@ export default abstract class CacheMgr {
async getList( async getList(
scope: string, scope: string,
subKeys: string[], subKeys: string[],
orderBy?: {
key: string;
dir?: 'asc' | 'desc';
isString?: boolean;
},
): Promise<{ ): Promise<{
list: any[]; list: any[];
isNoneList: boolean; isNoneList: boolean;
@ -327,19 +332,48 @@ export default abstract class CacheMgr {
); );
} }
} }
const list = values.map((res) => {
return { try {
list: values.map((res) => { const o = JSON.parse(res);
try { if (typeof o === 'object') {
const o = JSON.parse(res); return o.value;
if (typeof o === 'object') {
return o.value;
}
} catch (e) {
return res;
} }
} catch (e) {
return res; return res;
}), }
return res;
});
// Check if orderBy parameter is provided and valid
if (orderBy?.key) {
// Destructure the properties from orderBy object
const { key, dir, isString } = orderBy;
// Determine the multiplier for sorting order: -1 for descending, 1 for ascending
const orderMultiplier = dir === 'desc' ? -1 : 1;
// Sort the values array based on the specified property
list.sort((a, b) => {
// Get the property values from a and b
const aValue = a?.[key];
const bValue = b?.[key];
// If aValue is null or undefined, move it to the end
if (aValue === null || aValue === undefined) return 1;
// If bValue is null or undefined, move it to the end
if (bValue === null || bValue === undefined) return -1;
if (isString) {
// If the property is a string, use localeCompare for comparison
return orderMultiplier * String(aValue).localeCompare(String(bValue));
}
// If the property is a number, subtract the values directly
return orderMultiplier * (aValue - bValue);
});
}
return {
list,
isNoneList, isNoneList,
}; };
} }

7
packages/nocodb/src/cache/NocoCache.ts vendored

@ -62,6 +62,11 @@ export default class NocoCache {
public static async getList( public static async getList(
scope: string, scope: string,
subKeys: string[], subKeys: string[],
orderBy?: {
key: string;
dir?: 'asc' | 'desc';
isString?: boolean;
},
): Promise<{ ): Promise<{
list: any[]; list: any[];
isNoneList: boolean; isNoneList: boolean;
@ -71,7 +76,7 @@ export default class NocoCache {
list: [], list: [],
isNoneList: false, isNoneList: false,
}); });
return this.client.getList(scope, subKeys); return this.client.getList(scope, subKeys, orderBy);
} }
public static async setList( public static async setList(

53
packages/nocodb/src/models/Filter.ts

@ -385,9 +385,13 @@ export default class Filter implements FilterType {
): Promise<Filter[]> { ): Promise<Filter[]> {
if (this.children) return this.children; if (this.children) return this.children;
if (!this.is_group) return null; if (!this.is_group) return null;
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [ const cachedList = await NocoCache.getList(
this.id, CacheScope.FILTER_EXP,
]); [this.id],
{
key: 'order',
},
);
let { list: childFilters } = cachedList; let { list: childFilters } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !childFilters.length) { if (!isNoneList && !childFilters.length) {
@ -438,9 +442,13 @@ export default class Filter implements FilterType {
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta,
): Promise<FilterType> { ): Promise<FilterType> {
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [ const cachedList = await NocoCache.getList(
viewId || hookId || linkColId, CacheScope.FILTER_EXP,
]); [viewId || hookId || linkColId],
{
key: 'order',
},
);
let { list: filters } = cachedList; let { list: filters } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !filters.length) { if (!isNoneList && !filters.length) {
@ -596,7 +604,13 @@ export default class Filter implements FilterType {
{ viewId }: { viewId: any }, { viewId }: { viewId: any },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta,
) { ) {
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [viewId]); const cachedList = await NocoCache.getList(
CacheScope.FILTER_EXP,
[viewId],
{
key: 'order',
},
);
let { list: filterObjs } = cachedList; let { list: filterObjs } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !filterObjs.length) { if (!isNoneList && !filterObjs.length) {
@ -623,7 +637,11 @@ export default class Filter implements FilterType {
{ hookId }: { hookId: any }, { hookId }: { hookId: any },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta,
) { ) {
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [hookId]); const cachedList = await NocoCache.getList(
CacheScope.FILTER_EXP,
[hookId],
{ key: 'order' },
);
let { list: filterObjs } = cachedList; let { list: filterObjs } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !filterObjs.length) { if (!isNoneList && !filterObjs.length) {
@ -654,9 +672,11 @@ export default class Filter implements FilterType {
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta,
) { ) {
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [ const cachedList = await NocoCache.getList(
parentId, CacheScope.FILTER_EXP,
]); [parentId],
{ key: 'order' },
);
let { list: filterObjs } = cachedList; let { list: filterObjs } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !filterObjs.length) { if (!isNoneList && !filterObjs.length) {
@ -690,10 +710,13 @@ export default class Filter implements FilterType {
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta,
) { ) {
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [ const cachedList = await NocoCache.getList(
hookId, CacheScope.FILTER_EXP,
parentId, [hookId, parentId],
]); {
key: 'order',
},
);
let { list: filterObjs } = cachedList; let { list: filterObjs } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !filterObjs.length) { if (!isNoneList && !filterObjs.length) {

20
packages/nocodb/src/models/HookFilter.ts

@ -247,9 +247,11 @@ export default class Filter {
): Promise<Filter[]> { ): Promise<Filter[]> {
if (this.children) return this.children; if (this.children) return this.children;
if (!this.is_group) return null; if (!this.is_group) return null;
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [ const cachedList = await NocoCache.getList(
this.id, CacheScope.FILTER_EXP,
]); [this.id],
{ key: 'order' },
);
let { list: childFilters } = cachedList; let { list: childFilters } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !childFilters.length) { if (!isNoneList && !childFilters.length) {
@ -293,7 +295,11 @@ export default class Filter {
}, },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta,
): Promise<FilterType> { ): Promise<FilterType> {
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [viewId]); const cachedList = await NocoCache.getList(
CacheScope.FILTER_EXP,
[viewId],
{ key: 'order' },
);
let { list: filters } = cachedList; let { list: filters } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !filters.length) { if (!isNoneList && !filters.length) {
@ -393,7 +399,11 @@ export default class Filter {
{ viewId }: { viewId: any }, { viewId }: { viewId: any },
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta,
) { ) {
const cachedList = await NocoCache.getList(CacheScope.FILTER_EXP, [viewId]); const cachedList = await NocoCache.getList(
CacheScope.FILTER_EXP,
[viewId],
{ key: 'order' },
);
let { list: filterObjs } = cachedList; let { list: filterObjs } = cachedList;
const { isNoneList } = cachedList; const { isNoneList } = cachedList;
if (!isNoneList && !filterObjs.length) { if (!isNoneList && !filterObjs.length) {

Loading…
Cancel
Save