js-data-rethinkdb
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -0,1 +1,11 @@ | ||
##### 1.2.0 - 26 March 2015 | ||
###### Backwards compatible API changes | ||
- #3 - Add support for loading relations | ||
- #4 - Add support for auto-creating secondary indexes based on the relation definitions | ||
###### Backwards compatible bug fixes | ||
- #5 - Should not be saving relations (duplicating data) | ||
- #6 - Need to use removeCircular | ||
##### 1.1.2 - 07 March 2015 | ||
@@ -2,0 +12,0 @@ |
# Contributing Guide | ||
First, feel free to contact me with questions. [Mailing List](https://groups.io/org/groupsio/jsdata). [Issues](https://github.com/js-data/js-data-rethinkdb/issues). | ||
First, support is handled via the [Mailing List](https://groups.io/org/groupsio/jsdata). Ask your questions there. | ||
When submitting issues on GitHub, please include as much detail as possible to make debugging quick and easy. | ||
- good - Your versions of js-data, js-data-rethinkdb, etc., relevant console logs/error, code examples that revealed the issue | ||
- better - A [plnkr](http://plnkr.co/), [fiddle](http://jsfiddle.net/), or [bin](http://jsbin.com/?html,output) that demonstrates the issue | ||
- best - A Pull Request that fixes the issue, including test coverage for the issue and the fix | ||
[Github Issues](https://github.com/js-data/js-data-rethinkdb/issues). | ||
#### Pull Requests | ||
1. Contribute to the issue that is the reason you'll be developing in the first place | ||
@@ -6,0 +16,0 @@ 1. Fork js-data-rethinkdb |
@@ -48,2 +48,4 @@ module.exports = | ||
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; }; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
@@ -53,17 +55,27 @@ | ||
var rethinkdbdash = __webpack_require__(1); | ||
var contains = __webpack_require__(2); | ||
var forOwn = __webpack_require__(3); | ||
var keys = __webpack_require__(4); | ||
var deepMixIn = __webpack_require__(5); | ||
var forEach = __webpack_require__(6); | ||
var isObject = __webpack_require__(7); | ||
var isArray = __webpack_require__(8); | ||
var isEmpty = __webpack_require__(9); | ||
var isString = __webpack_require__(10); | ||
var upperCase = __webpack_require__(11); | ||
var underscore = __webpack_require__(12); | ||
var JSData = __webpack_require__(13); | ||
var P = JSData.DSUtils.Promise; | ||
var rethinkdbdash = _interopRequire(__webpack_require__(1)); | ||
var JSData = _interopRequire(__webpack_require__(2)); | ||
var DSUtils = JSData.DSUtils; | ||
var P = DSUtils.Promise; | ||
var contains = DSUtils.contains; | ||
var forOwn = DSUtils.forOwn; | ||
var deepMixIn = DSUtils.deepMixIn; | ||
var forEach = DSUtils.forEach; | ||
var isObject = DSUtils.isObject; | ||
var isArray = DSUtils.isArray; | ||
var isString = DSUtils.isString; | ||
var removeCircular = DSUtils.removeCircular; | ||
var keys = _interopRequire(__webpack_require__(3)); | ||
var isEmpty = _interopRequire(__webpack_require__(4)); | ||
var upperCase = _interopRequire(__webpack_require__(5)); | ||
var underscore = _interopRequire(__webpack_require__(6)); | ||
var omit = _interopRequire(__webpack_require__(7)); | ||
var Defaults = function Defaults() { | ||
@@ -83,106 +95,2 @@ _classCallCheck(this, Defaults); | ||
function filterQuery(resourceConfig, params, options) { | ||
var r = this.r; | ||
params = params || {}; | ||
options = options || {}; | ||
params.where = params.where || {}; | ||
params.orderBy = params.orderBy || params.sort; | ||
params.skip = params.skip || params.offset; | ||
forEach(keys(params), function (k) { | ||
var v = params[k]; | ||
if (!contains(reserved, k)) { | ||
if (isObject(v)) { | ||
params.where[k] = v; | ||
} else { | ||
params.where[k] = { | ||
"==": v | ||
}; | ||
} | ||
delete params[k]; | ||
} | ||
}); | ||
var query = r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)); | ||
if (!isEmpty(params.where)) { | ||
query = query.filter(function (row) { | ||
var subQuery = undefined; | ||
forOwn(params.where, function (criteria, field) { | ||
if (!isObject(criteria)) { | ||
params.where[field] = { | ||
"==": criteria | ||
}; | ||
} | ||
forOwn(criteria, function (v, op) { | ||
if (op === "==" || op === "===") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).eq(v)) : row(field)["default"](null).eq(v); | ||
} else if (op === "!=" || op === "!==") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).ne(v)) : row(field)["default"](null).ne(v); | ||
} else if (op === ">") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).gt(v)) : row(field)["default"](null).gt(v); | ||
} else if (op === ">=") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).ge(v)) : row(field)["default"](null).ge(v); | ||
} else if (op === "<") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).lt(v)) : row(field)["default"](null).lt(v); | ||
} else if (op === "<=") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).le(v)) : row(field)["default"](null).le(v); | ||
} else if (op === "isectEmpty") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().eq(0)) : row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().eq(0); | ||
} else if (op === "isectNotEmpty") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().ne(0)) : row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().ne(0); | ||
} else if (op === "in") { | ||
subQuery = subQuery ? subQuery.and(r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null))) : r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)); | ||
} else if (op === "notIn") { | ||
subQuery = subQuery ? subQuery.and(r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)).not()) : r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)).not(); | ||
} else if (op === "|==" || op === "|===") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).eq(v)) : row(field)["default"](null).eq(v); | ||
} else if (op === "|!=" || op === "|!==") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).ne(v)) : row(field)["default"](null).ne(v); | ||
} else if (op === "|>") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).gt(v)) : row(field)["default"](null).gt(v); | ||
} else if (op === "|>=") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).ge(v)) : row(field)["default"](null).ge(v); | ||
} else if (op === "|<") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).lt(v)) : row(field)["default"](null).lt(v); | ||
} else if (op === "|<=") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).le(v)) : row(field)["default"](null).le(v); | ||
} else if (op === "|isectEmpty") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().eq(0)) : row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().eq(0); | ||
} else if (op === "|isectNotEmpty") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().ne(0)) : row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().ne(0); | ||
} else if (op === "|in") { | ||
subQuery = subQuery ? subQuery.or(r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null))) : r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)); | ||
} else if (op === "|notIn") { | ||
subQuery = subQuery ? subQuery.or(r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)).not()) : r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)).not(); | ||
} | ||
}); | ||
}); | ||
return subQuery; | ||
}); | ||
} | ||
if (params.orderBy) { | ||
if (isString(params.orderBy)) { | ||
params.orderBy = [[params.orderBy, "asc"]]; | ||
} | ||
for (var i = 0; i < params.orderBy.length; i++) { | ||
if (isString(params.orderBy[i])) { | ||
params.orderBy[i] = [params.orderBy[i], "asc"]; | ||
} | ||
query = upperCase(params.orderBy[i][1]) === "DESC" ? query.orderBy(r.desc(params.orderBy[i][0])) : query.orderBy(params.orderBy[i][0]); | ||
} | ||
} | ||
if (params.skip) { | ||
query = query.skip(params.skip); | ||
} | ||
if (params.limit) { | ||
query = query.limit(params.limit); | ||
} | ||
return query; | ||
} | ||
var DSRethinkDBAdapter = (function () { | ||
@@ -202,11 +110,119 @@ function DSRethinkDBAdapter(options) { | ||
_createClass(DSRethinkDBAdapter, { | ||
selectTable: { | ||
value: function selectTable(resourceConfig, options) { | ||
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)); | ||
} | ||
}, | ||
filterSequence: { | ||
value: function filterSequence(sequence, params) { | ||
var r = this.r; | ||
params = params || {}; | ||
params.where = params.where || {}; | ||
params.orderBy = params.orderBy || params.sort; | ||
params.skip = params.skip || params.offset; | ||
forEach(keys(params), function (k) { | ||
var v = params[k]; | ||
if (!contains(reserved, k)) { | ||
if (isObject(v)) { | ||
params.where[k] = v; | ||
} else { | ||
params.where[k] = { | ||
"==": v | ||
}; | ||
} | ||
delete params[k]; | ||
} | ||
}); | ||
var query = sequence; | ||
if (!isEmpty(params.where)) { | ||
query = query.filter(function (row) { | ||
var subQuery = undefined; | ||
forOwn(params.where, function (criteria, field) { | ||
if (!isObject(criteria)) { | ||
params.where[field] = { | ||
"==": criteria | ||
}; | ||
} | ||
forOwn(criteria, function (v, op) { | ||
if (op === "==" || op === "===") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).eq(v)) : row(field)["default"](null).eq(v); | ||
} else if (op === "!=" || op === "!==") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).ne(v)) : row(field)["default"](null).ne(v); | ||
} else if (op === ">") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).gt(v)) : row(field)["default"](null).gt(v); | ||
} else if (op === ">=") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).ge(v)) : row(field)["default"](null).ge(v); | ||
} else if (op === "<") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).lt(v)) : row(field)["default"](null).lt(v); | ||
} else if (op === "<=") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"](null).le(v)) : row(field)["default"](null).le(v); | ||
} else if (op === "isectEmpty") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().eq(0)) : row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().eq(0); | ||
} else if (op === "isectNotEmpty") { | ||
subQuery = subQuery ? subQuery.and(row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().ne(0)) : row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().ne(0); | ||
} else if (op === "in") { | ||
subQuery = subQuery ? subQuery.and(r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null))) : r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)); | ||
} else if (op === "notIn") { | ||
subQuery = subQuery ? subQuery.and(r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)).not()) : r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)).not(); | ||
} else if (op === "|==" || op === "|===") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).eq(v)) : row(field)["default"](null).eq(v); | ||
} else if (op === "|!=" || op === "|!==") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).ne(v)) : row(field)["default"](null).ne(v); | ||
} else if (op === "|>") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).gt(v)) : row(field)["default"](null).gt(v); | ||
} else if (op === "|>=") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).ge(v)) : row(field)["default"](null).ge(v); | ||
} else if (op === "|<") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).lt(v)) : row(field)["default"](null).lt(v); | ||
} else if (op === "|<=") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"](null).le(v)) : row(field)["default"](null).le(v); | ||
} else if (op === "|isectEmpty") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().eq(0)) : row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().eq(0); | ||
} else if (op === "|isectNotEmpty") { | ||
subQuery = subQuery ? subQuery.or(row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().ne(0)) : row(field)["default"]([]).setIntersection(r.expr(v)["default"]([])).count().ne(0); | ||
} else if (op === "|in") { | ||
subQuery = subQuery ? subQuery.or(r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null))) : r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)); | ||
} else if (op === "|notIn") { | ||
subQuery = subQuery ? subQuery.or(r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)).not()) : r.expr(v)["default"](r.expr([])).contains(row(field)["default"](null)).not(); | ||
} | ||
}); | ||
}); | ||
return subQuery; | ||
}); | ||
} | ||
if (params.orderBy) { | ||
if (isString(params.orderBy)) { | ||
params.orderBy = [[params.orderBy, "asc"]]; | ||
} | ||
for (var i = 0; i < params.orderBy.length; i++) { | ||
if (isString(params.orderBy[i])) { | ||
params.orderBy[i] = [params.orderBy[i], "asc"]; | ||
} | ||
query = upperCase(params.orderBy[i][1]) === "DESC" ? query.orderBy(r.desc(params.orderBy[i][0])) : query.orderBy(params.orderBy[i][0]); | ||
} | ||
} | ||
if (params.skip) { | ||
query = query.skip(params.skip); | ||
} | ||
if (params.limit) { | ||
query = query.limit(params.limit); | ||
} | ||
return query; | ||
} | ||
}, | ||
waitForDb: { | ||
value: function waitForDb(options) { | ||
var _this = this; | ||
options = options || {}; | ||
var db = options.db || _this.defaults.db; | ||
if (!_this.databases[db]) { | ||
_this.databases[db] = _this.r.branch(_this.r.dbList().contains(db), true, _this.r.dbCreate(db)).run(); | ||
var db = options.db || this.defaults.db; | ||
if (!this.databases[db]) { | ||
this.databases[db] = this.r.branch(this.r.dbList().contains(db), true, this.r.dbCreate(db)).run(); | ||
} | ||
return _this.databases[db]; | ||
return this.databases[db]; | ||
} | ||
@@ -217,5 +233,6 @@ }, | ||
var _this = this; | ||
options = options || {}; | ||
var db = options.db || _this.defaults.db; | ||
return _this.waitForDb(options).then(function () { | ||
var db = options.db || this.defaults.db; | ||
return this.waitForDb(options).then(function () { | ||
_this.tables[db] = _this.tables[db] || {}; | ||
@@ -232,5 +249,6 @@ if (!_this.tables[db][table]) { | ||
var _this = this; | ||
options = options || {}; | ||
var db = options.db || _this.defaults.db; | ||
return _this.waitForDb(options).then(function () { | ||
var db = options.db || this.defaults.db; | ||
return this.waitForDb(options).then(function () { | ||
return _this.waitForTable(table, options); | ||
@@ -252,2 +270,3 @@ }).then(function () { | ||
var _this = this; | ||
var newModels = {}; | ||
@@ -258,3 +277,3 @@ var models = {}; | ||
var table = resourceConfig.table || underscore(resourceConfig.name); | ||
var tasks = [_this.waitForTable(table, options)]; | ||
var tasks = [this.waitForTable(table, options)]; | ||
forEach(resourceConfig.relationList, function (def) { | ||
@@ -265,2 +284,4 @@ var relationName = def.relation; | ||
throw new JSData.DSErrors.NER(relationName); | ||
} else if (!options["with"] || !contains(options["with"], relationName)) { | ||
return; | ||
} | ||
@@ -278,3 +299,3 @@ if (def.foreignKey) { | ||
models[relationName] = resourceConfig.getResource(relationName); | ||
if (!options["with"] || !options["with"].length || !contains(options["with"], relationName)) { | ||
if (!options["with"] || !contains(options["with"], relationName)) { | ||
return; | ||
@@ -347,6 +368,77 @@ } | ||
var _this = this; | ||
options = options || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return filterQuery.call(_this, resourceConfig, params, options).run(); | ||
var table = resourceConfig.table || underscore(resourceConfig.name); | ||
var tasks = [this.waitForTable(table, options)]; | ||
var models = {}; | ||
var merge = {}; | ||
var newModels = {}; | ||
forEach(resourceConfig.relationList, function (def) { | ||
var relationName = def.relation; | ||
var relationDef = resourceConfig.getResource(relationName); | ||
if (!relationDef) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
} else if (!options["with"] || !contains(options["with"], relationName)) { | ||
return; | ||
} | ||
if (def.foreignKey) { | ||
tasks.push(_this.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, options)); | ||
} else if (def.localKey) { | ||
tasks.push(_this.waitForIndex(resourceConfig.table || underscore(resourceConfig.name), def.localKey, options)); | ||
} | ||
}); | ||
return P.all(tasks).then(function () { | ||
var query = _this.filterSequence(_this.selectTable(resourceConfig, options), params); | ||
if (options["with"] && options["with"].length) { | ||
query = query.map(function (doc) { | ||
var id = doc(resourceConfig.idAttribute); | ||
forEach(resourceConfig.relationList, function (def) { | ||
var relationName = def.relation; | ||
models[relationName] = resourceConfig.getResource(relationName); | ||
if (!options["with"] || !contains(options["with"], relationName)) { | ||
return; | ||
} | ||
if (!models[relationName]) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
} | ||
var localKey = def.localKey; | ||
var localField = def.localField; | ||
var foreignKey = def.foreignKey; | ||
if (def.type === "belongsTo") { | ||
merge[localField] = _this.r.table(models[relationName].table || underscore(models[relationName].name)).get(doc(localKey)["default"]("")); | ||
newModels[localField] = { | ||
modelName: relationName, | ||
relation: "belongsTo" | ||
}; | ||
} else if (def.type === "hasMany") { | ||
merge[localField] = _this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, { index: foreignKey }).coerceTo("ARRAY"); | ||
newModels[localField] = { | ||
modelName: relationName, | ||
relation: "hasMany" | ||
}; | ||
} else if (def.type === "hasOne") { | ||
merge[localField] = _this.r.table(models[relationName].table || underscore(models[relationName].name)); | ||
if (localKey) { | ||
merge[localField] = merge[localField].get(localKey); | ||
} else { | ||
merge[localField] = merge[localField].getAll(id, { index: foreignKey }).coerceTo("ARRAY"); | ||
} | ||
newModels[localField] = { | ||
modelName: relationName, | ||
relation: "hasOne" | ||
}; | ||
} | ||
}); | ||
if (!isEmpty(merge)) { | ||
return doc.merge(merge); | ||
} | ||
return doc; | ||
}); | ||
} | ||
return query.run(); | ||
}); | ||
} | ||
@@ -357,4 +449,6 @@ }, | ||
var _this = this; | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).insert(attrs, { returnChanges: true }).run(); | ||
@@ -369,4 +463,6 @@ }).then(function (cursor) { | ||
var _this = this; | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run(); | ||
@@ -381,6 +477,8 @@ }).then(function (cursor) { | ||
var _this = this; | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
params = params || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return filterQuery.call(_this, resourceConfig, params, options).update(attrs, { returnChanges: true }).run(); | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return _this.filterSequence(_this.selectTable(resourceConfig, options), params).update(attrs, { returnChanges: true }).run(); | ||
}).then(function (cursor) { | ||
@@ -398,4 +496,5 @@ var items = []; | ||
var _this = this; | ||
options = options || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id)["delete"]().run(); | ||
@@ -410,6 +509,7 @@ }).then(function () { | ||
var _this = this; | ||
options = options || {}; | ||
params = params || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return filterQuery.call(_this, resourceConfig, params, options)["delete"]().run(); | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return _this.filterSequence(_this.selectTable(resourceConfig, options), params)["delete"]().run(); | ||
}).then(function () { | ||
@@ -437,3 +537,3 @@ return undefined; | ||
module.exports = require("mout/array/contains"); | ||
module.exports = require("js-data"); | ||
@@ -444,3 +544,3 @@ /***/ }, | ||
module.exports = require("mout/object/forOwn"); | ||
module.exports = require("mout/object/keys"); | ||
@@ -451,3 +551,3 @@ /***/ }, | ||
module.exports = require("mout/object/keys"); | ||
module.exports = require("mout/lang/isEmpty"); | ||
@@ -458,3 +558,3 @@ /***/ }, | ||
module.exports = require("mout/object/deepMixIn"); | ||
module.exports = require("mout/string/upperCase"); | ||
@@ -465,3 +565,3 @@ /***/ }, | ||
module.exports = require("mout/array/forEach"); | ||
module.exports = require("mout/string/underscore"); | ||
@@ -472,41 +572,5 @@ /***/ }, | ||
module.exports = require("mout/lang/isObject"); | ||
module.exports = require("mout/object/omit"); | ||
/***/ }, | ||
/* 8 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
module.exports = require("mout/lang/isArray"); | ||
/***/ }, | ||
/* 9 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
module.exports = require("mout/lang/isEmpty"); | ||
/***/ }, | ||
/* 10 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
module.exports = require("mout/lang/isString"); | ||
/***/ }, | ||
/* 11 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
module.exports = require("mout/string/upperCase"); | ||
/***/ }, | ||
/* 12 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
module.exports = require("mout/string/underscore"); | ||
/***/ }, | ||
/* 13 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
module.exports = require("js-data"); | ||
/***/ } | ||
/******/ ]); |
/* | ||
* js-data-rethinkdb | ||
* http://github.com/js-data/js-data-rethinkdb | ||
* https://github.com/js-data/js-data-rethinkdb | ||
* | ||
* Copyright (c) 2014-2015 Jason Dobry <http://www.js-data.io/js-data-rethinkdb> | ||
* Copyright (c) 2014-2015 Jason Dobry <http://www.js-data.io/docs/dsrethinkdbadapter> | ||
* Licensed under the MIT license. <https://github.com/js-data/js-data-rethinkdb/blob/master/LICENSE> | ||
@@ -21,6 +21,2 @@ */ | ||
pkg: pkg, | ||
jshint: { | ||
all: ['Gruntfile.js', 'src/**/*.js', 'test/*.js'], | ||
jshintrc: '.jshintrc' | ||
}, | ||
watch: { | ||
@@ -56,11 +52,5 @@ dist: { | ||
externals: [ | ||
'mout/array/contains', | ||
'mout/object/forOwn', | ||
'mout/object/keys', | ||
'mout/object/deepMixIn', | ||
'mout/array/forEach', | ||
'mout/lang/isObject', | ||
'mout/lang/isArray', | ||
'mout/object/omit', | ||
'mout/lang/isEmpty', | ||
'mout/lang/isString', | ||
'mout/string/upperCase', | ||
@@ -67,0 +57,0 @@ 'mout/string/underscore', |
@@ -63,3 +63,7 @@ /*global assert:true */ | ||
localField: 'posts', | ||
foreignKey: 'post' | ||
foreignKey: 'userId' | ||
}, | ||
comment: { | ||
localField: 'comments', | ||
foreignKey: 'userId' | ||
} | ||
@@ -66,0 +70,0 @@ } |
{ | ||
"name": "js-data-rethinkdb", | ||
"description": "RethinkDB adapter for js-data.", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"homepage": "http://www.js-data.io/docs/dsrethinkdbadapter", | ||
@@ -31,4 +31,4 @@ "repository": { | ||
"devDependencies": { | ||
"babel-core": "^4.7.3", | ||
"babel-loader": "^4.1.0", | ||
"babel-core": "4.7.16", | ||
"babel-loader": "4.2.0", | ||
"chai": "2.1.1", | ||
@@ -39,10 +39,10 @@ "grunt": "0.4.5", | ||
"grunt-mocha-test": "0.12.7", | ||
"grunt-webpack": "^1.0.8", | ||
"grunt-webpack": "1.0.8", | ||
"jit-grunt": "0.9.1", | ||
"jshint": "^2.6.3", | ||
"jshint-loader": "^0.8.3", | ||
"jshint": "2.6.3", | ||
"jshint-loader": "0.8.3", | ||
"sinon": "1.13.0", | ||
"time-grunt": "1.1.0", | ||
"webpack": "^1.7.2", | ||
"webpack-dev-server": "^1.7.0" | ||
"webpack": "1.7.3", | ||
"webpack-dev-server": "1.7.0" | ||
}, | ||
@@ -53,6 +53,8 @@ "scripts": { | ||
"dependencies": { | ||
"js-data": ">=1.2.0", | ||
"mout": "0.11.0", | ||
"mout": "0.11.0" | ||
}, | ||
"peerDependencies": { | ||
"js-data": ">=1.5.7", | ||
"rethinkdbdash": ">=1.15.0" | ||
} | ||
} |
<img src="https://raw.githubusercontent.com/js-data/js-data/master/js-data.png" alt="js-data logo" title="js-data" align="right" width="64" height="64" /> | ||
## js-data-rethinkdb [![NPM version](https://badge.fury.io/js/js-data-rethinkdb.png)](http://badge.fury.io/js/js-data-rethinkdb) | ||
## js-data-rethinkdb [![npm version](https://img.shields.io/npm/v/js-data-rethinkdb.svg?style=flat-square)](https://www.npmjs.org/package/js-data-rethinkdb) [![Travis CI](https://img.shields.io/travis/js-data/js-data-rethinkdb.svg?style=flat-square)](https://travis-ci.org/js-data/js-data-rethinkdb) [![npm downloads](https://img.shields.io/npm/dm/js-data-rethinkdb.svg?style=flat-square)](https://www.npmjs.org/package/js-data-rethinkdb) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/js-data/js-data-rethinkdb/blob/master/LICENSE) | ||
RethinkDB adapter for [js-data](http://www.js-data.io/). | ||
## API Documentation | ||
### API Documentation | ||
[DSRethinkDBAdapter](http://www.js-data.io/docs/dsrethinkdbadapter) | ||
## Example App | ||
### Example App | ||
[js-data-examples/server/rethinkdb](https://github.com/js-data/js-data-examples/tree/master/server/rethinkdb) | ||
## Project Status | ||
### Project Status | ||
| Branch | Master | | ||
| ------ | ------ | | ||
| NPM | [![NPM](https://nodei.co/npm/js-data-rethinkdb.png?downloads=true&stars=true)](https://nodei.co/npm/js-data-rethinkdb/) | | ||
| Build Status | [![Build Status](https://travis-ci.org/js-data/js-data-rethinkdb.png?branch=master)](https://travis-ci.org/js-data/js-data-rethinkdb) | | ||
| Code Climate | [![Code Climate](https://codeclimate.com/github/js-data/js-data-rethinkdb.png)](https://codeclimate.com/github/js-data/js-data-rethinkdb) | | ||
| Dependency Status | [![Dependency Status](https://gemnasium.com/js-data/js-data-rethinkdb.png)](https://gemnasium.com/js-data/js-data-rethinkdb) | | ||
| Coverage | [![Coverage Status](https://coveralls.io/repos/js-data/js-data-rethinkdb/badge.png?branch=master)](https://coveralls.io/r/js-data/js-data-rethinkdb?branch=master) | | ||
__Latest Release:__ [![Latest Release](https://img.shields.io/github/release/js-data/js-data-rethinkdb.svg?style=flat-square)](https://github.com/js-data/js-data-rethinkdb/releases) | ||
## Quick Start | ||
__Status:__ | ||
[![Dependency Status](https://img.shields.io/gemnasium/js-data/js-data-rethinkdb.svg?style=flat-square)](https://gemnasium.com/js-data/js-data-rethinkdb) [![Codacity](https://img.shields.io/codacy/69206fcb0df6462ca559610af32fd1fb.svg?style=flat-square)](https://www.codacy.com/public/jasondobry/js-data-rethinkdb/dashboard) | ||
__Supported Platforms:__ | ||
[![node version](https://img.shields.io/badge/Node-0.10%2B-green.svg?style=flat-square)](https://github.com/js-data/js-data) | ||
### Quick Start | ||
`npm install --save js-data js-data-rethinkdb`. | ||
@@ -40,6 +42,6 @@ | ||
## Changelog | ||
### Changelog | ||
[CHANGELOG.md](https://github.com/js-data/js-data-rethinkdb/blob/master/CHANGELOG.md) | ||
## Community | ||
### Community | ||
- [Mailing List](https://groups.io/org/groupsio/jsdata) - Ask your questions! | ||
@@ -50,6 +52,16 @@ - [Issues](https://github.com/js-data/js-data-rethinkdb/issues) - Found a bug? Feature request? Submit an issue! | ||
## Contributing | ||
### Contributing | ||
First, feel free to contact me with questions. [Mailing List](https://groups.io/org/groupsio/jsdata). [Issues](https://github.com/js-data/js-data-rethinkdb/issues). | ||
First, support is handled via the [Mailing List](https://groups.io/org/groupsio/jsdata). Ask your questions there. | ||
When submitting issues on GitHub, please include as much detail as possible to make debugging quick and easy. | ||
- good - Your versions of js-data, js-data-rethinkdb, etc., relevant console logs/error, code examples that revealed the issue | ||
- better - A [plnkr](http://plnkr.co/), [fiddle](http://jsfiddle.net/), or [bin](http://jsbin.com/?html,output) that demonstrates the issue | ||
- best - A Pull Request that fixes the issue, including test coverage for the issue and the fix | ||
[Github Issues](https://github.com/js-data/js-data-rethinkdb/issues). | ||
#### Pull Requests | ||
1. Contribute to the issue that is the reason you'll be developing in the first place | ||
@@ -64,3 +76,3 @@ 1. Fork js-data-rethinkdb | ||
## License | ||
### License | ||
@@ -67,0 +79,0 @@ The MIT License (MIT) |
389
src/index.js
@@ -1,16 +0,11 @@ | ||
let rethinkdbdash = require('rethinkdbdash'); | ||
let contains = require('mout/array/contains'); | ||
let forOwn = require('mout/object/forOwn'); | ||
let keys = require('mout/object/keys'); | ||
let deepMixIn = require('mout/object/deepMixIn'); | ||
let forEach = require('mout/array/forEach'); | ||
let isObject = require('mout/lang/isObject'); | ||
let isArray = require('mout/lang/isArray'); | ||
let isEmpty = require('mout/lang/isEmpty'); | ||
let isString = require('mout/lang/isString'); | ||
let upperCase = require('mout/string/upperCase'); | ||
let underscore = require('mout/string/underscore'); | ||
let JSData = require('js-data'); | ||
let P = JSData.DSUtils.Promise; | ||
import rethinkdbdash from 'rethinkdbdash'; | ||
import JSData from 'js-data'; | ||
let { DSUtils } = JSData; | ||
let { Promise: P, contains, forOwn, deepMixIn, forEach, isObject, isArray, isString, removeCircular } = DSUtils; | ||
import keys from 'mout/object/keys'; | ||
import isEmpty from 'mout/lang/isEmpty'; | ||
import upperCase from 'mout/string/upperCase'; | ||
import underscore from 'mout/string/underscore'; | ||
import omit from 'mout/object/omit'; | ||
@@ -38,139 +33,140 @@ class Defaults { | ||
function filterQuery(resourceConfig, params, options) { | ||
let r = this.r; | ||
params = params || {}; | ||
options = options || {}; | ||
params.where = params.where || {}; | ||
params.orderBy = params.orderBy || params.sort; | ||
params.skip = params.skip || params.offset; | ||
class DSRethinkDBAdapter { | ||
constructor(options) { | ||
options = options || {}; | ||
this.defaults = new Defaults(); | ||
deepMixIn(this.defaults, options); | ||
this.r = rethinkdbdash(this.defaults); | ||
this.databases = {}; | ||
this.tables = {}; | ||
this.indices = {}; | ||
} | ||
forEach(keys(params), k => { | ||
let v = params[k]; | ||
if (!contains(reserved, k)) { | ||
if (isObject(v)) { | ||
params.where[k] = v; | ||
} else { | ||
params.where[k] = { | ||
'==': v | ||
}; | ||
} | ||
delete params[k]; | ||
} | ||
}); | ||
selectTable(resourceConfig, options) { | ||
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)); | ||
} | ||
let query = r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)); | ||
filterSequence(sequence, params) { | ||
let r = this.r; | ||
params = params || {}; | ||
params.where = params.where || {}; | ||
params.orderBy = params.orderBy || params.sort; | ||
params.skip = params.skip || params.offset; | ||
if (!isEmpty(params.where)) { | ||
query = query.filter(row => { | ||
let subQuery; | ||
forOwn(params.where, (criteria, field) => { | ||
if (!isObject(criteria)) { | ||
params.where[field] = { | ||
'==': criteria | ||
forEach(keys(params), k => { | ||
let v = params[k]; | ||
if (!contains(reserved, k)) { | ||
if (isObject(v)) { | ||
params.where[k] = v; | ||
} else { | ||
params.where[k] = { | ||
'==': v | ||
}; | ||
} | ||
forOwn(criteria, (v, op) => { | ||
if (op === '==' || op === '===') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).eq(v)) : row(field).default(null).eq(v); | ||
} else if (op === '!=' || op === '!==') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).ne(v)) : row(field).default(null).ne(v); | ||
} else if (op === '>') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).gt(v)) : row(field).default(null).gt(v); | ||
} else if (op === '>=') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).ge(v)) : row(field).default(null).ge(v); | ||
} else if (op === '<') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).lt(v)) : row(field).default(null).lt(v); | ||
} else if (op === '<=') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).le(v)) : row(field).default(null).le(v); | ||
} else if (op === 'isectEmpty') { | ||
subQuery = subQuery ? subQuery.and(row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0); | ||
} else if (op === 'isectNotEmpty') { | ||
subQuery = subQuery ? subQuery.and(row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0); | ||
} else if (op === 'in') { | ||
subQuery = subQuery ? subQuery.and(r.expr(v).default(r.expr([])).contains(row(field).default(null))) : r.expr(v).default(r.expr([])).contains(row(field).default(null)); | ||
} else if (op === 'notIn') { | ||
subQuery = subQuery ? subQuery.and(r.expr(v).default(r.expr([])).contains(row(field).default(null)).not()) : r.expr(v).default(r.expr([])).contains(row(field).default(null)).not(); | ||
} else if (op === '|==' || op === '|===') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).eq(v)) : row(field).default(null).eq(v); | ||
} else if (op === '|!=' || op === '|!==') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).ne(v)) : row(field).default(null).ne(v); | ||
} else if (op === '|>') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).gt(v)) : row(field).default(null).gt(v); | ||
} else if (op === '|>=') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).ge(v)) : row(field).default(null).ge(v); | ||
} else if (op === '|<') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).lt(v)) : row(field).default(null).lt(v); | ||
} else if (op === '|<=') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).le(v)) : row(field).default(null).le(v); | ||
} else if (op === '|isectEmpty') { | ||
subQuery = subQuery ? subQuery.or(row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0); | ||
} else if (op === '|isectNotEmpty') { | ||
subQuery = subQuery ? subQuery.or(row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0); | ||
} else if (op === '|in') { | ||
subQuery = subQuery ? subQuery.or(r.expr(v).default(r.expr([])).contains(row(field).default(null))) : r.expr(v).default(r.expr([])).contains(row(field).default(null)); | ||
} else if (op === '|notIn') { | ||
subQuery = subQuery ? subQuery.or(r.expr(v).default(r.expr([])).contains(row(field).default(null)).not()) : r.expr(v).default(r.expr([])).contains(row(field).default(null)).not(); | ||
delete params[k]; | ||
} | ||
}); | ||
let query = sequence; | ||
if (!isEmpty(params.where)) { | ||
query = query.filter(row => { | ||
let subQuery; | ||
forOwn(params.where, (criteria, field) => { | ||
if (!isObject(criteria)) { | ||
params.where[field] = { | ||
'==': criteria | ||
}; | ||
} | ||
forOwn(criteria, (v, op) => { | ||
if (op === '==' || op === '===') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).eq(v)) : row(field).default(null).eq(v); | ||
} else if (op === '!=' || op === '!==') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).ne(v)) : row(field).default(null).ne(v); | ||
} else if (op === '>') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).gt(v)) : row(field).default(null).gt(v); | ||
} else if (op === '>=') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).ge(v)) : row(field).default(null).ge(v); | ||
} else if (op === '<') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).lt(v)) : row(field).default(null).lt(v); | ||
} else if (op === '<=') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).le(v)) : row(field).default(null).le(v); | ||
} else if (op === 'isectEmpty') { | ||
subQuery = subQuery ? subQuery.and(row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0); | ||
} else if (op === 'isectNotEmpty') { | ||
subQuery = subQuery ? subQuery.and(row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0); | ||
} else if (op === 'in') { | ||
subQuery = subQuery ? subQuery.and(r.expr(v).default(r.expr([])).contains(row(field).default(null))) : r.expr(v).default(r.expr([])).contains(row(field).default(null)); | ||
} else if (op === 'notIn') { | ||
subQuery = subQuery ? subQuery.and(r.expr(v).default(r.expr([])).contains(row(field).default(null)).not()) : r.expr(v).default(r.expr([])).contains(row(field).default(null)).not(); | ||
} else if (op === '|==' || op === '|===') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).eq(v)) : row(field).default(null).eq(v); | ||
} else if (op === '|!=' || op === '|!==') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).ne(v)) : row(field).default(null).ne(v); | ||
} else if (op === '|>') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).gt(v)) : row(field).default(null).gt(v); | ||
} else if (op === '|>=') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).ge(v)) : row(field).default(null).ge(v); | ||
} else if (op === '|<') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).lt(v)) : row(field).default(null).lt(v); | ||
} else if (op === '|<=') { | ||
subQuery = subQuery ? subQuery.or(row(field).default(null).le(v)) : row(field).default(null).le(v); | ||
} else if (op === '|isectEmpty') { | ||
subQuery = subQuery ? subQuery.or(row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().eq(0); | ||
} else if (op === '|isectNotEmpty') { | ||
subQuery = subQuery ? subQuery.or(row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0)) : row(field).default([]).setIntersection(r.expr(v).default([])).count().ne(0); | ||
} else if (op === '|in') { | ||
subQuery = subQuery ? subQuery.or(r.expr(v).default(r.expr([])).contains(row(field).default(null))) : r.expr(v).default(r.expr([])).contains(row(field).default(null)); | ||
} else if (op === '|notIn') { | ||
subQuery = subQuery ? subQuery.or(r.expr(v).default(r.expr([])).contains(row(field).default(null)).not()) : r.expr(v).default(r.expr([])).contains(row(field).default(null)).not(); | ||
} | ||
}); | ||
}); | ||
return subQuery; | ||
}); | ||
return subQuery; | ||
}); | ||
} | ||
} | ||
if (params.orderBy) { | ||
if (isString(params.orderBy)) { | ||
params.orderBy = [ | ||
[params.orderBy, 'asc'] | ||
]; | ||
} | ||
for (var i = 0; i < params.orderBy.length; i++) { | ||
if (isString(params.orderBy[i])) { | ||
params.orderBy[i] = [params.orderBy[i], 'asc']; | ||
if (params.orderBy) { | ||
if (isString(params.orderBy)) { | ||
params.orderBy = [ | ||
[params.orderBy, 'asc'] | ||
]; | ||
} | ||
query = upperCase(params.orderBy[i][1]) === 'DESC' ? query.orderBy(r.desc(params.orderBy[i][0])) : query.orderBy(params.orderBy[i][0]); | ||
for (var i = 0; i < params.orderBy.length; i++) { | ||
if (isString(params.orderBy[i])) { | ||
params.orderBy[i] = [params.orderBy[i], 'asc']; | ||
} | ||
query = upperCase(params.orderBy[i][1]) === 'DESC' ? query.orderBy(r.desc(params.orderBy[i][0])) : query.orderBy(params.orderBy[i][0]); | ||
} | ||
} | ||
} | ||
if (params.skip) { | ||
query = query.skip(params.skip); | ||
} | ||
if (params.skip) { | ||
query = query.skip(params.skip); | ||
} | ||
if (params.limit) { | ||
query = query.limit(params.limit); | ||
} | ||
if (params.limit) { | ||
query = query.limit(params.limit); | ||
} | ||
return query; | ||
} | ||
class DSRethinkDBAdapter { | ||
constructor(options) { | ||
options = options || {}; | ||
this.defaults = new Defaults(); | ||
deepMixIn(this.defaults, options); | ||
this.r = rethinkdbdash(this.defaults); | ||
this.databases = {}; | ||
this.tables = {}; | ||
this.indices = {}; | ||
return query; | ||
} | ||
waitForDb(options) { | ||
let _this = this; | ||
options = options || {}; | ||
let db = options.db || _this.defaults.db; | ||
if (!_this.databases[db]) { | ||
_this.databases[db] = _this.r.branch(_this.r.dbList().contains(db), true, _this.r.dbCreate(db)).run(); | ||
let db = options.db || this.defaults.db; | ||
if (!this.databases[db]) { | ||
this.databases[db] = this.r.branch(this.r.dbList().contains(db), true, this.r.dbCreate(db)).run(); | ||
} | ||
return _this.databases[db]; | ||
return this.databases[db]; | ||
} | ||
waitForTable(table, options) { | ||
let _this = this; | ||
options = options || {}; | ||
let db = options.db || _this.defaults.db; | ||
return _this.waitForDb(options).then(() => { | ||
_this.tables[db] = _this.tables[db] || {}; | ||
if (!_this.tables[db][table]) { | ||
_this.tables[db][table] = _this.r.branch(_this.r.db(db).tableList().contains(table), true, _this.r.db(db).tableCreate(table)).run(); | ||
let db = options.db || this.defaults.db; | ||
return this.waitForDb(options).then(() => { | ||
this.tables[db] = this.tables[db] || {}; | ||
if (!this.tables[db][table]) { | ||
this.tables[db][table] = this.r.branch(this.r.db(db).tableList().contains(table), true, this.r.db(db).tableCreate(table)).run(); | ||
} | ||
return _this.tables[db][table]; | ||
return this.tables[db][table]; | ||
}); | ||
@@ -180,14 +176,13 @@ } | ||
waitForIndex(table, index, options) { | ||
let _this = this; | ||
options = options || {}; | ||
let db = options.db || _this.defaults.db; | ||
return _this.waitForDb(options).then(() => _this.waitForTable(table, options)).then(() => { | ||
_this.indices[db] = _this.indices[db] || {}; | ||
_this.indices[db][table] = _this.indices[db][table] || {}; | ||
if (!_this.tables[db][table][index]) { | ||
_this.tables[db][table][index] = _this.r.branch(_this.r.db(db).table(table).indexList().contains(index), true, _this.r.db(db).table(table).indexCreate(index)).run().then(() => { | ||
return _this.r.db(db).table(table).indexWait(index).run(); | ||
let db = options.db || this.defaults.db; | ||
return this.waitForDb(options).then(() => this.waitForTable(table, options)).then(() => { | ||
this.indices[db] = this.indices[db] || {}; | ||
this.indices[db][table] = this.indices[db][table] || {}; | ||
if (!this.tables[db][table][index]) { | ||
this.tables[db][table][index] = this.r.branch(this.r.db(db).table(table).indexList().contains(index), true, this.r.db(db).table(table).indexCreate(index)).run().then(() => { | ||
return this.r.db(db).table(table).indexWait(index).run(); | ||
}); | ||
} | ||
return _this.tables[db][table][index]; | ||
return this.tables[db][table][index]; | ||
}); | ||
@@ -197,3 +192,2 @@ } | ||
find(resourceConfig, id, options) { | ||
let _this = this; | ||
let newModels = {}; | ||
@@ -204,3 +198,3 @@ let models = {}; | ||
let table = resourceConfig.table || underscore(resourceConfig.name); | ||
let tasks = [_this.waitForTable(table, options)]; | ||
let tasks = [this.waitForTable(table, options)]; | ||
forEach(resourceConfig.relationList, def => { | ||
@@ -211,15 +205,17 @@ let relationName = def.relation; | ||
throw new JSData.DSErrors.NER(relationName); | ||
} else if (!options.with || !contains(options.with, relationName)) { | ||
return; | ||
} | ||
if (def.foreignKey) { | ||
tasks.push(_this.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, options)); | ||
tasks.push(this.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, options)); | ||
} else if (def.localKey) { | ||
tasks.push(_this.waitForIndex(resourceConfig.table || underscore(resourceConfig.name), def.localKey, options)); | ||
tasks.push(this.waitForIndex(resourceConfig.table || underscore(resourceConfig.name), def.localKey, options)); | ||
} | ||
}); | ||
return P.all(tasks).then(() => { | ||
return _this.r.do(_this.r.table(table).get(id), doc => { | ||
return this.r.do(this.r.table(table).get(id), doc => { | ||
forEach(resourceConfig.relationList, def => { | ||
let relationName = def.relation; | ||
models[relationName] = resourceConfig.getResource(relationName); | ||
if (!options.with || !options.with.length || !contains(options.with, relationName)) { | ||
if (!options.with || !contains(options.with, relationName)) { | ||
return; | ||
@@ -234,3 +230,3 @@ } | ||
if (def.type === 'belongsTo') { | ||
merge[localField] = _this.r.table(models[relationName].table || underscore(models[relationName].name)).get(doc(localKey).default('')); | ||
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).get(doc(localKey).default('')); | ||
newModels[localField] = { | ||
@@ -241,3 +237,3 @@ modelName: relationName, | ||
} else if (def.type === 'hasMany') { | ||
merge[localField] = _this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, { index: foreignKey }).coerceTo('ARRAY'); | ||
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, { index: foreignKey }).coerceTo('ARRAY'); | ||
@@ -249,3 +245,3 @@ newModels[localField] = { | ||
} else if (def.type === 'hasOne') { | ||
merge[localField] = _this.r.table(models[relationName].table || underscore(models[relationName].name)); | ||
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)); | ||
@@ -293,14 +289,83 @@ if (localKey) { | ||
findAll(resourceConfig, params, options) { | ||
let _this = this; | ||
options = options || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return filterQuery.call(_this, resourceConfig, params, options).run(); | ||
let table = resourceConfig.table || underscore(resourceConfig.name); | ||
let tasks = [this.waitForTable(table, options)]; | ||
let models = {}; | ||
let merge = {}; | ||
let newModels = {}; | ||
forEach(resourceConfig.relationList, def => { | ||
let relationName = def.relation; | ||
let relationDef = resourceConfig.getResource(relationName); | ||
if (!relationDef) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
} else if (!options.with || !contains(options.with, relationName)) { | ||
return; | ||
} | ||
if (def.foreignKey) { | ||
tasks.push(this.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, options)); | ||
} else if (def.localKey) { | ||
tasks.push(this.waitForIndex(resourceConfig.table || underscore(resourceConfig.name), def.localKey, options)); | ||
} | ||
}); | ||
return P.all(tasks).then(() => { | ||
let query = this.filterSequence(this.selectTable(resourceConfig, options), params); | ||
if (options.with && options.with.length) { | ||
query = query.map(doc => { | ||
let id = doc(resourceConfig.idAttribute); | ||
forEach(resourceConfig.relationList, def => { | ||
let relationName = def.relation; | ||
models[relationName] = resourceConfig.getResource(relationName); | ||
if (!options.with || !contains(options.with, relationName)) { | ||
return; | ||
} | ||
if (!models[relationName]) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
} | ||
let localKey = def.localKey; | ||
let localField = def.localField; | ||
let foreignKey = def.foreignKey; | ||
if (def.type === 'belongsTo') { | ||
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).get(doc(localKey).default('')); | ||
newModels[localField] = { | ||
modelName: relationName, | ||
relation: 'belongsTo' | ||
}; | ||
} else if (def.type === 'hasMany') { | ||
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, { index: foreignKey }).coerceTo('ARRAY'); | ||
newModels[localField] = { | ||
modelName: relationName, | ||
relation: 'hasMany' | ||
}; | ||
} else if (def.type === 'hasOne') { | ||
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)); | ||
if (localKey) { | ||
merge[localField] = merge[localField].get(localKey); | ||
} else { | ||
merge[localField] = merge[localField].getAll(id, { index: foreignKey }).coerceTo('ARRAY'); | ||
} | ||
newModels[localField] = { | ||
modelName: relationName, | ||
relation: 'hasOne' | ||
}; | ||
} | ||
}); | ||
if (!isEmpty(merge)) { | ||
return doc.merge(merge); | ||
} | ||
return doc; | ||
}); | ||
} | ||
return query.run(); | ||
}); | ||
} | ||
create(resourceConfig, attrs, options) { | ||
let _this = this; | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).insert(attrs, { returnChanges: true }).run(); | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).insert(attrs, { returnChanges: true }).run(); | ||
}).then(cursor => cursor.changes[0].new_val); | ||
@@ -310,6 +375,6 @@ } | ||
update(resourceConfig, id, attrs, options) { | ||
let _this = this; | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run(); | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run(); | ||
}).then(cursor => { | ||
@@ -321,7 +386,7 @@ return cursor.changes[0].new_val; | ||
updateAll(resourceConfig, attrs, params, options) { | ||
let _this = this; | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
params = params || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return filterQuery.call(_this, resourceConfig, params, options).update(attrs, { returnChanges: true }).run(); | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return this.filterSequence(this.selectTable(resourceConfig, options), params).update(attrs, { returnChanges: true }).run(); | ||
}).then(cursor => { | ||
@@ -335,6 +400,5 @@ let items = []; | ||
destroy(resourceConfig, id, options) { | ||
let _this = this; | ||
options = options || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).delete().run(); | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).delete().run(); | ||
}).then(() => undefined); | ||
@@ -344,7 +408,6 @@ } | ||
destroyAll(resourceConfig, params, options) { | ||
let _this = this; | ||
options = options || {}; | ||
params = params || {}; | ||
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return filterQuery.call(_this, resourceConfig, params, options).delete().run(); | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return this.filterSequence(this.selectTable(resourceConfig, options), params).delete().run(); | ||
}).then(() => undefined); | ||
@@ -351,0 +414,0 @@ } |
describe('DSRethinkDBAdapter#create', function () { | ||
it('should create a user in RethinkDB', function (done) { | ||
it('should create a user in RethinkDB', function () { | ||
var id; | ||
adapter.create(User, { name: 'John' }).then(function (user) { | ||
return adapter.create(User, { name: 'John' }).then(function (user) { | ||
id = user.id; | ||
@@ -21,9 +21,8 @@ assert.equal(user.name, 'John'); | ||
.then(function () { | ||
done('Should not have reached here!'); | ||
throw new Error('Should not have reached here!'); | ||
}) | ||
.catch(function (err) { | ||
assert.equal(err.message, 'Not Found!'); | ||
done(); | ||
}); | ||
}); | ||
}); |
describe('DSRethinkDBAdapter#destroy', function () { | ||
it('should destroy a user from RethinkDB', function (done) { | ||
it('should destroy a user from RethinkDB', function () { | ||
var id; | ||
adapter.create(User, { name: 'John' }) | ||
return adapter.create(User, { name: 'John' }) | ||
.then(function (user) { | ||
@@ -14,9 +14,8 @@ id = user.id; | ||
.then(function () { | ||
done('Should not have reached here!'); | ||
throw new Error('Should not have reached here!'); | ||
}) | ||
.catch(function (err) { | ||
assert.equal(err.message, 'Not Found!'); | ||
done(); | ||
}); | ||
}); | ||
}); |
describe('DSRethinkDBAdapter#destroyAll', function () { | ||
it('should destroy all items', function (done) { | ||
it('should destroy all items', function () { | ||
var id; | ||
adapter.create(User, { name: 'John' }) | ||
return adapter.create(User, { name: 'John' }) | ||
.then(function (user) { | ||
@@ -22,5 +22,4 @@ id = user.id; | ||
assert.equal(users.length, 0); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
}); |
describe('DSRethinkDBAdapter#find', function () { | ||
it('should find a user in RethinkDB', function (done) { | ||
it('should find a user in RethinkDB', function () { | ||
var id, id2, _user, _post, _comments; | ||
adapter.create(User, { name: 'John' }) | ||
return adapter.create(User, { name: 'John' }) | ||
.then(function (user) { | ||
@@ -63,9 +63,9 @@ _user = user; | ||
.then(function () { | ||
done('Should not have reached here!'); | ||
throw new Error('Should not have reached here!'); | ||
}) | ||
.catch(function (err) { | ||
assert.equal(err.message, 'Not Found!'); | ||
done(); | ||
return true; | ||
}); | ||
}); | ||
}); |
describe('DSRethinkDBAdapter#findAll', function () { | ||
it('should filter users', function (done) { | ||
var id; | ||
it('should filter users', function () { | ||
var id, postId; | ||
adapter.findAll(User, { | ||
return adapter.findAll(User, { | ||
age: 30 | ||
@@ -12,18 +12,21 @@ }).then(function (users) { | ||
id = user.id; | ||
return adapter.create(Post, { title: 'foo', userId: user.id }); | ||
}).then(function (post) { | ||
postId = post.id; | ||
assert.equal(post.userId, id); | ||
return adapter.findAll(User, { | ||
name: 'John' | ||
}); | ||
}, { with: ['post', 'comment']}); | ||
}).then(function (users) { | ||
assert.equal(users.length, 1); | ||
assert.deepEqual(users[0], { id: id, name: 'John' }); | ||
assert.deepEqual(users[0], { id: id, name: 'John', comments: [], posts: [ { id: postId, userId:id, title: 'foo' } ] }); | ||
return adapter.destroy(User, id); | ||
}).then(function (destroyedUser) { | ||
assert.isFalse(!!destroyedUser); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
it('should filter users using the "in" operator', function (done) { | ||
it('should filter users using the "in" operator', function () { | ||
var id; | ||
adapter.findAll(User, { | ||
return adapter.findAll(User, { | ||
where: { | ||
@@ -48,5 +51,4 @@ age: { | ||
assert.isFalse(!!destroyedUser); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
}); |
describe('DSRethinkDBAdapter#update', function () { | ||
it('should update a user in RethinkDB', function (done) { | ||
it('should update a user in RethinkDB', function () { | ||
var id; | ||
adapter.create(User, { name: 'John' }) | ||
return adapter.create(User, { name: 'John' }) | ||
.then(function (user) { | ||
@@ -34,9 +34,8 @@ id = user.id; | ||
.then(function () { | ||
done('Should not have reached here!'); | ||
throw new Error('Should not have reached here!'); | ||
}) | ||
.catch(function (err) { | ||
assert.equal(err.message, 'Not Found!'); | ||
done(); | ||
}); | ||
}); | ||
}); |
describe('DSRethinkDBAdapter#updateAll', function () { | ||
it('should update all items', function (done) { | ||
it('should update all items', function () { | ||
var id, id2; | ||
adapter.create(User, { name: 'John', age: 20 }) | ||
return adapter.create(User, { name: 'John', age: 20 }) | ||
.then(function (user) { | ||
@@ -45,5 +45,4 @@ id = user.id; | ||
assert.isFalse(!!destroyedUser); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
}); |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
66148
1323
0
97
- Removedjs-data@>=1.2.0
- Removedrethinkdbdash@>=1.15.0