You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been experimenting with Keystone, and couldn't find an example on how to model a page tree structure.
So far I came to the following approach, curious to hear if anyone has any better/other approaches.
This enables a quick look-up based on the path property, which contains the whole pathname/url of the page, and should always be unique.
Page: list({fields: {slug: text(),parent: relationship({ref: "Page"}),path: text({isIndexed: "unique",ui: {itemView: {fieldMode: "read",},},}),},hooks: {resolveInput: async({ context, item, inputData, resolvedData })=>{constslug=inputData?.slug||item?.slug||"";constparentId=inputData?.parent?.connect?.id||item?.parentId||undefined;// No parent, return just the slugif(!parentId){return{
...resolvedData,path: "/"+slug,};}// There is a parent, combine the parent path with the page slugconstpage=awaitcontext.prisma.page.findFirst({where: {id: parentId},});if(!page){throwError("Parent page not found");}if(page.path==="/"){throwError("Page can't have the homepage as parent page");}return{
...resolvedData,path: page.path+"/"+slug,};},},}),
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I've been experimenting with Keystone, and couldn't find an example on how to model a page tree structure.
So far I came to the following approach, curious to hear if anyone has any better/other approaches.
This enables a quick look-up based on the path property, which contains the whole pathname/url of the page, and should always be unique.
Data look as follows then:
Beta Was this translation helpful? Give feedback.
All reactions