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 pathexample.js
More file actions
201 lines (184 loc) · 5.48 KB
/
example.js
File metadata and controls
201 lines (184 loc) · 5.48 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const db = require('../index')({
baseUrl: process.env.DB_URL || 'http://localhost:5984',
requestTimeout: 2000, // default is 10000
verifyCertificate: false // default is true
})
const dbName = 'testdb'
db.getInfo()
.then(console.log)
// { headers: { ... },
// data:
// { couchdb: 'Welcome',
// version: '2.0.0',
// vendor: { name: 'The Apache Software Foundation' } },
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 36 }
db.createDatabase(dbName)
.then(console.log)
// { headers: { ... },
// data: { ok: true },
// status: 201,
// message: 'Created - Database created successfully',
// duration: 131 }
.then(() => db.getDatabaseHead(dbName))
.then(console.log)
// { headers: { ... },
// data: {},
// status: 200,
// message: 'OK - Database exists',
// duration: 4 }
.then(() => db.listDatabases())
.then(console.log)
// { headers: { ... },
// data: [ '_replicator', '_users', 'testdb' ],
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 4 }
.then(() => db.createDocument(dbName, {name: 'Bob'}))
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: 'daae0752c6909d7ca4cd833f46014605',
// rev: '1-5a26fa4b20e40bc9e2d3e47b168be460' },
// status: 201,
// message: 'Created – Document created and stored on disk',
// duration: 42 }
.then(() => db.createDocument(dbName, {name: 'Alice'}, 'doc2'))
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: 'doc2',
// rev: '1-88b10f13383b5d1e34d1d66d296f061f' },
// status: 201,
// message: 'Created – Document created and stored on disk',
// duration: 38 }
.then(() => db.getDocumentHead(dbName, 'doc2'))
.then(console.log)
// { headers:
// { server: 'CouchDB/1.6.1 (Erlang OTP/18)',
// etag: '"1-88b10f13383b5d1e34d1d66d296f061f"',
// date: 'Sat, 01 Oct 2016 09:46:09 GMT',
// 'content-type': 'application/json',
// 'content-length': '74',
// 'cache-control': 'must-revalidate' },
// data: {},
// status: 200,
// message: 'OK - Document exists',
// duration: 6 }
.then(() => db.getDocument(dbName, 'doc2'))
.then(response => { console.log(response); return response.data })
// { headers: { ... },
// data:
// { _id: 'doc2',
// _rev: '1-88b10f13383b5d1e34d1d66d296f061f',
// name: 'Alice' },
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 11 }
.then((doc) => {
doc.age = 42
return db.createDocument(dbName, doc, 'doc2')
})
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: 'doc2',
// rev: '2-ee5ea9ac0bb1bec913a9b5e7bc11b113' },
// status: 201,
// message: 'Created – Document created and stored on disk',
// duration: 36 }
.then(() => db.getAllDocuments(dbName, {
descending: true,
include_docs: true
}))
.then(console.log)
// { headers: { ... },
// data: { total_rows: 2, offset: 0, rows: [ [Object], [Object] ] },
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 9 }
.then(() => db.createBulkDocuments(dbName, [
{name: 'Tick'}, {name: 'Trick'}, {name: 'Track'}
], {all_or_nothing: false}))
.then(console.log)
// { headers: { ... },
// data:
// [ { ok: true,
// id: '5413cf41edaedaec6b63aee93db86e1f',
// rev: '1-d7f23e94e65978ea9252d753fe5dc3f6' },
// { ok: true,
// id: '5413cf41edaedaec6b63aee93db877cc',
// rev: '1-646cd5f84634632f42fee2bdf4ff753a' },
// { ok: true,
// id: '5413cf41edaedaec6b63aee93db87c3d',
// rev: '1-9cc8cf1e775b686ca337f69cd39ff772' } ],
// status: 201,
// message: 'Created – Document(s) have been created or updated',
// duration: 74 }
.then(() => db.findDocuments(dbName, {
selector: {
$or: [{ name: 'Tick' }, {name: 'Track'}]
},
fields: ['_id', 'name']
}))
.catch(response => {
if (response.status === 400) {
return 'Warning: findDocument() requires CouchDB >= 2.0.0. Error ignored'
} else {
return Promise.reject(response)
}
})
.then(console.log)
// { headers: { ... },
// data:
// { warning: 'no matching index found, create an index to optimize query time',
// docs: [ [Object], [Object] ] },
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 14 }
.then(() => db.deleteDocument(dbName, 'doc2', '2-ee5ea9ac0bb1bec913a9b5e7bc11b113'))
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: 'doc2',
// rev: '3-ec0a86a95c5e98a5cd52c29b79b66372' },
// status: 200,
// message: 'OK - Document successfully removed',
// duration: 39 }
.then(() => db.getUuids(3))
.then(console.log)
// { headers: { ... },
// data:
// { uuids:
// [ 'daae0752c6909d7ca4cd833f46014c47',
// 'daae0752c6909d7ca4cd833f460150c5',
// 'daae0752c6909d7ca4cd833f460156c5' ] },
// status: 200,
// message: 'OK - Request completed successfully',
// duration: 4 }
.then(() => db.deleteDatabase(dbName))
.then(console.log)
// { headers: { ... },
// data: { ok: true },
// status: 200,
// message: 'OK - Database removed successfully',
// duration: 40 }
.then(() => db.getUrlPath('_all_dbs'))
.then(console.log)
// { headers: { ... },
// data: [ '_replicator', '_users' ],
// status: 200,
// message: 'OK',
// duration: 6 }
.then(() => db.getDocument(dbName, 'doc1'))
.catch(console.error)
// { headers: { ... },
// data: { error: 'not_found', reason: 'no_db_file' },
// status: 404,
// message: 'Not Found - Document not found',
// duration: 5 }