chore: renames insertRow to upsertRow
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { Create } from 'payload/dist/database/types';
|
import { Create } from 'payload/dist/database/types';
|
||||||
import toSnakeCase from 'to-snake-case';
|
import toSnakeCase from 'to-snake-case';
|
||||||
import { insertRow } from './insertRow';
|
import { upsertRow } from '../upsertRow';
|
||||||
|
|
||||||
export const create: Create = async function create({
|
export const create: Create = async function create({
|
||||||
collection: collectionSlug,
|
collection: collectionSlug,
|
||||||
@@ -9,7 +9,7 @@ export const create: Create = async function create({
|
|||||||
}) {
|
}) {
|
||||||
const collection = this.payload.collections[collectionSlug].config;
|
const collection = this.payload.collections[collectionSlug].config;
|
||||||
|
|
||||||
const result = await insertRow({
|
const result = await upsertRow({
|
||||||
adapter: this,
|
adapter: this,
|
||||||
data,
|
data,
|
||||||
fallbackLocale: req.fallbackLocale,
|
fallbackLocale: req.fallbackLocale,
|
||||||
|
|||||||
@@ -1,23 +1,35 @@
|
|||||||
import { UpdateOne } from 'payload/dist/database/types';
|
import { UpdateOne } from 'payload/dist/database/types';
|
||||||
import toSnakeCase from 'to-snake-case';
|
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({
|
export const updateOne: UpdateOne = async function updateOne({
|
||||||
collection: collectionSlug,
|
collection: collectionSlug,
|
||||||
data,
|
data,
|
||||||
req,
|
req,
|
||||||
|
where,
|
||||||
|
draft,
|
||||||
|
locale,
|
||||||
}) {
|
}) {
|
||||||
// const collection = this.payload.collections[collectionSlug].config;
|
const collection = this.payload.collections[collectionSlug].config;
|
||||||
|
|
||||||
// const result = await insertRow({
|
const query = await buildQuery({
|
||||||
// adapter: this,
|
adapter: this,
|
||||||
// data,
|
collectionSlug,
|
||||||
// fallbackLocale: req.fallbackLocale,
|
locale,
|
||||||
// fields: collection.fields,
|
where,
|
||||||
// locale: req.locale,
|
});
|
||||||
// operation: 'update',
|
|
||||||
// tableName: toSnakeCase(collectionSlug),
|
|
||||||
// });
|
|
||||||
|
|
||||||
// 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;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import { Field } from 'payload/types';
|
import { Field } from 'payload/types';
|
||||||
import { eq } from 'drizzle-orm';
|
import { SQL } from 'drizzle-orm';
|
||||||
import { PostgresAdapter } from '../types';
|
import { PostgresAdapter } from '../types';
|
||||||
import { traverseFields } from './traverseFields';
|
import { traverseFields } from './traverseFields';
|
||||||
import { transform } from '../transform';
|
import { transform } from '../transform';
|
||||||
@@ -9,25 +9,25 @@ import { insertArrays } from './insertArrays';
|
|||||||
|
|
||||||
type Args = {
|
type Args = {
|
||||||
adapter: PostgresAdapter
|
adapter: PostgresAdapter
|
||||||
|
data: Record<string, unknown>
|
||||||
fallbackLocale?: string | false
|
fallbackLocale?: string | false
|
||||||
fields: Field[]
|
fields: Field[]
|
||||||
id?: string | number
|
|
||||||
locale: string
|
locale: string
|
||||||
operation: 'create' | 'update'
|
operation: 'create' | 'update'
|
||||||
path?: string
|
path?: string
|
||||||
data: Record<string, unknown>
|
query?: SQL<unknown>
|
||||||
tableName: string
|
tableName: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const insertRow = async ({
|
export const upsertRow = async ({
|
||||||
adapter,
|
adapter,
|
||||||
|
data,
|
||||||
fallbackLocale,
|
fallbackLocale,
|
||||||
fields,
|
fields,
|
||||||
id,
|
|
||||||
locale,
|
locale,
|
||||||
operation,
|
operation,
|
||||||
path = '',
|
path = '',
|
||||||
data,
|
query,
|
||||||
tableName,
|
tableName,
|
||||||
}: Args): Promise<Record<string, unknown>> => {
|
}: Args): Promise<Record<string, unknown>> => {
|
||||||
// Split out the incoming data into the corresponding:
|
// Split out the incoming data into the corresponding:
|
||||||
@@ -66,7 +66,7 @@ export const insertRow = async ({
|
|||||||
.values(rowToInsert.row).returning();
|
.values(rowToInsert.row).returning();
|
||||||
} else {
|
} else {
|
||||||
[insertedRow] = await adapter.db.update(adapter.tables[tableName])
|
[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>;
|
let localeToInsert: Record<string, unknown>;
|
||||||
@@ -147,7 +147,7 @@ export const insertRow = async ({
|
|||||||
const insertedBlockRows: Record<string, Record<string, unknown>[]> = {};
|
const insertedBlockRows: Record<string, Record<string, unknown>[]> = {};
|
||||||
|
|
||||||
Object.entries(blocksToInsert).forEach(([blockName, blockRows]) => {
|
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 () => {
|
promises.push(async () => {
|
||||||
insertedBlockRows[blockName] = await adapter.db.insert(adapter.tables[`${tableName}_${blockName}`])
|
insertedBlockRows[blockName] = await adapter.db.insert(adapter.tables[`${tableName}_${blockName}`])
|
||||||
.values(blockRows.map(({ row }) => row)).returning();
|
.values(blockRows.map(({ row }) => row)).returning();
|
||||||
@@ -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',
|
collection: 'posts',
|
||||||
data: {
|
data: {
|
||||||
title: 'hello',
|
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',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user