Browse Source

fix: move the shuffle logic to BaseModelSqlv2

pull/2558/head
Wing-Kam Wong 2 years ago
parent
commit
888344b7d7
  1. 35
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts
  2. 8
      packages/nocodb/src/lib/meta/helpers/PagedResponse.ts

35
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -42,6 +42,7 @@ import { NcError } from '../../../../meta/helpers/catchError';
import { customAlphabet } from 'nanoid'; import { customAlphabet } from 'nanoid';
import DOMPurify from 'isomorphic-dompurify'; import DOMPurify from 'isomorphic-dompurify';
import { sanitize, unsanitize } from './helpers/sanitize'; import { sanitize, unsanitize } from './helpers/sanitize';
import { fisherYatesShuffle } from '../../../../meta/helpers/shuffleHelper';
const GROUP_COL = '__nc_group_id'; const GROUP_COL = '__nc_group_id';
@ -247,8 +248,10 @@ class BaseModelSqlv2 {
if (!ignoreFilterSort) applyPaginate(qb, rest); if (!ignoreFilterSort) applyPaginate(qb, rest);
const proto = await this.getProto(); const proto = await this.getProto();
const data = await this.extractRawQueryAndExec(qb); let data = await this.extractRawQueryAndExec(qb);
if (+rest?.shuffle) {
data = fisherYatesShuffle(data);
}
return data?.map((d) => { return data?.map((d) => {
d.__proto__ = proto; d.__proto__ = proto;
return d; return d;
@ -354,8 +357,11 @@ class BaseModelSqlv2 {
qb.groupBy(args.column_name); qb.groupBy(args.column_name);
if (sorts) await sortV2(sorts, qb, this.dbDriver); if (sorts) await sortV2(sorts, qb, this.dbDriver);
applyPaginate(qb, rest); applyPaginate(qb, rest);
let data = await qb;
return await qb; if (+rest?.shuffle) {
data = fisherYatesShuffle(data);
}
return data;
} }
async multipleHmList({ colId, ids }, args?: { limit?; offset? }) { async multipleHmList({ colId, ids }, args?: { limit?; offset? }) {
@ -861,8 +867,11 @@ class BaseModelSqlv2 {
applyPaginate(qb, args); applyPaginate(qb, args);
const proto = await childModel.getProto(); const proto = await childModel.getProto();
let data = await qb;
return (await qb).map((c) => { if (+args?.shuffle) {
data = fisherYatesShuffle(data);
}
return data.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}); });
@ -954,8 +963,11 @@ class BaseModelSqlv2 {
applyPaginate(qb, args); applyPaginate(qb, args);
const proto = await childModel.getProto(); const proto = await childModel.getProto();
let data = await this.extractRawQueryAndExec(qb);
return (await this.extractRawQueryAndExec(qb)).map((c) => { if (+args?.shuffle) {
data = fisherYatesShuffle(data);
}
return data.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}); });
@ -1047,7 +1059,11 @@ class BaseModelSqlv2 {
applyPaginate(qb, args); applyPaginate(qb, args);
const proto = await parentModel.getProto(); const proto = await parentModel.getProto();
return (await this.extractRawQueryAndExec(qb)).map((c) => { let data = await this.extractRawQueryAndExec(qb);
if (+args?.shuffle) {
data = fisherYatesShuffle(data);
}
return data.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}); });
@ -1227,6 +1243,7 @@ class BaseModelSqlv2 {
const obj: XcFilter = {}; const obj: XcFilter = {};
obj.where = args.where || args.w || ''; obj.where = args.where || args.w || '';
obj.having = args.having || args.h || ''; obj.having = args.having || args.h || '';
obj.shuffle = args.shuffle || args.r || '';
obj.condition = args.condition || args.c || {}; obj.condition = args.condition || args.c || {};
obj.conditionGraph = args.conditionGraph || {}; obj.conditionGraph = args.conditionGraph || {};
obj.limit = Math.max( obj.limit = Math.max(

8
packages/nocodb/src/lib/meta/helpers/PagedResponse.ts

@ -7,20 +7,12 @@ export class PagedResponseImpl<T> {
limit = 25, limit = 25,
offset = 0, offset = 0,
count = null, count = null,
shuffle = 0,
}: { }: {
limit?: number; limit?: number;
offset?: number; offset?: number;
count?: number; count?: number;
shuffle?: number;
} = {} } = {}
) { ) {
if (+shuffle) {
for (let i = list.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[list[i], list[j]] = [list[j], list[i]];
}
}
this.list = list; this.list = list;
if (count !== null) { if (count !== null) {
this.pageInfo = { totalRows: +count }; this.pageInfo = { totalRows: +count };

Loading…
Cancel
Save