Skip to content

Typescript library help you manipulate array with SQL-like syntax (completely type support)

License

Notifications You must be signed in to change notification settings

Yumitoya8569/ts-array-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typescript Array SQL

Typescript library help you manipulate array with SQL-like syntax (completely type support)

Quickstart

Install

npm i "@yumitoya8569/ts-array-sql"

Import

import { ArraySQL, SortDir } from '@yumitoya8569/ts-array-sql';

Example

Init Table

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 */];

With SQL

  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

With ArraySQL

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)
    }));

About

Typescript library help you manipulate array with SQL-like syntax (completely type support)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published