© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago
nomz eater

dynamic relational queries column returns

Hi I'm not entirely sure if this is the intended result when trying to make a relational query.

//type is derived from a mysql table
type User = {
  id: string;
  username: string;
  email: string;
}

type UniqueQuery = 'id' | 'email' | 'username'
type UserColumns = { [p in keyof User]?: boolean }

const findOne = async(query: UniqueQuery, value: string, columns?: UserColumns ) => {

    //The object that will appear will be an empty object like => { } 
    const user = await this.db.query.users.findFirst({
      columns: columns
        ? columns
        : {
            id: true,
            username: true,
            email: true,
          },
      //method defined elsewhere
      where: await validateQuery(query, value),
    });

    if (!user) throw new Error("User not found");

    return user;
  }

// if method is defined like this though the user's return object works as expected
  async findOne<Columns extends { [Columns in keyof User]?: boolean }>(
    query: UniqueQuery,
    value: string,
    columns?: Columns,
  ) {
    const user = await this.db.query.users.findFirst({
      columns: columns
        ? columns
        : {
            id: true,
            username: true,
            email: true,
          },
      where: await validateQuery(query, value),
    });

    if (!user) throw new Error("User not found");

    return user;
  }

//example using the method using generics
const user = await findOne("id", "asdf", {
  id: true,
  username: true,
});
//return object will look like this { id: /* some string */, username: /* some username */ }

// using method without generics using same values { } 
//type is derived from a mysql table
type User = {
  id: string;
  username: string;
  email: string;
}

type UniqueQuery = 'id' | 'email' | 'username'
type UserColumns = { [p in keyof User]?: boolean }

const findOne = async(query: UniqueQuery, value: string, columns?: UserColumns ) => {

    //The object that will appear will be an empty object like => { } 
    const user = await this.db.query.users.findFirst({
      columns: columns
        ? columns
        : {
            id: true,
            username: true,
            email: true,
          },
      //method defined elsewhere
      where: await validateQuery(query, value),
    });

    if (!user) throw new Error("User not found");

    return user;
  }

// if method is defined like this though the user's return object works as expected
  async findOne<Columns extends { [Columns in keyof User]?: boolean }>(
    query: UniqueQuery,
    value: string,
    columns?: Columns,
  ) {
    const user = await this.db.query.users.findFirst({
      columns: columns
        ? columns
        : {
            id: true,
            username: true,
            email: true,
          },
      where: await validateQuery(query, value),
    });

    if (!user) throw new Error("User not found");

    return user;
  }

//example using the method using generics
const user = await findOne("id", "asdf", {
  id: true,
  username: true,
});
//return object will look like this { id: /* some string */, username: /* some username */ }

// using method without generics using same values { } 


keep in mind that the only thing that doesn't work is the return type definition, the query works as intended meaning it grabs and returns the data that I want to return from the specified columns identified as true.
CleanShot_2023-06-16_at_23.58.022x.png
CleanShot_2023-06-16_at_23.58.252x.png
CleanShot_2023-06-16_at_23.59.362x.png
CleanShot_2023-06-16_at_23.59.482x.png
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Dynamic Drizzle Relational Queries
Drizzle TeamDTDrizzle Team / help
3y ago
Conditional Relational Queries
Drizzle TeamDTDrizzle Team / help
3y ago
Relational queries (PlanetScale)
Drizzle TeamDTDrizzle Team / help
3y ago
Help with relational queries
Drizzle TeamDTDrizzle Team / help
9mo ago