chore: adds todo for update
This commit is contained in:
@@ -1,5 +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 { PostgresAdapter } from '../types';
|
import { PostgresAdapter } from '../types';
|
||||||
import { traverseFields } from './traverseFields';
|
import { traverseFields } from './traverseFields';
|
||||||
import { transform } from '../transform';
|
import { transform } from '../transform';
|
||||||
@@ -10,6 +11,7 @@ type Args = {
|
|||||||
adapter: PostgresAdapter
|
adapter: PostgresAdapter
|
||||||
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
|
||||||
@@ -21,6 +23,7 @@ export const insertRow = async ({
|
|||||||
adapter,
|
adapter,
|
||||||
fallbackLocale,
|
fallbackLocale,
|
||||||
fields,
|
fields,
|
||||||
|
id,
|
||||||
locale,
|
locale,
|
||||||
operation,
|
operation,
|
||||||
path = '',
|
path = '',
|
||||||
@@ -55,9 +58,16 @@ export const insertRow = async ({
|
|||||||
row: rowToInsert.row,
|
row: rowToInsert.row,
|
||||||
});
|
});
|
||||||
|
|
||||||
// First, we insert the main row
|
let insertedRow: Record<string, unknown>;
|
||||||
const [insertedRow] = await adapter.db.insert(adapter.tables[tableName])
|
|
||||||
.values(rowToInsert.row).returning();
|
// 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>;
|
let localeToInsert: Record<string, unknown>;
|
||||||
const relationsToInsert: 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,
|
if (operation === 'update') {
|
||||||
// filling parent ID accordingly
|
// TODO: delete existing data for paths that are updated
|
||||||
await insertArrays({
|
// 1. Arrays, and all sub-arrays from that point on including all locales
|
||||||
adapter,
|
// 2. Blocks by path including locales, and sub arrays
|
||||||
arrays: [rowToInsert.arrays],
|
// 3. Relationships by path
|
||||||
parentRows: [insertedRow],
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// //////////////////////////////////
|
// //////////////////////////////////
|
||||||
// INSERT LOCALES
|
// INSERT LOCALES
|
||||||
@@ -108,8 +117,13 @@ export const insertRow = async ({
|
|||||||
|
|
||||||
if (localeToInsert) {
|
if (localeToInsert) {
|
||||||
promises.push(async () => {
|
promises.push(async () => {
|
||||||
[insertedLocaleRow] = await adapter.db.insert(adapter.tables[`${tableName}_locales`])
|
if (operation === 'create') {
|
||||||
.values(localeToInsert).returning();
|
[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()));
|
await Promise.all(promises.map((promise) => promise()));
|
||||||
|
|
||||||
// //////////////////////////////////
|
// //////////////////////////////////
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { create } from './create';
|
|||||||
// import { findGlobal } from './findGlobal';
|
// import { findGlobal } from './findGlobal';
|
||||||
// import { findOne } from './findOne';
|
// import { findOne } from './findOne';
|
||||||
// import { updateGlobal } from './updateGlobal';
|
// import { updateGlobal } from './updateGlobal';
|
||||||
// import { updateOne } from './updateOne';
|
import { updateOne } from './update';
|
||||||
// import { updateVersion } from './updateVersion';
|
// import { updateVersion } from './updateVersion';
|
||||||
// import { deleteMany } from './deleteMany';
|
// import { deleteMany } from './deleteMany';
|
||||||
// import { destroy } from './destroy';
|
// import { destroy } from './destroy';
|
||||||
@@ -48,7 +48,7 @@ export function postgresAdapter(args: Args): PostgresAdapterResult {
|
|||||||
// findOne,
|
// findOne,
|
||||||
find,
|
find,
|
||||||
create,
|
create,
|
||||||
// updateOne,
|
updateOne,
|
||||||
// deleteOne,
|
// deleteOne,
|
||||||
// deleteMany,
|
// deleteMany,
|
||||||
// findGlobal,
|
// 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