# @feedloop/foundry

> Provides commonly-used data structures suitable for distributed applications thanks to foundationdb.

Latest version **0.3.0** (published 2022-03-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @feedloop/foundry
pnpm add @feedloop/foundry
yarn add @feedloop/foundry
bun add @feedloop/foundry
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; large bundle; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2022-03-22 |
| First published | 2021-11-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 81.8 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ajudan |

## Links

- npm: https://www.npmjs.com/package/@feedloop/foundry
- npm.io page: https://npm.io/package/@feedloop/foundry

## Dependencies (8)

- [jest](https://npm.io/package/jest.md) ^27.3.1
- [it-all](https://npm.io/package/it-all.md) ^1.0.6
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [typescript](https://npm.io/package/typescript.md) ^4.4.4
- [fraction.js](https://npm.io/package/fraction.js.md) ^4.1.1
- [foundationdb](https://npm.io/package/foundationdb.md) ^1.1.3
- [@datasert/cronjs-parser](https://npm.io/package/@datasert/cronjs-parser.md) ^1.1.0
- [@datasert/cronjs-matcher](https://npm.io/package/@datasert/cronjs-matcher.md) ^1.1.0

## Recent versions

- 0.3.0 (latest) — 2022-03-22
- 0.2.7 — 2022-01-25
- 0.2.6 — 2021-12-24
- 0.2.3 — 2021-12-21
- 0.2.2 — 2021-12-15
- 0.2.1 — 2021-12-14
- 0.2.0 — 2021-12-14
- 0.1.8 — 2021-12-07
- 0.1.7 — 2021-11-24
- 0.1.6 — 2021-11-24
- 0.1.5 — 2021-11-16
- 0.1.4 — 2021-11-11
- 0.1.3 — 2021-11-11
- 0.1.2 — 2021-11-11
- 0.1.1 — 2021-11-11

## README

# FOUNDRY

Provides commonly-used data structures suitable for distributed applications thanks to foundationdb.

## Usage

### Installation

**Before installing the package, you need to install the foundationdb client on your machine**. Please follow [this guide](https://apple.github.io/foundationdb/api-general.html#installing-foundationdb-client-binaries) to install it.

Install foundry with your package manager:

Using npm

```sh
npm install --save @feedloop/foundry
```

Using yarn

```sh
yarn add @feedloop/foundry
```

### Initialization

Foundry uses nodejs foundationdb client that looks for configs in several [ways](https://www.npmjs.com/package/foundationdb#connecting-to-your-cluster).

> Set FDB_API_VERSION to your environment variable to select the foundationdb api version, defaults to version 620.

```ts
import Foundry from "@feedloop/foundry";

// somewhere in your async scope
const foundry = await Foundry.init("your-database-name");
```

### Usage

```ts
import { Set } from "@feedloop/foundry";

// somewhere in your async scope
const hasBar = await foundry.exec(async (factory) => {
  const set = factory.open(Set, "my-set");
  await set.add("foo", "bar", "baz");
  return set.has("bar");
});

// hasBar will be `true`
```

All foundry's operations must be run within the `foundry.exec` scope because they will run in the same transaction. Each transaction will be re-run when a conflict is detected, so keep it simple to reduce client overhead.

## Available Data Structures

Foundry provides commonly used data structures suitable for distributed applications by utilizing foundationdb's transaction excellence.

### List

```ts
import { List } from "@feedloop/foundry";

// somewhere in your async scope
await foundry.exec(async (factory) => {
  const list = factory.open<List<string>>(List, "list1");
  await list.push("a", "b", "c"); // ["a", "b", "c"]
  const secondItem = await list.at(1); // returns "b"
  const last = await list.pop(); // returns "c"
  const len = await list.length(); // returns 2
});
```

### Set

```ts
import { Set } from "@feedloop/foundry";

// somewhere in your async scope
await foundry.exec(async (factory) => {
  const set = factory.open(Set, "my-set");
  await set.add("foo", "bar", "baz"); // ["foo", "bar", "baz"]
  await set.remove("bar"); // ["foo", "baz"]
  await set.size(); // 2
});
```

### Map

```ts
import { Map } from "@feedloop/foundry";

// somewhere in your async scope
await foundry.exec(async (factory) => {
  const map = factory.open(Map, "my-map");
  await map.set("foo", 1); // {foo: 1}
  await map.set("bar", 2); // {foo: 1, bar: 2}
  await map.delete("bar"); // {bar: 2}
  await map.size(); // 2
});
```

### PersistentQueue

```ts
import { PersistentQueue } from "@feedloop/foundry";

// somewhere in your async scope
await foundry.exec(async (factory) => {
  const queue = factory.open(PersistentQueue, "my-queue");
  await queue.write("foo"); // ["foo"]
  await queue.write("bar"); // ["foo", "bar"]

  // reader1 consuming messages
  await queue.read("reader1", 1, true); // returns ["foo"]
  await queue.readOffset("reader1"); // returns 1
  await queue.read("reader1", 1, true); // returns ["bar"]
  await queue.read("reader1", 1, true); // returns []
  await queue.readOffset("reader1"); // returns 2

  // reader2 consuming messages
  await queue.read("reader2", 2, true); // returns ["foo", "bar"]
  await queue.read("reader2", 2, true); // returns []
  await queue.size(); // return 2
});
```

### CronJob

```ts
import { CronJob } from "@feedloop/foundry";

// somewhere in your async scope
// set a cron schedule
await foundry.exec(async (factory) => {
  const cronjob = factory.open(CronJob, "my-cronjob");
  // At every 5th minute (https://crontab.guru/#*/5_*_*_*_*)
  await cronjob.schedule("5 * * * *");
});

// setup a polling to check for jobs
setInterval(async () => {
  const hasJob = await foundry.exec(async (factory) => {
    const cronjob = factory.open(CronJob, "my-cronjob");
    return cronjob.take();
  });
  if (hasJob) {
    console.log("I have a job!"); // will run at every 5th minute
  }
}, 1000 * 60); // run every 1 minute
```

---
_Source: https://npm.io/package/@feedloop/foundry · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
