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 },
      },
    });


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.


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

Or is this approach itself wrong?
Was this page helpful?