feat: non paged list (#204)

This commit is contained in:
ChengLei Shao 2022-02-21 20:14:41 +08:00 committed by GitHub
parent a7c4abb485
commit d486768eda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 7 deletions

View File

@ -99,6 +99,14 @@ describe('list action', () => {
expect(body.totalPage).toEqual(3);
});
test('list with non-paged', async () => {
const response = await app.agent().resource('posts').list({
paginate: false,
});
const body = response.body;
expect(body.length).toEqual(3);
});
test('list by association', async () => {
// tags with posts id eq 1
const response = await app

View File

@ -1,5 +1,7 @@
import { Context } from '..';
import { getRepositoryFromParams } from './utils';
import { Repository } from '@nocobase/database';
import { ActionParams } from '@nocobase/resourcer';
export const DEFAULT_PAGE = 1;
export const DEFAULT_PER_PAGE = 20;
@ -21,17 +23,18 @@ function totalPage(total, pageSize): number {
return Math.ceil(total / pageSize);
}
export async function list(ctx: Context, next) {
const { page = DEFAULT_PAGE, pageSize = DEFAULT_PER_PAGE, fields, filter, appends, except, sort } = ctx.action.params;
function findArgs(params: ActionParams) {
const { fields, filter, appends, except, sort } = params;
return { filter, fields, appends, except, sort };
}
async function listWithPagination(ctx: Context) {
const { page = DEFAULT_PAGE, pageSize = DEFAULT_PER_PAGE } = ctx.action.params;
const repository = getRepositoryFromParams(ctx);
const [rows, count] = await repository.findAndCount({
filter,
fields,
appends,
except,
sort,
...findArgs(ctx.action.params),
...pageArgsToLimitArgs(parseInt(String(page)), parseInt(String(pageSize))),
});
@ -42,6 +45,24 @@ export async function list(ctx: Context, next) {
pageSize,
totalPage: totalPage(count, pageSize),
};
}
async function listWithNonPaged(ctx: Context) {
const repository = getRepositoryFromParams(ctx);
const rows = await repository.find(findArgs(ctx.action.params));
ctx.body = rows;
}
export async function list(ctx: Context, next) {
const { paginate } = ctx.action.params;
if (paginate === false || paginate === 'false') {
await listWithNonPaged(ctx);
} else {
await listWithPagination(ctx);
}
await next();
}