postgres operations buildQuery updates

This commit is contained in:
Dan Ribbens
2023-09-07 11:49:21 -04:00
parent 2e086d95d9
commit 595173327e
7 changed files with 64 additions and 29 deletions

View File

@@ -7,14 +7,19 @@ import { buildFindManyArgs } from './find/buildFindManyArgs';
import { transform } from './transform/read';
export const deleteMany: DeleteMany = async function deleteMany(this: PostgresAdapter,
{ collection, where, req = {} as PayloadRequest }) {
{
collection,
where: incomingWhere,
req = {} as PayloadRequest,
}) {
const collectionConfig = this.payload.collections[collection].config;
const tableName = toSnakeCase(collection);
const query = await buildQuery({
const { where } = await buildQuery({
adapter: this,
collectionSlug: collection,
where,
fields: collectionConfig.fields,
where: incomingWhere,
tableName,
});
const findManyArgs = buildFindManyArgs({
@@ -24,7 +29,7 @@ export const deleteMany: DeleteMany = async function deleteMany(this: PostgresAd
tableName,
});
findManyArgs.where = query;
findManyArgs.where = where;
const docsToDelete = await this.db.query[tableName].findMany(findManyArgs);
@@ -36,7 +41,8 @@ export const deleteMany: DeleteMany = async function deleteMany(this: PostgresAd
});
});
await this.db.delete(this.tables[tableName]).where(query);
await this.db.delete(this.tables[tableName])
.where(where);
return result;
};

View File

@@ -8,15 +8,20 @@ import { transform } from './transform/read';
export const deleteOne: DeleteOne = async function deleteOne(
this: PostgresAdapter,
{ collection, where, req = {} as PayloadRequest },
{
collection,
where: incomingWhere,
req = {} as PayloadRequest,
},
) {
const collectionConfig = this.payload.collections[collection].config;
const tableName = toSnakeCase(collection);
const query = await buildQuery({
const { where } = await buildQuery({
adapter: this,
collectionSlug: collection,
where,
fields: collectionConfig.fields,
tableName,
where: incomingWhere,
});
const findManyArgs = buildFindManyArgs({
@@ -26,7 +31,7 @@ export const deleteOne: DeleteOne = async function deleteOne(
tableName,
});
findManyArgs.where = query;
findManyArgs.where = where;
const docToDelete = await this.db.query[tableName].findFirst(findManyArgs);
@@ -36,7 +41,8 @@ export const deleteOne: DeleteOne = async function deleteOne(
fields: collectionConfig.fields,
});
await this.db.delete(this.tables[tableName]).where(query);
await this.db.delete(this.tables[tableName])
.where(where);
return result;
};

View File

@@ -2,7 +2,7 @@ import { asc, desc, inArray, sql } from 'drizzle-orm';
import toSnakeCase from 'to-snake-case';
import type { Find } from 'payload/dist/database/types';
import type { PayloadRequest } from 'payload/dist/express/types';
import type { SanitizedCollectionConfig } from 'payload/dist/collections/config/types';
import type { SanitizedCollectionConfig, TypeWithID } from 'payload/dist/collections/config/types';
import buildQuery from './queries/buildQuery';
import { buildFindManyArgs } from './find/buildFindManyArgs';
import { transform } from './transform/read';
@@ -117,7 +117,7 @@ export const find: Find = async function find(
rawDocs.sort((a, b) => (orderedIDMap[a.id] - orderedIDMap[b.id]));
}
const docs = rawDocs.map((data) => {
const docs = rawDocs.map((data: TypeWithID) => {
return transform({
config: this.payload.config,
data,

View File

@@ -8,18 +8,19 @@ import { transform } from './transform/read';
export const findOne: FindOne = async function findOne({
collection,
where,
where: incomingWhere,
locale,
req = {} as PayloadRequest,
}) {
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config;
const tableName = toSnakeCase(collection);
const query = await buildQuery({
const { where } = await buildQuery({
adapter: this,
collectionSlug: collection,
fields: collectionConfig.fields,
tableName,
locale,
where,
where: incomingWhere,
});
const findManyArgs = buildFindManyArgs({
@@ -29,15 +30,13 @@ export const findOne: FindOne = async function findOne({
tableName,
});
findManyArgs.where = query;
findManyArgs.where = where;
const doc = await this.db.query[tableName].findFirst(findManyArgs);
const result = transform({
return transform({
config: this.payload.config,
data: doc,
fields: collectionConfig.fields,
});
return result;
};

View File

@@ -11,7 +11,7 @@ type BuildQueryArgs = {
adapter: PostgresAdapter
fields: Field[]
locale?: string
sort: string
sort?: string
tableName: string
where: Where
}

View File

@@ -44,6 +44,18 @@ export const getTableColumnFromPath = ({
.find((fieldToFind) => fieldAffectsData(fieldToFind) && fieldToFind.name === fieldPath) as Field | TabAsField;
let newTableName = tableName;
if (!field && fieldPath === 'id') {
return {
collectionPath,
field: {
name: 'id',
type: 'number',
},
table: adapter.tables[newTableName],
columnName: 'id',
};
}
if (field) {
switch (field.type) {
case 'tabs': {
@@ -87,7 +99,7 @@ export const getTableColumnFromPath = ({
}
case 'group': {
if (field.localized && adapter.payload.config.localization) {
if (locale && field.localized && adapter.payload.config.localization) {
newTableName = `${tableName}_locales`;
joins[tableName] = eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID);
}
@@ -105,7 +117,7 @@ export const getTableColumnFromPath = ({
case 'array': {
newTableName = `${tableName}_${toSnakeCase(field.name)}`;
if (field.localized && adapter.payload.config.localization) {
if (locale && field.localized && adapter.payload.config.localization) {
joins[newTableName] = and(
eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID),
eq(adapter[newTableName]._locale, locale),
@@ -135,7 +147,7 @@ export const getTableColumnFromPath = ({
let relationshipFields;
if (typeof field.relationTo === 'string') {
relationshipFields = adapter.payload.collections[field.relationTo];
if (field.localized && adapter.payload.config.localization) {
if (locale && field.localized && adapter.payload.config.localization) {
joins[newTableName] = and(
eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID),
eq(adapter[newTableName]._locale, locale),

View File

@@ -1,5 +1,17 @@
/* eslint-disable no-param-reassign */
import { AnyPgColumnBuilder, integer, text, varchar, numeric, IndexBuilder, PgNumericBuilder, PgVarcharBuilder, jsonb, unique, UniqueConstraintBuilder } from 'drizzle-orm/pg-core';
import {
AnyPgColumnBuilder,
IndexBuilder,
integer,
jsonb,
numeric,
PgNumericBuilder,
PgVarcharBuilder,
text,
unique,
UniqueConstraintBuilder,
varchar,
} from 'drizzle-orm/pg-core';
import { Field } from 'payload/types';
import toSnakeCase from 'to-snake-case';
import { fieldAffectsData } from 'payload/dist/fields/config/types';
@@ -67,7 +79,7 @@ export const traverseFields = ({
// If field is localized,
// add the column to the locale table instead of main table
if (field.localized || forceLocalized) {
if (adapter.payload.config.localization && (field.localized || forceLocalized)) {
hasLocalizedField = true;
targetTable = localesColumns;
targetIndexes = localesIndexes;
@@ -325,7 +337,7 @@ export const traverseFields = ({
relationships.add(field.relationTo);
}
if (field.localized) {
if (field.localized && adapter.payload.config.localization) {
hasLocalizedRelationshipField = true;
}
break;