Comparing version 1.0.4 to 1.1.0
280
index.js
@@ -1,279 +0,1 @@ | ||
'use strict' | ||
const http = require('http') | ||
const fs = require('fs') | ||
const request = require('request') | ||
const url = require('url') | ||
const stream = require('stream') | ||
class Database { | ||
constructor() { | ||
this.config = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: 5984 | ||
} | ||
if (arguments.length == 2) { // To be removed in version 2.0.0 | ||
let options = arguments[1] | ||
this.db = arguments[0] | ||
if (options.protocol) this.config.protocol = options.protocol | ||
if (options.hostname) this.config.hostname = options.hostname | ||
if (options.port) this.config.port = options.port | ||
} | ||
if (arguments.length == 1) { | ||
if (typeof arguments[0] == 'string') this.db = arguments[0] // To be removed in version 2.0.0 | ||
else { | ||
let options = arguments[0] | ||
this.db = options.db | ||
if (options.protocol) this.config.protocol = options.protocol | ||
if (options.hostname) this.config.hostname = options.hostname | ||
if (options.port) this.config.port = options.port | ||
} | ||
} | ||
this.connect() | ||
} | ||
connect(cb) { | ||
if (!cb) cb = ()=>{} | ||
let uri = url.format(this.getConfig({ | ||
pathname: '/'+encodeURIComponent(this.db) | ||
})) | ||
request(uri, (err, res, body)=>{ | ||
if (err) cb(err) | ||
else if (res.statusCode == 200) { | ||
console.log(`Connected to database successfully at ${uri}`) | ||
cb(null) | ||
} | ||
else if (res.statusCode == 404) { | ||
Database.createDB(this.getConfig({ | ||
db: this.db | ||
}), (err)=>{ | ||
if (err) cb(err) | ||
else { | ||
console.log(`Connected to database successfully at ${uri}`) | ||
cb(null) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
static createDB(options, cb) { | ||
if (!cb) cb = ()=>{} | ||
let config = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: 5984, | ||
} | ||
for (var attr in options) { | ||
config[attr] = options[attr] | ||
} | ||
let uri = url.format({ | ||
protocol : config.protocol, | ||
hostname : config.hostname, | ||
port : config.port, | ||
pathname : '/'+encodeURIComponent(config.db) | ||
}) | ||
request.put(uri, (err, res, body)=>{ | ||
if (err) cb(err) | ||
else { | ||
let data = JSON.parse(body) | ||
if (data.error) cb(new Error(data.reason)) | ||
else cb(null, data) | ||
} | ||
}) | ||
} | ||
static deleteDB(options, cb) { | ||
if (!cb) cb = ()=>{} | ||
let config = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: 5984, | ||
} | ||
for (var attr in options) { | ||
config[attr] = options[attr] | ||
} | ||
let uri = url.format({ | ||
protocol : config.protocol, | ||
hostname : config.hostname, | ||
port : config.port, | ||
pathname : '/'+encodeURIComponent(config.db) | ||
}) | ||
request.delete(uri, (err, res, body)=>{ | ||
if (err) cb(err) | ||
else { | ||
let data = JSON.parse(body) | ||
if (data.error) cb(new Error(data.reason)) | ||
else cb(null, data) | ||
} | ||
}) | ||
} | ||
getConfig(options) { | ||
if (!options) options = {} | ||
let config = {} | ||
for (var attr in this.config) { | ||
config[attr] = this.config[attr] | ||
} | ||
for (var attr in options) { | ||
config[attr] = options[attr] | ||
} | ||
return config | ||
} | ||
static getNewId(options, cb) { | ||
if (arguments.length == 1) { | ||
cb = options | ||
options = {} | ||
} | ||
let config = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: 5984, | ||
count: 1 | ||
} | ||
for (var attr in options) { | ||
config[attr] = options[attr] | ||
} | ||
let uri = url.format({ | ||
protocol : config.protocol, | ||
hostname : config.hostname, | ||
port : config.port, | ||
pathname : '/_uuids', | ||
query : { | ||
count : config.count | ||
} | ||
}) | ||
request(uri, (err, res, body)=>{ | ||
if (err) cb(err) | ||
else { | ||
let data = JSON.parse(body) | ||
if (data.error) cb(new Error(data.reason)) | ||
else cb(null, data.uuids) | ||
} | ||
}) | ||
} | ||
createDoc(id, doc, cb) { | ||
if (!cb) cb = ()=>{} | ||
let uri = url.format(this.getConfig({ | ||
pathname : '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id) | ||
})) | ||
const str = new stream.Readable() | ||
str.push(JSON.stringify(doc)) | ||
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(new Error(data.reason)) | ||
else cb(null, data) | ||
} | ||
})) | ||
} | ||
getDoc(id, cb) { | ||
if (!cb) cb = ()=>{} | ||
let uri = url.format(this.getConfig({ | ||
pathname : '/'+encodeURIComponent(this.db)+'/'+encodeURIComponent(id) | ||
})) | ||
request(uri, (err, res, body)=>{ | ||
if (err) cb(err) | ||
else { | ||
let data = JSON.parse(body) | ||
if (data.error) cb(new Error(data.reason)) | ||
else cb(null, data) | ||
} | ||
}) | ||
} | ||
getAllDocs(cb) { | ||
if (!cb) cb = ()=>{} | ||
let uri = url.format(this.getConfig({ | ||
pathname: '/'+encodeURIComponent(this.db)+'/_all_docs' | ||
})) | ||
request(uri, (err, res, body)=>{ | ||
if (err) cb(err) | ||
else { | ||
let data = JSON.parse(body) | ||
if (data.error) cb(new Error(data.reason)) | ||
else cb(null, data) | ||
} | ||
}) | ||
} | ||
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(this.getConfig({ | ||
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(new Error(data.reason)) | ||
else cb(null, data) | ||
} | ||
})) | ||
} | ||
}) | ||
} | ||
deleteDoc(id, cb) { | ||
if (!cb) cb = ()=>{} | ||
this.getDoc(id, (err, data)=>{ | ||
if (err) cb(err) | ||
else { | ||
let uri = url.format(this.getConfig({ | ||
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(new Error(data.reason)) | ||
else cb(null, data) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
attachFileToDoc(id, rev, file, cb) { | ||
if (!cb) cb = ()=>{} | ||
let fstream = fs.createReadStream(file.path) | ||
let uri = url.format(this.getConfig({ | ||
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(new Error(data.reason)) | ||
else cb(null, data) | ||
} | ||
})) | ||
} | ||
} | ||
module.exports = Database | ||
module.exports = require('./lib/index') |
{ | ||
"name": "couchdbjs", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "A node client for couchdb.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -216,1 +216,13 @@ # couchdbjs | ||
Here the file gets uploaded to <http://localhost:5984/db_name/id_doc/pic.jpg>. | ||
### Delete attachment from document | ||
```js | ||
db.deleteAttachment(id, rev, attachmentName, cb); | ||
``` | ||
Example | ||
```js | ||
db.attachFileToDoc('id_doc', '4-1579', 'pic.jpg', function(err, data) { | ||
if (err) console.error(err); | ||
else console.log(data); | ||
}); | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
15503
7
298
0
228
2