How to dynamically select columns in relation query with proper type inference ?

const result = await db.query.products.findMany({
columns: {
id: true,
title: true,
retailPrice: true,
discount: true,
price: true,
},
orderBy: orderBy,
limit: perPage,
offset: meta.offset,
})
const result = await db.query.products.findMany({
columns: {
id: true,
title: true,
retailPrice: true,
discount: true,
price: true,
},
orderBy: orderBy,
limit: perPage,
offset: meta.offset,
})
The above example generated following types
const result: {
title: string;
description: string | null;
id: string;
retailPrice: number;
discount: number;
price: number;
}[]
const result: {
title: string;
description: string | null;
id: string;
retailPrice: number;
discount: number;
price: number;
}[]
But if I use
const avalaibleColumns = ["id", "title", "price"]
const result = await db.query.products.findMany({
columns: {
id: avalaibleColumns.includes("id"),
title: avalaibleColumns.includes("title"),
retailPrice: avalaibleColumns.includes("retailPrice"),
discount: avalaibleColumns.includes("discount"),
price: avalaibleColumns.includes("price"),
},
orderBy: orderBy,
limit: perPage,
offset: meta.offset,
})
const avalaibleColumns = ["id", "title", "price"]
const result = await db.query.products.findMany({
columns: {
id: avalaibleColumns.includes("id"),
title: avalaibleColumns.includes("title"),
retailPrice: avalaibleColumns.includes("retailPrice"),
discount: avalaibleColumns.includes("discount"),
price: avalaibleColumns.includes("price"),
},
orderBy: orderBy,
limit: perPage,
offset: meta.offset,
})
const result: {
title: string;
description: string | null;
id: string;
... 20 more ...;
attributes: Record<...> | null;
}[]
const result: {
title: string;
description: string | null;
id: string;
... 20 more ...;
attributes: Record<...> | null;
}[]
5 Replies
Angelelz
Angelelz14mo ago
Give it a try with the new as const from TS:
const avalaibleColumns = ["id", "title", "price"] as const
...
const avalaibleColumns = ["id", "title", "price"] as const
...
I'm not sure if that's what you want. If you want it to be dynamic, it's impossible to infer definite types at build time. You'll need some type of generic helper. I'm not completely sure What you're trying to accomplish.
MAST
MAST13mo ago
You can also do this to generate the object as well:
const columns = ['id', 'name', 'age'] as const;
const avalaibleColumns = ["id", "title", "price"]
const result = await db.query.products.findMany({
columns: Object.fromEntries(columns.map((column) => [column, true])),
// or
columns: {
...Object.fromEntries(columns.map((column) => [column, true])),
someOtherColumn: true;
}
orderBy: orderBy,
limit: perPage,
offset: meta.offset,
})
const columns = ['id', 'name', 'age'] as const;
const avalaibleColumns = ["id", "title", "price"]
const result = await db.query.products.findMany({
columns: Object.fromEntries(columns.map((column) => [column, true])),
// or
columns: {
...Object.fromEntries(columns.map((column) => [column, true])),
someOtherColumn: true;
}
orderBy: orderBy,
limit: perPage,
offset: meta.offset,
})
Shubham-Sinha
Shubham-Sinha13mo ago
Will give it a try and let you guys know
nikhilkitty1234
nikhilkitty123412mo ago
Hi, is there a way to exclude certain columns in a similar fashion. I tried Object.fromEntries type of code as given here, but the resulting type is not known and giving errors at run time.
MAST
MAST12mo ago
Can you send the code snippet and error in another help thread?
Want results from more Discord servers?
Add your server