#Getting specific column results

4 messages · Page 1 of 1 (latest)

sick nest
#

Hi guys, how do i return a list of objects containing specified columns instead of the whole record itself? Thanks guys!

past bridge
#

Hey @sick nest , you can map over the results in your function:

import { query } from "./_generated/server";

export const listTasks = query({
  handler: async (ctx) => {
    const tasks = await ctx.db.query("tasks").collect();
    return tasks.map(({name, completed} => ({name, completed}));
  },
});

In this example only the name and completed columns are returned.

sick nest
#

Hey @past bridge thanks for the quick reply

export const getSchoolsByDate = query({
  args: { show_date: v.string() },
  handler: async (ctx, args) => {
    const _schools = await ctx.db
      .query("show_dates")
      .filter((q) => q.eq(q.field("show_date"), args.show_date))
      .collect();
    const schools: string[] = [];
    _schools.map((o) => {
      schools.push(o.school_name);
    });
    return schools;
  },
});

This was my work around, im not sure if it is efficient

past bridge
#

It is efficient (and similar to the code I gave - if you're gonna .push() you can use forEach() instead of map())