sizzF
sizzF
DTDrizzle Team
Created by sizzF on 12/12/2023 in #help
I've done db:generate and db:push, but nothing happens.
import * as dotenv from 'dotenv';
import type { Config } from 'drizzle-kit';

dotenv.config();

export default {
schema: './src/**/schema.ts',
out: './drizzle',
driver: 'pg',
dbCredentials: { connectionString: process.env.DB_CLIENT_URL || '' },
} satisfies Config;
import * as dotenv from 'dotenv';
import type { Config } from 'drizzle-kit';

dotenv.config();

export default {
schema: './src/**/schema.ts',
out: './drizzle',
driver: 'pg',
dbCredentials: { connectionString: process.env.DB_CLIENT_URL || '' },
} satisfies Config;
2 replies
DTDrizzle Team
Created by sizzF on 11/28/2023 in #help
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?
7 replies