From e25b77b411cee43d494406de5244fe17433f8dba Mon Sep 17 00:00:00 2001 From: James Date: Tue, 8 Aug 2023 15:35:35 -0400 Subject: [PATCH] chore: adds todo for update --- packages/db-postgres/src/create/insertRow.ts | 50 +++++++++++++++----- packages/db-postgres/src/index.ts | 4 +- packages/db-postgres/src/update/index.ts | 23 +++++++++ 3 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 packages/db-postgres/src/update/index.ts diff --git a/packages/db-postgres/src/create/insertRow.ts b/packages/db-postgres/src/create/insertRow.ts index 39eeb61131..5c212293b1 100644 --- a/packages/db-postgres/src/create/insertRow.ts +++ b/packages/db-postgres/src/create/insertRow.ts @@ -1,5 +1,6 @@ /* eslint-disable no-param-reassign */ import { Field } from 'payload/types'; +import { eq } from 'drizzle-orm'; import { PostgresAdapter } from '../types'; import { traverseFields } from './traverseFields'; import { transform } from '../transform'; @@ -10,6 +11,7 @@ type Args = { adapter: PostgresAdapter fallbackLocale?: string | false fields: Field[] + id?: string | number locale: string operation: 'create' | 'update' path?: string @@ -21,6 +23,7 @@ export const insertRow = async ({ adapter, fallbackLocale, fields, + id, locale, operation, path = '', @@ -55,9 +58,16 @@ export const insertRow = async ({ row: rowToInsert.row, }); - // First, we insert the main row - const [insertedRow] = await adapter.db.insert(adapter.tables[tableName]) - .values(rowToInsert.row).returning(); + let insertedRow: Record; + + // First, we insert / update the main row + if (operation === 'create') { + [insertedRow] = await adapter.db.insert(adapter.tables[tableName]) + .values(rowToInsert.row).returning(); + } else { + [insertedRow] = await adapter.db.update(adapter.tables[tableName]) + .set(rowToInsert.row).where(eq(adapter.tables[tableName].id, id)).returning(); + } let localeToInsert: Record; const relationsToInsert: Record[] = []; @@ -92,13 +102,12 @@ export const insertRow = async ({ }); }); - // Recursively insert arrays w/ their locales, one level at a time, - // filling parent ID accordingly - await insertArrays({ - adapter, - arrays: [rowToInsert.arrays], - parentRows: [insertedRow], - }); + if (operation === 'update') { + // TODO: delete existing data for paths that are updated + // 1. Arrays, and all sub-arrays from that point on including all locales + // 2. Blocks by path including locales, and sub arrays + // 3. Relationships by path + } // ////////////////////////////////// // INSERT LOCALES @@ -108,8 +117,13 @@ export const insertRow = async ({ if (localeToInsert) { promises.push(async () => { - [insertedLocaleRow] = await adapter.db.insert(adapter.tables[`${tableName}_locales`]) - .values(localeToInsert).returning(); + if (operation === 'create') { + [insertedLocaleRow] = await adapter.db.insert(adapter.tables[`${tableName}_locales`]) + .values(localeToInsert).returning(); + } else { + // TODO: QUERY for existing locale by parent ID, to get its ID + // and upsert if it doesn't exist already + } }); } @@ -175,6 +189,18 @@ export const insertRow = async ({ }); }); + // ////////////////////////////////// + // INSERT ARRAYS RECURSIVELY + // ////////////////////////////////// + + promises.push(async () => { + await insertArrays({ + adapter, + arrays: [rowToInsert.arrays], + parentRows: [insertedRow], + }); + }); + await Promise.all(promises.map((promise) => promise())); // ////////////////////////////////// diff --git a/packages/db-postgres/src/index.ts b/packages/db-postgres/src/index.ts index d2c5cd817c..b9d07d4f27 100644 --- a/packages/db-postgres/src/index.ts +++ b/packages/db-postgres/src/index.ts @@ -20,7 +20,7 @@ import { create } from './create'; // import { findGlobal } from './findGlobal'; // import { findOne } from './findOne'; // import { updateGlobal } from './updateGlobal'; -// import { updateOne } from './updateOne'; +import { updateOne } from './update'; // import { updateVersion } from './updateVersion'; // import { deleteMany } from './deleteMany'; // import { destroy } from './destroy'; @@ -48,7 +48,7 @@ export function postgresAdapter(args: Args): PostgresAdapterResult { // findOne, find, create, - // updateOne, + updateOne, // deleteOne, // deleteMany, // findGlobal, diff --git a/packages/db-postgres/src/update/index.ts b/packages/db-postgres/src/update/index.ts new file mode 100644 index 0000000000..f1daf45eee --- /dev/null +++ b/packages/db-postgres/src/update/index.ts @@ -0,0 +1,23 @@ +import { UpdateOne } from 'payload/dist/database/types'; +import toSnakeCase from 'to-snake-case'; +// import { insertRow } from './insertRow'; + +export const updateOne: UpdateOne = async function updateOne({ + collection: collectionSlug, + data, + req, +}) { + // 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), + // }); + + // return result; +};