© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
6 replies
sizzF

Checking Joined Data Directly from Type Recommendation and Retrieved Data in Dynamic findMany

When implementing a join using with in findMany, I wrote the following code to dynamically change the object to be set in with.

// normal
const result = await dbToUse.query.user.findMany({
      columns: { password: false },
      ...queryOptions,
      with: {
        Company: {
          columns: companyJoinColumns,
          with: { Users: true },
        },
        Partner: { columns: companyJoinColumns },
      },
    });
// normal
const result = await dbToUse.query.user.findMany({
      columns: { password: false },
      ...queryOptions,
      with: {
        Company: {
          columns: companyJoinColumns,
          with: { Users: true },
        },
        Partner: { columns: companyJoinColumns },
      },
    });


my code

I wrote this code because I wanted to allow users of the API to choose which objects to join.
// ex) offJoin = ["Company.User", "Partner"],
export const modifyWithOptions = (withOptions: any, offJoin: string[] | undefined, path: string = ''): any => {
  if (!offJoin) {
    return withOptions;
  }
  return Object.keys(withOptions).reduce((acc, key) => {
    const currentPath = path ? `${path}.${key}` : key;
    if (offJoin.includes(currentPath)) {
      return acc;
    }

    const value = withOptions[key];
    if (value.with) {
      value.with = modifyWithOptions(value.with, offJoin, currentPath);
    }

    return { ...acc, [key]: value }; 
  }, {});
};


   const result = await dbToUse.query.user.findMany({
      columns: { password: false },
      ...queryOptions,
      with: modifyWithOptions(
        {
          Company: {
            columns: companyJoinColumns,
            with: { Users: true },
          },
          Partner: { columns: companyJoinColumns },
        },
        params.offJoin
      ),
    });

//I want to use type inference in the modifyWithOptions function and have the data I include with in the results of findMany appear as recommendations.
// ex) offJoin = ["Company.User", "Partner"],
export const modifyWithOptions = (withOptions: any, offJoin: string[] | undefined, path: string = ''): any => {
  if (!offJoin) {
    return withOptions;
  }
  return Object.keys(withOptions).reduce((acc, key) => {
    const currentPath = path ? `${path}.${key}` : key;
    if (offJoin.includes(currentPath)) {
      return acc;
    }

    const value = withOptions[key];
    if (value.with) {
      value.with = modifyWithOptions(value.with, offJoin, currentPath);
    }

    return { ...acc, [key]: value }; 
  }, {});
};


   const result = await dbToUse.query.user.findMany({
      columns: { password: false },
      ...queryOptions,
      with: modifyWithOptions(
        {
          Company: {
            columns: companyJoinColumns,
            with: { Users: true },
          },
          Partner: { columns: companyJoinColumns },
        },
        params.offJoin
      ),
    });

//I want to use type inference in the modifyWithOptions function and have the data I include with in the results of findMany appear as recommendations.


Is there a way to check the joined data directly from the type recommendation and the retrieved data?

Or is this approach itself wrong?
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

Data Retrieved As Same Data
Drizzle TeamDTDrizzle Team / help
2y ago
recommendation on changing data type of column?
Drizzle TeamDTDrizzle Team / help
13mo ago
Trouble using findMany with joined relation and where clause on relation
Drizzle TeamDTDrizzle Team / help
2y ago
Conditional Select in M2M findMany Type Inference
Drizzle TeamDTDrizzle Team / help
3y ago