chore: bugs with sort, graphql tests with postgres
This commit is contained in:
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -17,7 +17,7 @@
|
|||||||
"type": "node-terminal"
|
"type": "node-terminal"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"command": "pnpm run dev:postgres postgres -- -I", // Allow input
|
"command": "pnpm run dev:postgres collections-graphql -- -I", // Allow input
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
"name": "Run Dev Postgres",
|
"name": "Run Dev Postgres",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
|
|||||||
@@ -114,14 +114,7 @@ export const find: Find = async function find(
|
|||||||
if (where) {
|
if (where) {
|
||||||
findManyArgs.where = where
|
findManyArgs.where = where
|
||||||
}
|
}
|
||||||
// orderBy will only be set if a complex sort is needed on a relation
|
findManyArgs.orderBy = orderBy.order(orderBy.column)
|
||||||
if (sort) {
|
|
||||||
if (sort[0] === '-') {
|
|
||||||
findManyArgs.orderBy = desc(this.tables[tableName][sort.substring(1)])
|
|
||||||
} else {
|
|
||||||
findManyArgs.orderBy = asc(this.tables[tableName][sort])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const findPromise = db.query[tableName].findMany(findManyArgs)
|
const findPromise = db.query[tableName].findMany(findManyArgs)
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import type { SQL } from 'drizzle-orm';
|
import type { SQL } from 'drizzle-orm'
|
||||||
import type { Field, Where } from 'payload/types';
|
import type { Field, Where } from 'payload/types'
|
||||||
|
|
||||||
import { asc, desc } from 'drizzle-orm';
|
import { asc, desc } from 'drizzle-orm'
|
||||||
|
|
||||||
import type { GenericColumn, PostgresAdapter } from '../types';
|
import type { GenericColumn, PostgresAdapter } from '../types'
|
||||||
|
|
||||||
import { getTableColumnFromPath } from './getTableColumnFromPath';
|
import { getTableColumnFromPath } from './getTableColumnFromPath'
|
||||||
import { parseParams } from './parseParams';
|
import { parseParams } from './parseParams'
|
||||||
|
|
||||||
export type BuildQueryJoins = Record<string, SQL>
|
export type BuildQueryJoins = Record<string, SQL>
|
||||||
|
|
||||||
@@ -38,28 +38,25 @@ const buildQuery = async function buildQuery({
|
|||||||
}: BuildQueryArgs): Promise<Result> {
|
}: BuildQueryArgs): Promise<Result> {
|
||||||
const selectFields: Record<string, GenericColumn> = {
|
const selectFields: Record<string, GenericColumn> = {
|
||||||
id: adapter.tables[tableName].id,
|
id: adapter.tables[tableName].id,
|
||||||
};
|
}
|
||||||
const joins: BuildQueryJoins = {};
|
const joins: BuildQueryJoins = {}
|
||||||
const orderBy: Result['orderBy'] = {
|
const orderBy: Result['orderBy'] = {
|
||||||
column: null,
|
column: null,
|
||||||
order: null,
|
order: null,
|
||||||
};
|
|
||||||
|
|
||||||
if (sort) {
|
|
||||||
let sortPath;
|
|
||||||
|
|
||||||
if (sort[0] === '-') {
|
|
||||||
sortPath = sort.substring(1);
|
|
||||||
orderBy.order = desc;
|
|
||||||
} else {
|
|
||||||
sortPath = sort;
|
|
||||||
orderBy.order = asc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
if (sort) {
|
||||||
columnName: sortTableColumnName,
|
let sortPath
|
||||||
table: sortTable,
|
|
||||||
} = getTableColumnFromPath({
|
if (sort[0] === '-') {
|
||||||
|
sortPath = sort.substring(1)
|
||||||
|
orderBy.order = desc
|
||||||
|
} else {
|
||||||
|
sortPath = sort
|
||||||
|
orderBy.order = asc
|
||||||
|
}
|
||||||
|
|
||||||
|
const { columnName: sortTableColumnName, table: sortTable } = getTableColumnFromPath({
|
||||||
adapter,
|
adapter,
|
||||||
collectionPath: sortPath,
|
collectionPath: sortPath,
|
||||||
fields,
|
fields,
|
||||||
@@ -68,16 +65,25 @@ const buildQuery = async function buildQuery({
|
|||||||
pathSegments: sortPath.split('.'),
|
pathSegments: sortPath.split('.'),
|
||||||
selectFields,
|
selectFields,
|
||||||
tableName,
|
tableName,
|
||||||
});
|
})
|
||||||
|
|
||||||
orderBy.column = sortTable[sortTableColumnName];
|
orderBy.column = sortTable[sortTableColumnName]
|
||||||
|
} else {
|
||||||
|
orderBy.order = desc
|
||||||
|
const createdAt = adapter.tables[tableName]?.createdAt
|
||||||
|
|
||||||
|
if (createdAt) {
|
||||||
|
orderBy.column = createdAt
|
||||||
|
} else {
|
||||||
|
orderBy.column = adapter.tables[tableName].id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (orderBy.column) {
|
if (orderBy.column) {
|
||||||
selectFields.sort = orderBy.column;
|
selectFields.sort = orderBy.column
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let where: SQL;
|
let where: SQL
|
||||||
|
|
||||||
if (Object.keys(incomingWhere).length > 0) {
|
if (Object.keys(incomingWhere).length > 0) {
|
||||||
where = await parseParams({
|
where = await parseParams({
|
||||||
@@ -88,7 +94,7 @@ const buildQuery = async function buildQuery({
|
|||||||
selectFields,
|
selectFields,
|
||||||
tableName,
|
tableName,
|
||||||
where: incomingWhere,
|
where: incomingWhere,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -96,7 +102,7 @@ const buildQuery = async function buildQuery({
|
|||||||
orderBy,
|
orderBy,
|
||||||
selectFields,
|
selectFields,
|
||||||
where,
|
where,
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export default buildQuery;
|
export default buildQuery
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import type { Relation } from 'drizzle-orm';
|
import type { Relation } from 'drizzle-orm'
|
||||||
import type { IndexBuilder, PgColumnBuilder, UniqueConstraintBuilder } from 'drizzle-orm/pg-core';
|
import type { IndexBuilder, PgColumnBuilder, UniqueConstraintBuilder } from 'drizzle-orm/pg-core'
|
||||||
import type { Field } from 'payload/types';
|
import type { Field } from 'payload/types'
|
||||||
|
|
||||||
import { relations } from 'drizzle-orm';
|
import { relations } from 'drizzle-orm'
|
||||||
import {
|
import {
|
||||||
PgNumericBuilder,
|
PgNumericBuilder,
|
||||||
PgVarcharBuilder,
|
PgVarcharBuilder,
|
||||||
@@ -13,16 +13,16 @@ import {
|
|||||||
text,
|
text,
|
||||||
unique,
|
unique,
|
||||||
varchar,
|
varchar,
|
||||||
} from 'drizzle-orm/pg-core';
|
} from 'drizzle-orm/pg-core'
|
||||||
import { fieldAffectsData } from 'payload/types';
|
import { fieldAffectsData } from 'payload/types'
|
||||||
import toSnakeCase from 'to-snake-case';
|
import toSnakeCase from 'to-snake-case'
|
||||||
|
|
||||||
import type { GenericColumns, PostgresAdapter } from '../types';
|
import type { GenericColumns, PostgresAdapter } from '../types'
|
||||||
|
|
||||||
import { hasLocalesTable } from '../utilities/hasLocalesTable';
|
import { hasLocalesTable } from '../utilities/hasLocalesTable'
|
||||||
import { buildTable } from './build';
|
import { buildTable } from './build'
|
||||||
import { createIndex } from './createIndex';
|
import { createIndex } from './createIndex'
|
||||||
import { parentIDColumnMap } from './parentIDColumnMap';
|
import { parentIDColumnMap } from './parentIDColumnMap'
|
||||||
|
|
||||||
type Args = {
|
type Args = {
|
||||||
adapter: PostgresAdapter
|
adapter: PostgresAdapter
|
||||||
@@ -62,33 +62,40 @@ export const traverseFields = ({
|
|||||||
parentTableName,
|
parentTableName,
|
||||||
relationships,
|
relationships,
|
||||||
}: Args): Result => {
|
}: Args): Result => {
|
||||||
let hasLocalizedField = false;
|
let hasLocalizedField = false
|
||||||
let hasLocalizedRelationshipField = false;
|
let hasLocalizedRelationshipField = false
|
||||||
|
|
||||||
let parentIDColType = 'integer';
|
let parentIDColType = 'integer'
|
||||||
if (columns.id instanceof PgNumericBuilder) parentIDColType = 'numeric';
|
if (columns.id instanceof PgNumericBuilder) parentIDColType = 'numeric'
|
||||||
if (columns.id instanceof PgVarcharBuilder) parentIDColType = 'varchar';
|
if (columns.id instanceof PgVarcharBuilder) parentIDColType = 'varchar'
|
||||||
|
|
||||||
fields.forEach((field) => {
|
fields.forEach((field) => {
|
||||||
if ('name' in field && field.name === 'id') return;
|
if ('name' in field && field.name === 'id') return
|
||||||
let columnName: string;
|
let columnName: string
|
||||||
|
|
||||||
let targetTable = columns;
|
let targetTable = columns
|
||||||
let targetIndexes = indexes;
|
let targetIndexes = indexes
|
||||||
|
|
||||||
if (fieldAffectsData(field)) {
|
if (fieldAffectsData(field)) {
|
||||||
columnName = `${columnPrefix || ''}${toSnakeCase(field.name)}`;
|
columnName = `${columnPrefix || ''}${toSnakeCase(field.name)}`
|
||||||
|
|
||||||
// If field is localized,
|
// If field is localized,
|
||||||
// add the column to the locale table instead of main table
|
// add the column to the locale table instead of main table
|
||||||
if (adapter.payload.config.localization && (field.localized || forceLocalized)) {
|
if (adapter.payload.config.localization && (field.localized || forceLocalized)) {
|
||||||
hasLocalizedField = true;
|
hasLocalizedField = true
|
||||||
targetTable = localesColumns;
|
targetTable = localesColumns
|
||||||
targetIndexes = localesIndexes;
|
targetIndexes = localesIndexes
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((field.unique || field.index) && !['array', 'blocks', 'group', 'relationship', 'upload'].includes(field.type)) {
|
if (
|
||||||
targetIndexes[`${field.name}Idx`] = createIndex({ columnName, name: field.name, unique: field.unique });
|
(field.unique || field.index) &&
|
||||||
|
!['array', 'blocks', 'group', 'relationship', 'upload'].includes(field.type)
|
||||||
|
) {
|
||||||
|
targetIndexes[`${field.name}Idx`] = createIndex({
|
||||||
|
name: field.name,
|
||||||
|
columnName,
|
||||||
|
unique: field.unique,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,55 +106,61 @@ export const traverseFields = ({
|
|||||||
case 'textarea': {
|
case 'textarea': {
|
||||||
// TODO: handle hasMany
|
// TODO: handle hasMany
|
||||||
// TODO: handle min / max length
|
// TODO: handle min / max length
|
||||||
targetTable[`${fieldPrefix || ''}${field.name}`] = varchar(columnName);
|
targetTable[`${fieldPrefix || ''}${field.name}`] = varchar(columnName)
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'number': {
|
case 'number': {
|
||||||
// TODO: handle hasMany
|
// TODO: handle hasMany
|
||||||
// TODO: handle min / max
|
// TODO: handle min / max
|
||||||
targetTable[`${fieldPrefix || ''}${field.name}`] = numeric(columnName);
|
targetTable[`${fieldPrefix || ''}${field.name}`] = numeric(columnName)
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'richText':
|
case 'richText':
|
||||||
case 'json': {
|
case 'json': {
|
||||||
targetTable[`${fieldPrefix || ''}${field.name}`] = jsonb(columnName);
|
targetTable[`${fieldPrefix || ''}${field.name}`] = jsonb(columnName)
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'date': {
|
case 'date': {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'point': {
|
case 'point': {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'radio': {
|
case 'radio': {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'select': {
|
case 'select': {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'array': {
|
case 'array': {
|
||||||
const baseColumns: Record<string, PgColumnBuilder> = {
|
const baseColumns: Record<string, PgColumnBuilder> = {
|
||||||
_order: integer('_order').notNull(),
|
_order: integer('_order').notNull(),
|
||||||
_parentID: parentIDColumnMap[parentIDColType]('_parent_id').references(() => adapter.tables[parentTableName].id, { onDelete: 'cascade' }).notNull(),
|
_parentID: parentIDColumnMap[parentIDColType]('_parent_id')
|
||||||
};
|
.references(() => adapter.tables[parentTableName].id, { onDelete: 'cascade' })
|
||||||
|
.notNull(),
|
||||||
const baseExtraConfig: Record<string, (cols: GenericColumns) => IndexBuilder | UniqueConstraintBuilder> = {};
|
|
||||||
|
|
||||||
if (field.localized && adapter.payload.config.localization) {
|
|
||||||
baseColumns._locale = adapter.enums._locales('_locale').notNull();
|
|
||||||
baseExtraConfig._parentOrderLocale = (cols) => unique().on(cols._parentID, cols._order, cols._locale);
|
|
||||||
} else {
|
|
||||||
baseExtraConfig._parentOrder = (cols) => unique().on(cols._parentID, cols._order);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const arrayTableName = `${newTableName}_${toSnakeCase(field.name)}`;
|
const baseExtraConfig: Record<
|
||||||
|
string,
|
||||||
|
(cols: GenericColumns) => IndexBuilder | UniqueConstraintBuilder
|
||||||
|
> = {}
|
||||||
|
|
||||||
|
if (field.localized && adapter.payload.config.localization) {
|
||||||
|
baseColumns._locale = adapter.enums._locales('_locale').notNull()
|
||||||
|
baseExtraConfig._parentOrderLocale = (cols) =>
|
||||||
|
unique().on(cols._parentID, cols._order, cols._locale)
|
||||||
|
} else {
|
||||||
|
baseExtraConfig._parentOrder = (cols) => unique().on(cols._parentID, cols._order)
|
||||||
|
}
|
||||||
|
|
||||||
|
const arrayTableName = `${newTableName}_${toSnakeCase(field.name)}`
|
||||||
|
|
||||||
const { arrayBlockRelations: subArrayBlockRelations } = buildTable({
|
const { arrayBlockRelations: subArrayBlockRelations } = buildTable({
|
||||||
adapter,
|
adapter,
|
||||||
@@ -155,9 +168,9 @@ export const traverseFields = ({
|
|||||||
baseExtraConfig,
|
baseExtraConfig,
|
||||||
fields: field.fields,
|
fields: field.fields,
|
||||||
tableName: arrayTableName,
|
tableName: arrayTableName,
|
||||||
});
|
})
|
||||||
|
|
||||||
arrayBlockRelations.set(`${fieldPrefix || ''}${field.name}`, arrayTableName);
|
arrayBlockRelations.set(`${fieldPrefix || ''}${field.name}`, arrayTableName)
|
||||||
|
|
||||||
const arrayTableRelations = relations(adapter.tables[arrayTableName], ({ many, one }) => {
|
const arrayTableRelations = relations(adapter.tables[arrayTableName], ({ many, one }) => {
|
||||||
const result: Record<string, Relation<string>> = {
|
const result: Record<string, Relation<string>> = {
|
||||||
@@ -165,41 +178,48 @@ export const traverseFields = ({
|
|||||||
fields: [adapter.tables[arrayTableName]._parentID],
|
fields: [adapter.tables[arrayTableName]._parentID],
|
||||||
references: [adapter.tables[parentTableName].id],
|
references: [adapter.tables[parentTableName].id],
|
||||||
}),
|
}),
|
||||||
};
|
}
|
||||||
|
|
||||||
if (hasLocalesTable(field.fields)) {
|
if (hasLocalesTable(field.fields)) {
|
||||||
result._locales = many(adapter.tables[`${arrayTableName}_locales`]);
|
result._locales = many(adapter.tables[`${arrayTableName}_locales`])
|
||||||
}
|
}
|
||||||
|
|
||||||
subArrayBlockRelations.forEach((val, key) => {
|
subArrayBlockRelations.forEach((val, key) => {
|
||||||
result[key] = many(adapter.tables[val]);
|
result[key] = many(adapter.tables[val])
|
||||||
});
|
})
|
||||||
|
|
||||||
return result;
|
return result
|
||||||
});
|
})
|
||||||
|
|
||||||
adapter.relations[`relations_${arrayTableName}`] = arrayTableRelations;
|
adapter.relations[`relations_${arrayTableName}`] = arrayTableRelations
|
||||||
|
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'blocks': {
|
case 'blocks': {
|
||||||
field.blocks.forEach((block) => {
|
field.blocks.forEach((block) => {
|
||||||
const blockTableName = `${newTableName}_${toSnakeCase(block.slug)}`;
|
const blockTableName = `${newTableName}_${toSnakeCase(block.slug)}`
|
||||||
if (!adapter.tables[blockTableName]) {
|
if (!adapter.tables[blockTableName]) {
|
||||||
const baseColumns: Record<string, PgColumnBuilder> = {
|
const baseColumns: Record<string, PgColumnBuilder> = {
|
||||||
_order: integer('_order').notNull(),
|
_order: integer('_order').notNull(),
|
||||||
_parentID: parentIDColumnMap[parentIDColType]('_parent_id').references(() => adapter.tables[parentTableName].id, { onDelete: 'cascade' }).notNull(),
|
_parentID: parentIDColumnMap[parentIDColType]('_parent_id')
|
||||||
|
.references(() => adapter.tables[parentTableName].id, { onDelete: 'cascade' })
|
||||||
|
.notNull(),
|
||||||
_path: text('_path').notNull(),
|
_path: text('_path').notNull(),
|
||||||
};
|
}
|
||||||
|
|
||||||
const baseExtraConfig: Record<string, (cols: GenericColumns) => IndexBuilder | UniqueConstraintBuilder> = {};
|
const baseExtraConfig: Record<
|
||||||
|
string,
|
||||||
|
(cols: GenericColumns) => IndexBuilder | UniqueConstraintBuilder
|
||||||
|
> = {}
|
||||||
|
|
||||||
if (field.localized && adapter.payload.config.localization) {
|
if (field.localized && adapter.payload.config.localization) {
|
||||||
baseColumns._locale = adapter.enums._locales('_locale').notNull();
|
baseColumns._locale = adapter.enums._locales('_locale').notNull()
|
||||||
baseExtraConfig._parentPathOrderLocale = (cols) => unique().on(cols._parentID, cols._path, cols._order, cols._locale);
|
baseExtraConfig._parentPathOrderLocale = (cols) =>
|
||||||
|
unique().on(cols._parentID, cols._path, cols._order, cols._locale)
|
||||||
} else {
|
} else {
|
||||||
baseExtraConfig._parentPathOrder = (cols) => unique().on(cols._parentID, cols._path, cols._order);
|
baseExtraConfig._parentPathOrder = (cols) =>
|
||||||
|
unique().on(cols._parentID, cols._path, cols._order)
|
||||||
}
|
}
|
||||||
|
|
||||||
const { arrayBlockRelations: subArrayBlockRelations } = buildTable({
|
const { arrayBlockRelations: subArrayBlockRelations } = buildTable({
|
||||||
@@ -208,34 +228,37 @@ export const traverseFields = ({
|
|||||||
baseExtraConfig,
|
baseExtraConfig,
|
||||||
fields: block.fields,
|
fields: block.fields,
|
||||||
tableName: blockTableName,
|
tableName: blockTableName,
|
||||||
});
|
})
|
||||||
|
|
||||||
const blockTableRelations = relations(adapter.tables[blockTableName], ({ many, one }) => {
|
const blockTableRelations = relations(
|
||||||
|
adapter.tables[blockTableName],
|
||||||
|
({ many, one }) => {
|
||||||
const result: Record<string, Relation<string>> = {
|
const result: Record<string, Relation<string>> = {
|
||||||
_parentID: one(adapter.tables[parentTableName], {
|
_parentID: one(adapter.tables[parentTableName], {
|
||||||
fields: [adapter.tables[blockTableName]._parentID],
|
fields: [adapter.tables[blockTableName]._parentID],
|
||||||
references: [adapter.tables[parentTableName].id],
|
references: [adapter.tables[parentTableName].id],
|
||||||
}),
|
}),
|
||||||
};
|
}
|
||||||
|
|
||||||
if (hasLocalesTable(block.fields)) {
|
if (hasLocalesTable(block.fields)) {
|
||||||
result._locales = many(adapter.tables[`${blockTableName}_locales`]);
|
result._locales = many(adapter.tables[`${blockTableName}_locales`])
|
||||||
}
|
}
|
||||||
|
|
||||||
subArrayBlockRelations.forEach((val, key) => {
|
subArrayBlockRelations.forEach((val, key) => {
|
||||||
result[key] = many(adapter.tables[val]);
|
result[key] = many(adapter.tables[val])
|
||||||
});
|
})
|
||||||
|
|
||||||
return result;
|
return result
|
||||||
});
|
},
|
||||||
|
)
|
||||||
|
|
||||||
adapter.relations[`relations_${blockTableName}`] = blockTableRelations;
|
adapter.relations[`relations_${blockTableName}`] = blockTableRelations
|
||||||
}
|
}
|
||||||
|
|
||||||
arrayBlockRelations.set(`_blocks_${block.slug}`, blockTableName);
|
arrayBlockRelations.set(`_blocks_${block.slug}`, blockTableName)
|
||||||
});
|
})
|
||||||
|
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'group': {
|
case 'group': {
|
||||||
@@ -257,11 +280,11 @@ export const traverseFields = ({
|
|||||||
newTableName: `${parentTableName}_${toSnakeCase(field.name)}`,
|
newTableName: `${parentTableName}_${toSnakeCase(field.name)}`,
|
||||||
parentTableName,
|
parentTableName,
|
||||||
relationships,
|
relationships,
|
||||||
});
|
})
|
||||||
|
|
||||||
if (groupHasLocalizedField) hasLocalizedField = true;
|
if (groupHasLocalizedField) hasLocalizedField = true
|
||||||
if (groupHasLocalizedRelationshipField) hasLocalizedRelationshipField = true;
|
if (groupHasLocalizedRelationshipField) hasLocalizedRelationshipField = true
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'tabs': {
|
case 'tabs': {
|
||||||
@@ -274,7 +297,7 @@ export const traverseFields = ({
|
|||||||
adapter,
|
adapter,
|
||||||
arrayBlockRelations,
|
arrayBlockRelations,
|
||||||
buildRelationships,
|
buildRelationships,
|
||||||
columnPrefix: `${columnName}_`,
|
columnPrefix: `${columnPrefix || ''}${toSnakeCase(tab.name)}_`,
|
||||||
columns,
|
columns,
|
||||||
fieldPrefix: `${fieldPrefix || ''}${tab.name}_`,
|
fieldPrefix: `${fieldPrefix || ''}${tab.name}_`,
|
||||||
fields: tab.fields,
|
fields: tab.fields,
|
||||||
@@ -284,19 +307,18 @@ export const traverseFields = ({
|
|||||||
newTableName: `${parentTableName}_${toSnakeCase(tab.name)}`,
|
newTableName: `${parentTableName}_${toSnakeCase(tab.name)}`,
|
||||||
parentTableName,
|
parentTableName,
|
||||||
relationships,
|
relationships,
|
||||||
});
|
})
|
||||||
|
|
||||||
if (tabHasLocalizedField) hasLocalizedField = true;
|
if (tabHasLocalizedField) hasLocalizedField = true
|
||||||
if (tabHasLocalizedRelationshipField) hasLocalizedRelationshipField = true;
|
if (tabHasLocalizedRelationshipField) hasLocalizedRelationshipField = true
|
||||||
} else {
|
} else {
|
||||||
({
|
;({ hasLocalizedField, hasLocalizedRelationshipField } = traverseFields({
|
||||||
hasLocalizedField,
|
|
||||||
hasLocalizedRelationshipField,
|
|
||||||
} = traverseFields({
|
|
||||||
adapter,
|
adapter,
|
||||||
arrayBlockRelations,
|
arrayBlockRelations,
|
||||||
buildRelationships,
|
buildRelationships,
|
||||||
|
columnPrefix,
|
||||||
columns,
|
columns,
|
||||||
|
fieldPrefix,
|
||||||
fields: tab.fields,
|
fields: tab.fields,
|
||||||
indexes,
|
indexes,
|
||||||
localesColumns,
|
localesColumns,
|
||||||
@@ -304,22 +326,21 @@ export const traverseFields = ({
|
|||||||
newTableName: parentTableName,
|
newTableName: parentTableName,
|
||||||
parentTableName,
|
parentTableName,
|
||||||
relationships,
|
relationships,
|
||||||
}));
|
}))
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'row':
|
case 'row':
|
||||||
case 'collapsible': {
|
case 'collapsible': {
|
||||||
({
|
;({ hasLocalizedField, hasLocalizedRelationshipField } = traverseFields({
|
||||||
hasLocalizedField,
|
|
||||||
hasLocalizedRelationshipField,
|
|
||||||
} = traverseFields({
|
|
||||||
adapter,
|
adapter,
|
||||||
arrayBlockRelations,
|
arrayBlockRelations,
|
||||||
buildRelationships,
|
buildRelationships,
|
||||||
|
columnPrefix,
|
||||||
columns,
|
columns,
|
||||||
|
fieldPrefix,
|
||||||
fields: field.fields,
|
fields: field.fields,
|
||||||
indexes,
|
indexes,
|
||||||
localesColumns,
|
localesColumns,
|
||||||
@@ -327,27 +348,27 @@ export const traverseFields = ({
|
|||||||
newTableName: parentTableName,
|
newTableName: parentTableName,
|
||||||
parentTableName,
|
parentTableName,
|
||||||
relationships,
|
relationships,
|
||||||
}));
|
}))
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'relationship':
|
case 'relationship':
|
||||||
case 'upload':
|
case 'upload':
|
||||||
if (Array.isArray(field.relationTo)) {
|
if (Array.isArray(field.relationTo)) {
|
||||||
field.relationTo.forEach((relation) => relationships.add(relation));
|
field.relationTo.forEach((relation) => relationships.add(relation))
|
||||||
} else {
|
} else {
|
||||||
relationships.add(field.relationTo);
|
relationships.add(field.relationTo)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.localized && adapter.payload.config.localization) {
|
if (field.localized && adapter.payload.config.localization) {
|
||||||
hasLocalizedRelationshipField = true;
|
hasLocalizedRelationshipField = true
|
||||||
}
|
}
|
||||||
break;
|
break
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
return { hasLocalizedField, hasLocalizedRelationshipField };
|
return { hasLocalizedField, hasLocalizedRelationshipField }
|
||||||
};
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import type { SanitizedConfig } from 'payload/config';
|
import type { SanitizedConfig } from 'payload/config'
|
||||||
import type { Field, TypeWithID } from 'payload/types';
|
import type { Field, TypeWithID } from 'payload/types'
|
||||||
|
|
||||||
import { createBlocksMap } from '../../utilities/createBlocksMap';
|
import { createBlocksMap } from '../../utilities/createBlocksMap'
|
||||||
import { createRelationshipMap } from '../../utilities/createRelationshipMap';
|
import { createRelationshipMap } from '../../utilities/createRelationshipMap'
|
||||||
import { traverseFields } from './traverseFields';
|
import { traverseFields } from './traverseFields'
|
||||||
|
|
||||||
type TransformArgs = {
|
type TransformArgs = {
|
||||||
config: SanitizedConfig
|
config: SanitizedConfig
|
||||||
@@ -16,34 +16,28 @@ type TransformArgs = {
|
|||||||
|
|
||||||
// This is the entry point to transform Drizzle output data
|
// This is the entry point to transform Drizzle output data
|
||||||
// into the shape Payload expects based on field schema
|
// into the shape Payload expects based on field schema
|
||||||
export const transform = <T extends TypeWithID>({
|
export const transform = <T extends TypeWithID>({ config, data, fields }: TransformArgs): T => {
|
||||||
config,
|
let relationships: Record<string, Record<string, unknown>[]> = {}
|
||||||
data,
|
|
||||||
fields,
|
|
||||||
}: TransformArgs): T => {
|
|
||||||
let relationships: Record<string, Record<string, unknown>[]> = {};
|
|
||||||
|
|
||||||
if ('_relationships' in data) {
|
if ('_relationships' in data) {
|
||||||
relationships = createRelationshipMap(data._relationships);
|
relationships = createRelationshipMap(data._relationships)
|
||||||
delete data._relationships;
|
delete data._relationships
|
||||||
}
|
}
|
||||||
|
|
||||||
const blocks = createBlocksMap(data);
|
const blocks = createBlocksMap(data)
|
||||||
|
|
||||||
const result = traverseFields<T>({
|
const result = traverseFields<T>({
|
||||||
blocks,
|
blocks,
|
||||||
|
columnPrefix: '',
|
||||||
config,
|
config,
|
||||||
data,
|
dataRef: {
|
||||||
|
id: data.id,
|
||||||
|
},
|
||||||
fields,
|
fields,
|
||||||
path: '',
|
path: '',
|
||||||
relationships,
|
relationships,
|
||||||
siblingData: data,
|
|
||||||
table: data,
|
table: data,
|
||||||
});
|
})
|
||||||
|
|
||||||
if ('_locales' in result) {
|
return result
|
||||||
delete result._locales;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -1,30 +1,35 @@
|
|||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import type { SanitizedConfig } from 'payload/config';
|
import type { SanitizedConfig } from 'payload/config'
|
||||||
import type { Field} from 'payload/types';
|
import type { Field, TabAsField } from 'payload/types'
|
||||||
|
|
||||||
import { fieldAffectsData } from 'payload/types';
|
import { fieldAffectsData, tabHasName } from 'payload/types'
|
||||||
|
import toSnakeCase from 'to-snake-case'
|
||||||
|
|
||||||
import type { BlocksMap } from '../../utilities/createBlocksMap';
|
import type { BlocksMap } from '../../utilities/createBlocksMap'
|
||||||
|
|
||||||
import { transformRelationship } from './relationship';
|
import { transformRelationship } from './relationship'
|
||||||
|
|
||||||
type TraverseFieldsArgs = {
|
type TraverseFieldsArgs = {
|
||||||
/**
|
/**
|
||||||
* Pre-formatted blocks map
|
* Pre-formatted blocks map
|
||||||
*/
|
*/
|
||||||
blocks: BlocksMap
|
blocks: BlocksMap
|
||||||
|
/**
|
||||||
|
* Column prefix can be built up by group and named tab fields
|
||||||
|
*/
|
||||||
|
columnPrefix: string
|
||||||
/**
|
/**
|
||||||
* The full Payload config
|
* The full Payload config
|
||||||
*/
|
*/
|
||||||
config: SanitizedConfig
|
config: SanitizedConfig
|
||||||
/**
|
/**
|
||||||
* The full data, as returned from the Drizzle query
|
* The data reference to be mutated within this recursive function
|
||||||
*/
|
*/
|
||||||
data: Record<string, unknown>
|
dataRef: Record<string, unknown>
|
||||||
/**
|
/**
|
||||||
* An array of Payload fields to traverse
|
* An array of Payload fields to traverse
|
||||||
*/
|
*/
|
||||||
fields: Field[]
|
fields: (Field | TabAsField)[]
|
||||||
/**
|
/**
|
||||||
* The current field path (in dot notation), used to merge in relationships
|
* The current field path (in dot notation), used to merge in relationships
|
||||||
*/
|
*/
|
||||||
@@ -33,158 +38,151 @@ type TraverseFieldsArgs = {
|
|||||||
* All related documents, as returned by Drizzle, keyed on an object by field path
|
* All related documents, as returned by Drizzle, keyed on an object by field path
|
||||||
*/
|
*/
|
||||||
relationships: Record<string, Record<string, unknown>[]>
|
relationships: Record<string, Record<string, unknown>[]>
|
||||||
/**
|
|
||||||
* Sibling data of the fields to traverse
|
|
||||||
*/
|
|
||||||
siblingData: Record<string, unknown>
|
|
||||||
/**
|
/**
|
||||||
* Data structure representing the nearest table from db
|
* Data structure representing the nearest table from db
|
||||||
*/
|
*/
|
||||||
table: Record<string, unknown>
|
table: Record<string, unknown>
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: clean up internal data structures:
|
|
||||||
// _order, _path, _parentID, _locales, etc.
|
|
||||||
|
|
||||||
// Traverse fields recursively, transforming data
|
// Traverse fields recursively, transforming data
|
||||||
// for each field type into required Payload shape
|
// for each field type into required Payload shape
|
||||||
export const traverseFields = <T extends Record<string, unknown>>({
|
export const traverseFields = <T extends Record<string, unknown>>({
|
||||||
blocks,
|
blocks,
|
||||||
|
columnPrefix,
|
||||||
config,
|
config,
|
||||||
data,
|
dataRef,
|
||||||
fields,
|
fields,
|
||||||
path,
|
path,
|
||||||
relationships,
|
relationships,
|
||||||
siblingData,
|
|
||||||
table,
|
table,
|
||||||
}: TraverseFieldsArgs): T => {
|
}: TraverseFieldsArgs): T => {
|
||||||
const sanitizedPath = path ? `${path}.` : path;
|
const sanitizedPath = path ? `${path}.` : path
|
||||||
|
|
||||||
const formatted = fields.reduce((result, field) => {
|
const formatted = fields.reduce((result, field) => {
|
||||||
if (fieldAffectsData(field)) {
|
if (fieldAffectsData(field)) {
|
||||||
if (field.type === 'array') {
|
if (field.type === 'array') {
|
||||||
const fieldData = result[field.name];
|
const fieldData = table[field.name]
|
||||||
|
|
||||||
if (Array.isArray(fieldData)) {
|
if (Array.isArray(fieldData)) {
|
||||||
if (field.localized) {
|
if (field.localized) {
|
||||||
result[field.name] = fieldData.reduce((arrayResult, row) => {
|
result[field.name] = fieldData.reduce((arrayResult, row) => {
|
||||||
if (typeof row._locale === 'string') {
|
if (typeof row._locale === 'string') {
|
||||||
if (!arrayResult[row._locale]) arrayResult[row._locale] = [];
|
if (!arrayResult[row._locale]) arrayResult[row._locale] = []
|
||||||
|
|
||||||
const rowResult = traverseFields<T>({
|
const rowResult = traverseFields<T>({
|
||||||
blocks,
|
blocks,
|
||||||
|
columnPrefix: '',
|
||||||
config,
|
config,
|
||||||
data,
|
dataRef: row,
|
||||||
fields: field.fields,
|
fields: field.fields,
|
||||||
path: `${sanitizedPath}${field.name}.${row._order - 1}`,
|
path: `${sanitizedPath}${field.name}.${row._order - 1}`,
|
||||||
relationships,
|
relationships,
|
||||||
siblingData: row,
|
|
||||||
table: row,
|
table: row,
|
||||||
});
|
})
|
||||||
|
|
||||||
arrayResult[row._locale].push(rowResult);
|
arrayResult[row._locale].push(rowResult)
|
||||||
delete rowResult._locale;
|
delete rowResult._locale
|
||||||
}
|
}
|
||||||
|
|
||||||
return arrayResult;
|
return arrayResult
|
||||||
}, {});
|
}, {})
|
||||||
} else {
|
} else {
|
||||||
result[field.name] = fieldData.map((row, i) => {
|
result[field.name] = fieldData.map((row, i) => {
|
||||||
return traverseFields<T>({
|
return traverseFields<T>({
|
||||||
blocks,
|
blocks,
|
||||||
|
columnPrefix: '',
|
||||||
config,
|
config,
|
||||||
data,
|
dataRef: row,
|
||||||
fields: field.fields,
|
fields: field.fields,
|
||||||
path: `${sanitizedPath}${field.name}.${i}`,
|
path: `${sanitizedPath}${field.name}.${i}`,
|
||||||
relationships,
|
relationships,
|
||||||
siblingData: row,
|
|
||||||
table: row,
|
table: row,
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.type === 'blocks') {
|
if (field.type === 'blocks') {
|
||||||
const blockFieldPath = `${sanitizedPath}${field.name}`;
|
const blockFieldPath = `${sanitizedPath}${field.name}`
|
||||||
|
|
||||||
if (Array.isArray(blocks[blockFieldPath])) {
|
if (Array.isArray(blocks[blockFieldPath])) {
|
||||||
if (field.localized) {
|
if (field.localized) {
|
||||||
result[field.name] = {};
|
result[field.name] = {}
|
||||||
|
|
||||||
blocks[blockFieldPath].forEach((row) => {
|
blocks[blockFieldPath].forEach((row) => {
|
||||||
if (typeof row._locale === 'string') {
|
if (typeof row._locale === 'string') {
|
||||||
if (!result[field.name][row._locale]) result[field.name][row._locale] = [];
|
if (!result[field.name][row._locale]) result[field.name][row._locale] = []
|
||||||
result[field.name][row._locale].push(row);
|
result[field.name][row._locale].push(row)
|
||||||
delete row._locale;
|
delete row._locale
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
Object.entries(result[field.name]).forEach(([locale, localizedBlocks]) => {
|
Object.entries(result[field.name]).forEach(([locale, localizedBlocks]) => {
|
||||||
result[field.name][locale] = localizedBlocks.map((row) => {
|
result[field.name][locale] = localizedBlocks.map((row) => {
|
||||||
const block = field.blocks.find(({ slug }) => slug === row.blockType);
|
const block = field.blocks.find(({ slug }) => slug === row.blockType)
|
||||||
|
|
||||||
if (block) {
|
if (block) {
|
||||||
const blockResult = traverseFields<T>({
|
const blockResult = traverseFields<T>({
|
||||||
blocks,
|
blocks,
|
||||||
|
columnPrefix: '',
|
||||||
config,
|
config,
|
||||||
data,
|
dataRef: row,
|
||||||
fields: block.fields,
|
fields: block.fields,
|
||||||
path: `${blockFieldPath}.${row._order - 1}`,
|
path: `${blockFieldPath}.${row._order - 1}`,
|
||||||
relationships,
|
relationships,
|
||||||
siblingData: row,
|
|
||||||
table: row,
|
table: row,
|
||||||
});
|
})
|
||||||
|
|
||||||
delete blockResult._order;
|
delete blockResult._order
|
||||||
return blockResult;
|
return blockResult
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {}
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
result[field.name] = blocks[blockFieldPath].map((row, i) => {
|
result[field.name] = blocks[blockFieldPath].map((row, i) => {
|
||||||
delete row._order;
|
delete row._order
|
||||||
const block = field.blocks.find(({ slug }) => slug === row.blockType);
|
const block = field.blocks.find(({ slug }) => slug === row.blockType)
|
||||||
|
|
||||||
if (block) {
|
if (block) {
|
||||||
return traverseFields<T>({
|
return traverseFields<T>({
|
||||||
blocks,
|
blocks,
|
||||||
|
columnPrefix: '',
|
||||||
config,
|
config,
|
||||||
data,
|
dataRef: row,
|
||||||
fields: block.fields,
|
fields: block.fields,
|
||||||
path: `${blockFieldPath}.${i}`,
|
path: `${blockFieldPath}.${i}`,
|
||||||
relationships,
|
relationships,
|
||||||
siblingData: row,
|
|
||||||
table: row,
|
table: row,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.type === 'relationship') {
|
if (field.type === 'relationship') {
|
||||||
const relationPathMatch = relationships[`${sanitizedPath}${field.name}`];
|
const relationPathMatch = relationships[`${sanitizedPath}${field.name}`]
|
||||||
if (!relationPathMatch) return result;
|
if (!relationPathMatch) return result
|
||||||
|
|
||||||
if (field.localized) {
|
if (field.localized) {
|
||||||
result[field.name] = {};
|
result[field.name] = {}
|
||||||
const relationsByLocale: Record<string, Record<string, unknown>[]> = {};
|
const relationsByLocale: Record<string, Record<string, unknown>[]> = {}
|
||||||
|
|
||||||
relationPathMatch.forEach((row) => {
|
relationPathMatch.forEach((row) => {
|
||||||
if (typeof row.locale === 'string') {
|
if (typeof row.locale === 'string') {
|
||||||
if (!relationsByLocale[row.locale]) relationsByLocale[row.locale] = [];
|
if (!relationsByLocale[row.locale]) relationsByLocale[row.locale] = []
|
||||||
relationsByLocale[row.locale].push(row);
|
relationsByLocale[row.locale].push(row)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
Object.entries(relationsByLocale).forEach(([locale, relations]) => {
|
Object.entries(relationsByLocale).forEach(([locale, relations]) => {
|
||||||
transformRelationship({
|
transformRelationship({
|
||||||
@@ -192,132 +190,170 @@ export const traverseFields = <T extends Record<string, unknown>>({
|
|||||||
locale,
|
locale,
|
||||||
ref: result,
|
ref: result,
|
||||||
relations,
|
relations,
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
transformRelationship({
|
transformRelationship({
|
||||||
field,
|
field,
|
||||||
ref: result,
|
ref: result,
|
||||||
relations: relationPathMatch,
|
relations: relationPathMatch,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
const localizedFieldData = {};
|
const localizedFieldData = {}
|
||||||
const valuesToTransform: { localeRow: Record<string, unknown>, ref: Record<string, unknown> }[] = [];
|
const valuesToTransform: {
|
||||||
|
ref: Record<string, unknown>
|
||||||
|
table: Record<string, unknown>
|
||||||
|
}[] = []
|
||||||
|
|
||||||
if (field.localized && Array.isArray(table._locales)) {
|
if (field.localized && Array.isArray(table._locales)) {
|
||||||
table._locales.forEach((localeRow) => {
|
table._locales.forEach((localeRow) => {
|
||||||
valuesToTransform.push({ localeRow, ref: localizedFieldData });
|
valuesToTransform.push({ ref: localizedFieldData, table: localeRow })
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
valuesToTransform.push({ localeRow: undefined, ref: result });
|
valuesToTransform.push({ ref: result, table })
|
||||||
}
|
}
|
||||||
|
|
||||||
valuesToTransform.forEach(({ localeRow, ref }) => {
|
valuesToTransform.forEach(({ ref, table }) => {
|
||||||
const fieldData = localeRow?.[field.name] || ref[field.name];
|
const fieldData = table[field.name]
|
||||||
const locale = localeRow?._locale;
|
const locale = table?._locale
|
||||||
|
|
||||||
switch (field.type) {
|
switch (field.type) {
|
||||||
|
case 'tab':
|
||||||
case 'group': {
|
case 'group': {
|
||||||
const groupData = {};
|
// if (field.type === 'tab') {
|
||||||
|
// console.log('got one')
|
||||||
|
// }
|
||||||
|
|
||||||
field.fields.forEach((subField) => {
|
if (!tabHasName(field)) {
|
||||||
if (fieldAffectsData(subField)) {
|
traverseFields({
|
||||||
const subFieldKey = `${sanitizedPath.replace(/\./g, '_')}${field.name}_${subField.name}`;
|
blocks,
|
||||||
|
columnPrefix,
|
||||||
|
config,
|
||||||
|
dataRef,
|
||||||
|
fields: field.fields,
|
||||||
|
path: sanitizedPath,
|
||||||
|
relationships,
|
||||||
|
table,
|
||||||
|
})
|
||||||
|
|
||||||
if (typeof locale === 'string') {
|
return
|
||||||
if (!ref[locale]) ref[locale] = {};
|
|
||||||
ref[locale][subField.name] = localeRow[subFieldKey];
|
|
||||||
} else {
|
|
||||||
groupData[subField.name] = table[subFieldKey];
|
|
||||||
delete table[subFieldKey];
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
const groupColumnPrefix = `${columnPrefix || ''}${toSnakeCase(field.name)}_`
|
||||||
|
const groupData = {}
|
||||||
|
|
||||||
if (field.localized) {
|
if (field.localized) {
|
||||||
|
if (typeof locale === 'string' && !ref[locale]) ref[locale] = {}
|
||||||
|
|
||||||
Object.entries(ref).forEach(([groupLocale, groupLocaleData]) => {
|
Object.entries(ref).forEach(([groupLocale, groupLocaleData]) => {
|
||||||
ref[groupLocale] = traverseFields<Record<string, unknown>>({
|
ref[groupLocale] = traverseFields<Record<string, unknown>>({
|
||||||
blocks,
|
blocks,
|
||||||
|
columnPrefix: groupColumnPrefix,
|
||||||
config,
|
config,
|
||||||
data,
|
dataRef: groupLocaleData as Record<string, unknown>,
|
||||||
fields: field.fields,
|
fields: field.fields,
|
||||||
path: `${sanitizedPath}${field.name}`,
|
path: `${sanitizedPath}${field.name}`,
|
||||||
relationships,
|
relationships,
|
||||||
siblingData: groupLocaleData as Record<string, unknown>,
|
|
||||||
table,
|
table,
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
ref[field.name] = traverseFields<Record<string, unknown>>({
|
ref[field.name] = traverseFields<Record<string, unknown>>({
|
||||||
blocks,
|
blocks,
|
||||||
|
columnPrefix: groupColumnPrefix,
|
||||||
config,
|
config,
|
||||||
data,
|
dataRef: groupData as Record<string, unknown>,
|
||||||
fields: field.fields,
|
fields: field.fields,
|
||||||
path: `${sanitizedPath}${field.name}`,
|
path: `${sanitizedPath}${field.name}`,
|
||||||
relationships,
|
relationships,
|
||||||
siblingData: groupData as Record<string, unknown>,
|
|
||||||
table,
|
table,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'number': {
|
case 'number': {
|
||||||
let val = fieldData;
|
let val = fieldData
|
||||||
// TODO: handle hasMany
|
// TODO: handle hasMany
|
||||||
if (typeof fieldData === 'string') {
|
if (typeof fieldData === 'string') {
|
||||||
val = Number.parseFloat(fieldData);
|
val = Number.parseFloat(fieldData)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof locale === 'string') {
|
if (typeof locale === 'string') {
|
||||||
ref[locale] = val;
|
ref[locale] = val
|
||||||
} else {
|
} else {
|
||||||
result[field.name] = val;
|
result[field.name] = val
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'date': {
|
case 'date': {
|
||||||
if (fieldData instanceof Date) {
|
if (fieldData instanceof Date) {
|
||||||
const val = fieldData.toISOString();
|
const val = fieldData.toISOString()
|
||||||
|
|
||||||
if (typeof locale === 'string') {
|
if (typeof locale === 'string') {
|
||||||
ref[locale] = val;
|
ref[locale] = val
|
||||||
} else {
|
} else {
|
||||||
result[field.name] = val;
|
result[field.name] = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
if (typeof locale === 'string') {
|
if (typeof locale === 'string') {
|
||||||
ref[locale] = fieldData;
|
ref[locale] = fieldData
|
||||||
} else {
|
} else {
|
||||||
result[field.name] = fieldData;
|
result[field.name] = fieldData
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
if (Object.keys(localizedFieldData).length > 0) {
|
if (Object.keys(localizedFieldData).length > 0) {
|
||||||
result[field.name] = localizedFieldData;
|
result[field.name] = localizedFieldData
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
return siblingData;
|
if (field.type === 'tabs') {
|
||||||
}, siblingData);
|
traverseFields({
|
||||||
|
blocks,
|
||||||
|
columnPrefix,
|
||||||
|
config,
|
||||||
|
dataRef,
|
||||||
|
fields: field.tabs.map((tab) => ({ ...tab, type: 'tab' })),
|
||||||
|
path: sanitizedPath,
|
||||||
|
relationships,
|
||||||
|
table,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return formatted as T;
|
if (field.type === 'collapsible' || field.type === 'row') {
|
||||||
};
|
traverseFields({
|
||||||
|
blocks,
|
||||||
|
columnPrefix,
|
||||||
|
config,
|
||||||
|
dataRef,
|
||||||
|
fields: field.fields,
|
||||||
|
path: sanitizedPath,
|
||||||
|
relationships,
|
||||||
|
table,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return dataRef
|
||||||
|
}, dataRef)
|
||||||
|
|
||||||
|
return formatted as T
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import type { Field } from 'payload/types';
|
import type { Field } from 'payload/types'
|
||||||
|
|
||||||
import { fieldAffectsData } from 'payload/types';
|
import { fieldAffectsData } from 'payload/types'
|
||||||
import toSnakeCase from 'to-snake-case';
|
import toSnakeCase from 'to-snake-case'
|
||||||
|
|
||||||
import type { ArrayRowToInsert, BlockRowToInsert } from './types';
|
import type { ArrayRowToInsert, BlockRowToInsert } from './types'
|
||||||
|
|
||||||
import { isArrayOfRows } from '../../utilities/isArrayOfRows';
|
import { isArrayOfRows } from '../../utilities/isArrayOfRows'
|
||||||
import { transformArray } from './array';
|
import { transformArray } from './array'
|
||||||
import { transformBlocks } from './blocks';
|
import { transformBlocks } from './blocks'
|
||||||
import { transformRelationship } from './relationships';
|
import { transformRelationship } from './relationships'
|
||||||
|
|
||||||
type Args = {
|
type Args = {
|
||||||
arrays: {
|
arrays: {
|
||||||
@@ -49,17 +49,17 @@ export const traverseFields = ({
|
|||||||
row,
|
row,
|
||||||
}: Args) => {
|
}: Args) => {
|
||||||
fields.forEach((field) => {
|
fields.forEach((field) => {
|
||||||
let columnName = '';
|
let columnName = ''
|
||||||
let fieldData: unknown;
|
let fieldData: unknown
|
||||||
|
|
||||||
if (fieldAffectsData(field)) {
|
if (fieldAffectsData(field)) {
|
||||||
columnName = `${columnPrefix || ''}${field.name}`;
|
columnName = `${columnPrefix || ''}${field.name}`
|
||||||
fieldData = data[field.name];
|
fieldData = data[field.name]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.type === 'array') {
|
if (field.type === 'array') {
|
||||||
const arrayTableName = `${newTableName}_${toSnakeCase(field.name)}`;
|
const arrayTableName = `${newTableName}_${toSnakeCase(field.name)}`
|
||||||
if (!arrays[arrayTableName]) arrays[arrayTableName] = [];
|
if (!arrays[arrayTableName]) arrays[arrayTableName] = []
|
||||||
|
|
||||||
if (field.localized) {
|
if (field.localized) {
|
||||||
if (typeof data[field.name] === 'object' && data[field.name] !== null) {
|
if (typeof data[field.name] === 'object' && data[field.name] !== null) {
|
||||||
@@ -74,11 +74,11 @@ export const traverseFields = ({
|
|||||||
locale: localeKey,
|
locale: localeKey,
|
||||||
path,
|
path,
|
||||||
relationships,
|
relationships,
|
||||||
});
|
})
|
||||||
|
|
||||||
arrays[arrayTableName] = arrays[arrayTableName].concat(newRows);
|
arrays[arrayTableName] = arrays[arrayTableName].concat(newRows)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const newRows = transformArray({
|
const newRows = transformArray({
|
||||||
@@ -89,12 +89,12 @@ export const traverseFields = ({
|
|||||||
field,
|
field,
|
||||||
path,
|
path,
|
||||||
relationships,
|
relationships,
|
||||||
});
|
})
|
||||||
|
|
||||||
arrays[arrayTableName] = arrays[arrayTableName].concat(newRows);
|
arrays[arrayTableName] = arrays[arrayTableName].concat(newRows)
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.type === 'blocks') {
|
if (field.type === 'blocks') {
|
||||||
@@ -110,9 +110,9 @@ export const traverseFields = ({
|
|||||||
path,
|
path,
|
||||||
relationships,
|
relationships,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
} else if (isArrayOfRows(fieldData)) {
|
} else if (isArrayOfRows(fieldData)) {
|
||||||
transformBlocks({
|
transformBlocks({
|
||||||
@@ -122,10 +122,10 @@ export const traverseFields = ({
|
|||||||
path,
|
path,
|
||||||
relationships,
|
relationships,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.type === 'group') {
|
if (field.type === 'group') {
|
||||||
@@ -146,8 +146,8 @@ export const traverseFields = ({
|
|||||||
path: `${path || ''}${field.name}.`,
|
path: `${path || ''}${field.name}.`,
|
||||||
relationships,
|
relationships,
|
||||||
row,
|
row,
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
traverseFields({
|
traverseFields({
|
||||||
arrays,
|
arrays,
|
||||||
@@ -162,15 +162,90 @@ export const traverseFields = ({
|
|||||||
path: `${path || ''}${field.name}.`,
|
path: `${path || ''}${field.name}.`,
|
||||||
relationships,
|
relationships,
|
||||||
row,
|
row,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (field.type === 'tabs') {
|
||||||
|
field.tabs.forEach((tab) => {
|
||||||
|
if ('name' in tab) {
|
||||||
|
if (typeof data[tab.name] === 'object' && data[tab.name] !== null) {
|
||||||
|
if (tab.localized) {
|
||||||
|
Object.entries(data[tab.name]).forEach(([localeKey, localeData]) => {
|
||||||
|
traverseFields({
|
||||||
|
arrays,
|
||||||
|
blocks,
|
||||||
|
columnPrefix: `${columnPrefix || ''}${tab.name}_`,
|
||||||
|
data: localeData as Record<string, unknown>,
|
||||||
|
existingLocales,
|
||||||
|
fields: tab.fields,
|
||||||
|
forcedLocale: localeKey,
|
||||||
|
locales,
|
||||||
|
newTableName: `${parentTableName}_${toSnakeCase(tab.name)}`,
|
||||||
|
parentTableName,
|
||||||
|
path: `${path || ''}${tab.name}.`,
|
||||||
|
relationships,
|
||||||
|
row,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
traverseFields({
|
||||||
|
arrays,
|
||||||
|
blocks,
|
||||||
|
columnPrefix: `${columnPrefix || ''}${tab.name}_`,
|
||||||
|
data: data[tab.name] as Record<string, unknown>,
|
||||||
|
existingLocales,
|
||||||
|
fields: tab.fields,
|
||||||
|
locales,
|
||||||
|
newTableName: `${parentTableName}_${toSnakeCase(tab.name)}`,
|
||||||
|
parentTableName,
|
||||||
|
path: `${path || ''}${tab.name}.`,
|
||||||
|
relationships,
|
||||||
|
row,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
traverseFields({
|
||||||
|
arrays,
|
||||||
|
blocks,
|
||||||
|
columnPrefix,
|
||||||
|
data,
|
||||||
|
existingLocales,
|
||||||
|
fields: tab.fields,
|
||||||
|
locales,
|
||||||
|
newTableName: parentTableName,
|
||||||
|
parentTableName,
|
||||||
|
path,
|
||||||
|
relationships,
|
||||||
|
row,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (field.type === 'row' || field.type === 'collapsible') {
|
||||||
|
traverseFields({
|
||||||
|
arrays,
|
||||||
|
blocks,
|
||||||
|
columnPrefix,
|
||||||
|
data,
|
||||||
|
existingLocales,
|
||||||
|
fields: field.fields,
|
||||||
|
locales,
|
||||||
|
newTableName: parentTableName,
|
||||||
|
parentTableName,
|
||||||
|
path,
|
||||||
|
relationships,
|
||||||
|
row,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (field.type === 'relationship') {
|
if (field.type === 'relationship') {
|
||||||
const relationshipPath = `${path || ''}${field.name}`;
|
const relationshipPath = `${path || ''}${field.name}`
|
||||||
|
|
||||||
if (field.localized) {
|
if (field.localized) {
|
||||||
if (typeof fieldData === 'object') {
|
if (typeof fieldData === 'object') {
|
||||||
@@ -183,8 +258,8 @@ export const traverseFields = ({
|
|||||||
data: localeData,
|
data: localeData,
|
||||||
field,
|
field,
|
||||||
relationships,
|
relationships,
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
transformRelationship({
|
transformRelationship({
|
||||||
@@ -194,134 +269,93 @@ export const traverseFields = ({
|
|||||||
data: fieldData,
|
data: fieldData,
|
||||||
field,
|
field,
|
||||||
relationships,
|
relationships,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fieldAffectsData(field)) {
|
if (fieldAffectsData(field)) {
|
||||||
const valuesToTransform: { localeKey?: string, ref: unknown, value: unknown }[] = [];
|
const valuesToTransform: { localeKey?: string; ref: unknown; value: unknown }[] = []
|
||||||
|
|
||||||
if ((field.localized)) {
|
if (field.localized) {
|
||||||
if (typeof fieldData === 'object' && fieldData !== null) {
|
if (typeof fieldData === 'object' && fieldData !== null) {
|
||||||
Object.entries(fieldData).forEach(([localeKey, localeData]) => {
|
Object.entries(fieldData).forEach(([localeKey, localeData]) => {
|
||||||
if (!locales[localeKey]) locales[localeKey] = {};
|
if (!locales[localeKey]) locales[localeKey] = {}
|
||||||
|
|
||||||
valuesToTransform.push({
|
valuesToTransform.push({
|
||||||
localeKey,
|
localeKey,
|
||||||
ref: locales,
|
ref: locales,
|
||||||
value: localeData,
|
value: localeData,
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let ref = row;
|
let ref = row
|
||||||
|
|
||||||
if (forcedLocale) {
|
if (forcedLocale) {
|
||||||
if (!locales[forcedLocale]) locales[forcedLocale] = {};
|
if (!locales[forcedLocale]) locales[forcedLocale] = {}
|
||||||
ref = locales[forcedLocale];
|
ref = locales[forcedLocale]
|
||||||
}
|
}
|
||||||
|
|
||||||
valuesToTransform.push({ ref, value: fieldData });
|
valuesToTransform.push({ ref, value: fieldData })
|
||||||
}
|
}
|
||||||
|
|
||||||
valuesToTransform.forEach(({ localeKey, ref, value }) => {
|
valuesToTransform.forEach(({ localeKey, ref, value }) => {
|
||||||
if (typeof value !== 'undefined') {
|
if (typeof value !== 'undefined') {
|
||||||
let formattedValue = value;
|
let formattedValue = value
|
||||||
|
|
||||||
switch (field.type) {
|
switch (field.type) {
|
||||||
case 'number': {
|
case 'number': {
|
||||||
// TODO: handle hasMany
|
// TODO: handle hasMany
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'select': {
|
case 'select': {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'date': {
|
case 'date': {
|
||||||
if (typeof fieldData === 'string') {
|
if (typeof fieldData === 'string') {
|
||||||
const parsedDate = new Date(fieldData);
|
const parsedDate = new Date(fieldData)
|
||||||
formattedValue = parsedDate;
|
formattedValue = parsedDate
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// case 'tabs': {
|
case 'row':
|
||||||
// await Promise.all(field.tabs.map(async (tab) => {
|
case 'collapsible': {
|
||||||
// if ('name' in tab) {
|
traverseFields({
|
||||||
// if (typeof data[tab.name] === 'object' && data[tab.name] !== null) {
|
adapter,
|
||||||
// await traverseFields({
|
arrayRowPromises,
|
||||||
// adapter,
|
blockRows,
|
||||||
// arrayRowPromises,
|
columnPrefix,
|
||||||
// blockRows,
|
data,
|
||||||
// columnPrefix: `${columnName}_`,
|
fields: field.fields,
|
||||||
// data: data[tab.name] as Record<string, unknown>,
|
locale,
|
||||||
// fields: tab.fields,
|
localeRow,
|
||||||
// locale,
|
operation,
|
||||||
// localeRow,
|
path,
|
||||||
// operation,
|
relationshipRows,
|
||||||
// path: `${path || ''}${tab.name}.`,
|
row,
|
||||||
// relationshipRows,
|
tableName,
|
||||||
// row,
|
})
|
||||||
// tableName,
|
break
|
||||||
// });
|
}
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// await traverseFields({
|
|
||||||
// adapter,
|
|
||||||
// arrayRowPromises,
|
|
||||||
// blockRows,
|
|
||||||
// columnPrefix,
|
|
||||||
// data,
|
|
||||||
// fields: tab.fields,
|
|
||||||
// locale,
|
|
||||||
// localeRow,
|
|
||||||
// operation,
|
|
||||||
// path,
|
|
||||||
// relationshipRows,
|
|
||||||
// row,
|
|
||||||
// tableName,
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }));
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// case 'row':
|
|
||||||
// case 'collapsible': {
|
|
||||||
// await traverseFields({
|
|
||||||
// adapter,
|
|
||||||
// arrayRowPromises,
|
|
||||||
// blockRows,
|
|
||||||
// columnPrefix,
|
|
||||||
// data,
|
|
||||||
// fields: field.fields,
|
|
||||||
// locale,
|
|
||||||
// localeRow,
|
|
||||||
// operation,
|
|
||||||
// path,
|
|
||||||
// relationshipRows,
|
|
||||||
// row,
|
|
||||||
// tableName,
|
|
||||||
// });
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (localeKey) {
|
if (localeKey) {
|
||||||
ref[localeKey][columnName] = formattedValue;
|
ref[localeKey][columnName] = formattedValue
|
||||||
} else {
|
} else {
|
||||||
ref[columnName] = formattedValue;
|
ref[columnName] = formattedValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
|
|||||||
@@ -1,42 +1,39 @@
|
|||||||
import type { UpdateOne } from 'payload/database';
|
import type { UpdateOne } from 'payload/database'
|
||||||
|
|
||||||
import toSnakeCase from 'to-snake-case';
|
import toSnakeCase from 'to-snake-case'
|
||||||
|
|
||||||
import buildQuery from '../queries/buildQuery';
|
import type { PostgresAdapter } from '../types'
|
||||||
import { upsertRow } from '../upsertRow';
|
|
||||||
|
|
||||||
export const updateOne: UpdateOne = async function updateOne({
|
import buildQuery from '../queries/buildQuery'
|
||||||
collection: collectionSlug,
|
import { upsertRow } from '../upsertRow'
|
||||||
data,
|
|
||||||
draft,
|
export const updateOne: UpdateOne = async function updateOne(
|
||||||
id,
|
this: PostgresAdapter,
|
||||||
locale,
|
{ id, collection: collectionSlug, data, draft, locale, req, where: whereArg },
|
||||||
req,
|
) {
|
||||||
where: whereArg,
|
const db = this.sessions?.[req.transactionID] || this.db
|
||||||
}) {
|
const collection = this.payload.collections[collectionSlug].config
|
||||||
const db = req.transactionID ? this.sessions[req.transactionID] : this.db;
|
const tableName = toSnakeCase(collectionSlug)
|
||||||
const collection = this.payload.collections[collectionSlug].config;
|
const whereToUse = whereArg || { id: { equals: id } }
|
||||||
const tableName = toSnakeCase(collection);
|
|
||||||
const whereToUse = whereArg || { id: { equals: id } };
|
|
||||||
|
|
||||||
const { where } = await buildQuery({
|
const { where } = await buildQuery({
|
||||||
adapter: this,
|
adapter: this,
|
||||||
fields: collection.fields,
|
fields: collection.fields,
|
||||||
locale,
|
locale,
|
||||||
tableName,
|
tableName,
|
||||||
where: whereToUse
|
where: whereToUse,
|
||||||
});
|
})
|
||||||
|
|
||||||
const result = await upsertRow({
|
const result = await upsertRow({
|
||||||
|
id,
|
||||||
adapter: this,
|
adapter: this,
|
||||||
data,
|
data,
|
||||||
db,
|
db,
|
||||||
fields: collection.fields,
|
fields: collection.fields,
|
||||||
id,
|
|
||||||
operation: 'update',
|
operation: 'update',
|
||||||
tableName: toSnakeCase(collectionSlug),
|
tableName: toSnakeCase(collectionSlug),
|
||||||
where,
|
where,
|
||||||
});
|
})
|
||||||
|
|
||||||
return result;
|
return result
|
||||||
};
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ function initCollectionsGraphQL(payload: Payload): void {
|
|||||||
baseFields.id = { type: idType }
|
baseFields.id = { type: idType }
|
||||||
whereInputFields.push({
|
whereInputFields.push({
|
||||||
name: 'id',
|
name: 'id',
|
||||||
type: 'text',
|
type: payload.db.defaultIDType as 'text',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { mongooseAdapter } from '../packages/db-mongodb/src/index'
|
|||||||
import { postgresAdapter } from '../packages/db-postgres/src/index'
|
import { postgresAdapter } from '../packages/db-postgres/src/index'
|
||||||
import { buildConfig as buildPayloadConfig } from '../packages/payload/src/config/build'
|
import { buildConfig as buildPayloadConfig } from '../packages/payload/src/config/build'
|
||||||
|
|
||||||
process.env.PAYLOAD_DATABASE = 'postgres'
|
// process.env.PAYLOAD_DATABASE = 'postgres'
|
||||||
|
|
||||||
const databaseAdapters = {
|
const databaseAdapters = {
|
||||||
mongoose: mongooseAdapter({
|
mongoose: mongooseAdapter({
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ export default buildConfigWithDefaults({
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
onInit: async (payload) => {
|
onInit: async (payload) => {
|
||||||
await payload.create({
|
const user = await payload.create({
|
||||||
collection: 'users',
|
collection: 'users',
|
||||||
data: {
|
data: {
|
||||||
email: devUser.email,
|
email: devUser.email,
|
||||||
|
|||||||
@@ -38,9 +38,12 @@ describe('collections-graphql', () => {
|
|||||||
|
|
||||||
describe('CRUD', () => {
|
describe('CRUD', () => {
|
||||||
let existingDoc: Post
|
let existingDoc: Post
|
||||||
|
let existingDocGraphQLID
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
existingDoc = await createPost()
|
existingDoc = await createPost()
|
||||||
|
existingDocGraphQLID =
|
||||||
|
payload.db.defaultIDType === 'number' ? existingDoc.id : `"${existingDoc.id}"`
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should create', async () => {
|
it('should create', async () => {
|
||||||
@@ -54,7 +57,7 @@ describe('collections-graphql', () => {
|
|||||||
const doc: Post = response.createPost
|
const doc: Post = response.createPost
|
||||||
|
|
||||||
expect(doc).toMatchObject({ title })
|
expect(doc).toMatchObject({ title })
|
||||||
expect(doc.id.length).toBeGreaterThan(0)
|
expect(doc.id).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should create using graphql variables', async () => {
|
it('should create using graphql variables', async () => {
|
||||||
@@ -68,12 +71,12 @@ describe('collections-graphql', () => {
|
|||||||
const doc: Post = response.createPost
|
const doc: Post = response.createPost
|
||||||
|
|
||||||
expect(doc).toMatchObject({ title })
|
expect(doc).toMatchObject({ title })
|
||||||
expect(doc.id.length).toBeGreaterThan(0)
|
expect(doc.id).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should read', async () => {
|
it('should read', async () => {
|
||||||
const query = `query {
|
const query = `query {
|
||||||
Post(id: "${existingDoc.id}") {
|
Post(id: ${existingDocGraphQLID}) {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
}
|
}
|
||||||
@@ -123,7 +126,7 @@ describe('collections-graphql', () => {
|
|||||||
const updatedTitle = 'updated title'
|
const updatedTitle = 'updated title'
|
||||||
|
|
||||||
const query = `mutation {
|
const query = `mutation {
|
||||||
updatePost(id: "${existingDoc.id}", data: { title: "${updatedTitle}"}) {
|
updatePost(id: ${existingDocGraphQLID}, data: { title: "${updatedTitle}"}) {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
}
|
}
|
||||||
@@ -136,7 +139,7 @@ describe('collections-graphql', () => {
|
|||||||
|
|
||||||
it('should delete', async () => {
|
it('should delete', async () => {
|
||||||
const query = `mutation {
|
const query = `mutation {
|
||||||
deletePost(id: "${existingDoc.id}") {
|
deletePost(id: ${existingDocGraphQLID}) {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user