orbitdb/src/db/events.js
2023-04-13 09:15:27 +03:00

50 lines
1.3 KiB
JavaScript

/**
* @namespace Database-Events
* @memberof module:Database
* @description Events Database
*/
import Database from '../database.js'
const Events = () => 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 get = async (hash) => {
const entry = await log.get(hash)
return entry.payload.value
}
const iterator = async function * ({ gt, gte, lt, lte, amount } = {}) {
const it = log.iterator({ gt, gte, lt, lte, amount })
for await (const event of it) {
const hash = event.hash
const value = event.payload.value
yield { hash, value }
}
}
const all = async () => {
const values = []
for await (const entry of iterator()) {
values.unshift(entry)
}
return values
}
return {
...database,
type: 'events',
add,
get,
iterator,
all
}
}
export default Events