Comparing version 0.2.0 to 1.0.0
155
index.js
@@ -205,82 +205,85 @@ 'use strict' | ||
updateDoc(id, doc, cb) { | ||
if (!cb) cb = ()=>{}; | ||
this.getDoc(id, (err, data)=>{ | ||
if (err) cb(err); | ||
else { | ||
for (var attr in doc) { | ||
data[attr] = doc[attr]; | ||
} | ||
let uri = url.format({ | ||
protocol: this.config.protocol, | ||
hostname: this.config.hostname, | ||
port: this.config.port, | ||
pathname: '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id) | ||
}); | ||
let str = new stream.Readable(); | ||
str.push(JSON.stringify(data)); | ||
str.push(null); | ||
str.pipe(request.put(uri, (err, res, body)=>{ | ||
if (err) cb(err); | ||
else { | ||
let data = JSON.parse(body); | ||
if (data.error) cb(data); | ||
else cb(null, data); | ||
} | ||
})); | ||
} | ||
}); | ||
} | ||
// | ||
// updateDoc(id, doc, callback) { | ||
// if (!callback) callback = console.log; | ||
// let req = http.request({ | ||
// hostname: this.config.hostname, | ||
// port: this.config.port, | ||
// method: 'PUT', | ||
// path: '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id) | ||
// }, (res)=>{ | ||
// let data = ''; | ||
// res.on('data', (chunk)=>{ data+=chunk; }); | ||
// res.on('end', ()=>{ | ||
// data = JSON.parse(data); | ||
// if(!data.error) { | ||
// callback(false, data); | ||
// } else callback(data); | ||
// }); | ||
// }); | ||
// req.write(JSON.stringify(doc)); | ||
// req.end(); | ||
// } | ||
// | ||
// attachFileInDoc(id, rev, file, callback) { | ||
// if(!callback) callback = console.log; | ||
// var fstream = fs.createReadStream(file.path); | ||
// var path = '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id)+'/'+file.name; | ||
// var u = url.format({ | ||
// protocol: this.config.protocol, | ||
// hostname: this.config.hostname, | ||
// port: this.config.port, | ||
// pathname: path, | ||
// query: {rev: rev} | ||
// }); | ||
// fstream.pipe(request.put({ | ||
// url: u, | ||
// headers: { | ||
// 'Content-type': file.mimetype | ||
// } | ||
// }, callback)); | ||
// } | ||
// | ||
// deleteDoc(id, callback) { | ||
// if (!callback) callback = console.log; | ||
// http.request({ | ||
// hostname: this.config.hostname, | ||
// port: this.config.port, | ||
// method: 'GET', | ||
// path: '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id) | ||
// }, (res)=>{ | ||
// let data = ''; | ||
// res.on('data', (chunk)=>{ data+=chunk; }); | ||
// res.on('end', ()=>{ | ||
// data = JSON.parse(data); | ||
// if(!data.error) { | ||
// http.request({ | ||
// hostname: this.config.hostname, | ||
// port: this.config.port, | ||
// method: 'DELETE', | ||
// path: '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id)+'?rev='+data["_rev"] | ||
// }, (res)=>{ | ||
// let data = ''; | ||
// res.on('data', (chunk)=>{ data+=chunk; }); | ||
// res.on('end', ()=>{ | ||
// data = JSON.parse(data); | ||
// if(!data.error) { | ||
// callback(false); | ||
// } else callback(data); | ||
// }); | ||
// }).end(); | ||
// } else callback(data); | ||
// }); | ||
// }).end(); | ||
// } | ||
// | ||
// | ||
deleteDoc(id, cb) { | ||
if (!cb) cb = ()=>{}; | ||
this.getDoc(id, (err, data)=>{ | ||
if (err) cb(err); | ||
else { | ||
let uri = url.format({ | ||
protocol: this.config.protocol, | ||
hostname: this.config.hostname, | ||
port: this.config.port, | ||
pathname: '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id), | ||
query: { | ||
rev: data['_rev'] | ||
} | ||
}); | ||
request.delete(uri, (err, res, body)=>{ | ||
if (err) cb(err); | ||
else { | ||
let data = JSON.parse(body); | ||
if (data.error) cb(data); | ||
else cb(null, data); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
attachFileToDoc(id, rev, file, cb) { | ||
if (!cb) cb = ()=>{}; | ||
let fstream = fs.createReadStream(file.path); | ||
let uri = url.format({ | ||
protocol: this.config.protocol, | ||
hostname: this.config.hostname, | ||
port: this.config.port, | ||
pathname: '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id)+'/'+file.name, | ||
query: { | ||
rev: rev | ||
} | ||
}); | ||
fstream.pipe(request.put({ | ||
url: uri, | ||
headers: { | ||
'Content-type': file.mimetype | ||
} | ||
}, (err, res, body)=>{ | ||
if (err) cb(err); | ||
else { | ||
let data = JSON.parse(body); | ||
if (data.error) cb(data); | ||
else cb(null, data); | ||
} | ||
})); | ||
} | ||
}; | ||
module.exports = DBHandler; |
{ | ||
"name": "couchdbjs", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "A node client for couchdb.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -133,1 +133,74 @@ # couchdbjs | ||
``` | ||
### Update a document | ||
```js | ||
db.updateDoc(id, doc, cb); | ||
``` | ||
`id`, `doc`, `cb` are id of document to be updated, document object with filed to be updated and callback function with parameters `err` and `data` respectively. | ||
Example | ||
Suppose this a document already present | ||
```js | ||
{ | ||
'id': 'id_doc', | ||
'_rev': '1-1357', | ||
'a': 1, | ||
'b': 'asd' | ||
} | ||
``` | ||
If the following function is used to update the document | ||
```js | ||
db.upadteDoc('id_doc', {b: 'cs', c: 5}, function(err, data) {}); | ||
``` | ||
then the final document will be | ||
```js | ||
{ | ||
'id': 'id_doc', | ||
'_rev': '2-2468', | ||
'a': 1, | ||
'b': 'cs', | ||
'c': 5 | ||
} | ||
``` | ||
After that if the following function is used to update the document | ||
```js | ||
db.upadteDoc('id_doc', {b: 'cse', a: undefined}, function(err, data) {}); | ||
``` | ||
then the final document will be | ||
```js | ||
{ | ||
'id': 'id_doc', | ||
'_rev': '3-3579', | ||
'b': 'cse', | ||
'c': 5 | ||
} | ||
``` | ||
To update whole document by overwriting the existing one use | ||
```js | ||
db.createDoc(id, doc, cb); | ||
``` | ||
with `_rev` field in the `doc` document. | ||
### Delete a document | ||
```js | ||
db.deleteDoc(id, function(err, data) { | ||
if (err) console.error(err); | ||
else console.log(data); | ||
}); | ||
``` | ||
### Attach files to a document | ||
```js | ||
db.attachFileToDoc(id, rev, file, cb); | ||
``` | ||
Example | ||
```js | ||
db.attachFileToDoc('id_doc', '3-3579', { | ||
name: 'pic.jpg', | ||
path: '/home/user/image.jpg' | ||
mimetype: 'image/jpeg' | ||
}, function(err, data) { | ||
if (err) console.error(err); | ||
else console.log(data); | ||
}); | ||
``` | ||
Here the file gets uploaded to <http://localhost:5984/db_name/id_doc/pic.jpg>. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15420
275
1
206
1