chore: renames insertRow to upsertRow

This commit is contained in:
James
2023-08-08 16:02:42 -04:00
parent e25b77b411
commit 353e851b29
7 changed files with 125 additions and 23 deletions

View File

@@ -1,6 +1,6 @@
import { Create } from 'payload/dist/database/types';
import toSnakeCase from 'to-snake-case';
import { insertRow } from './insertRow';
import { upsertRow } from '../upsertRow';
export const create: Create = async function create({
collection: collectionSlug,
@@ -9,7 +9,7 @@ export const create: Create = async function create({
}) {
const collection = this.payload.collections[collectionSlug].config;
const result = await insertRow({
const result = await upsertRow({
adapter: this,
data,
fallbackLocale: req.fallbackLocale,

View File

@@ -1,23 +1,35 @@
import { UpdateOne } from 'payload/dist/database/types';
import toSnakeCase from 'to-snake-case';
// import { insertRow } from './insertRow';
import { upsertRow } from '../upsertRow';
import buildQuery from '../queries/buildQuery';
export const updateOne: UpdateOne = async function updateOne({
collection: collectionSlug,
data,
req,
where,
draft,
locale,
}) {
// const collection = this.payload.collections[collectionSlug].config;
const collection = this.payload.collections[collectionSlug].config;
// const result = await insertRow({
// adapter: this,
// data,
// fallbackLocale: req.fallbackLocale,
// fields: collection.fields,
// locale: req.locale,
// operation: 'update',
// tableName: toSnakeCase(collectionSlug),
// });
const query = await buildQuery({
adapter: this,
collectionSlug,
locale,
where,
});
// return result;
const result = await upsertRow({
adapter: this,
data,
fallbackLocale: req.fallbackLocale,
fields: collection.fields,
locale: req.locale,
operation: 'update',
query,
tableName: toSnakeCase(collectionSlug),
});
return result;
};

View File

@@ -1,6 +1,6 @@
/* eslint-disable no-param-reassign */
import { Field } from 'payload/types';
import { eq } from 'drizzle-orm';
import { SQL } from 'drizzle-orm';
import { PostgresAdapter } from '../types';
import { traverseFields } from './traverseFields';
import { transform } from '../transform';
@@ -9,25 +9,25 @@ import { insertArrays } from './insertArrays';
type Args = {
adapter: PostgresAdapter
data: Record<string, unknown>
fallbackLocale?: string | false
fields: Field[]
id?: string | number
locale: string
operation: 'create' | 'update'
path?: string
data: Record<string, unknown>
query?: SQL<unknown>
tableName: string
}
export const insertRow = async ({
export const upsertRow = async ({
adapter,
data,
fallbackLocale,
fields,
id,
locale,
operation,
path = '',
data,
query,
tableName,
}: Args): Promise<Record<string, unknown>> => {
// Split out the incoming data into the corresponding:
@@ -66,7 +66,7 @@ export const insertRow = async ({
.values(rowToInsert.row).returning();
} else {
[insertedRow] = await adapter.db.update(adapter.tables[tableName])
.set(rowToInsert.row).where(eq(adapter.tables[tableName].id, id)).returning();
.set(rowToInsert.row).where(query).returning();
}
let localeToInsert: Record<string, unknown>;
@@ -147,7 +147,7 @@ export const insertRow = async ({
const insertedBlockRows: Record<string, Record<string, unknown>[]> = {};
Object.entries(blocksToInsert).forEach(([blockName, blockRows]) => {
// For each block, push insert into promises to run parallen
// For each block, push insert into promises to run parallel
promises.push(async () => {
insertedBlockRows[blockName] = await adapter.db.insert(adapter.tables[`${tableName}_${blockName}`])
.values(blockRows.map(({ row }) => row)).returning();

View File

@@ -212,7 +212,14 @@ const config = buildConfigWithDefaults({
},
});
await payload.create({
const person2 = await payload.create({
collection: 'people',
data: {
fullName: 'Elliot DeNolf',
},
});
const post = await payload.create({
collection: 'posts',
data: {
title: 'hello',
@@ -294,6 +301,89 @@ const config = buildConfigWithDefaults({
],
},
});
await payload.update({
collection: 'posts',
id: post.id,
data: {
title: 'hello 2',
number: 1338,
myGroup: {
subFieldLocalized: 'hello in english updated',
subGroup: {
subSubField: 'sub hello updated',
subSubFieldLocalized: 'sub hello in english updated',
},
groupArray: [
{
groupArrayText: 'hello 1 updated',
},
{
groupArrayText: 'hello 2 updated',
},
],
},
relationHasOne: page2.id,
relationHasOnePoly: {
relationTo: 'people',
value: person2.id,
},
relationHasMany: [page2.id, page1.id],
relationHasManyPoly: [
{
relationTo: 'pages',
value: page2.id,
},
{
relationTo: 'people',
value: person1.id,
},
],
myArray: [
{
subField: 'hello 1 updated',
mySubArray: [
{
subSubField: 'row 1 subrow 1 updated',
},
{
subSubField: 'row 1 subrow 2 updated',
},
],
},
{
subField: 'hello 2 updated',
mySubArray: [
{
subSubField: 'row 2 subrow 1 updated',
},
{
subSubField: 'row 2 subrow 2 updated',
},
],
},
],
myBlocks: [
{
blockType: 'block1',
nonLocalizedText: 'hello updated',
localizedText: 'hello in english updated',
},
{
blockType: 'block2',
number: 1234,
blockArray: [
{
subBlockArray: 'row 1 updated',
},
{
subBlockArray: 'row 2 updated',
},
],
},
],
},
});
},
});