brianc / node-sql

SQL generation for node.js
MIT License
1.05k stars 191 forks source link

Alternative query building suggestion #375

Open jtheisen opened 7 years ago

jtheisen commented 7 years ago

Hi folks, this isn't a bug report but something the authors of node-sql may find useful.

I spent the last couple of weeks researching ways to get a good query syntax in TypeScript working - before I found this library.

I now have a proof of concept and a readme explaining what I found. In particular, there's a way to write queries like this:

processQuery(query(() => {
    const orderNoWithInvoiceCount = query(() => {
        const i = from(invoices);
        groupBy(i.orderNo);
        return { orderNo: i.orderNo, numberOfInvoices: count() }
    })
    const { orderNo, numberOfInvoices } = from(orderNoWithInvoiceCount)
    const latestInvoice = outerApply(query(() => {
        const i2 = from(invoices)
        where(i2.orderNo.eq(orderNo))
        orderByDesc(i2.createdAt)
        fetchOnly(1)
        return i2
    }))

    return { orderNo, numberOfInvoices, latestInvoice };
}))

So this ticket really is just an ad for a sort of blog post, but I the information is interesting for anyone involved with SQL query builders.