UNPKG

@orbitdb/set-db

Version:

Set database type for orbit-db.

64 lines 1.82 kB
import { Database, } from "@orbitdb/core"; const type = "set"; const Set = () => async ({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, onUpdate, }) => { const database = await Database({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, onUpdate, }); const { addOperation, log } = database; const add = async (value) => { return addOperation({ op: "ADD", key: null, value }); }; const del = async (value) => { return addOperation({ op: "DEL", key: null, value }); }; const iterator = async function* ({ amount, } = {}) { const vals = {}; let count = 0; for await (const entry of log.traverse()) { const { op, value } = entry.payload; const key = JSON.stringify(value); if (op === "ADD" && !vals[key]) { vals[key] = true; count++; const hash = entry.hash; yield { value, hash }; } else if (op === "DEL" && !vals[key]) { vals[key] = true; } if (amount !== undefined && count >= amount) { break; } } }; const all = async () => { const values = []; for await (const entry of iterator()) { values.unshift(entry); } return values; }; return { ...database, type, add, del, iterator, all, }; }; Set.type = type; export default Set; //# sourceMappingURL=set.js.map