Return items with referenced items from dynamic dataset .getItems()

Is there any way to return items with their referenced items instead of just referenced item IDs from a dynamic dataset?

I know the wixData.query() function allows you to append a .include(“referencedCollection”) function on it so it returns referenced items. I was just wondering if there was a simple way to do the same thing when pulling data from a dynamic dataset?

Trying to avoid iterating through each item in the collection and using a wixData.get() to retrieve its referenced items (tried this and it takes too long). In worst case, would it be more efficient to do what’s mentioned in the previous sentence, or to have a dataset for each referenced collection on the page and pull data from those, or to just do a wixData.query()? Thanks for any help in advance!

const results = await $w('#dynamicDataset').getItems(0, 10);
console.log(results.items[0]);

// returns the following
title: "hello"
referencedItemFromCollectionA: "3f3hsk74-8c61-452b-bfdb-8bc6080cee64"
referencedItemFromCollectionB: "6a7fea74-8c61-452b-bfdb-8bc6080cee64"
referencedItemFromCollectionC: "283jjw12-8c61-452b-bfdb-8bc6080sdw33"

// would like to return the following
title: "hello"
referencedItemFromCollectionA: {...}
referencedItemFromCollectionB: {...}

Hey Steven, I’ve had to do something similar before with a dataset. Are all of your files pointing to the same referencedItemCollection?

There are a couple of things you could do since the dataset doesn’t natively give the referenced item.

  1. You can .query() the full Referenced Collection, and store it locally in an object. That way you can more quickly reference each of the IDs. It’s kind of like creating your own Cache of items for quicker retrieval.

  2. You can set up a data hook for afterQuery that will create the object for you, and should come up in the dataset’s query result.

Both of these options should be a bit quicker than querying the referenced collection each time.

Hi Chris thanks so much for the help. I am trying to implement option 2 but am unable to assign the referenced collection object I get using wixData.get() to the referenced collection property of the item variable passed into the afterQuery function. This property originally contains the referenced collection id string instead of the object, so I was wondering if it might not let me make the assignment due to the types being different (but we can see that’s not the case below). I believe I also ruled out that making a deep copy and then updating and returning it won’t fix the issue either. Any thoughts?

// from data.js in backend
import wixData from 'wix-data';

export async function TopCollection_afterQuery(item, context) {
    item["referencedItemFromCollectionA"] = await wixData.get("CollectionA", item["referencedItemFromCollectionA"]);
    item["referencedItemFromCollectionB"] = {};
    item["newProperty"] = {};
    item["referencedItemFromCollectionC"] = JSON.parse(JSON.stringify(await wixData.get("CollectionA", item["referencedItemFromCollectionA"])));
    let newItem = JSON.parse(JSON.stringify(item));
    newItem["referencedItemFromCollectionA"] = await wixData.get("CollectionA", item["referencedItemFromCollectionA"]);
    return item; // or newItem
}

//
export async function dynamicDataset_ready() {
    const results = await $w("#dynamicDataset").getItems(0, 10);
    console.log(results.items[0]);
}

// console if item is returned
title:"hello" referencedItemFromCollectionA:"3f3hsk74-8c61-452b-bfdb-8bc6080cee64" 
referencedItemFromCollectionB:{}
referencedItemFromCollectionC:"283jjw12-8c61-452b-bfdb-8bc6080sdw33"
newProperty: {}

// console if newItem is returned
title:"hello" referencedItemFromCollectionA:"3f3hsk74-8c61-452b-bfdb-8bc6080cee64" 
referencedItemFromCollectionB: "6a7fea74-8c61-452b-bfdb-8bc6080cee64"
referencedItemFromCollectionC:"283jjw12-8c61-452b-bfdb-8bc6080sdw33"

It’s good that you tried both the deep copy newItem, and the newProperty way, skips a couple debugging steps.

I’m not seeing in your code where you actually tried assigning an object to newProperty , to see if that would work as opposed to overriding it. Based on the code above, it’ll always be {}

Yup that’s what I ended up doing Chris. I learned that if you assign an object with an id property to a reference property whose initial value was the same as the object’s id, the new reference value stays as the referenced item id instead of the object. Thus I had to create a new property to assign the object to. Really appreciate your help, the data hooks got me on the right path

No problem, major Kudos to you for implementing it the right way. :clap: