Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(cached-local-api): remove consoe log, afterDelete sync with findOne, pass more args to buildWhere #56

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "payload-enchants",
"version": "1.1.19",
"version": "1.1.20",
"private": true,
"description": "",
"keywords": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/better-localized-fields/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/better-localized-fields",
"version": "1.1.19",
"version": "1.1.20",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/better-use-as-title/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/better-use-as-title",
"version": "1.1.19",
"version": "1.1.20",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/bump-payload/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bump-payload",
"version": "1.1.19",
"version": "1.1.20",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
10 changes: 6 additions & 4 deletions packages/cached-local-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ export type Args = {
};

export type FindOneFieldConfig = {
/** @default "where: { equals: value }" */
buildWhere?: (valueToSearch: unknown) => Where;

/** @default "doc[fieldName]" */
buildWhere?: (args: {
args: FindOneArgs<any>;
fieldName: string;
shouldCache: boolean;
value: unknown;
}) => Where;
getFieldFromDoc?: (doc: Record<string, any>) => unknown;
name: string;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/cached-local-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/cached-local-api",
"version": "1.1.19",
"version": "1.1.20",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
4 changes: 2 additions & 2 deletions packages/cached-local-api/src/operations/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export const buildFindOne = ({
{ field: 'args.field', message: 'Invalid findOne field' + args.field },
]);

const where = field.buildWhere(args.value);

const shouldCache = await ctx.shouldCacheFindOperation(args);

const where = field.buildWhere({ args, fieldName: field.name, shouldCache, value: args.value });

const fullArgs = {
...args,
where,
Expand Down
87 changes: 58 additions & 29 deletions packages/cached-local-api/src/plugin/extendCollectionConfig.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,85 @@
import type { CollectionConfig } from 'payload/types';
import type {
CollectionAfterChangeHook,
CollectionAfterDeleteHook,
CollectionConfig,
} from 'payload/types';

import type { SanitizedArgsContext } from '../types';

export const extendCollectionConfig = ({
const executeHook = async ({
cachedCollectionConfig,
collection,
ctx,
hookArgs,
type,
}: {
cachedCollectionConfig: SanitizedArgsContext['collections'][0];
collection: CollectionConfig;
ctx: SanitizedArgsContext;
}): CollectionConfig => {
hookArgs: Parameters<CollectionAfterChangeHook | CollectionAfterDeleteHook>[0];
type: 'afterChange' | 'afterDelete';
}) => {
if (!!hookArgs.doc?.id) return;
const shouldFunction =
type === 'afterDelete' ? ctx.shouldRevalidateOnDelete : ctx.shouldRevalidateOnChange;

const shouldValidate = await shouldFunction(hookArgs as any);

if (!shouldValidate) return;

const { slug } = collection;

ctx.revalidateTag(ctx.buildTagFindByID({ id: hookArgs.doc.id, slug: slug as string }));
ctx.revalidateTag(ctx.buildTagFind({ slug }));

for (const field of cachedCollectionConfig.findOneFields) {
const value = field.getFieldFromDoc(hookArgs.doc);

ctx.revalidateTag(
ctx.buildTagFindOne({
fieldName: field.name,
slug,
value,
}),
);
}
};

export const extendCollectionConfig = ({
cachedCollectionConfig,
collection,
ctx,
}: {
cachedCollectionConfig: SanitizedArgsContext['collections'][0];
collection: CollectionConfig;
ctx: SanitizedArgsContext;
}): CollectionConfig => {
return {
...collection,
hooks: {
...(collection.hooks ?? {}),
afterChange: [
...(collection.hooks?.afterChange ?? []),
async (hookArgs) => {
if (!!hookArgs.doc?.id) return;
const shouldValidate = await ctx.shouldRevalidateOnChange(hookArgs);

if (!shouldValidate) return;

ctx.revalidateTag(ctx.buildTagFindByID({ id: hookArgs.doc.id, slug }));
ctx.revalidateTag(ctx.buildTagFind({ slug }));

for (const field of cachedCollectionConfig.findOneFields) {
const value = field.getFieldFromDoc(hookArgs.doc);

ctx.revalidateTag(
ctx.buildTagFindOne({
fieldName: field.name,
slug,
value,
}),
);
}
await executeHook({
cachedCollectionConfig,
collection,
ctx,
hookArgs,
type: 'afterChange',
});
},
],
afterDelete: [
...(collection.hooks?.afterDelete ?? []),
async (hookArgs) => {
if (!!hookArgs.doc?.id) return;
const shouldValidate = await ctx.shouldRevalidateOnDelete(hookArgs);

if (!shouldValidate) return;

ctx.revalidateTag(ctx.buildTagFindByID({ id: hookArgs.doc.id, slug: slug as string }));
ctx.revalidateTag(ctx.buildTagFind({ slug }));
await executeHook({
cachedCollectionConfig,
collection,
ctx,
hookArgs,
type: 'afterDelete',
});
},
],
},
Expand Down
2 changes: 0 additions & 2 deletions packages/cached-local-api/src/populate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export const populate = async ({
populationList.filter((each) => each.collection.slug === collection).map((each) => each.id),
);

console.log(ids);

ids.forEach((id) => {
populatedPromises.push(
new Promise(async (resolve) => {
Expand Down
2 changes: 0 additions & 2 deletions packages/cached-local-api/src/populate/traverseRichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export const traverseRichText = ({
const isRelationship = key === 'value' && 'relationTo' in data;

if (isRelationship) {
console.log('here', data);

const id = data[key] && typeof data[key] === 'object' ? data[key].id : data[key];

const collection = payload.collections[data.relationTo]?.config;
Expand Down
18 changes: 10 additions & 8 deletions packages/cached-local-api/src/sanitizedArgsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ const defaultBuildTagFindOne: SanitizedArgsContext['buildTagFindOne'] = ({
value,
}) => `${slug}-${fieldName}-${value}`;

const defaultBuildWhere: (fieldName: string) => NonNullable<FindOneFieldConfig['buildWhere']> =
(fieldName) => (value) => ({
[fieldName]: {
equals: value,
},
});
const defaultBuildWhere: NonNullable<FindOneFieldConfig['buildWhere']> = ({
fieldName,
value,
}) => ({
[fieldName]: {
equals: value,
},
});

const defaultGetFieldFromDoc: (
fieldName: string,
Expand All @@ -39,13 +41,13 @@ export const sanitizedArgsContext = (args: Args): SanitizedArgsContext => {
findOneFields: (each.findOneFields ?? []).map((each) => {
if (typeof each === 'object')
return {
buildWhere: each.buildWhere ?? defaultBuildWhere(each.name),
buildWhere: each.buildWhere ?? defaultBuildWhere,
getFieldFromDoc: each.getFieldFromDoc ?? defaultGetFieldFromDoc(each.name),
name: each.name,
};

return {
buildWhere: defaultBuildWhere(each),
buildWhere: defaultBuildWhere,
getFieldFromDoc: defaultGetFieldFromDoc(each),
name: each,
};
Expand Down
12 changes: 6 additions & 6 deletions packages/cached-local-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ export type Count = Payload['count'];
export type CountArgs = Parameters<Count>[0];

export type FindOneFieldConfig = {
/** @default "where: {equals: value }" */
buildWhere?: (valueToSearch: unknown) => Where;

/** @default "doc[fieldName]" */
buildWhere?: (args: {
args: FindOneArgs<any>;
fieldName: string;
shouldCache: boolean;
value: unknown;
}) => Where;
getFieldFromDoc?: (doc: Record<string, any>) => unknown;
name: string;
};

export type Args = {
collections?: Array<{
/** array with the fields to use in findOne (name or config, see below) */
findOneFields?: (FindOneFieldConfig | string)[];

slug: keyof GeneratedTypes['collections'];
}>;
globals?: Array<{
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-reorder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/docs-reorder",
"version": "1.1.19",
"version": "1.1.20",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/seo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/seo",
"version": "1.1.19",
"version": "1.1.20",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/translator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/translator",
"version": "1.1.19",
"version": "1.1.20",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "test",
"version": "1.1.19",
"version": "1.1.20",
"private": true,
"type": "module",
"scripts": {
Expand Down