Merge branch 'master' into budi-8238-rewrite-postgresspects-to-use-knex

This commit is contained in:
Sam Rose 2024-06-17 16:45:33 +01:00 committed by GitHub
commit 75e79156f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 88 additions and 109 deletions

View File

@ -596,6 +596,9 @@ class InternalBuilder {
const offset = page * paginate.limit const offset = page * paginate.limit
foundLimit = paginate.limit foundLimit = paginate.limit
foundOffset = offset foundOffset = offset
} else if (paginate && paginate.offset && paginate.limit) {
foundLimit = paginate.limit
foundOffset = paginate.offset
} else if (paginate && paginate.limit) { } else if (paginate && paginate.limit) {
foundLimit = paginate.limit foundLimit = paginate.limit
} }

View File

@ -512,14 +512,10 @@ describe.each([
]) ])
}) })
// TODO(samwho): fix for SQS
!isSqs &&
it("should match the session user id in a multi user field", async () => { it("should match the session user id in a multi user field", async () => {
const allUsers = [...globalUsers, config.getUser()].map( const allUsers = [...globalUsers, config.getUser()].map((user: any) => {
(user: any) => {
return { _id: user._id } return { _id: user._id }
} })
)
await expectQuery({ await expectQuery({
contains: { multi_user: ["{{ [user]._id }}"] }, contains: { multi_user: ["{{ [user]._id }}"] },
@ -531,14 +527,10 @@ describe.each([
]) ])
}) })
// TODO(samwho): fix for SQS
!isSqs &&
it("should match the session user id in a deprecated multi user field", async () => { it("should match the session user id in a deprecated multi user field", async () => {
const allUsers = [...globalUsers, config.getUser()].map( const allUsers = [...globalUsers, config.getUser()].map((user: any) => {
(user: any) => {
return { _id: user._id } return { _id: user._id }
} })
)
await expectQuery({ await expectQuery({
contains: { deprecated_multi_user: ["{{ [user]._id }}"] }, contains: { deprecated_multi_user: ["{{ [user]._id }}"] },
@ -550,8 +542,6 @@ describe.each([
]) ])
}) })
// TODO(samwho): fix for SQS
!isSqs &&
it("should not match the session user id in a multi user field", async () => { it("should not match the session user id in a multi user field", async () => {
await expectQuery({ await expectQuery({
notContains: { multi_user: ["{{ [user]._id }}"] }, notContains: { multi_user: ["{{ [user]._id }}"] },
@ -566,8 +556,6 @@ describe.each([
]) ])
}) })
// TODO(samwho): fix for SQS
!isSqs &&
it("should not match the session user id in a deprecated multi user field", async () => { it("should not match the session user id in a deprecated multi user field", async () => {
await expectQuery({ await expectQuery({
notContains: { deprecated_multi_user: ["{{ [user]._id }}"] }, notContains: { deprecated_multi_user: ["{{ [user]._id }}"] },
@ -1552,8 +1540,6 @@ describe.each([
}) })
}) })
// TODO(samwho): fix for SQS
!isSqs &&
describe("pagination", () => { describe("pagination", () => {
it("should paginate through all rows", async () => { it("should paginate through all rows", async () => {
// @ts-ignore // @ts-ignore
@ -1578,10 +1564,8 @@ describe.each([
bookmark = response.bookmark bookmark = response.bookmark
} }
expect(rows).toHaveLength(10) const autoValues = rows.map(row => row.auto).sort((a, b) => a - b)
expect(rows.map(row => row.auto)).toEqual( expect(autoValues).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect.arrayContaining([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)
}) })
}) })
}) })

View File

@ -167,13 +167,9 @@ export async function search(
const sortField = table.schema[params.sort] const sortField = table.schema[params.sort]
const sortType = const sortType =
sortField.type === FieldType.NUMBER ? SortType.NUMBER : SortType.STRING sortField.type === FieldType.NUMBER ? SortType.NUMBER : SortType.STRING
const sortDirection =
params.sortOrder === SortOrder.ASCENDING
? SortOrder.ASCENDING
: SortOrder.DESCENDING
request.sort = { request.sort = {
[sortField.name]: { [sortField.name]: {
direction: sortDirection, direction: params.sortOrder || SortOrder.DESCENDING,
type: sortType as SortType, type: sortType as SortType,
}, },
} }
@ -182,14 +178,15 @@ export async function search(
if (params.bookmark && typeof params.bookmark !== "number") { if (params.bookmark && typeof params.bookmark !== "number") {
throw new Error("Unable to paginate with string based bookmarks") throw new Error("Unable to paginate with string based bookmarks")
} }
const bookmark: number = (params.bookmark as number) || 1
const limit = params.limit const bookmark: number = (params.bookmark as number) || 0
if (paginate && params.limit) { if (paginate && params.limit) {
request.paginate = { request.paginate = {
limit: params.limit + 1, limit: params.limit + 1,
page: bookmark, offset: bookmark * params.limit,
} }
} }
try { try {
const rows = await runSqlQuery(request, allTables) const rows = await runSqlQuery(request, allTables)
@ -221,18 +218,12 @@ export async function search(
} }
// check for pagination // check for pagination
if (paginate && limit) { if (paginate) {
const response: SearchResponse<Row> = { const response: SearchResponse<Row> = {
rows: finalRows, rows: finalRows,
} }
const prevLimit = request.paginate!.limit if (nextRow) {
request.paginate = { response.hasNextPage = true
limit: 1,
page: bookmark * prevLimit + 1,
}
const hasNextPage = !!nextRow
response.hasNextPage = hasNextPage
if (hasNextPage) {
response.bookmark = bookmark + 1 response.bookmark = bookmark + 1
} }
return response return response

View File

@ -85,6 +85,7 @@ export interface SortJson {
export interface PaginationJson { export interface PaginationJson {
limit: number limit: number
page?: string | number page?: string | number
offset?: number
} }
export interface RenameColumn { export interface RenameColumn {