-
Notifications
You must be signed in to change notification settings - Fork 1
/
pouchdb.jsy
68 lines (55 loc) · 1.91 KB
/
pouchdb.jsy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {bkc_hexkey_api, as_hex_key, as_u8_key} from '../_utils.jsy'
export {as_hex_key, as_u8_key}
export default bkc_with_pouchdb
const _mode_auto = /* #__PURE__ */ @{}
to_attach: 'undefined' !== typeof process ? u8 => u8
: (u8, type) => new Blob([u8], {type})
from_attach: async v =>
v.arrayBuffer
? new Uint8Array(await v.arrayBuffer())
: Uint8Array.from(v)
export async function bkc_with_pouchdb(pouchdb, opt={}) ::
let immutable = !! opt.immutable
let opt_path = '' + (opt.path || '/phorbas/').replace(/^\/+/, '')
let opt_pre = opt.pre || 'phorbas-'
let opt_ext = opt.ext || '.cbor'
let opt_mime = opt.mime || 'application/x-phorbas+cbor'
let {to_attach, from_attach} = opt.mode || _mode_auto
return bkc_hexkey_api @:
bkc_opt: @{} immutable
async hk_has(hex_key) ::
let docId = opt_path + hex_key
let attachmentId = opt_pre + hex_key + opt_ext
try ::
let {_attachments} = await pouchdb.get(docId)
return _attachments[attachmentId] ? 1 : 0
catch err ::
if 404 !== err.status ::
throw err
return 0
async hk_get(hex_key) ::
let docId = opt_path + hex_key
let attachmentId = opt_pre + hex_key + opt_ext
try ::
let res = await pouchdb.getAttachment(docId, attachmentId)
if null != res ::
return from_attach(res)
catch err ::
if 404 !== err.status ::
throw err
async hk_set(hex_key, u8_content) ::
let docId = opt_path + hex_key
try ::
var {_rev} = await pouchdb.get(docId)
if immutable && _rev ::
return false
catch err ::
if 404 !== err.status ::
return err
let attachmentId = opt_pre + hex_key + opt_ext
try ::
await pouchdb.putAttachment @
docId, attachmentId, _rev,
to_attach(u8_content, opt_mime), opt_mime
catch err ::
return err