freddie
freddie
DTDrizzle Team
Created by shahidcodes on 4/13/2024 in #help
Fetching many to many along with some aggregation using select
btw lodash’s get is well tested and gets bundled with tree shaking so i chose over rolling my own recursive
16 replies
DTDrizzle Team
Created by tzezar on 3/3/2024 in #help
Conditional join
you’ll need dynamic with generics like the example, let me make u a sample when i’m back on computer
6 replies
DTDrizzle Team
Created by tzezar on 3/3/2024 in #help
Conditional join
https://orm.drizzle.team/docs/dynamic-query-building
let query = await db
.select()
.from(documentLine)
.$dynamic()

if (filters.includes(documentId)) {
// add to query (optionally use $dynamic() again)
return query.leftJoin(document, eq(document.id, documentLine.documentId)));
}

// fallback to just running it
return query.run();
let query = await db
.select()
.from(documentLine)
.$dynamic()

if (filters.includes(documentId)) {
// add to query (optionally use $dynamic() again)
return query.leftJoin(document, eq(document.id, documentLine.documentId)));
}

// fallback to just running it
return query.run();
6 replies
DTDrizzle Team
Created by bernardoquina on 5/23/2023 in #help
Count in relational queries
and learn some sql along the way! drizzle core API > query builder 🤣
7 replies
DTDrizzle Team
Created by shahidcodes on 4/13/2024 in #help
Fetching many to many along with some aggregation using select
well discord wont let me paste the code here, so here it goes: https://tsplay.dev/wE2LgN
16 replies
DTDrizzle Team
Created by shahidcodes on 4/13/2024 in #help
Fetching many to many along with some aggregation using select
ok so I made a more reliable verson of that function but it uses lodash’s get
16 replies
DTDrizzle Team
Created by shahidcodes on 4/13/2024 in #help
Fetching many to many along with some aggregation using select
im doing some improvements on this function already, might release that as a little drizzle toolkit library later
16 replies
DTDrizzle Team
Created by shahidcodes on 4/13/2024 in #help
Fetching many to many along with some aggregation using select
you can use it like:
const results = await db
.select({
name: BatchModel.name,
customer: CustomerModel
})
.from(BatchModel)
.fullJoin(BatchToCustomer, eq(BatchModel.id, BatchToCustomer.batch_id))
.fullJoin(CustomerModel, eq(BatchToCustomer.customer_id, CustomerModel.id))
.groupBy(BatchModel.id)
.all();

const aggregated = aggregate({rows: results, id: "name", fields: {customers: "customer"}})
const results = await db
.select({
name: BatchModel.name,
customer: CustomerModel
})
.from(BatchModel)
.fullJoin(BatchToCustomer, eq(BatchModel.id, BatchToCustomer.batch_id))
.fullJoin(CustomerModel, eq(BatchToCustomer.customer_id, CustomerModel.id))
.groupBy(BatchModel.id)
.all();

const aggregated = aggregate({rows: results, id: "name", fields: {customers: "customer"}})
lmk if that works for you!
16 replies