This repository was archived by the owner on Feb 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathview.js
More file actions
103 lines (95 loc) · 2.88 KB
/
view.js
File metadata and controls
103 lines (95 loc) · 2.88 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict'
const db = require('../index')({
baseUrl: process.env.DB_URL || 'http://localhost:5984'
})
const dbName = 'testdb_' + Math.random().toString(36).slice(2, 8)
const months = ['January', 'February', 'March', 'April', 'Mai', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
// create database and insert some documents
db.createDatabase(dbName)
.then(() => db.createBulkDocuments(dbName,
months.map((x, i) => { return {name: x, number: i + 1} })
))
.then(console.log)
// create new design document
.then(() => db.createDesignDocument(dbName, {
language: 'javascript',
views: {
view1: {
map: 'function (doc) {emit(doc.name, doc.number)}'
}
}
}, 'ddoc1'))
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: '_design/ddoc1',
// rev: '1-548c68d8cc2c1fac99964990a58f66fd' },
// status: 201,
// message: 'Created – Document created and stored on disk',
// duration: 271 }
// get design document
.then(() => db.getDesignDocument(dbName, 'ddoc1'))
.then(console.log)
// { headers: { ... },
// data:
// { _id: '_design/ddoc1',
// _rev: '1-548c68d8cc2c1fac99964990a58f66fd',
// language: 'javascript',
// views: { view1: [Object] } },
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 5 }
// get design document info
.then(() => db.getDesignDocumentInfo(dbName, 'ddoc1'))
.then(console.log)
// { headers: { ... },
// data:
// { name: 'ddoc1',
// view_index:
// { updates_pending: [Object],
// waiting_commit: false,
// waiting_clients: 0,
// updater_running: false,
// update_seq: 0,
// sizes: [Object],
// signature: '1e86d92af43c47ef58da4b645dbd47f1',
// purge_seq: 0,
// language: 'javascript',
// disk_size: 408,
// data_size: 0,
// compact_running: false } },
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 54 }
// request some data from view
// see https://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options
.then(() => db.getView(dbName, 'ddoc1', 'view1', {
decending: true,
limit: 3
}))
.then(console.log)
// { headers: { ... },
// data:
// { total_rows: 12,
// offset: 0,
// rows: [ [Object], [Object], [Object] ] },
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 834 }
// delete design document
// get current revision - then delete
.then(() => db.getDesignDocument(dbName, 'ddoc1'))
.then(response => db.deleteDesignDocument(dbName, 'ddoc1', response.data._rev))
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: '_design/ddoc1',
// rev: '2-fd68157ec3c1915ebe0b248343292d34' },
// status: 200,
// message: 'OK - Document successfully removed',
// duration: 49 }
// delete database
.then(() => db.deleteDatabase(dbName))
.catch(console.error)