UNPKG

1.45 kBJavaScriptView Raw
1// see documentation for more detail: http://pouchdb.com/api.html
2
3const PouchDB = require('pouchdb')
4require('pouchdb/extras/memory') /* this is used here just for compatibility with Tonic.
5 you can omit this line and the {adapter: 'memory'}
6 in the next, then your databases will be saved to disk or
7 browser storage.
8 */
9// create a database (here with memory storage):
10const db = new PouchDB('test', {adapter: 'memory'})
11
12// create a new doc with an _id of 'mydoc':
13let response = await db.put({
14 _id: 'mydoc',
15 title: 'Heroes'
16})
17
18// update an existing doc using _rev
19await db.put({
20 _id: 'mydoc',
21 _rev: response.rev,
22 title: "Sound and Vision",
23})
24
25// later you can fetch your doc
26console.log(await db.get('mydoc'))
27
28// or add many more docs
29response = await db.bulkDocs([
30 {_id: 'myotherdoc', title: 'The Magisters', type: "fake band"},
31 {_id: 'another', title: 'Kowabunga', type: "fake band"},
32 {title: 'Without an _id', type: null}
33])
34
35console.log('bulkDocs response: ' + JSON.stringify(response, null, 2))
36
37// and query them
38await db.put({
39 _id: '_design/fakebands',
40 views: {
41 fakebands: {
42 map: (function (doc) {
43 if (doc.type == "fake band") {
44 emit(doc.title)
45 }
46 }).toString()
47 }
48 }
49})
50await db.query('fakebands', {include_docs: true})