Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

js-data-rethinkdb

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-data-rethinkdb - npm Package Compare versions

Comparing version
1.1.1
to
1.1.2
+490
dist/js-data-rethinkdb.js
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
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; }; })();
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
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 Defaults = function Defaults() {
_classCallCheck(this, Defaults);
};
Defaults.prototype.host = "localhost";
Defaults.prototype.port = 28015;
Defaults.prototype.authKey = "";
Defaults.prototype.db = "test";
Defaults.prototype.min = 10;
Defaults.prototype.max = 50;
Defaults.prototype.bufferSize = 10;
var reserved = ["orderBy", "sort", "limit", "offset", "skip", "where"];
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 () {
function DSRethinkDBAdapter(options) {
_classCallCheck(this, DSRethinkDBAdapter);
options = options || {};
this.defaults = new Defaults();
deepMixIn(this.defaults, options);
this.r = rethinkdbdash(this.defaults);
this.databases = {};
this.tables = {};
this.indices = {};
}
_createClass(DSRethinkDBAdapter, {
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();
}
return _this.databases[db];
}
},
waitForTable: {
value: function waitForTable(table, options) {
var _this = this;
options = options || {};
var db = options.db || _this.defaults.db;
return _this.waitForDb(options).then(function () {
_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];
});
}
},
waitForIndex: {
value: function waitForIndex(table, index, options) {
var _this = this;
options = options || {};
var db = options.db || _this.defaults.db;
return _this.waitForDb(options).then(function () {
return _this.waitForTable(table, options);
}).then(function () {
_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(function () {
return _this.r.db(db).table(table).indexWait(index).run();
});
}
return _this.tables[db][table][index];
});
}
},
find: {
value: function find(resourceConfig, id, options) {
var _this = this;
var newModels = {};
var models = {};
var merge = {};
options = options || {};
var table = resourceConfig.table || underscore(resourceConfig.name);
var tasks = [_this.waitForTable(table, options)];
forEach(resourceConfig.relationList, function (def) {
var relationName = def.relation;
var relationDef = resourceConfig.getResource(relationName);
if (!relationDef) {
throw new JSData.DSErrors.NER(relationName);
}
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 () {
return _this.r["do"](_this.r.table(table).get(id), function (doc) {
forEach(resourceConfig.relationList, function (def) {
var relationName = def.relation;
models[relationName] = resourceConfig.getResource(relationName);
if (!options["with"] || !options["with"].length || !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;
}).run();
}).then(function (item) {
if (!item) {
return P.reject(new Error("Not Found!"));
} else {
forOwn(item, function (localValue, localKey) {
if (localKey in newModels) {
if (isObject(localValue)) {
item[localKey] = item[localKey];
} else if (isArray(localValue)) {
if (newModels[localKey].relation === "hasOne" && localValue.length) {
item[localKey] = localValue[0];
} else {
item[localKey] = localValue;
}
}
}
});
return item;
}
});
}
},
findAll: {
value: function findAll(resourceConfig, params, options) {
var _this = this;
options = options || {};
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
return filterQuery.call(_this, resourceConfig, params, options).run();
});
}
},
create: {
value: function create(resourceConfig, attrs, options) {
var _this = this;
options = options || {};
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();
}).then(function (cursor) {
return cursor.changes[0].new_val;
});
}
},
update: {
value: function update(resourceConfig, id, attrs, options) {
var _this = this;
options = options || {};
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();
}).then(function (cursor) {
return cursor.changes[0].new_val;
});
}
},
updateAll: {
value: function updateAll(resourceConfig, attrs, params, options) {
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).update(attrs, { returnChanges: true }).run();
}).then(function (cursor) {
var items = [];
cursor.changes.forEach(function (change) {
return items.push(change.new_val);
});
return items;
});
}
},
destroy: {
value: function destroy(resourceConfig, id, options) {
var _this = this;
options = options || {};
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();
}).then(function () {
return undefined;
});
}
},
destroyAll: {
value: function destroyAll(resourceConfig, params, options) {
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();
}).then(function () {
return undefined;
});
}
}
});
return DSRethinkDBAdapter;
})();
module.exports = DSRethinkDBAdapter;
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
module.exports = require("rethinkdbdash");
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
module.exports = require("mout/array/contains");
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
module.exports = require("mout/object/forOwn");
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
module.exports = require("mout/object/keys");
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
module.exports = require("mout/object/deepMixIn");
/***/ },
/* 6 */
/***/ function(module, exports, __webpack_require__) {
module.exports = require("mout/array/forEach");
/***/ },
/* 7 */
/***/ function(module, exports, __webpack_require__) {
module.exports = require("mout/lang/isObject");
/***/ },
/* 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");
/***/ }
/******/ ]);
+1
-2
{
"node": false,
"browser": true,
"es5": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"camelcase": false,
"curly": true,

@@ -9,0 +8,0 @@ "eqeqeq": true,

@@ -0,1 +1,6 @@

##### 1.1.2 - 07 March 2015
###### Other
- Converted to ES6, using Babel.js to transpile to ES5.
##### 1.1.1 - 25 February 2015

@@ -2,0 +7,0 @@

@@ -5,3 +5,3 @@ /*

*
* Copyright (c) 2014 Jason Dobry <http://www.js-data.io/js-data-rethinkdb>
* Copyright (c) 2014-2015 Jason Dobry <http://www.js-data.io/js-data-rethinkdb>
* Licensed under the MIT license. <https://github.com/js-data/js-data-rethinkdb/blob/master/LICENSE>

@@ -45,2 +45,42 @@ */

}
},
webpack: {
dist: {
debug: true,
entry: './src/index.js',
output: {
filename: './dist/js-data-rethinkdb.js',
libraryTarget: 'commonjs2',
library: 'js-data-rethinkdb'
},
externals: [
'mout/array/contains',
'mout/object/forOwn',
'mout/object/keys',
'mout/object/deepMixIn',
'mout/array/forEach',
'mout/lang/isObject',
'mout/lang/isArray',
'mout/lang/isEmpty',
'mout/lang/isString',
'mout/string/upperCase',
'mout/string/underscore',
'bluebird',
'js-data',
'js-data-schema',
'rethinkdbdash'
],
module: {
loaders: [
{ test: /(src)(.+)\.js$/, exclude: /node_modules/, loader: 'babel-loader?blacklist=useStrict' }
],
preLoaders: [
{
test: /(src)(.+)\.js$|(test)(.+)\.js$/, // include .js files
exclude: /node_modules/, // exclude any and all files in the node_modules folder
loader: "jshint-loader?failOnHint=true"
}
]
}
}
}

@@ -53,3 +93,3 @@ });

grunt.registerTask('build', [
'jshint'
'webpack'
]);

@@ -56,0 +96,0 @@ grunt.registerTask('go', ['build', 'watch:dist']);

@@ -51,3 +51,5 @@ /*global assert:true */

beforeEach(function () {
store = new JSData.DS();
store = new JSData.DS({
log: false
});
adapter = new DSRethinkDBAdapter();

@@ -54,0 +56,0 @@ DSUtils = JSData.DSUtils;

{
"name": "js-data-rethinkdb",
"description": "RethinkDB adapter for js-data.",
"version": "1.1.1",
"version": "1.1.2",
"homepage": "http://www.js-data.io/docs/dsrethinkdbadapter",

@@ -20,3 +20,3 @@ "repository": {

],
"main": "./src/index.js",
"main": "./dist/js-data-rethinkdb.js",
"keywords": [

@@ -32,11 +32,17 @@ "data",

"devDependencies": {
"chai": "2.1.0",
"babel-core": "^4.7.3",
"babel-loader": "^4.1.0",
"chai": "2.1.1",
"grunt": "0.4.5",
"grunt-contrib-jshint": "0.11.0",
"grunt-contrib-watch": "0.6.1",
"grunt-karma-coveralls": "2.5.3",
"grunt-mocha-test": "0.12.7",
"grunt-webpack": "^1.0.8",
"jit-grunt": "0.9.1",
"sinon": "1.12.2",
"time-grunt": "1.1.0"
"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"
},

@@ -43,0 +49,0 @@ "scripts": {

+202
-211

@@ -1,18 +0,19 @@

var rethinkdbdash = require('rethinkdbdash');
var contains = require('mout/array/contains');
var forOwn = require('mout/object/forOwn');
var keys = require('mout/object/keys');
var deepMixIn = require('mout/object/deepMixIn');
var forEach = require('mout/array/forEach');
var isObject = require('mout/lang/isObject');
var isArray = require('mout/lang/isArray');
var isEmpty = require('mout/lang/isEmpty');
var isString = require('mout/lang/isString');
var upperCase = require('mout/string/upperCase');
var underscore = require('mout/string/underscore');
var JSData = require('js-data');
var P = JSData.DSUtils.Promise;
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;
function Defaults() {
class Defaults {
}

@@ -28,3 +29,3 @@

var reserved = [
let reserved = [
'orderBy',

@@ -39,3 +40,3 @@ 'sort',

function filterQuery(resourceConfig, params, options) {
var r = this.r;
let r = this.r;
params = params || {};

@@ -47,4 +48,4 @@ options = options || {};

forEach(keys(params), function (k) {
var v = params[k];
forEach(keys(params), k => {
let v = params[k];
if (!contains(reserved, k)) {

@@ -62,8 +63,8 @@ if (isObject(v)) {

var query = 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));
if (!isEmpty(params.where)) {
query = query.filter(function (row) {
var subQuery;
forOwn(params.where, function (criteria, field) {
query = query.filter(row => {
let subQuery;
forOwn(params.where, (criteria, field) => {
if (!isObject(criteria)) {

@@ -74,3 +75,3 @@ params.where[field] = {

}
forOwn(criteria, function (v, op) {
forOwn(criteria, (v, op) => {
if (op === '==' || op === '===') {

@@ -148,209 +149,199 @@ subQuery = subQuery ? subQuery.and(row(field).default(null).eq(v)) : row(field).default(null).eq(v);

function DSRethinkDBAdapter(options) {
options = options || {};
this.defaults = new Defaults();
deepMixIn(this.defaults, options);
this.r = rethinkdbdash(this.defaults);
this.databases = {};
this.tables = {};
this.indices = {};
}
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 = {};
}
var dsRethinkDBAdapterPrototype = DSRethinkDBAdapter.prototype;
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();
}
return _this.databases[db];
}
dsRethinkDBAdapterPrototype.waitForDb = 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();
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();
}
return _this.tables[db][table];
});
}
return _this.databases[db];
};
dsRethinkDBAdapterPrototype.waitForTable = function waitForTable(table, options) {
var _this = this;
options = options || {};
var db = options.db || _this.defaults.db;
return _this.waitForDb(options).then(function () {
_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];
});
};
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();
});
}
return _this.tables[db][table][index];
});
}
dsRethinkDBAdapterPrototype.waitForIndex = function waitForIndex(table, index, options) {
var _this = this;
options = options || {};
var db = options.db || _this.defaults.db;
return _this.waitForDb(options).then(function () {
return _this.waitForTable(table, options);
}).then(function () {
_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(function () {
return _this.r.db(db).table(table).indexWait(index).run();
});
}
return _this.tables[db][table][index];
});
};
find(resourceConfig, id, options) {
let _this = this;
let newModels = {};
let models = {};
let merge = {};
options = options || {};
let table = resourceConfig.table || underscore(resourceConfig.name);
let tasks = [_this.waitForTable(table, options)];
forEach(resourceConfig.relationList, def => {
let relationName = def.relation;
let relationDef = resourceConfig.getResource(relationName);
if (!relationDef) {
throw new JSData.DSErrors.NER(relationName);
}
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(() => {
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)) {
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');
dsRethinkDBAdapterPrototype.find = function find(resourceConfig, id, options) {
var _this = this;
var newModels = {};
var models = {};
var merge = {};
options = options || {};
var table = resourceConfig.table || underscore(resourceConfig.name);
var tasks = [_this.waitForTable(table, options)];
forEach(resourceConfig.relationList, function (def) {
var relationName = def.relation;
var relationDef = resourceConfig.getResource(relationName);
if (!relationDef) {
throw new JSData.DSErrors.NER(relationName);
}
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 () {
return _this.r.do(_this.r.table(table).get(id), function (doc) {
forEach(resourceConfig.relationList, function (def) {
var relationName = def.relation;
models[relationName] = resourceConfig.getResource(relationName);
if (!options.with || !options.with.length || !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));
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');
}
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'
};
}
});
newModels[localField] = {
modelName: relationName,
relation: 'hasOne'
};
if (!isEmpty(merge)) {
return doc.merge(merge);
}
});
if (!isEmpty(merge)) {
return doc.merge(merge);
}
return doc;
}).run();
}).then(function (item) {
if (!item) {
return P.reject(new Error('Not Found!'));
} else {
forOwn(item, function (localValue, localKey) {
if (localKey in newModels) {
if (isObject(localValue)) {
item[localKey] = item[localKey];
} else if (isArray(localValue)) {
if (newModels[localKey].relation === 'hasOne' && localValue.length) {
item[localKey] = localValue[0];
} else {
item[localKey] = localValue;
return doc;
}).run();
}).then(item => {
if (!item) {
return P.reject(new Error('Not Found!'));
} else {
forOwn(item, (localValue, localKey) => {
if (localKey in newModels) {
if (isObject(localValue)) {
item[localKey] = item[localKey];
} else if (isArray(localValue)) {
if (newModels[localKey].relation === 'hasOne' && localValue.length) {
item[localKey] = localValue[0];
} else {
item[localKey] = localValue;
}
}
}
}
});
return item;
}
});
};
});
return item;
}
});
}
dsRethinkDBAdapterPrototype.findAll = function (resourceConfig, params, options) {
var _this = this;
options = options || {};
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
return filterQuery.call(_this, resourceConfig, params, options).run();
});
};
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();
});
}
dsRethinkDBAdapterPrototype.create = function (resourceConfig, attrs, options) {
var _this = this;
options = options || {};
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();
}).then(function (cursor) {
return cursor.changes[0].new_val;
});
};
create(resourceConfig, attrs, 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)).insert(attrs, { returnChanges: true }).run();
}).then(cursor => cursor.changes[0].new_val);
}
dsRethinkDBAdapterPrototype.update = function (resourceConfig, id, attrs, options) {
var _this = this;
options = options || {};
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();
}).then(function (cursor) {
return cursor.changes[0].new_val;
});
};
update(resourceConfig, id, attrs, 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).update(attrs, { returnChanges: true }).run();
}).then(cursor => {
return cursor.changes[0].new_val;
});
}
dsRethinkDBAdapterPrototype.updateAll = function (resourceConfig, attrs, params, options) {
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).update(attrs, { returnChanges: true }).run();
}).then(function (cursor) {
var items = [];
cursor.changes.forEach(function (change) {
items.push(change.new_val);
updateAll(resourceConfig, attrs, 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).update(attrs, { returnChanges: true }).run();
}).then(cursor => {
let items = [];
cursor.changes.forEach(change => items.push(change.new_val));
return items;
});
return items;
});
};
}
dsRethinkDBAdapterPrototype.destroy = function (resourceConfig, id, options) {
var _this = this;
options = options || {};
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();
}).then(function () {
return undefined;
});
};
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();
}).then(() => undefined);
}
dsRethinkDBAdapterPrototype.destroyAll = function (resourceConfig, params, options) {
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();
}).then(function () {
return undefined;
});
};
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();
}).then(() => undefined);
}
}
module.exports = DSRethinkDBAdapter;
export default DSRethinkDBAdapter;