Skip to content

Commit

Permalink
Merge branch 'master' into remove-get-db-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Sep 10, 2021
2 parents 975b584 + 9f0a4cc commit 1c3f800
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-lions-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

The item view page will only fetch the item once to determine the field modes rather than once per field.
38 changes: 35 additions & 3 deletions packages/keystone/src/admin-ui/system/getAdminMetaSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AdminMetaRootVal,
ListMetaRootVal,
FieldMetaRootVal,
ItemRootValue,
} from '../../types';
import { InitialisedList } from '../../lib/core/types-for-lists';

Expand Down Expand Up @@ -154,9 +155,10 @@ export function getAdminMetaSchema({
) {
return 'read';
}
const item = await context
.sudo()
.db.lists[rootVal.listKey].findOne({ where: { id: rootVal.itemId } });
const item = await fetchItemForItemViewFieldMode(context)(
rootVal.listKey,
rootVal.itemId
);
const listConfig = config.lists[rootVal.listKey];
const sessionFunction =
lists[rootVal.listKey].fields[rootVal.fieldPath].ui?.itemView?.fieldMode ??
Expand Down Expand Up @@ -340,3 +342,33 @@ function runMaybeFunction<Return extends string | boolean, T>(
}
return sessionFunction;
}

const fetchItemForItemViewFieldMode = extendContext(context => {
type ListKey = string;
type ItemId = string;
const lists = new Map<ListKey, Map<ItemId, Promise<ItemRootValue | null>>>();
return (listKey: ListKey, id: ItemId) => {
if (!lists.has(listKey)) {
lists.set(listKey, new Map());
}
const items = lists.get(listKey)!;
if (items.has(id)) {
return items.get(id)!;
}
let promise = context.db.lists[listKey].findOne({ where: { id } });
items.set(id, promise);
return promise;
};
});

function extendContext<T>(cb: (context: KeystoneContext) => T) {
const cache = new WeakMap<KeystoneContext, T>();
return (context: KeystoneContext) => {
if (cache.has(context)) {
return cache.get(context)!;
}
const result = cb(context);
cache.set(context, result);
return result;
};
}

0 comments on commit 1c3f800

Please sign in to comment.