js-data-rethinkdb
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -0,1 +1,6 @@ | ||
##### 2.0.2 - 09 September 2015 | ||
###### Backwards compatible bug fixes | ||
- #9 - CI tests failing because of outdated npm | ||
##### 2.0.1 - 03 July 2015 | ||
@@ -2,0 +7,0 @@ |
# Contributing Guide | ||
First, support is handled via the [Mailing List](https://groups.io/org/groupsio/jsdata). Ask your questions there. | ||
First, support is handled via the [Gitter Channel](https://gitter.im/js-data/js-data) and the [Mailing List](https://groups.io/org/groupsio/jsdata). Ask your questions there. | ||
@@ -13,11 +13,13 @@ When submitting issues on GitHub, please include as much detail as possible to make debugging quick and easy. | ||
#### Pull Requests | ||
#### Submitting Pull Requests | ||
1. Contribute to the issue that is the reason you'll be developing in the first place | ||
1. Contribute to the issue/discussion that is the reason you'll be developing in the first place | ||
1. Fork js-data-rethinkdb | ||
1. `git clone https://github.com/<you>/js-data-rethinkdb.git` | ||
1. `git clone git@github.com:<you>/js-data-rethinkdb.git` | ||
1. `cd js-data-rethinkdb; npm install; bower install;` | ||
1. `grunt go` (builds and starts a watch) | ||
1. (in another terminal) `grunt karma:dev` (runs the tests) | ||
1. Write your code, including relevant documentation and tests | ||
1. Submit a PR and we'll review | ||
1. Run `grunt test` (build and test) | ||
1. Your code will be linted and checked for formatting, the tests will be run | ||
1. The `dist/` folder & files will be generated, do NOT commit `dist/*`! They will be committed when a release is cut. | ||
1. Submit your PR and we'll review! | ||
1. Thanks! |
@@ -517,3 +517,3 @@ module.exports = | ||
/* 1 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ function(module, exports) { | ||
@@ -524,3 +524,3 @@ module.exports = require("rethinkdbdash"); | ||
/* 2 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ function(module, exports) { | ||
@@ -531,3 +531,3 @@ module.exports = require("js-data"); | ||
/* 3 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ function(module, exports) { | ||
@@ -534,0 +534,0 @@ module.exports = require("mout/string/underscore"); |
@@ -72,2 +72,18 @@ /* | ||
grunt.registerTask('standard', function () { | ||
var child_process = require('child_process'); | ||
var done = this.async(); | ||
grunt.log.writeln('Linting for correcting formatting...'); | ||
child_process.exec('node node_modules/standard/bin/cmd.js --parser babel-eslint src/index.js', function (err, stdout) { | ||
console.log(stdout); | ||
if (err) { | ||
grunt.log.writeln('Failed due to ' + (stdout.split('\n').length - 2) + ' lint errors!'); | ||
done(err); | ||
} else { | ||
grunt.log.writeln('Done linting.'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
grunt.registerTask('n', ['mochaTest']); | ||
@@ -77,2 +93,3 @@ | ||
grunt.registerTask('build', [ | ||
'standard', | ||
'webpack' | ||
@@ -79,0 +96,0 @@ ]); |
{ | ||
"name": "js-data-rethinkdb", | ||
"description": "RethinkDB adapter for js-data.", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"homepage": "http://www.js-data.io/docs/dsrethinkdbadapter", | ||
@@ -31,9 +31,10 @@ "repository": { | ||
"devDependencies": { | ||
"babel-core": "5.6.15", | ||
"babel-loader": "5.3.0", | ||
"bluebird": "2.9.31", | ||
"chai": "3.0.0", | ||
"babel-core": "5.8.23", | ||
"babel-eslint": "4.1.1", | ||
"babel-loader": "5.3.2", | ||
"bluebird": "2.10.0", | ||
"chai": "3.2.0", | ||
"grunt": "0.4.5", | ||
"grunt-contrib-watch": "0.6.1", | ||
"grunt-karma-coveralls": "2.5.3", | ||
"grunt-karma-coveralls": "2.5.4", | ||
"grunt-mocha-test": "0.12.7", | ||
@@ -44,3 +45,4 @@ "grunt-webpack": "1.0.11", | ||
"jshint-loader": "0.8.3", | ||
"sinon": "1.15.4", | ||
"sinon": "1.16.1", | ||
"standard": "5.2.2", | ||
"time-grunt": "1.2.1", | ||
@@ -47,0 +49,0 @@ "webpack-dev-server": "1.10.1" |
@@ -55,3 +55,3 @@ <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" /> | ||
First, support is handled via the [Mailing List](https://groups.io/org/groupsio/jsdata). Ask your questions there. | ||
First, support is handled via the [Gitter Channel](https://gitter.im/js-data/js-data) and the [Mailing List](https://groups.io/org/groupsio/jsdata). Ask your questions there. | ||
@@ -66,12 +66,14 @@ When submitting issues on GitHub, please include as much detail as possible to make debugging quick and easy. | ||
#### Pull Requests | ||
#### Submitting Pull Requests | ||
1. Contribute to the issue that is the reason you'll be developing in the first place | ||
1. Contribute to the issue/discussion that is the reason you'll be developing in the first place | ||
1. Fork js-data-rethinkdb | ||
1. `git clone https://github.com/<you>/js-data-rethinkdb.git` | ||
1. `git clone git@github.com:<you>/js-data-rethinkdb.git` | ||
1. `cd js-data-rethinkdb; npm install; bower install;` | ||
1. `grunt go` (builds and starts a watch) | ||
1. (in another terminal) `grunt karma:dev` (runs the tests) | ||
1. Write your code, including relevant documentation and tests | ||
1. Submit a PR and we'll review | ||
1. Run `grunt test` (build and test) | ||
1. Your code will be linted and checked for formatting, the tests will be run | ||
1. The `dist/` folder & files will be generated, do NOT commit `dist/*`! They will be committed when a release is cut. | ||
1. Submit your PR and we'll review! | ||
1. Thanks! | ||
@@ -78,0 +80,0 @@ ### License |
400
src/index.js
@@ -1,7 +0,7 @@ | ||
let rethinkdbdash = require('rethinkdbdash'); | ||
let JSData = require('js-data'); | ||
let { DSUtils } = JSData; | ||
let { upperCase, contains, forOwn, isEmpty, keys, deepMixIn, forEach, isObject, isArray, isString, removeCircular, omit } = DSUtils; | ||
let rethinkdbdash = require('rethinkdbdash') | ||
let JSData = require('js-data') | ||
let { DSUtils } = JSData | ||
let { upperCase, contains, forOwn, isEmpty, keys, deepMixIn, forEach, isObject, isArray, isString, removeCircular, omit } = DSUtils | ||
let underscore = require('mout/string/underscore'); | ||
let underscore = require('mout/string/underscore') | ||
@@ -12,9 +12,9 @@ class 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; | ||
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 | ||
@@ -28,97 +28,97 @@ let reserved = [ | ||
'where' | ||
]; | ||
] | ||
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 = {}; | ||
constructor (options) { | ||
options = options || {} | ||
this.defaults = new Defaults() | ||
deepMixIn(this.defaults, options) | ||
this.r = rethinkdbdash(this.defaults) | ||
this.databases = {} | ||
this.tables = {} | ||
this.indices = {} | ||
} | ||
selectTable(resourceConfig, options) { | ||
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)); | ||
selectTable (resourceConfig, options) { | ||
return this.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; | ||
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 | ||
forEach(keys(params), k => { | ||
let v = params[k]; | ||
forEach(keys(params), function (k) { | ||
let v = params[k] | ||
if (!contains(reserved, k)) { | ||
if (isObject(v)) { | ||
params.where[k] = v; | ||
params.where[k] = v | ||
} else { | ||
params.where[k] = { | ||
'==': v | ||
}; | ||
} | ||
} | ||
delete params[k]; | ||
delete params[k] | ||
} | ||
}); | ||
}) | ||
let query = sequence; | ||
let query = sequence | ||
if (!isEmpty(params.where)) { | ||
query = query.filter(row => { | ||
let subQuery; | ||
forOwn(params.where, (criteria, field) => { | ||
let subQuery | ||
forOwn(params.where, function (criteria, field) { | ||
if (!isObject(criteria)) { | ||
params.where[field] = { | ||
'==': criteria | ||
}; | ||
} | ||
} | ||
forOwn(criteria, (v, op) => { | ||
forOwn(criteria, function (v, op) { | ||
if (op === '==' || op === '===') { | ||
subQuery = subQuery ? subQuery.and(row(field).default(null).eq(v)) : row(field).default(null).eq(v); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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)); | ||
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(); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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); | ||
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)); | ||
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(); | ||
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 | ||
}) | ||
} | ||
@@ -130,9 +130,9 @@ | ||
[params.orderBy, 'asc'] | ||
]; | ||
] | ||
} | ||
for (var i = 0; i < params.orderBy.length; i++) { | ||
if (isString(params.orderBy[i])) { | ||
params.orderBy[i] = [params.orderBy[i], 'asc']; | ||
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]); | ||
query = upperCase(params.orderBy[i][1]) === 'DESC' ? query.orderBy(r.desc(params.orderBy[i][0])) : query.orderBy(params.orderBy[i][0]) | ||
} | ||
@@ -142,91 +142,91 @@ } | ||
if (params.skip) { | ||
query = query.skip(params.skip); | ||
query = query.skip(params.skip) | ||
} | ||
if (params.limit) { | ||
query = query.limit(params.limit); | ||
query = query.limit(params.limit) | ||
} | ||
return query; | ||
return query | ||
} | ||
waitForDb(options) { | ||
options = options || {}; | ||
let db = options.db || this.defaults.db; | ||
waitForDb (options) { | ||
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(); | ||
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) { | ||
options = options || {}; | ||
let db = options.db || this.defaults.db; | ||
waitForTable (table, options) { | ||
options = options || {} | ||
let db = options.db || this.defaults.db | ||
return this.waitForDb(options).then(() => { | ||
this.tables[db] = this.tables[db] || {}; | ||
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(); | ||
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] | ||
}) | ||
} | ||
waitForIndex(table, index, options) { | ||
options = options || {}; | ||
let db = options.db || this.defaults.db; | ||
waitForIndex (table, index, options) { | ||
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] || {}; | ||
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.r.db(db).table(table).indexWait(index).run() | ||
}) | ||
} | ||
return this.tables[db][table][index]; | ||
}); | ||
return this.tables[db][table][index] | ||
}) | ||
} | ||
find(resourceConfig, id, options) { | ||
let newModels = {}; | ||
let models = {}; | ||
let merge = {}; | ||
options = options || {}; | ||
let table = resourceConfig.table || underscore(resourceConfig.name); | ||
let tasks = [this.waitForTable(table, options)]; | ||
find (resourceConfig, id, options) { | ||
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); | ||
let relationName = def.relation | ||
let relationDef = resourceConfig.getResource(relationName) | ||
if (!relationDef) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
throw new JSData.DSErrors.NER(relationName) | ||
} else if (!options.with || !contains(options.with, relationName)) { | ||
return; | ||
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 DSUtils.Promise.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); | ||
let relationName = def.relation | ||
models[relationName] = resourceConfig.getResource(relationName) | ||
if (!options.with || !contains(options.with, relationName)) { | ||
return; | ||
return | ||
} | ||
if (!models[relationName]) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
throw new JSData.DSErrors.NER(relationName) | ||
} | ||
let localKey = def.localKey; | ||
let localField = def.localField; | ||
let foreignKey = def.foreignKey; | ||
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('')); | ||
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'); | ||
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, {index: foreignKey}).coerceTo('ARRAY') | ||
@@ -236,10 +236,10 @@ newModels[localField] = { | ||
relation: 'hasMany' | ||
}; | ||
} | ||
} 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)) | ||
if (localKey) { | ||
merge[localField] = merge[localField].get(localKey); | ||
merge[localField] = merge[localField].get(localKey) | ||
} else { | ||
merge[localField] = merge[localField].getAll(id, {index: foreignKey}).coerceTo('ARRAY'); | ||
merge[localField] = merge[localField].getAll(id, {index: foreignKey}).coerceTo('ARRAY') | ||
} | ||
@@ -250,14 +250,14 @@ | ||
relation: 'hasOne' | ||
}; | ||
} | ||
} | ||
}); | ||
}) | ||
if (!isEmpty(merge)) { | ||
return doc.merge(merge); | ||
return doc.merge(merge) | ||
} | ||
return doc; | ||
}).run(); | ||
return doc | ||
}).run() | ||
}).then(item => { | ||
if (!item) { | ||
return DSUtils.Promise.reject(new Error('Not Found!')); | ||
return DSUtils.Promise.reject(new Error('Not Found!')) | ||
} else { | ||
@@ -267,63 +267,63 @@ forOwn(item, (localValue, localKey) => { | ||
if (isObject(localValue)) { | ||
item[localKey] = item[localKey]; | ||
item[localKey] = item[localKey] | ||
} else if (isArray(localValue)) { | ||
if (newModels[localKey].relation === 'hasOne' && localValue.length) { | ||
item[localKey] = localValue[0]; | ||
item[localKey] = localValue[0] | ||
} else { | ||
item[localKey] = localValue; | ||
item[localKey] = localValue | ||
} | ||
} | ||
} | ||
}); | ||
return item; | ||
}) | ||
return item | ||
} | ||
}); | ||
}) | ||
} | ||
findAll(resourceConfig, params, options) { | ||
options = options || {}; | ||
let table = resourceConfig.table || underscore(resourceConfig.name); | ||
let tasks = [this.waitForTable(table, options)]; | ||
let models = {}; | ||
let merge = {}; | ||
let newModels = {}; | ||
findAll (resourceConfig, params, options) { | ||
options = options || {} | ||
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); | ||
let relationName = def.relation | ||
let relationDef = resourceConfig.getResource(relationName) | ||
if (!relationDef) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
throw new JSData.DSErrors.NER(relationName) | ||
} else if (!options.with || !contains(options.with, relationName)) { | ||
return; | ||
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 DSUtils.Promise.all(tasks).then(() => { | ||
let query = this.filterSequence(this.selectTable(resourceConfig, options), params); | ||
let query = this.filterSequence(this.selectTable(resourceConfig, options), params) | ||
if (options.with && options.with.length) { | ||
query = query.map(doc => { | ||
let id = doc(resourceConfig.idAttribute); | ||
let id = doc(resourceConfig.idAttribute) | ||
forEach(resourceConfig.relationList, def => { | ||
let relationName = def.relation; | ||
models[relationName] = resourceConfig.getResource(relationName); | ||
let relationName = def.relation | ||
models[relationName] = resourceConfig.getResource(relationName) | ||
if (!options.with || !contains(options.with, relationName)) { | ||
return; | ||
return | ||
} | ||
if (!models[relationName]) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
throw new JSData.DSErrors.NER(relationName) | ||
} | ||
let localKey = def.localKey; | ||
let localField = def.localField; | ||
let foreignKey = def.foreignKey; | ||
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('')); | ||
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'); | ||
merge[localField] = this.r.table(models[relationName].table || underscore(models[relationName].name)).getAll(id, {index: foreignKey}).coerceTo('ARRAY') | ||
@@ -333,10 +333,10 @@ newModels[localField] = { | ||
relation: 'hasMany' | ||
}; | ||
} | ||
} 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)) | ||
if (localKey) { | ||
merge[localField] = merge[localField].get(localKey); | ||
merge[localField] = merge[localField].get(localKey) | ||
} else { | ||
merge[localField] = merge[localField].getAll(id, {index: foreignKey}).coerceTo('ARRAY'); | ||
merge[localField] = merge[localField].getAll(id, {index: foreignKey}).coerceTo('ARRAY') | ||
} | ||
@@ -347,71 +347,71 @@ | ||
relation: 'hasOne' | ||
}; | ||
} | ||
} | ||
}); | ||
}) | ||
if (!isEmpty(merge)) { | ||
return doc.merge(merge); | ||
return doc.merge(merge) | ||
} | ||
return doc; | ||
}); | ||
return doc | ||
}) | ||
} | ||
return query.run(); | ||
}); | ||
return query.run() | ||
}) | ||
} | ||
create(resourceConfig, attrs, options) { | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
create (resourceConfig, attrs, options) { | ||
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(); | ||
}).then(cursor => cursor.changes[0].new_val); | ||
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) | ||
} | ||
update(resourceConfig, id, attrs, options) { | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
update (resourceConfig, id, attrs, options) { | ||
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.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, {returnChanges: true}).run() | ||
}).then(cursor => { | ||
if (cursor.changes && cursor.changes.length && cursor.changes[0].new_val) { | ||
return cursor.changes[0].new_val; | ||
return cursor.changes[0].new_val | ||
} else { | ||
return this.selectTable(resourceConfig, options).get(id).run(); | ||
return this.selectTable(resourceConfig, options).get(id).run() | ||
} | ||
}); | ||
}) | ||
} | ||
updateAll(resourceConfig, attrs, params, options) { | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
params = params || {}; | ||
updateAll (resourceConfig, attrs, params, options) { | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])) | ||
options = options || {} | ||
params = params || {} | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return this.filterSequence(this.selectTable(resourceConfig, options), params).update(attrs, {returnChanges: true}).run(); | ||
return this.filterSequence(this.selectTable(resourceConfig, options), params).update(attrs, {returnChanges: true}).run() | ||
}).then(cursor => { | ||
if (cursor && cursor.changes && cursor.changes.length) { | ||
let items = []; | ||
cursor.changes.forEach(change => items.push(change.new_val)); | ||
return items; | ||
let items = [] | ||
cursor.changes.forEach(change => items.push(change.new_val)) | ||
return items | ||
} else { | ||
return this.filterSequence(this.selectTable(resourceConfig, options), params).run(); | ||
return this.filterSequence(this.selectTable(resourceConfig, options), params).run() | ||
} | ||
}); | ||
}) | ||
} | ||
destroy(resourceConfig, id, options) { | ||
options = options || {}; | ||
destroy (resourceConfig, id, options) { | ||
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); | ||
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).delete().run() | ||
}).then(() => undefined) | ||
} | ||
destroyAll(resourceConfig, params, options) { | ||
options = options || {}; | ||
params = params || {}; | ||
destroyAll (resourceConfig, params, options) { | ||
options = options || {} | ||
params = params || {} | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => { | ||
return this.filterSequence(this.selectTable(resourceConfig, options), params).delete().run(); | ||
}).then(() => undefined); | ||
return this.filterSequence(this.selectTable(resourceConfig, options), params).delete().run() | ||
}).then(() => undefined) | ||
} | ||
} | ||
module.exports = DSRethinkDBAdapter; | ||
module.exports = DSRethinkDBAdapter |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69724
1411
101
17
1