export const getEntityData = query({
args: { entityId: v.id("entities") },
handler: async (ctx, args) => {
// Retrieve all documents for the given entity
const allEntityDocs = await ctx.db
.query("entity_graph_data")
.withIndex("by_entity_id", (q) => q.eq("entity_id", args.entityId))
.collect();
// Return an empty array if no data is found
if (!allEntityDocs || allEntityDocs.length === 0) return [];
// Sort the documents from oldest to newest using _creationTime
allEntityDocs.sort(
(a, b) => Number(a._creationTime) - Number(b._creationTime)
);
// Merge all the 'value' arrays into a single array.
const mergedValues = allEntityDocs.reduce(
(acc, doc) => acc.concat(doc.value),
[] as number[][]
);
// Final sort by the timestamp within each tuple (assumed to be at index 0)
mergedValues.sort((a, b) => a[0] - b[0]);
return mergedValues;
},
});
export const getEntityData = query({
args: { entityId: v.id("entities") },
handler: async (ctx, args) => {
// Retrieve all documents for the given entity
const allEntityDocs = await ctx.db
.query("entity_graph_data")
.withIndex("by_entity_id", (q) => q.eq("entity_id", args.entityId))
.collect();
// Return an empty array if no data is found
if (!allEntityDocs || allEntityDocs.length === 0) return [];
// Sort the documents from oldest to newest using _creationTime
allEntityDocs.sort(
(a, b) => Number(a._creationTime) - Number(b._creationTime)
);
// Merge all the 'value' arrays into a single array.
const mergedValues = allEntityDocs.reduce(
(acc, doc) => acc.concat(doc.value),
[] as number[][]
);
// Final sort by the timestamp within each tuple (assumed to be at index 0)
mergedValues.sort((a, b) => a[0] - b[0]);
return mergedValues;
},
});