pouchdb-design
Version:
Design doc helpers for PouchDB.
254 lines (219 loc) • 6.49 kB
JavaScript
'use strict';
var lodash = require('lodash');
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function validateName(name, type) {
if (typeof name !== "string" || name === "") {
throw new Error("Expecting non-empty string for " + type + " name.");
}
}
function normalizeValue(value) {
if (value != null) {
var type = typeof value === "undefined" ? "undefined" : _typeof(value);
if (type === "object") {
if (!lodash.isPlainObject(value)) value = value.toJSON();
} else if (type === "function") {
var fn = value;
value = fn.toString();
if (fn.name) {
var args = [];
for (var i = 0; i < fn.length; i++) {
args.push("_a" + i);
}
value = "function(" + args.join(", ") + ") {\nreturn (" + value + ").apply(this, arguments);\n}";
}
} else if (!~["string", "number", "boolean"].indexOf(type)) {
value = value.toString();
}
}
return value;
}
var Design = function () {
function Design(db, doc) {
_classCallCheck(this, Design);
this.db = db;
if (typeof doc === "string") doc = { _id: doc };
this.doc = lodash.assign({}, doc);
this.id = this.doc._id;
}
_createClass(Design, [{
key: "clone",
value: function clone(db) {
return new Design(db || this.db, this.doc);
}
}, {
key: "get",
value: function get$$1(path) {
return lodash.get(this.doc, path);
}
}, {
key: "set",
value: function set$$1(path, value) {
lodash.set(this.doc, path, normalizeValue(value));
return this;
}
}, {
key: "unset",
value: function unset$$1(path) {
lodash.unset(this.doc, path);
return this;
}
}, {
key: "lib",
value: function lib(name, fn) {
var src = "module.exports=";
if (typeof fn === "function") {
if (fn.name) src += fn.name + ";\n";
src += fn.toString();
} else {
src += JSON.stringify(fn);
}
return this.set(name, src);
}
}, {
key: "view",
value: function view(name, map, reduce) {
var _this = this;
if ((typeof name === "undefined" ? "undefined" : _typeof(name)) === "object" && !map) {
lodash.forEach(name, function (v, n) {
return _this.view(n, v);
});
return this;
}
validateName(name, "view");
if ((typeof map === "undefined" ? "undefined" : _typeof(map)) === "object" && map != null && reduce == null) {
var _ref = [map.reduce, map.map];
reduce = _ref[0];
map = _ref[1];
}
this.set(["views", name, "map"], map);
this.set(["views", name, "reduce"], reduce);
return this;
}
}, {
key: "show",
value: function show(name, fn) {
var _this2 = this;
if ((typeof name === "undefined" ? "undefined" : _typeof(name)) === "object" && !fn) {
lodash.forEach(name, function (v, n) {
return _this2.show(n, v);
});
return this;
}
validateName(name, "show");
return this.set(["shows", name], fn);
}
}, {
key: "list",
value: function list(name, fn) {
var _this3 = this;
if ((typeof name === "undefined" ? "undefined" : _typeof(name)) === "object" && !fn) {
lodash.forEach(name, function (v, n) {
return _this3.list(n, v);
});
return this;
}
validateName(name, "list");
return this.set(["lists", name], fn);
}
}, {
key: "update",
value: function update(name, fn) {
var _this4 = this;
if ((typeof name === "undefined" ? "undefined" : _typeof(name)) === "object" && !fn) {
lodash.forEach(name, function (v, n) {
return _this4.update(n, v);
});
return this;
}
validateName(name, "update");
return this.set(["updates", name], fn);
}
}, {
key: "filter",
value: function filter(name, fn) {
var _this5 = this;
if ((typeof name === "undefined" ? "undefined" : _typeof(name)) === "object" && !fn) {
lodash.forEach(name, function (v, n) {
return _this5.filter(n, v);
});
return this;
}
validateName(name, "filter");
return this.set(["filters", name], fn);
}
}, {
key: "validate",
value: function validate(fn) {
return this.set("validate_doc_update", fn);
}
}, {
key: "fetch",
value: function fetch() {
var _this6 = this;
return this._fetch().then(function (doc) {
_this6.doc = doc;
return _this6;
});
}
}, {
key: "_fetch",
value: function _fetch() {
var id = this.id;
if (!id) throw new Error("Design doc is missing an id.");
return this.db.get(id).catch(function (e) {
if (e.status !== 404) throw e;
return { _id: id };
});
}
}, {
key: "save",
value: function save() {
var _this7 = this;
var upsert = function upsert() {
var doc = _this7.toJSON();
return _this7._fetch().then(function (d) {
if (d) doc._rev = d._rev;
if (lodash.isEqual(doc, d)) return;
return _this7.db.put(doc).catch(function (e) {
if (e.status === 409) return upsert();
throw e;
});
});
};
return upsert();
}
}, {
key: "toJSON",
value: function toJSON() {
return lodash.assign({ language: "javascript" }, this.doc);
}
}, {
key: "id",
get: function get$$1() {
return this.doc._id;
},
set: function set$$1(id) {
if (typeof id === "string" && id !== "") {
if (!/^_design\//.test(id)) id = "_design/" + id;
} else {
id = null;
}
return this.doc._id = id;
}
}]);
return Design;
}();
function plugin(PouchDB) {
PouchDB.Design = Design;
PouchDB.design = function (doc) {
return new Design(null, doc);
};
lodash.assign(PouchDB.prototype, plugin);
}
plugin.Design = Design;
plugin.design = function (doc) {
return new Design(this, doc);
};
module.exports = plugin;