orbitdb/src/db/keyvalue.js
Hayden Young 428ce83878
Refactor/ac (#61)
* refactor: Move Manifest to own module.

* refactor: Modularize orbitdb access controller.

* chore: Check for correct access controller path and modify if necessary.

* fix: Linting.

* refactor: AC interface no longer needed.

* refactor: Move IPFS-specific AC list back into IPFS AC.

* refactor: Explicitly name access controller param.

* refactor: Pass in manifest settings as object.

* refactor: Config access controllers.

* refactor: ACs should expose specific params before being called with generic params.

* feat: Pass write access to root IPFS AC.

* refactor: AC should handle type prefix.

* test: Test for type.

* refactor: Pass generic access to Database (and inheriting dbs).

* refactor: Use AccessControllers module to manage custom ACs.

* chore: Remove excess console logging.

* test: Fix ipfs module import.
2023-04-03 19:56:47 +08:00

65 lines
1.5 KiB
JavaScript

const KeyValue = async ({ OpLog, Database, ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically }) => {
const database = await Database({ OpLog, ipfs, identity, address, name, access, directory, storage, meta, syncAutomatically })
const { addOperation, log } = database
const put = async (key, value) => {
return addOperation({ op: 'PUT', key, value })
}
const del = async (key) => {
return addOperation({ op: 'DEL', key, value: null })
}
const get = async (key) => {
for await (const entry of log.traverse()) {
const { op, key: k, value } = entry.payload
if (op === 'PUT' && k === key) {
return value
} else if (op === 'DEL' && k === key) {
return
}
}
}
const iterator = async function * ({ amount } = {}) {
const keys = {}
let count = 0
for await (const entry of log.traverse()) {
const { op, key, value } = entry.payload
if (op === 'PUT' && !keys[key]) {
keys[key] = true
count++
const hash = entry.hash
yield { hash, key, value }
} else if (op === 'DEL' && !keys[key]) {
keys[key] = true
}
if (count >= amount) {
break
}
}
}
const all = async () => {
const values = []
for await (const entry of iterator()) {
values.unshift(entry)
}
return values
}
return {
...database,
type: 'keyvalue',
put,
set: put, // Alias for put()
del,
get,
iterator,
all
}
}
export default KeyValue