chore: wip postgres collections-graphql

This commit is contained in:
Dan Ribbens
2023-09-18 11:10:32 -04:00
parent 9bd8f14ba8
commit 025d647bf7
3 changed files with 41 additions and 32 deletions

View File

@@ -1,10 +1,12 @@
import type { SQL } from 'drizzle-orm';
import type { PgSelect } from 'drizzle-orm/pg-core';
import type { Find } from 'payload/database';
import type { PayloadRequest, SanitizedCollectionConfig, TypeWithID } from 'payload/types';
import { asc, desc, inArray, sql } from 'drizzle-orm';
import toSnakeCase from 'to-snake-case';
import type { PostgresAdapter } from './types';
import type { GenericColumn, PostgresAdapter } from './types';
import { buildFindManyArgs } from './find/buildFindManyArgs';
import buildQuery from './queries/buildQuery';
@@ -50,11 +52,16 @@ export const find: Find = async function find(
const db = req.transactionID ? this.sessions[req.transactionID] : this.db;
const orderedIDMap: Record<number | string, number> = {};
let selectQuery: PgSelect<string, Record<string, GenericColumn>, 'partial', Record<string, 'not-null'>>;
let selectCount: PgSelect<string, { count: SQL<number>; }, "partial", Record<string, "not-null">>;
const selectQuery = db.selectDistinct(selectFields)
.from(table);
if (orderBy?.order && orderBy?.column) {
selectQuery.orderBy(orderBy.order(orderBy.column));
selectQuery = db.selectDistinct(selectFields)
.from(table)
.orderBy(orderBy.order(orderBy.column));
} else {
selectQuery = db.selectDistinct(selectFields)
.from(table)
}
const findManyArgs = buildFindManyArgs({
@@ -118,7 +125,7 @@ export const find: Find = async function find(
const findPromise = db.query[tableName].findMany(findManyArgs);
if (pagination !== false || selectDistinctResult?.length > limit) {
const selectCount = db.select({ count: sql<number>`count(*)` })
selectCount = db.select({ count: sql<number>`count(*)` })
.from(table)
.where(where);
Object.entries(joins)

View File

@@ -1,18 +1,18 @@
import type { AccessResult } from '../../config/types'
import type { PaginatedDocs } from '../../database/types'
import type { PayloadRequest } from '../../express/types'
import type { Where } from '../../types'
import type { Collection, TypeWithID } from '../config/types'
import type { AccessResult } from '../../config/types';
import type { PaginatedDocs } from '../../database/types';
import type { PayloadRequest } from '../../express/types';
import type { Where } from '../../types';
import type { Collection, TypeWithID } from '../config/types';
import executeAccess from '../../auth/executeAccess'
import { combineQueries } from '../../database/combineQueries'
import { validateQueryPaths } from '../../database/queryValidation/validateQueryPaths'
import { afterRead } from '../../fields/hooks/afterRead'
import { initTransaction } from '../../utilities/initTransaction'
import { killTransaction } from '../../utilities/killTransaction'
import { buildVersionCollectionFields } from '../../versions/buildCollectionFields'
import { appendVersionToQueryKey } from '../../versions/drafts/appendVersionToQueryKey'
import { buildAfterOperation } from './utils'
import executeAccess from '../../auth/executeAccess';
import { combineQueries } from '../../database/combineQueries';
import { validateQueryPaths } from '../../database/queryValidation/validateQueryPaths';
import { afterRead } from '../../fields/hooks/afterRead';
import { initTransaction } from '../../utilities/initTransaction';
import { killTransaction } from '../../utilities/killTransaction';
import { buildVersionCollectionFields } from '../../versions/buildCollectionFields';
import { appendVersionToQueryKey } from '../../versions/drafts/appendVersionToQueryKey';
import { buildAfterOperation } from './utils';
export type Arguments = {
collection: Collection
@@ -156,11 +156,11 @@ async function find<T extends TypeWithID & Record<string, unknown>>(
result = await payload.db.find<T>({
collection: collectionConfig.slug,
limit: sanitizedLimit,
sort: sort ? sort.replace(/__/gi, '.') : undefined,
locale,
page: sanitizedPage,
pagination,
req,
sort: sort ? sort.replace(/__/g, '.') : undefined,
where: fullWhere,
})
}

View File

@@ -1,11 +1,11 @@
import { GraphQLClient } from 'graphql-request'
import { GraphQLClient } from 'graphql-request';
import type { Post } from './payload-types'
import type { Post } from './payload-types';
import payload from '../../packages/payload/src'
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync'
import { initPayloadTest } from '../helpers/configHelpers'
import configPromise, { pointSlug, slug } from './config'
import payload from '../../packages/payload/src';
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync';
import { initPayloadTest } from '../helpers/configHelpers';
import configPromise, { pointSlug, slug } from './config';
const title = 'title'
@@ -20,12 +20,14 @@ describe('collections-graphql', () => {
// Wait for indexes to be created,
// as we need them to query by point
await new Promise((resolve, reject) => {
payload.db.collections.point.ensureIndexes(function (err) {
if (err) reject(err)
resolve(true)
})
})
if (payload.db?.collections?.point?.ensureIndexes) {
await new Promise((resolve, reject) => {
payload.db?.collections?.point?.ensureIndexes(function (err) {
if (err) reject(err);
resolve(true);
});
});
}
})
afterAll(async () => {