mono2712
DTDrizzle Team
•Created by mono2712 on 11/21/2023 in #help
Dynamic Drizzle Relational Queries
@Angelelz I created this
buildAndConditions
function to generate the where, but typed it as any
would you happen to know how I could type this properly?
const testQueryObject = {
price: { gte: 2000 },
difficulty: 'medium',
};
// how would you type this without using any?
const buildAndConditions = ({ table, operations, queryObject }: any) => {
const conditions = [];
for (const key in queryObject) {
const filter = queryObject[key];
if (filter instanceof Object) {
Object.entries(filter).forEach(([operator, value]) => {
conditions.push(operations[operator](table[key], value));
});
} else {
conditions.push(operations.eq(table[key], filter));
}
}
return operations.and(...conditions);
};
const query = db.query.tours.findMany({
where: (tours, operations) => {
const conditions = buildAndConditions({
table: tours,
operations,
queryObject: testQueryObject,
});
return conditions;
},
with: {
images: true,
startDates: true,
},
});
const testQueryObject = {
price: { gte: 2000 },
difficulty: 'medium',
};
// how would you type this without using any?
const buildAndConditions = ({ table, operations, queryObject }: any) => {
const conditions = [];
for (const key in queryObject) {
const filter = queryObject[key];
if (filter instanceof Object) {
Object.entries(filter).forEach(([operator, value]) => {
conditions.push(operations[operator](table[key], value));
});
} else {
conditions.push(operations.eq(table[key], filter));
}
}
return operations.and(...conditions);
};
const query = db.query.tours.findMany({
where: (tours, operations) => {
const conditions = buildAndConditions({
table: tours,
operations,
queryObject: testQueryObject,
});
return conditions;
},
with: {
images: true,
startDates: true,
},
});
8 replies