js-data-rethinkdb
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -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": { |
413
src/index.js
@@ -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; |
Sorry, the diff of this file is not supported yet
57731
19
1218
15