Typescript library help you manipulate array with SQL-like syntax (completely type support)
npm i "@yumitoya8569/ts-array-sql"
import { ArraySQL, SortDir } from '@yumitoya8569/ts-array-sql';
import { ArraySQL, SortDir } from '@yumitoya8569/ts-array-sql';
type Customer = {
c_id: string,
name: string,
city: string,
address: string,
phone: string,
salary: number
};
type Order = {
o_id: string,
order_no: string,
c_id: string,
price: number
};
const customers: Customer[] = [ /* your data here */];
const orders: Order[] = [/* your data here */];
SELECT a.name, SUM(b.price) sum_price
FROM customers a
LEFT JOIN orders b
ON a.c_id = b.c_id
GROUP BY a.c_id
ORDER BY a.c_id ASC
const result = ArraySQL
.from({ a: customers })
.leftJoin({ b: orders })
.on(({ a, b }) => a.c_id === b.c_id)
.groupBy(({ a }) => [a.c_id])
.orderBy(({ a }) => [a.c_id], [SortDir.ASC])
.select(({ a }, gp) => ({
name: a.name,
sum_price: gp?.sum(({ b }) => b.price)
}));