forked from WebMemex/webmemex-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpouchdb.js
More file actions
50 lines (41 loc) · 1.29 KB
/
Copy pathpouchdb.js
File metadata and controls
50 lines (41 loc) · 1.29 KB
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
import fromPairs from 'lodash/fp/fromPairs'
import PouchDB from 'pouchdb-browser'
import PouchDBQuickSearch from 'pouchdb-quick-search'
import PouchDBFind from 'pouchdb-find'
import PouchDBUpsert from 'pouchdb-upsert'
PouchDB.plugin(PouchDBQuickSearch)
PouchDB.plugin(PouchDBFind)
PouchDB.plugin(PouchDBUpsert)
const db = new PouchDB({
name: 'webmemex',
auto_compaction: true,
})
export default db
// Expose db for debugging
if (process.env.NODE_ENV !== 'production') {
window.db = db
}
// Calls the callback on any change to the database
export function onDatabaseChange(callback) {
return db.changes({
live: true,
since: 'now',
}).on('change', callback)
}
// The couch/pouch way to match keys with a given prefix (e.g. one type of docs).
export const keyRangeForPrefix = prefix => ({
startkey: `${prefix}`,
endkey: `${prefix}\uffff`,
})
// Present db.find results in the same structure as other PouchDB results.
export const normaliseFindResult = result => ({
rows: result.docs.map(doc => ({
doc,
id: doc._id,
key: doc._id,
value: {rev: doc._rev},
})),
})
// Get rows of a query result indexed by doc id, as an {id: row} object.
export const resultRowsById = result =>
fromPairs(result.rows.map(row => [row.id, row]))