diff --git a/packages/db-postgres/src/find.ts b/packages/db-postgres/src/find.ts index dbec04af4c..399ae58b9a 100644 --- a/packages/db-postgres/src/find.ts +++ b/packages/db-postgres/src/find.ts @@ -1,21 +1,30 @@ +import { sql } from 'drizzle-orm'; import toSnakeCase from 'to-snake-case'; import type { Find } from 'payload/dist/database/types'; -import { PayloadRequest } from 'payload/dist/express/types'; +import type { PayloadRequest } from 'payload/dist/express/types'; +import type { SanitizedCollectionConfig } from 'payload/dist/collections/config/types'; import buildQuery from './queries/buildQuery'; export const find: Find = async function find({ collection, where, - page, - limit, + page = 1, + limit: limitArg, sort: sortArg, locale, pagination, req = {} as PayloadRequest, }) { - const collectionConfig = this.payload.collections[collection].config; + const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config; const tableName = toSnakeCase(collection); const table = this.tables[tableName]; + const limit = typeof limitArg === 'number' ? limitArg : collectionConfig.admin.pagination.defaultLimit; + const sort = typeof sortArg === 'string' ? sortArg : collectionConfig.defaultSort; + let totalDocs; + let totalPages; + let hasPrevPage; + let hasNextPage; + let pagingCounter; const query = await buildQuery({ collectionSlug: collection, @@ -24,19 +33,31 @@ export const find: Find = async function find({ where, }); - console.log(query); + if (pagination !== false) { + const countResult = await this.db.select({ count: sql`count(*)` }).from(table).where(query); + totalDocs = Number(countResult[0].count); + totalPages = Math.ceil(totalDocs / limit); + hasPrevPage = page > 1; + hasNextPage = totalPages > page; + pagingCounter = ((page - 1) * limit) + 1; + } - const result = await this.db.select().from(table).where(query); + const docs = await this.db.select() + .from(table) + .limit(limit === 0 ? undefined : limit) + .offset((page - 1) * limit) + .where(query); - return result; - // - // - // return { - // ...result, - // docs: docs.map((doc) => { - // // eslint-disable-next-line no-param-reassign - // doc.id = doc._id; - // return sanitizeInternalFields(doc); - // }), - // }; + return { + docs, // : T[] + totalDocs, // : number + limit, // : number + totalPages, // : number + page, // ?: number + pagingCounter, // : number + hasPrevPage, // : boolean + hasNextPage, // : boolean + prevPage: hasPrevPage ? page - 1 : null, // ?: number | null | undefined + nextPage: hasNextPage ? page + 1 : null, // ?: number | null | undefined + }; }; diff --git a/test/postgres/config.ts b/test/postgres/config.ts index f230777b8e..c30dedc6d6 100644 --- a/test/postgres/config.ts +++ b/test/postgres/config.ts @@ -1,6 +1,5 @@ import { CollectionConfig } from '../../src/collections/config/types'; import { buildConfigWithDefaults } from '../buildConfigWithDefaults'; -import { devUser } from '../credentials'; export const Posts: CollectionConfig = { slug: 'posts', @@ -198,12 +197,10 @@ const config = buildConfigWithDefaults({ }, }); - // const findResult = await payload.find({ - // collection: 'pages', - // where: { slug: { equals: 'second' } }, - // }); - - // console.log(findResult); + const findResult = await payload.find({ + collection: 'pages', + where: { slug: { equals: 'second' } }, + }); const person1 = await payload.create({ collection: 'people',