chore: adds todo for update
This commit is contained in:
@@ -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<string, unknown>;
|
||||
|
||||
// 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<string, unknown>;
|
||||
const relationsToInsert: Record<string, unknown>[] = [];
|
||||
@@ -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()));
|
||||
|
||||
// //////////////////////////////////
|
||||
|
||||
@@ -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,
|
||||
|
||||
23
packages/db-postgres/src/update/index.ts
Normal file
23
packages/db-postgres/src/update/index.ts
Normal file
@@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user