Is it possible to just get x y z from a collection, rather than the entire document? I've added
depth: 1
but it's still quite a lot of data that isn't just needed. Is it possible to add a mongo query or something? I feel like i've looked through the docs a million times and haven't seen anything... any help would be great
@plainnn You can add custom endpoints to a collection
For instance, if you want to add another GET endpoint that only pulls specific fields, etc
Collections have an "endpoints" property that takes an array of endpoint confiigurations
let me provide an example
Ah nice, i was just thinking of building a next api end point which did something similar
but keeping it all in payload is much nicer.
OK
I just cooked this up, so forgive me if it's a little messy
It's early
endpoints: [
{
path: "/custom-post",
method: "post",
handler: async (req, res, next) => {
try {
if (!id) throw new Error('ID is required')
const { id } = req.body;
const result = await payload.findByID({
collection: "posts", // required
id, // required
// depth: 2,
// locale: "en",
// fallbackLocale: false,
// user: dummyUser,
// overrideAccess: false,
// showHiddenFields: true,
});
// Modify the returned document from the result
delete result.whatever
res.json(result);
} catch (error) {
console.log(error);
res.json({
ok: false,
error: "Example error message",
});
}
},
},
],
Hahaha, not an issue at all - it's super appreciated!
errr its supposed to be custom-post
there we go
So we check to see if ID was included in the request
then we pass the ID to payload.findByID
Then we modify the returned document and send it back
Awesome, this is amazing!
Out of interest, i'm using this code in
getServerSideProps
Thanks! Hopefuly that helps!
const behindTheScenesPosts = await payload.find({
collection: 'behind-the-scenes',
sort: '-date',
limit: 999,
depth: 1,
})
const dates = behindTheScenesPosts.docs.map(post => {
return post.date
})
basically I want the last post date, the most recent date, and then an array of all the dates
is it more perfomant to do that with an endpoint?
good questio
I was just concerned that getting
allposts server side wasn't ideal
if we check out the doc for Find
// Result will be a paginated set of Posts.
// See /docs/queries/pagination for more.
const result = await payload.find({
collection: "posts", // required
depth: 2,
page: 1,
limit: 10,
where: {}, // pass a `where` query here
sort: "-title",
locale: "en",
fallbackLocale: false,
user: dummyUser,
overrideAccess: false,
showHiddenFields: true,
});
There are maybe some ways on here
im looking at "Sort"
so if you had two reqs
-date, date
and limit it to one
that's one idea
Then you'd have 3 reqs tho
but less data
another idea is there where clause
Yeah that is nice, but I still need an array of
allthe dates
which takes some conditions
Right
I'd make the initial call for all the dates
And IIRC dates are numbers?
(could be wrong)
but in any case, you could likely just grab the first and last item if they are sorted
From payload-types
date: string
Ah
I think I still prefer it being an API end point 🤔
rather than server side
So maybe the Date native oculd help
to get the number, and sort by that, faster than string sort
but also, sorting shouldn't be too costly
I think though with this scenario, im not as helpful
that's more of like a data manip question i think
Though, maybe another payload user on here could assist with your second question 😄
I think there should be an easy way of doing it, i just dont know
Hahah no that's fine, I can do the getting of dates etc, just want to know what is best with end points or serverSideProps 😄
You have done plenty!
Cool cool! Well, always hapy to help, feel free to ping me if you get stuck on anything
endpoints: [
{
path: '/get-dates',
method: 'post',
handler: async (req, res, next) => {
try {
const behindTheScenes = await payload.find({
collection: 'behind-the-scenes',
depth: 1,
sort: '-date',
})
const dates = behindTheScenes.docs.map(behindTheScene => {
return behindTheScene.date
})
const firstDate = dates[0]
const lastDate = dates[dates.length - 1]
return res.json({
firstDate,
lastDate,
dates,
})
} catch (error) {
console.log(error)
res.json({
ok: false,
error: 'Couldnt get dates',
})
}
},
},
],
Think we're looking good
Nice!!
Also, not super necessary, but you also have access to req.params
if you want it to be a GET req
am I right in thinking the end point should be
/api/behind-the-scenes/get-dates
? - not hitting it atm
right, i was going to mention the url
I think it's relative to the collection base
Yeah, I don't need any params at the moment, but definitely nice
so yeah, api/behind-the-scenes/get-dates
(Assuming this collection's url is /api/behind-the-scenes/)
Interesting..
NotFound: The requested resource was not found.
Yeah, i'm using that for other reqs
and it was a POST?
🤦♂️
hehe
that's the issue lmfao
trust, ive done it 10000 times
so easily done!
😄
Also, side note, the main payload config also takes an endpoints array
if you want global routes
rather than per-collection
Oh awesome, that's super nice!
At my agency i keep saying payload is the gift that keeps on giving 😄
It's a pretty small request, but it's hard to test vs serverSideProps
I agree! We are using it at work, but i also used it when i was doing freelance too
That's a very tiny req
yea
I might have a shot at making a mongo db request in a next api end point
interesting, for custom data?
Yeah, just so I can query the dates and only the dates, to get it as small as possible
It's not an issue at the moment since we have 11 items in the collection, but we plan to have many many many more
Hmm
So you think for instance, 1000 items
(potentially thousands in yrs to come)
youll get a larger ms delay
?
yeah valid, i'm thinking wrong - the response wont change, just the time
You can also optimize / index collections IIRC, not sure if that is relevant here
Well time is important but
If it scales proportionally, then at 10,000 docs thats 190ms
😄 - I think we are fine
which is still not even a quarter of a second
😄
I could be wrong though
Awesome, i'll leave it at that
Thanks a lot (not) chris
Hey of course Plain, feel free to ping me or just say hey to talk about web dev!
Thanks for checking out Payload, the dev team is super rad 😄
I will for sure 🙌
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.