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 to install it.
Install foundry with your package manager:
Using npm
npm install --save @feedloop/foundry
Using yarn
yarn add @feedloop/foundry
Initialization
Foundry uses nodejs foundationdb client that looks for configs in several ways.
Set FDB_API_VERSION to your environment variable to select the foundationdb api version, defaults to version 620.
import Foundry from "@feedloop/foundry";
const foundry = await Foundry.init("your-database-name");
Usage
import { Set } from "@feedloop/foundry";
const hasBar = await foundry.exec(async (factory) => {
const set = factory.open(Set, "my-set");
await set.add("foo", "bar", "baz");
return set.has("bar");
});
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
import { List } from "@feedloop/foundry";
await foundry.exec(async (factory) => {
const list = factory.open<List<string>>(List, "list1");
await list.push("a", "b", "c");
const secondItem = await list.at(1);
const last = await list.pop();
const len = await list.length();
});
Set
import { Set } from "@feedloop/foundry";
await foundry.exec(async (factory) => {
const set = factory.open(Set, "my-set");
await set.add("foo", "bar", "baz");
await set.remove("bar");
await set.size();
});
Map
import { Map } from "@feedloop/foundry";
await foundry.exec(async (factory) => {
const map = factory.open(Map, "my-map");
await map.set("foo", 1);
await map.set("bar", 2);
await map.delete("bar");
await map.size();
});
PersistentQueue
import { PersistentQueue } from "@feedloop/foundry";
await foundry.exec(async (factory) => {
const queue = factory.open(PersistentQueue, "my-queue");
await queue.write("foo");
await queue.write("bar");
await queue.read("reader1", 1, true);
await queue.readOffset("reader1");
await queue.read("reader1", 1, true);
await queue.read("reader1", 1, true);
await queue.readOffset("reader1");
await queue.read("reader2", 2, true);
await queue.read("reader2", 2, true);
await queue.size();
});
CronJob
import { CronJob } from "@feedloop/foundry";
await foundry.exec(async (factory) => {
const cronjob = factory.open(CronJob, "my-cronjob");
await cronjob.schedule("5 * * * *");
});
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!");
}
}, 1000 * 60);