get() supports msg hash

This commit is contained in:
Andre Staltz 2023-04-08 17:37:32 +03:00
parent 448bd44a41
commit eec9e739c3
3 changed files with 53 additions and 2 deletions

View File

@ -294,9 +294,12 @@ exports.init = function initDB(peer, config) {
}
function getRecord(msgId) {
const isUri = msgId.startsWith('ppppp:')
for (let i = 0; i < recs.length; i++) {
const rec = recs[i]
if (rec && rec.id === msgId) return rec
if (!rec) continue
if (isUri && rec.id === msgId) return rec
else if (!isUri && rec.id.endsWith(msgId)) return rec
}
return null
}

View File

@ -1,5 +1,5 @@
{
"name": "@ppppp/db",
"name": "ppppp-db",
"version": "0.0.1",
"description": "Default ppppp database",
"main": "index.js",

48
test/get.test.js Normal file
View File

@ -0,0 +1,48 @@
const test = require('tape')
const path = require('path')
const os = require('os')
const rimraf = require('rimraf')
const SecretStack = require('secret-stack')
const caps = require('ssb-caps')
const p = require('util').promisify
const FeedV1 = require('../lib/feed-v1')
const { generateKeypair } = require('./util')
const DIR = path.join(os.tmpdir(), 'ppppp-db-get')
rimraf.sync(DIR)
const keys = generateKeypair('alice')
let peer
let msgHash1
let msgId1
test('setup', async (t) => {
peer = SecretStack({ appKey: caps.shs })
.use(require('../'))
.use(require('ssb-box'))
.call(null, { keys, path: DIR })
await peer.db.loaded()
const rec1 = await p(peer.db.create)({
type: 'post',
content: { text: 'I am 1st post' },
})
msgHash1 = FeedV1.getMsgHash(rec1.msg)
msgId1 = FeedV1.getMsgId(rec1.msg)
})
test('get() supports ppppp URIs', async (t) => {
const msg = peer.db.get(msgId1)
t.ok(msg, 'msg exists')
t.equals(msg.content.text, 'I am 1st post')
})
test('get() supports msg hashes', async (t) => {
const msg = peer.db.get(msgHash1)
t.ok(msg, 'msg exists')
t.equals(msg.content.text, 'I am 1st post')
})
test('teardown', (t) => {
peer.close(t.end)
})