kstulgys
kstulgys5mo ago

Am I doing this ent query correctly?

export const get = authQuery({
args: {
orderId: v.id("orders"),
},
handler: async (ctx, args) => {
if (!ctx.user?.currentAccountId) return null;
const order = await ctx
.table("accounts")
.getX(ctx.user.currentAccountId)
.edgeX("orders")
.filter((q) => q.eq(q.field("_id"), args.orderId))
.firstX();
return order;
},
});
export const get = authQuery({
args: {
orderId: v.id("orders"),
},
handler: async (ctx, args) => {
if (!ctx.user?.currentAccountId) return null;
const order = await ctx
.table("accounts")
.getX(ctx.user.currentAccountId)
.edgeX("orders")
.filter((q) => q.eq(q.field("_id"), args.orderId))
.firstX();
return order;
},
});
I want to get order by id but that order must belong to the account that is ctx.user.currentAccountId
2 Replies
kstulgys
kstulgysOP5mo ago
I guess this is better, right?
export const get = authQuery({
args: {
orderId: v.id("orders"),
},
handler: async (ctx, args) => {
if (!ctx.user?.currentAccountId) return null;
const order = await ctx.table("orders").getX(args.orderId)
const account = await order.edgeX("account");
if(account._id !== ctx.user.currentAccountId) return null;
return order;
},
});
export const get = authQuery({
args: {
orderId: v.id("orders"),
},
handler: async (ctx, args) => {
if (!ctx.user?.currentAccountId) return null;
const order = await ctx.table("orders").getX(args.orderId)
const account = await order.edgeX("account");
if(account._id !== ctx.user.currentAccountId) return null;
return order;
},
});
Michal Srb
Michal Srb5mo ago
Yes, the latter is much better, since _id is unique!

Did you find this page helpful?