diff --git a/lib/plugin.js b/lib/plugin.js index e91ed7e..867b36c 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -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 } diff --git a/package.json b/package.json index a46127c..8c75815 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@ppppp/db", + "name": "ppppp-db", "version": "0.0.1", "description": "Default ppppp database", "main": "index.js", diff --git a/test/get.test.js b/test/get.test.js new file mode 100644 index 0000000..4af8733 --- /dev/null +++ b/test/get.test.js @@ -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) +})