๐ Functional queries for ECMAScript
Removes an item from the dataset where condition matches.
const { result } = deleteFrom<Item>(data).where((item) => item.id === 2);
Note: Unlike SQL, there is no default behavior for deleteFrom
. If you want to remove all items from the dataset, pass Boolean
or () => true
to where()
.
where
Adds an item to the dataset.
const { result } = insertInto<Item>(data).set({ id: 3, name: "cashews" });
set
Returns items that match the provided conditions
const { result } = selectFrom<Item>(data)
.where(({ value }) => value > 4)
.orderBy("id", "ASC")
.columns(["name", "value"]);
Notes: Unlike SQL, the order of the statements matters.
columns
orderBy
where
Updates all of the items which match the provided condition
const { result } = update<Item>(data)
.where((item) => item.id === 3)
.set((item) => ({ ...item, name: "hazelnuts" }));
set
where
To reduce the boilerplate for creating functional queries, you can use the Schema
function:
const fq = Schema<Item>();
fq.deleteFrom(data).where(/* ... */);
fq.insertInto(data).values(/* ... */);
fq.selectFrom(data).columns(/* ... */);
fq.update(data).where(/* ... */);
The functions returned all received the initial item type.
If you are working with an external data source like localStorage
, you can go one step further and pass a resolver to Schema
:
const fq = Schema<Item>(() =>
JSON.stringify(localStorage.getItem("example") as Item[])
);
fq.deleteFrom().where(/* ... */);
fq.insertInto().values(/* ... */);
fq.selectFrom().columns(/* ... */);
fq.update().where(/* ... */);
ISC ยฉ 2021 Sean McPherson