feat: ensures you can query on blocks via specifying locale or not specifying locale

This commit is contained in:
James
2022-08-15 17:52:08 -07:00
parent b1a1575122
commit 078e8dcc51
6 changed files with 84 additions and 33 deletions

View File

@@ -142,7 +142,7 @@ class ParamParser {
},
];
pathSegments.forEach((segment, i) => {
pathSegments.every((segment, i) => {
const lastIncompletePath = paths.find(({ complete }) => !complete);
const { path } = lastIncompletePath;
@@ -152,7 +152,7 @@ class ParamParser {
if (currentSchemaPathType === 'nested') {
lastIncompletePath.path = currentPath;
return;
return true;
}
const upcomingSegment = pathSegments[i + 1];
@@ -161,25 +161,25 @@ class ParamParser {
const currentSchemaTypeOptions = getSchemaTypeOptions(currentSchemaType);
if (currentSchemaTypeOptions.localized) {
const upcomingLocalizedPath = `${currentPath}.${upcomingSegment}`;
const upcomingSchemaTypeWithLocale = schema.path(upcomingLocalizedPath);
if (upcomingSchemaTypeWithLocale) {
lastIncompletePath.path = currentPath;
return true;
}
const localePath = `${currentPath}.${this.locale}`;
const localizedSchemaType = schema.path(localePath);
if (localizedSchemaType || operator === 'near') {
lastIncompletePath.path = localePath;
return;
}
const upcomingPathWithLocale = `${currentPath}.${this.locale}.${upcomingSegment}`;
const upcomingSchemaTypeWithLocale = schema.path(upcomingPathWithLocale);
if (upcomingSchemaTypeWithLocale) {
lastIncompletePath.path = upcomingPathWithLocale;
return;
return true;
}
}
lastIncompletePath.path = currentPath;
return;
return true;
}
const priorSchemaType = schema.path(path);
@@ -197,19 +197,16 @@ class ParamParser {
...paths,
...this.getLocalizedPaths(RefModel, remainingPath, operator),
];
return;
return false;
}
}
if (priorSchemaType.instance === 'Mixed' || priorSchemaType.instance === 'Array') {
lastIncompletePath.path = currentPath;
}
} else {
if (operator === 'near' || currentSchemaPathType === 'adhocOrUndefined') {
lastIncompletePath.path = currentPath;
}
if (operator === 'near') {
lastIncompletePath.path = currentPath;
}
return true;
});
return paths;

View File

@@ -334,19 +334,9 @@ const fieldToSchemaMap: Record<string, FieldSchemaGenerator> = {
},
blocks: (field: BlockField, schema: Schema, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): void => {
const fieldSchema = [new Schema({}, { _id: false, discriminatorKey: 'blockType' })];
let schemaToReturn;
if (field.localized && config.localization) {
schemaToReturn = config.localization.locales.reduce((localeSchema, locale) => ({
...localeSchema,
[locale]: fieldSchema,
}), {});
} else {
schemaToReturn = fieldSchema;
}
schema.add({
[field.name]: schemaToReturn,
[field.name]: localizeSchema(field, fieldSchema, config.localization),
});
field.blocks.forEach((blockItem: Block) => {

View File

@@ -72,7 +72,14 @@ export const blocksField: Field = {
const BlockFields: CollectionConfig = {
slug: 'block-fields',
fields: [blocksField],
fields: [
blocksField,
{
...blocksField,
name: 'localizedBlocks',
localized: true,
},
],
};
export const blocksFieldSeedData = [
@@ -107,6 +114,7 @@ export const blocksFieldSeedData = [
export const blocksDoc = {
blocks: blocksFieldSeedData,
localizedBlocks: blocksFieldSeedData,
};
export default BlockFields;

View File

@@ -13,6 +13,11 @@ const TextFields: CollectionConfig = {
type: 'text',
required: true,
},
{
name: 'localizedText',
type: 'text',
localized: true,
},
{
name: 'defaultFunction',
type: 'text',
@@ -32,6 +37,7 @@ const TextFields: CollectionConfig = {
export const textDoc = {
text: 'Seeded text document',
localizedText: 'Localized text',
};
export default TextFields;

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import path from 'path';
import fs from 'fs';
import { buildConfig } from '../buildConfig';
@@ -91,9 +92,10 @@ export default buildConfig({
const blocksDocWithRichText = { ...blocksDoc };
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
blocksDocWithRichText.blocks[0].richText = richTextDocWithRelationship.richText;
// @ts-ignore
blocksDocWithRichText.localizedBlocks[0].richText = richTextDocWithRelationship.richText;
await payload.create({ collection: 'block-fields', data: blocksDocWithRichText });
},

View File

@@ -362,6 +362,54 @@ describe('Fields', () => {
expect(blockFieldsFail.docs).toHaveLength(0);
});
it('should query based on richtext data within a localized block, specifying locale', async () => {
const blockFieldsSuccess = await payload.find({
collection: 'block-fields',
where: {
'localizedBlocks.en.richText.children.text': {
like: 'fun',
},
},
});
expect(blockFieldsSuccess.docs).toHaveLength(1);
const blockFieldsFail = await payload.find({
collection: 'block-fields',
where: {
'localizedBlocks.en.richText.children.text': {
like: 'funny',
},
},
});
expect(blockFieldsFail.docs).toHaveLength(0);
});
it('should query based on richtext data within a localized block, without specifying locale', async () => {
const blockFieldsSuccess = await payload.find({
collection: 'block-fields',
where: {
'localizedBlocks.richText.children.text': {
like: 'fun',
},
},
});
expect(blockFieldsSuccess.docs).toHaveLength(1);
const blockFieldsFail = await payload.find({
collection: 'block-fields',
where: {
'localizedBlocks.richText.children.text': {
like: 'funny',
},
},
});
expect(blockFieldsFail.docs).toHaveLength(0);
});
});
describe('richText', () => {