esfq

0.1.1 • Public • Published

ESFQ

🔍 Functional queries for ECMAScript

Functions

deleteFrom

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().

Methods

  • where

insertInto

Adds an item to the dataset.

const { result } = insertInto<Item>(data).set({ id: 3, name: "cashews" });

Methods

  • set

selectFrom

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.

Methods

  • columns
  • orderBy
  • where

update

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

Methods

  • set
  • where

Schema

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(/* ... */);

License

ISC © 2021 Sean McPherson

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.1
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.1.1
    1
  • 0.1.0
    0

Package Sidebar

Install

npm i esfq

Weekly Downloads

1

Version

0.1.1

License

ISC

Unpacked Size

21.9 kB

Total Files

9

Last publish

Collaborators

  • seanmcp