chore: localized groups
This commit is contained in:
64
packages/db-postgres/src/transform/write/blocks.ts
Normal file
64
packages/db-postgres/src/transform/write/blocks.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { BlockField } from 'payload/types';
|
||||
import toSnakeCase from 'to-snake-case';
|
||||
import { BlockRowToInsert } from './types';
|
||||
import { traverseFields } from './traverseFields';
|
||||
|
||||
type Args = {
|
||||
blocks: {
|
||||
[blockType: string]: BlockRowToInsert[]
|
||||
}
|
||||
data: Record<string, unknown>[]
|
||||
field: BlockField
|
||||
locale: string
|
||||
path: string
|
||||
relationships: Record<string, unknown>[]
|
||||
tableName
|
||||
}
|
||||
export const transformBlocks = ({
|
||||
blocks,
|
||||
data,
|
||||
field,
|
||||
locale,
|
||||
path,
|
||||
relationships,
|
||||
tableName,
|
||||
}: Args) => {
|
||||
data.forEach((blockRow, i) => {
|
||||
if (typeof blockRow.blockType !== 'string') return;
|
||||
const matchedBlock = field.blocks.find(({ slug }) => slug === blockRow.blockType);
|
||||
if (!matchedBlock) return;
|
||||
|
||||
if (!blocks[blockRow.blockType]) blocks[blockRow.blockType] = [];
|
||||
|
||||
const newRow: BlockRowToInsert = {
|
||||
arrays: {},
|
||||
row: {
|
||||
_order: i + 1,
|
||||
_path: `${path}${field.name}`,
|
||||
},
|
||||
locales: {},
|
||||
};
|
||||
|
||||
if (field.localized) newRow.row._locale = locale;
|
||||
|
||||
const blockTableName = `${tableName}_${toSnakeCase(blockRow.blockType)}`;
|
||||
|
||||
traverseFields({
|
||||
arrays: newRow.arrays,
|
||||
blocks,
|
||||
columnPrefix: '',
|
||||
data: blockRow,
|
||||
fields: matchedBlock.fields,
|
||||
locale,
|
||||
locales: newRow.locales,
|
||||
newTableName: blockTableName,
|
||||
parentTableName: blockTableName,
|
||||
path: `${path || ''}${field.name}.${i}.`,
|
||||
relationships,
|
||||
row: newRow.row,
|
||||
});
|
||||
|
||||
blocks[blockRow.blockType].push(newRow);
|
||||
});
|
||||
};
|
||||
@@ -5,6 +5,7 @@ import { fieldAffectsData, valueIsValueWithRelation } from 'payload/dist/fields/
|
||||
import { ArrayRowToInsert, BlockRowToInsert } from './types';
|
||||
import { isArrayOfRows } from '../../utilities/isArrayOfRows';
|
||||
import { transformArray } from './array';
|
||||
import { transformBlocks } from './blocks';
|
||||
|
||||
type Args = {
|
||||
arrays: {
|
||||
@@ -98,46 +99,29 @@ export const traverseFields = ({
|
||||
if (field.type === 'blocks') {
|
||||
if (field.localized) {
|
||||
if (typeof data[field.name] === 'object' && data[field.name] !== null) {
|
||||
// loop over each locale
|
||||
console.log(data[field.name]);
|
||||
Object.entries(data[field.name]).forEach(([localeKey, localeData]) => {
|
||||
if (Array.isArray(localeData)) {
|
||||
transformBlocks({
|
||||
blocks,
|
||||
data: localeData,
|
||||
field,
|
||||
locale: localeKey,
|
||||
path,
|
||||
relationships,
|
||||
tableName: newTableName,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (isArrayOfRows(fieldData)) {
|
||||
fieldData.forEach((blockRow, i) => {
|
||||
if (typeof blockRow.blockType !== 'string') return;
|
||||
const matchedBlock = field.blocks.find(({ slug }) => slug === blockRow.blockType);
|
||||
if (!matchedBlock) return;
|
||||
|
||||
if (!blocks[blockRow.blockType]) blocks[blockRow.blockType] = [];
|
||||
|
||||
const newRow: BlockRowToInsert = {
|
||||
arrays: {},
|
||||
row: {
|
||||
_order: i + 1,
|
||||
_path: `${path}${field.name}`,
|
||||
},
|
||||
locales: {},
|
||||
};
|
||||
|
||||
if (field.localized) newRow.row._locale = locale;
|
||||
|
||||
const blockTableName = `${newTableName}_${toSnakeCase(blockRow.blockType)}`;
|
||||
|
||||
traverseFields({
|
||||
arrays: newRow.arrays,
|
||||
blocks,
|
||||
columnPrefix: '',
|
||||
data: blockRow,
|
||||
fields: matchedBlock.fields,
|
||||
locale,
|
||||
locales: newRow.locales,
|
||||
newTableName: blockTableName,
|
||||
parentTableName: blockTableName,
|
||||
path: `${path || ''}${field.name}.${i}.`,
|
||||
relationships,
|
||||
row: newRow.row,
|
||||
});
|
||||
|
||||
blocks[blockRow.blockType].push(newRow);
|
||||
transformBlocks({
|
||||
blocks,
|
||||
data: fieldData,
|
||||
field,
|
||||
locale,
|
||||
path,
|
||||
relationships,
|
||||
tableName: newTableName,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -209,7 +193,7 @@ export const traverseFields = ({
|
||||
if (fieldAffectsData(field)) {
|
||||
const valuesToTransform: { localeKey?: string, ref: unknown, value: unknown }[] = [];
|
||||
|
||||
if ((field.localized || forceLocalized)) {
|
||||
if ((field.localized)) {
|
||||
if (typeof fieldData === 'object' && fieldData !== null) {
|
||||
Object.entries(fieldData).forEach(([localeKey, localeData]) => {
|
||||
if (!locales[localeKey]) locales[localeKey] = {};
|
||||
@@ -222,7 +206,14 @@ export const traverseFields = ({
|
||||
});
|
||||
}
|
||||
} else {
|
||||
valuesToTransform.push({ value: fieldData, ref: row });
|
||||
let ref = row;
|
||||
|
||||
if (forceLocalized) {
|
||||
if (!locales[locale]) locales[locale] = {};
|
||||
ref = locales[locale];
|
||||
}
|
||||
|
||||
valuesToTransform.push({ value: fieldData, ref });
|
||||
}
|
||||
|
||||
valuesToTransform.forEach(({ localeKey, ref, value }) => {
|
||||
|
||||
@@ -41,7 +41,6 @@ export const deleteExistingRowsByPath = async ({
|
||||
if (localizedPathsToDelete.size > 0 && locale) {
|
||||
const whereConstraints = [
|
||||
eq(table[parentColumnName], parentID),
|
||||
eq(table[localeColumnName], locale),
|
||||
];
|
||||
|
||||
if (pathColumnName) whereConstraints.push(inArray(table[pathColumnName], Array.from(localizedPathsToDelete)));
|
||||
@@ -62,6 +61,6 @@ export const deleteExistingRowsByPath = async ({
|
||||
await adapter.db.delete(table)
|
||||
.where(
|
||||
and(...whereConstraints),
|
||||
).returning();
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user