mirror of
https://github.com/donl/slouch.git
synced 2026-05-25 22:07:24 -06:00
* doc(readme): clean up reasons * doc(motto) * test(db-and-doc): more coverage * test(create-or-update-ignore-conflict) * test(upsert) * test(ignore-missing) * test(post-and-ignore-conflict) * test(get-merge-put) * refactor(all): rename post and put * test(get-merge-create-or-update) * test(get-merge-update-ignore-conflict) * test(get-merge-upsert) * test(get-modify-upsert) * refactor(doc): redundant code * test(destroy-ignore-conflict) * test(get-and-destroy) * test(mark-as-destroyed) * test(set-destroyed) * refactor(attachment) * test(doc): 100% coverage * test(attachment): create with base 64 * test(attachment): clean up binary code * test(attachment): get * test(attachment): destroy * test(system): is couchdb 1 * test(system): get * test(system): reset * test(updates) * test(updates) * test(all): unique DB names * test(system): reactivate tests * test(user): add role * test(user): downsert role * feat(stream-iterator): indefinite * test(user): 100% coverage * test(request-class) * test(request-class): 100% coverage * test(config) * test(config): more coverage * test(config): more coverage * test(config): 100% coverage * test(all): 100% coverage * refactor(beautify) * test(coverage): enforce 100% * test(system): fix race condition * test(user): shortcut for browser * test(updates): test continuous stream in phantomjs * test(updates): test continuous stream in phantomjs * test(continuous): mock for phantomjs * test(system): abort iterators * test(system): fake abort
124 lines
3.2 KiB
JavaScript
124 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
var promisedRequest = require('./request'),
|
|
PersistentStreamIterator = require('quelle').PersistentStreamIterator,
|
|
request = require('request');
|
|
|
|
var DB = function (slouch) {
|
|
this._slouch = slouch;
|
|
this._request = request;
|
|
};
|
|
|
|
DB.prototype._create = function (dbName) {
|
|
return promisedRequest.request({
|
|
uri: this._slouch._url + '/' + dbName,
|
|
method: 'PUT'
|
|
});
|
|
};
|
|
|
|
DB.prototype.create = function (dbName) {
|
|
var self = this;
|
|
|
|
return self._create(dbName).catch(function (err) {
|
|
// During heavy traffic, CouchDB does this strange thing where it will return an error even when
|
|
// the DB has been created. So, we check to see if the DB exists and then only throw the error
|
|
// if the DB does not exist.
|
|
return self.exists(dbName).then(function (exists) {
|
|
if (!exists) {
|
|
throw err;
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
DB.prototype.destroy = function (dbName) {
|
|
return promisedRequest.request({
|
|
uri: this._slouch._url + '/' + dbName,
|
|
method: 'DELETE'
|
|
});
|
|
};
|
|
|
|
DB.prototype.get = function (dbName) {
|
|
return promisedRequest.request({
|
|
uri: this._slouch._url + '/' + dbName,
|
|
method: 'GET'
|
|
}, true);
|
|
};
|
|
|
|
DB.prototype.exists = function (dbName) {
|
|
return promisedRequest.request({
|
|
uri: this._slouch._url + '/' + dbName,
|
|
method: 'GET'
|
|
}).then(function () {
|
|
return true;
|
|
}).catch(function () {
|
|
return false;
|
|
});
|
|
};
|
|
|
|
// Use a JSONStream so that we don't have to load a large JSON structure into memory
|
|
DB.prototype.changes = function (dbName, params) {
|
|
|
|
var indefinite = false,
|
|
jsonStreamParseStr = null;
|
|
|
|
if (params && params.feed === 'continuous') {
|
|
indefinite = true;
|
|
jsonStreamParseStr = undefined;
|
|
} else {
|
|
jsonStreamParseStr = 'results.*';
|
|
}
|
|
|
|
return new PersistentStreamIterator({
|
|
url: this._slouch._url + '/' + dbName + '/_changes',
|
|
method: 'GET',
|
|
qs: params
|
|
}, jsonStreamParseStr, indefinite, this._request);
|
|
|
|
};
|
|
|
|
DB.prototype.view = function (dbName, viewDocId, view, params) {
|
|
return new PersistentStreamIterator({
|
|
url: this._slouch._url + '/' + dbName + '/' + viewDocId + '/_view/' + view,
|
|
qs: params
|
|
}, 'rows.*');
|
|
};
|
|
|
|
DB.prototype.viewArray = function (dbName, viewDocId, view, params) {
|
|
return promisedRequest.request({
|
|
url: this._slouch._url + '/' + dbName + '/' + viewDocId + '/_view/' + view,
|
|
qs: params
|
|
}, true);
|
|
};
|
|
|
|
// Use a JSONStream so that we don't have to load a large JSON structure into memory
|
|
DB.prototype.all = function () {
|
|
return new PersistentStreamIterator({
|
|
url: this._slouch._url + '/_all_dbs'
|
|
}, '*');
|
|
};
|
|
|
|
DB.prototype.replicate = function (params) {
|
|
return promisedRequest.request({
|
|
url: this._slouch._url + '/_replicate',
|
|
method: 'POST',
|
|
json: params
|
|
});
|
|
};
|
|
|
|
// TODO: support fromDBName and toDbName also being URLs
|
|
DB.prototype.copy = function (fromDBName, toDBName) {
|
|
var self = this;
|
|
return self.create(toDBName).then(function () {
|
|
return self._slouch.security.get(fromDBName);
|
|
}).then(function (security) {
|
|
return self._slouch.security.set(toDBName, security);
|
|
}).then(function () {
|
|
return self.replicate({
|
|
source: self._slouch._url + '/' + fromDBName,
|
|
target: self._slouch._url + '/' + toDBName
|
|
});
|
|
});
|
|
};
|
|
|
|
module.exports = DB;
|