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

View File

@@ -334,19 +334,9 @@ const fieldToSchemaMap: Record<string, FieldSchemaGenerator> = {
}, },
blocks: (field: BlockField, schema: Schema, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): void => { blocks: (field: BlockField, schema: Schema, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): void => {
const fieldSchema = [new Schema({}, { _id: false, discriminatorKey: 'blockType' })]; 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({ schema.add({
[field.name]: schemaToReturn, [field.name]: localizeSchema(field, fieldSchema, config.localization),
}); });
field.blocks.forEach((blockItem: Block) => { field.blocks.forEach((blockItem: Block) => {

View File

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

View File

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

View File

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

View File

@@ -362,6 +362,54 @@ describe('Fields', () => {
expect(blockFieldsFail.docs).toHaveLength(0); 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', () => { describe('richText', () => {