js-data-rethinkdb
Advanced tools
Comparing version 3.0.0-beta.3 to 3.0.0-beta.4
@@ -0,1 +1,6 @@ | ||
##### 3.0.0-beta.4 - 10 May 2016 | ||
###### Breaking changes | ||
- Moved `rethinkdbdash` options to `RethinkDBAdapter#rOpts`. | ||
##### 3.0.0-beta.3 - 30 April 2016 | ||
@@ -2,0 +7,0 @@ |
@@ -11,12 +11,6 @@ import {Adapter} from 'js-data-adapter' | ||
interface IBaseRethinkDBAdapter extends IBaseAdapter { | ||
authKey?: string | ||
bufferSize?: number | ||
db?: string | ||
deleteOpts?: IDict | ||
host?: string | ||
insertOpts?: IDict | ||
max?: number | ||
min?: number | ||
operators?: IDict | ||
port?: number | ||
rOpts?: IDict | ||
runOpts?: IDict | ||
@@ -23,0 +17,0 @@ updateOpts?: IDict |
@@ -12,66 +12,5 @@ 'use strict'; | ||
var DEFAULTS = { | ||
/** | ||
* RethinkDB authorization key. | ||
* | ||
* @name RethinkDBAdapter#authKey | ||
* @type {string} | ||
*/ | ||
authKey: '', | ||
/** | ||
* Buffer size for connection pool. | ||
* | ||
* @name RethinkDBAdapter#bufferSize | ||
* @type {number} | ||
* @default 10 | ||
*/ | ||
bufferSize: 10, | ||
/** | ||
* Default database. | ||
* | ||
* @name RethinkDBAdapter#db | ||
* @type {string} | ||
* @default "test" | ||
*/ | ||
db: 'test', | ||
/** | ||
* RethinkDB host. | ||
* | ||
* @name RethinkDBAdapter#host | ||
* @type {string} | ||
* @default "localhost" | ||
*/ | ||
host: 'localhost', | ||
/** | ||
* Minimum connections in pool. | ||
* | ||
* @name RethinkDBAdapter#min | ||
* @type {number} | ||
* @default 10 | ||
*/ | ||
min: 10, | ||
/** | ||
* Maximum connections in pool. | ||
* | ||
* @name RethinkDBAdapter#max | ||
* @type {number} | ||
* @default 50 | ||
*/ | ||
max: 50, | ||
/** | ||
* RethinkDB port. | ||
* | ||
* @name RethinkDBAdapter#port | ||
* @type {number} | ||
* @default 28015 | ||
*/ | ||
port: 28015 | ||
var R_OPTS_DEFAULTS = { | ||
db: 'test' | ||
}; | ||
var INSERT_OPTS_DEFAULTS = {}; | ||
@@ -158,3 +97,3 @@ var UPDATE_OPTS_DEFAULTS = {}; | ||
* import {Container} from 'js-data' | ||
* import RethinkDBAdapter from 'js-data-rethinkdb' | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* | ||
@@ -176,14 +115,9 @@ * // Create a store to hold your Mappers | ||
* @param {Object} [opts] Configuration options. | ||
* @param {string} [opts.authKey=""] See {@link RethinkDBAdapter#authKey}. | ||
* @param {number} [opts.bufferSize=10] See {@link RethinkDBAdapter#bufferSize}. | ||
* @param {string} [opts.db="test"] Default database. | ||
* @param {boolean} [opts.debug=false] See {@link Adapter#debug}. | ||
* @param {Object} [opts.deleteOpts={}] See {@link RethinkDBAdapter#deleteOpts}. | ||
* @param {string} [opts.host="localhost"] See {@link RethinkDBAdapter#host}. | ||
* @param {Object} [opts.insertOpts={}] See {@link RethinkDBAdapter#insertOpts}. | ||
* @param {number} [opts.max=50] See {@link RethinkDBAdapter#max}. | ||
* @param {number} [opts.min=10] See {@link RethinkDBAdapter#min}. | ||
* @param {Object} [opts.operators] See {@link RethinkDBAdapter#operators}. | ||
* @param {number} [opts.port=28015] See {@link RethinkDBAdapter#port}. | ||
* @param {Object} [opts.operators={@link module:js-data-rethinkdb.OPERATORS}] See {@link RethinkDBAdapter#operators}. | ||
* @param {Object} [opts.r] See {@link RethinkDBAdapter#r}. | ||
* @param {boolean} [opts.raw=false] See {@link Adapter#raw}. | ||
* @param {Object} [opts.rOpts={}] See {@link RethinkDBAdapter#rOpts}. | ||
* @param {Object} [opts.runOpts={}] See {@link RethinkDBAdapter#runOpts}. | ||
@@ -193,8 +127,55 @@ * @param {Object} [opts.updateOpts={}] See {@link RethinkDBAdapter#updateOpts}. | ||
function RethinkDBAdapter(opts) { | ||
var self = this; | ||
jsData.utils.classCallCheck(self, RethinkDBAdapter); | ||
jsData.utils.classCallCheck(this, RethinkDBAdapter); | ||
opts || (opts = {}); | ||
jsData.utils.fillIn(opts, DEFAULTS); | ||
jsDataAdapter.Adapter.call(self, opts); | ||
// Setup non-enumerable properties | ||
Object.defineProperties(this, { | ||
/** | ||
* The rethinkdbdash instance used by this adapter. Use this directly when | ||
* you need to write custom queries. | ||
* | ||
* @example <caption>Use default instance.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter() | ||
* adapter.r.dbDrop('foo').then(...) | ||
* | ||
* @example <caption>Configure default instance.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter({ | ||
* rOpts: { | ||
* user: 'myUser', | ||
* password: 'myPassword' | ||
* } | ||
* }) | ||
* adapter.r.dbDrop('foo').then(...) | ||
* | ||
* @example <caption>Provide a custom instance.</caption> | ||
* import rethinkdbdash from 'rethinkdbdash' | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const r = rethinkdbdash() | ||
* const adapter = new RethinkDBAdapter({ | ||
* r: r | ||
* }) | ||
* adapter.r.dbDrop('foo').then(...) | ||
* | ||
* @name RethinkDBAdapter#r | ||
* @type {Object} | ||
*/ | ||
r: { | ||
writable: true, | ||
value: undefined | ||
}, | ||
databases: { | ||
value: {} | ||
}, | ||
indices: { | ||
value: {} | ||
}, | ||
tables: { | ||
value: {} | ||
} | ||
}); | ||
jsDataAdapter.Adapter.call(this, opts); | ||
/** | ||
@@ -207,4 +188,4 @@ * Default options to pass to r#insert. | ||
*/ | ||
self.insertOpts || (self.insertOpts = {}); | ||
jsData.utils.fillIn(self.insertOpts, INSERT_OPTS_DEFAULTS); | ||
this.insertOpts || (this.insertOpts = {}); | ||
jsData.utils.fillIn(this.insertOpts, INSERT_OPTS_DEFAULTS); | ||
@@ -218,4 +199,4 @@ /** | ||
*/ | ||
self.updateOpts || (self.updateOpts = {}); | ||
jsData.utils.fillIn(self.updateOpts, UPDATE_OPTS_DEFAULTS); | ||
this.updateOpts || (this.updateOpts = {}); | ||
jsData.utils.fillIn(this.updateOpts, UPDATE_OPTS_DEFAULTS); | ||
@@ -229,4 +210,4 @@ /** | ||
*/ | ||
self.deleteOpts || (self.deleteOpts = {}); | ||
jsData.utils.fillIn(self.deleteOpts, DELETE_OPTS_DEFAULTS); | ||
this.deleteOpts || (this.deleteOpts = {}); | ||
jsData.utils.fillIn(this.deleteOpts, DELETE_OPTS_DEFAULTS); | ||
@@ -240,7 +221,7 @@ /** | ||
*/ | ||
self.runOpts || (self.runOpts = {}); | ||
jsData.utils.fillIn(self.runOpts, RUN_OPTS_DEFAULTS); | ||
this.runOpts || (this.runOpts = {}); | ||
jsData.utils.fillIn(this.runOpts, RUN_OPTS_DEFAULTS); | ||
/** | ||
* Override the default predicate functions for specified operators. | ||
* Override the default predicate functions for the specified operators. | ||
* | ||
@@ -251,17 +232,52 @@ * @name RethinkDBAdapter#operators | ||
*/ | ||
self.operators || (self.operators = {}); | ||
this.operators || (this.operators = {}); | ||
jsData.utils.fillIn(this.operators, OPERATORS); | ||
jsData.utils.fillIn(self.operators, OPERATORS); | ||
/** | ||
* The rethinkdbdash instance used by this adapter. Use this directly when you | ||
* need to write custom queries. | ||
* Options to pass to a new `rethinkdbdash` instance, if one was not provided | ||
* at {@link RethinkDBAdapter#r}. See the [rethinkdbdash README][readme] for | ||
* instance options. | ||
* | ||
* @name RethinkDBAdapter#r | ||
* [readme]: https://github.com/neumino/rethinkdbdash#importing-the-driver | ||
* | ||
* @example <caption>Connect to localhost:8080, and let the driver find other instances.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter({ | ||
* rOpts: { | ||
* discovery: true | ||
* } | ||
* }) | ||
* | ||
* @example <caption>Connect to and only to localhost:8080.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter() | ||
* | ||
* @example <caption>Do not create a connection pool.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter({ | ||
* rOpts: { | ||
* pool: false | ||
* } | ||
* }) | ||
* | ||
* @example <caption>Connect to a cluster seeding from `192.168.0.100`, `192.168.0.101`, `192.168.0.102`.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter({ | ||
* rOpts: { | ||
* servers: [ | ||
* { host: '192.168.0.100', port: 28015 }, | ||
* { host: '192.168.0.101', port: 28015 }, | ||
* { host: '192.168.0.102', port: 28015 } | ||
* ] | ||
* } | ||
* }) | ||
* | ||
* @name RethinkDBAdapter#rOpts | ||
* @see https://github.com/neumino/rethinkdbdash#importing-the-driver | ||
* @type {Object} | ||
*/ | ||
self.r = rethinkdbdash(opts); | ||
self.databases = {}; | ||
self.tables = {}; | ||
self.indices = {}; | ||
this.rOpts || (this.rOpts = {}); | ||
jsData.utils.fillIn(this.rOpts, R_OPTS_DEFAULTS); | ||
this.r || (this.r = rethinkdbdash(this.rOpts)); | ||
} | ||
@@ -298,8 +314,8 @@ | ||
* | ||
* @name RethinkDBAdapter.extend | ||
* @method | ||
* @method RethinkDBAdapter.extend | ||
* @static | ||
* @param {Object} [instanceProps] Properties that will be added to the | ||
* prototype of the Subclass. | ||
* prototype of the subclass. | ||
* @param {Object} [classProps] Properties that will be added as static | ||
* properties to the Subclass itself. | ||
* properties to the subclass itself. | ||
* @return {Constructor} Subclass of `RethinkDBAdapter`. | ||
@@ -471,3 +487,3 @@ */ | ||
selectDb: function selectDb(opts) { | ||
return this.r.db(jsData.utils.isUndefined(opts.db) ? this.db : opts.db); | ||
return this.r.db(jsData.utils.isUndefined(opts.db) ? this.rOpts.db : opts.db); | ||
}, | ||
@@ -497,5 +513,6 @@ selectTable: function selectTable(mapper, opts) { | ||
filterSequence: function filterSequence(sequence, query, opts) { | ||
var self = this; | ||
var r = self.r; | ||
var _this = this; | ||
var r = this.r; | ||
query = jsData.utils.plainCopy(query || {}); | ||
@@ -542,3 +559,3 @@ opts || (opts = {}); | ||
} | ||
var predicateFn = self.getOperator(operator, opts); | ||
var predicateFn = _this.getOperator(operator, opts); | ||
if (predicateFn) { | ||
@@ -586,40 +603,39 @@ var predicateResult = predicateFn(r, row, field, value); | ||
waitForDb: function waitForDb(opts) { | ||
var self = this; | ||
opts || (opts = {}); | ||
var db = jsData.utils.isUndefined(opts.db) ? self.db : opts.db; | ||
if (!self.databases[db]) { | ||
self.databases[db] = self.r.branch(self.r.dbList().contains(db), true, self.r.dbCreate(db)).run(); | ||
var db = jsData.utils.isUndefined(opts.db) ? this.rOpts.db : opts.db; | ||
if (!this.databases[db]) { | ||
this.databases[db] = this.r.branch(this.r.dbList().contains(db), true, this.r.dbCreate(db)).run(); | ||
} | ||
return self.databases[db]; | ||
return this.databases[db]; | ||
}, | ||
waitForTable: function waitForTable(mapper, options) { | ||
var _this = this; | ||
waitForTable: function waitForTable(mapper, opts) { | ||
var _this2 = this; | ||
opts || (opts = {}); | ||
var table = jsData.utils.isString(mapper) ? mapper : mapper.table || underscore(mapper.name); | ||
options = options || {}; | ||
var db = jsData.utils.isUndefined(options.db) ? this.db : options.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(); | ||
var db = jsData.utils.isUndefined(opts.db) ? this.rOpts.db : opts.db; | ||
return this.waitForDb(opts).then(function () { | ||
_this2.tables[db] = _this2.tables[db] || {}; | ||
if (!_this2.tables[db][table]) { | ||
_this2.tables[db][table] = _this2.r.branch(_this2.r.db(db).tableList().contains(table), true, _this2.r.db(db).tableCreate(table)).run(); | ||
} | ||
return _this.tables[db][table]; | ||
return _this2.tables[db][table]; | ||
}); | ||
}, | ||
waitForIndex: function waitForIndex(table, index, options) { | ||
var _this2 = this; | ||
waitForIndex: function waitForIndex(table, index, opts) { | ||
var _this3 = this; | ||
options = options || {}; | ||
var db = jsData.utils.isUndefined(options.db) ? this.db : options.db; | ||
return this.waitForDb(options).then(function () { | ||
return _this2.waitForTable(table, options); | ||
opts || (opts = {}); | ||
var db = jsData.utils.isUndefined(opts.db) ? this.rOpts.db : opts.db; | ||
return this.waitForDb(opts).then(function () { | ||
return _this3.waitForTable(table, opts); | ||
}).then(function () { | ||
_this2.indices[db] = _this2.indices[db] || {}; | ||
_this2.indices[db][table] = _this2.indices[db][table] || {}; | ||
if (!_this2.tables[db][table][index]) { | ||
_this2.tables[db][table][index] = _this2.r.branch(_this2.r.db(db).table(table).indexList().contains(index), true, _this2.r.db(db).table(table).indexCreate(index)).run().then(function () { | ||
return _this2.r.db(db).table(table).indexWait(index).run(); | ||
_this3.indices[db] = _this3.indices[db] || {}; | ||
_this3.indices[db][table] = _this3.indices[db][table] || {}; | ||
if (!_this3.tables[db][table][index]) { | ||
_this3.tables[db][table][index] = _this3.r.branch(_this3.r.db(db).table(table).indexList().contains(index), true, _this3.r.db(db).table(table).indexCreate(index)).run().then(function () { | ||
return _this3.r.db(db).table(table).indexWait(index).run(); | ||
}); | ||
} | ||
return _this2.tables[db][table][index]; | ||
return _this3.tables[db][table][index]; | ||
}); | ||
@@ -651,8 +667,9 @@ }, | ||
count: function count(mapper, query, opts) { | ||
var self = this; | ||
var _this4 = this; | ||
opts || (opts = {}); | ||
query || (query = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.count.call(self, mapper, query, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.count.call(_this4, mapper, query, opts); | ||
}); | ||
@@ -677,8 +694,9 @@ }, | ||
create: function create(mapper, props, opts) { | ||
var self = this; | ||
var _this5 = this; | ||
props || (props = {}); | ||
opts || (opts = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.create.call(self, mapper, props, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.create.call(_this5, mapper, props, opts); | ||
}); | ||
@@ -703,8 +721,9 @@ }, | ||
createMany: function createMany(mapper, props, opts) { | ||
var self = this; | ||
var _this6 = this; | ||
props || (props = {}); | ||
opts || (opts = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.createMany.call(self, mapper, props, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.createMany.call(_this6, mapper, props, opts); | ||
}); | ||
@@ -729,7 +748,8 @@ }, | ||
destroy: function destroy(mapper, id, opts) { | ||
var self = this; | ||
var _this7 = this; | ||
opts || (opts = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.destroy.call(self, mapper, id, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.destroy.call(_this7, mapper, id, opts); | ||
}); | ||
@@ -762,8 +782,9 @@ }, | ||
destroyAll: function destroyAll(mapper, query, opts) { | ||
var self = this; | ||
var _this8 = this; | ||
opts || (opts = {}); | ||
query || (query = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.destroyAll.call(self, mapper, query, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.destroyAll.call(_this8, mapper, query, opts); | ||
}); | ||
@@ -788,3 +809,4 @@ }, | ||
find: function find(mapper, id, opts) { | ||
var self = this; | ||
var _this9 = this; | ||
opts || (opts = {}); | ||
@@ -794,3 +816,3 @@ opts.with || (opts.with = []); | ||
var relationList = mapper.relationList || []; | ||
var tasks = [self.waitForTable(mapper, opts)]; | ||
var tasks = [this.waitForTable(mapper, opts)]; | ||
@@ -805,5 +827,5 @@ relationList.forEach(function (def) { | ||
if (def.type === 'belongsTo') { | ||
tasks.push(self.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)); | ||
tasks.push(_this9.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)); | ||
} else { | ||
tasks.push(self.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)); | ||
tasks.push(_this9.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)); | ||
} | ||
@@ -813,3 +835,3 @@ } | ||
return Promise.all(tasks).then(function () { | ||
return __super__.find.call(self, mapper, id, opts); | ||
return __super__.find.call(_this9, mapper, id, opts); | ||
}); | ||
@@ -842,3 +864,4 @@ }, | ||
findAll: function findAll(mapper, query, opts) { | ||
var self = this; | ||
var _this10 = this; | ||
opts || (opts = {}); | ||
@@ -849,3 +872,3 @@ opts.with || (opts.with = []); | ||
var relationList = mapper.relationList || []; | ||
var tasks = [self.waitForTable(mapper, opts)]; | ||
var tasks = [this.waitForTable(mapper, opts)]; | ||
@@ -860,5 +883,5 @@ relationList.forEach(function (def) { | ||
if (def.type === 'belongsTo') { | ||
tasks.push(self.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)); | ||
tasks.push(_this10.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)); | ||
} else { | ||
tasks.push(self.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)); | ||
tasks.push(_this10.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)); | ||
} | ||
@@ -868,3 +891,3 @@ } | ||
return Promise.all(tasks).then(function () { | ||
return __super__.findAll.call(self, mapper, query, opts); | ||
return __super__.findAll.call(_this10, mapper, query, opts); | ||
}); | ||
@@ -918,8 +941,9 @@ }, | ||
sum: function sum(mapper, field, query, opts) { | ||
var self = this; | ||
var _this11 = this; | ||
opts || (opts = {}); | ||
query || (query = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.sum.call(self, mapper, field, query, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.sum.call(_this11, mapper, field, query, opts); | ||
}); | ||
@@ -945,8 +969,9 @@ }, | ||
update: function update(mapper, id, props, opts) { | ||
var self = this; | ||
var _this12 = this; | ||
props || (props = {}); | ||
opts || (opts = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.update.call(self, mapper, id, props, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.update.call(_this12, mapper, id, props, opts); | ||
}); | ||
@@ -980,3 +1005,4 @@ }, | ||
updateAll: function updateAll(mapper, props, query, opts) { | ||
var self = this; | ||
var _this13 = this; | ||
props || (props = {}); | ||
@@ -986,4 +1012,4 @@ query || (query = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.updateAll.call(self, mapper, props, query, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.updateAll.call(_this13, mapper, props, query, opts); | ||
}); | ||
@@ -1008,8 +1034,9 @@ }, | ||
updateMany: function updateMany(mapper, records, opts) { | ||
var self = this; | ||
var _this14 = this; | ||
records || (records = []); | ||
opts || (opts = {}); | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.updateMany.call(self, mapper, records, opts); | ||
return this.waitForTable(mapper, opts).then(function () { | ||
return __super__.updateMany.call(_this14, mapper, records, opts); | ||
}); | ||
@@ -1022,2 +1049,10 @@ } | ||
* | ||
* @example <caption>ES2015 modules import</caption> | ||
* import {version} from 'js-data-rethinkdb' | ||
* console.log(version.full) | ||
* | ||
* @example <caption>CommonJS import</caption> | ||
* var version = require('js-data-rethinkdb').version | ||
* console.log(version.full) | ||
* | ||
* @name module:js-data-rethinkdb.version | ||
@@ -1035,4 +1070,4 @@ * @type {Object} | ||
var version = { | ||
beta: 3, | ||
full: '3.0.0-beta.3', | ||
beta: 4, | ||
full: '3.0.0-beta.4', | ||
major: 3, | ||
@@ -1039,0 +1074,0 @@ minor: 0, |
{ | ||
"name": "js-data-rethinkdb", | ||
"description": "RethinkDB adapter for js-data.", | ||
"version": "3.0.0-beta.3", | ||
"version": "3.0.0-beta.4", | ||
"homepage": "https://github.com/js-data/js-data-rethinkdb", | ||
@@ -63,12 +63,12 @@ "repository": { | ||
"peerDependencies": { | ||
"js-data": "^3.0.0-beta.3", | ||
"rethinkdbdash": ">=1.15.0" | ||
"js-data": "^3.0.0-beta.5", | ||
"rethinkdbdash": ">=2.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel-polyfill": "6.7.4", | ||
"babel-polyfill": "6.8.0", | ||
"babel-preset-es2015-rollup": "1.1.1", | ||
"istanbul": "0.4.3", | ||
"js-data-adapter-tests": "^2.0.0-alpha.16", | ||
"js-data-repo-tools": "0.5.0", | ||
"rollup": "0.26.1", | ||
"js-data-adapter-tests": "^2.0.0-alpha.20", | ||
"js-data-repo-tools": "0.5.1", | ||
"rollup": "0.26.2", | ||
"rollup-plugin-babel": "2.4.0", | ||
@@ -75,0 +75,0 @@ "source-map-support": "0.4.0", |
391
src/index.js
@@ -11,66 +11,5 @@ import {utils} from 'js-data' | ||
const DEFAULTS = { | ||
/** | ||
* RethinkDB authorization key. | ||
* | ||
* @name RethinkDBAdapter#authKey | ||
* @type {string} | ||
*/ | ||
authKey: '', | ||
/** | ||
* Buffer size for connection pool. | ||
* | ||
* @name RethinkDBAdapter#bufferSize | ||
* @type {number} | ||
* @default 10 | ||
*/ | ||
bufferSize: 10, | ||
/** | ||
* Default database. | ||
* | ||
* @name RethinkDBAdapter#db | ||
* @type {string} | ||
* @default "test" | ||
*/ | ||
db: 'test', | ||
/** | ||
* RethinkDB host. | ||
* | ||
* @name RethinkDBAdapter#host | ||
* @type {string} | ||
* @default "localhost" | ||
*/ | ||
host: 'localhost', | ||
/** | ||
* Minimum connections in pool. | ||
* | ||
* @name RethinkDBAdapter#min | ||
* @type {number} | ||
* @default 10 | ||
*/ | ||
min: 10, | ||
/** | ||
* Maximum connections in pool. | ||
* | ||
* @name RethinkDBAdapter#max | ||
* @type {number} | ||
* @default 50 | ||
*/ | ||
max: 50, | ||
/** | ||
* RethinkDB port. | ||
* | ||
* @name RethinkDBAdapter#port | ||
* @type {number} | ||
* @default 28015 | ||
*/ | ||
port: 28015 | ||
const R_OPTS_DEFAULTS = { | ||
db: 'test' | ||
} | ||
const INSERT_OPTS_DEFAULTS = {} | ||
@@ -157,3 +96,3 @@ const UPDATE_OPTS_DEFAULTS = {} | ||
* import {Container} from 'js-data' | ||
* import RethinkDBAdapter from 'js-data-rethinkdb' | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* | ||
@@ -175,14 +114,9 @@ * // Create a store to hold your Mappers | ||
* @param {Object} [opts] Configuration options. | ||
* @param {string} [opts.authKey=""] See {@link RethinkDBAdapter#authKey}. | ||
* @param {number} [opts.bufferSize=10] See {@link RethinkDBAdapter#bufferSize}. | ||
* @param {string} [opts.db="test"] Default database. | ||
* @param {boolean} [opts.debug=false] See {@link Adapter#debug}. | ||
* @param {Object} [opts.deleteOpts={}] See {@link RethinkDBAdapter#deleteOpts}. | ||
* @param {string} [opts.host="localhost"] See {@link RethinkDBAdapter#host}. | ||
* @param {Object} [opts.insertOpts={}] See {@link RethinkDBAdapter#insertOpts}. | ||
* @param {number} [opts.max=50] See {@link RethinkDBAdapter#max}. | ||
* @param {number} [opts.min=10] See {@link RethinkDBAdapter#min}. | ||
* @param {Object} [opts.operators] See {@link RethinkDBAdapter#operators}. | ||
* @param {number} [opts.port=28015] See {@link RethinkDBAdapter#port}. | ||
* @param {Object} [opts.operators={@link module:js-data-rethinkdb.OPERATORS}] See {@link RethinkDBAdapter#operators}. | ||
* @param {Object} [opts.r] See {@link RethinkDBAdapter#r}. | ||
* @param {boolean} [opts.raw=false] See {@link Adapter#raw}. | ||
* @param {Object} [opts.rOpts={}] See {@link RethinkDBAdapter#rOpts}. | ||
* @param {Object} [opts.runOpts={}] See {@link RethinkDBAdapter#runOpts}. | ||
@@ -192,8 +126,55 @@ * @param {Object} [opts.updateOpts={}] See {@link RethinkDBAdapter#updateOpts}. | ||
export function RethinkDBAdapter (opts) { | ||
const self = this | ||
utils.classCallCheck(self, RethinkDBAdapter) | ||
utils.classCallCheck(this, RethinkDBAdapter) | ||
opts || (opts = {}) | ||
utils.fillIn(opts, DEFAULTS) | ||
Adapter.call(self, opts) | ||
// Setup non-enumerable properties | ||
Object.defineProperties(this, { | ||
/** | ||
* The rethinkdbdash instance used by this adapter. Use this directly when | ||
* you need to write custom queries. | ||
* | ||
* @example <caption>Use default instance.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter() | ||
* adapter.r.dbDrop('foo').then(...) | ||
* | ||
* @example <caption>Configure default instance.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter({ | ||
* rOpts: { | ||
* user: 'myUser', | ||
* password: 'myPassword' | ||
* } | ||
* }) | ||
* adapter.r.dbDrop('foo').then(...) | ||
* | ||
* @example <caption>Provide a custom instance.</caption> | ||
* import rethinkdbdash from 'rethinkdbdash' | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const r = rethinkdbdash() | ||
* const adapter = new RethinkDBAdapter({ | ||
* r: r | ||
* }) | ||
* adapter.r.dbDrop('foo').then(...) | ||
* | ||
* @name RethinkDBAdapter#r | ||
* @type {Object} | ||
*/ | ||
r: { | ||
writable: true, | ||
value: undefined | ||
}, | ||
databases: { | ||
value: {} | ||
}, | ||
indices: { | ||
value: {} | ||
}, | ||
tables: { | ||
value: {} | ||
} | ||
}) | ||
Adapter.call(this, opts) | ||
/** | ||
@@ -206,4 +187,4 @@ * Default options to pass to r#insert. | ||
*/ | ||
self.insertOpts || (self.insertOpts = {}) | ||
utils.fillIn(self.insertOpts, INSERT_OPTS_DEFAULTS) | ||
this.insertOpts || (this.insertOpts = {}) | ||
utils.fillIn(this.insertOpts, INSERT_OPTS_DEFAULTS) | ||
@@ -217,4 +198,4 @@ /** | ||
*/ | ||
self.updateOpts || (self.updateOpts = {}) | ||
utils.fillIn(self.updateOpts, UPDATE_OPTS_DEFAULTS) | ||
this.updateOpts || (this.updateOpts = {}) | ||
utils.fillIn(this.updateOpts, UPDATE_OPTS_DEFAULTS) | ||
@@ -228,4 +209,4 @@ /** | ||
*/ | ||
self.deleteOpts || (self.deleteOpts = {}) | ||
utils.fillIn(self.deleteOpts, DELETE_OPTS_DEFAULTS) | ||
this.deleteOpts || (this.deleteOpts = {}) | ||
utils.fillIn(this.deleteOpts, DELETE_OPTS_DEFAULTS) | ||
@@ -239,7 +220,7 @@ /** | ||
*/ | ||
self.runOpts || (self.runOpts = {}) | ||
utils.fillIn(self.runOpts, RUN_OPTS_DEFAULTS) | ||
this.runOpts || (this.runOpts = {}) | ||
utils.fillIn(this.runOpts, RUN_OPTS_DEFAULTS) | ||
/** | ||
* Override the default predicate functions for specified operators. | ||
* Override the default predicate functions for the specified operators. | ||
* | ||
@@ -250,17 +231,52 @@ * @name RethinkDBAdapter#operators | ||
*/ | ||
self.operators || (self.operators = {}) | ||
this.operators || (this.operators = {}) | ||
utils.fillIn(this.operators, OPERATORS) | ||
utils.fillIn(self.operators, OPERATORS) | ||
/** | ||
* The rethinkdbdash instance used by this adapter. Use this directly when you | ||
* need to write custom queries. | ||
* Options to pass to a new `rethinkdbdash` instance, if one was not provided | ||
* at {@link RethinkDBAdapter#r}. See the [rethinkdbdash README][readme] for | ||
* instance options. | ||
* | ||
* @name RethinkDBAdapter#r | ||
* [readme]: https://github.com/neumino/rethinkdbdash#importing-the-driver | ||
* | ||
* @example <caption>Connect to localhost:8080, and let the driver find other instances.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter({ | ||
* rOpts: { | ||
* discovery: true | ||
* } | ||
* }) | ||
* | ||
* @example <caption>Connect to and only to localhost:8080.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter() | ||
* | ||
* @example <caption>Do not create a connection pool.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter({ | ||
* rOpts: { | ||
* pool: false | ||
* } | ||
* }) | ||
* | ||
* @example <caption>Connect to a cluster seeding from `192.168.0.100`, `192.168.0.101`, `192.168.0.102`.</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter({ | ||
* rOpts: { | ||
* servers: [ | ||
* { host: '192.168.0.100', port: 28015 }, | ||
* { host: '192.168.0.101', port: 28015 }, | ||
* { host: '192.168.0.102', port: 28015 } | ||
* ] | ||
* } | ||
* }) | ||
* | ||
* @name RethinkDBAdapter#rOpts | ||
* @see https://github.com/neumino/rethinkdbdash#importing-the-driver | ||
* @type {Object} | ||
*/ | ||
self.r = rethinkdbdash(opts) | ||
self.databases = {} | ||
self.tables = {} | ||
self.indices = {} | ||
this.rOpts || (this.rOpts = {}) | ||
utils.fillIn(this.rOpts, R_OPTS_DEFAULTS) | ||
this.r || (this.r = rethinkdbdash(this.rOpts)) | ||
} | ||
@@ -297,8 +313,8 @@ | ||
* | ||
* @name RethinkDBAdapter.extend | ||
* @method | ||
* @method RethinkDBAdapter.extend | ||
* @static | ||
* @param {Object} [instanceProps] Properties that will be added to the | ||
* prototype of the Subclass. | ||
* prototype of the subclass. | ||
* @param {Object} [classProps] Properties that will be added as static | ||
* properties to the Subclass itself. | ||
* properties to the subclass itself. | ||
* @return {Constructor} Subclass of `RethinkDBAdapter`. | ||
@@ -502,3 +518,3 @@ */ | ||
selectDb (opts) { | ||
return this.r.db(utils.isUndefined(opts.db) ? this.db : opts.db) | ||
return this.r.db(utils.isUndefined(opts.db) ? this.rOpts.db : opts.db) | ||
}, | ||
@@ -528,4 +544,3 @@ | ||
filterSequence (sequence, query, opts) { | ||
const self = this | ||
const r = self.r | ||
const r = this.r | ||
@@ -541,3 +556,3 @@ query = utils.plainCopy(query || {}) | ||
// Transform non-keyword properties to "where" clause configuration | ||
utils.forOwn(query, function (config, keyword) { | ||
utils.forOwn(query, (config, keyword) => { | ||
if (reserved.indexOf(keyword) === -1) { | ||
@@ -560,6 +575,6 @@ if (utils.isObject(config)) { | ||
// Filter sequence using filter function | ||
rql = rql.filter(function (row) { | ||
rql = rql.filter((row) => { | ||
let subQuery | ||
// Apply filter for each field | ||
utils.forOwn(query.where, function (criteria, field) { | ||
utils.forOwn(query.where, (criteria, field) => { | ||
if (!utils.isObject(criteria)) { | ||
@@ -569,3 +584,3 @@ criteria = { '==': criteria } | ||
// Apply filter for each operator | ||
utils.forOwn(criteria, function (value, operator) { | ||
utils.forOwn(criteria, (value, operator) => { | ||
let isOr = false | ||
@@ -576,3 +591,3 @@ if (operator && operator[0] === '|') { | ||
} | ||
let predicateFn = self.getOperator(operator, opts) | ||
let predicateFn = this.getOperator(operator, opts) | ||
if (predicateFn) { | ||
@@ -623,20 +638,19 @@ const predicateResult = predicateFn(r, row, field, value) | ||
waitForDb (opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
const db = utils.isUndefined(opts.db) ? self.db : opts.db | ||
if (!self.databases[db]) { | ||
self.databases[db] = self.r.branch( | ||
self.r.dbList().contains(db), | ||
const db = utils.isUndefined(opts.db) ? this.rOpts.db : opts.db | ||
if (!this.databases[db]) { | ||
this.databases[db] = this.r.branch( | ||
this.r.dbList().contains(db), | ||
true, | ||
self.r.dbCreate(db) | ||
this.r.dbCreate(db) | ||
).run() | ||
} | ||
return self.databases[db] | ||
return this.databases[db] | ||
}, | ||
waitForTable (mapper, options) { | ||
waitForTable (mapper, opts) { | ||
opts || (opts = {}) | ||
const table = utils.isString(mapper) ? mapper : (mapper.table || underscore(mapper.name)) | ||
options = options || {} | ||
let db = utils.isUndefined(options.db) ? this.db : options.db | ||
return this.waitForDb(options).then(() => { | ||
let db = utils.isUndefined(opts.db) ? this.rOpts.db : opts.db | ||
return this.waitForDb(opts).then(() => { | ||
this.tables[db] = this.tables[db] || {} | ||
@@ -650,6 +664,6 @@ if (!this.tables[db][table]) { | ||
waitForIndex (table, index, options) { | ||
options = options || {} | ||
let db = utils.isUndefined(options.db) ? this.db : options.db | ||
return this.waitForDb(options).then(() => this.waitForTable(table, options)).then(() => { | ||
waitForIndex (table, index, opts) { | ||
opts || (opts = {}) | ||
let db = utils.isUndefined(opts.db) ? this.rOpts.db : opts.db | ||
return this.waitForDb(opts).then(() => this.waitForTable(table, opts)).then(() => { | ||
this.indices[db] = this.indices[db] || {} | ||
@@ -688,9 +702,7 @@ this.indices[db][table] = this.indices[db][table] || {} | ||
count (mapper, query, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
query || (query = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.count.call(self, mapper, query, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.count.call(this, mapper, query, opts)) | ||
}, | ||
@@ -713,9 +725,7 @@ | ||
create (mapper, props, opts) { | ||
const self = this | ||
props || (props = {}) | ||
opts || (opts = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.create.call(self, mapper, props, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.create.call(this, mapper, props, opts)) | ||
}, | ||
@@ -738,9 +748,7 @@ | ||
createMany (mapper, props, opts) { | ||
const self = this | ||
props || (props = {}) | ||
opts || (opts = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.createMany.call(self, mapper, props, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.createMany.call(this, mapper, props, opts)) | ||
}, | ||
@@ -763,8 +771,6 @@ | ||
destroy (mapper, id, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.destroy.call(self, mapper, id, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.destroy.call(this, mapper, id, opts)) | ||
}, | ||
@@ -795,9 +801,7 @@ | ||
destroyAll (mapper, query, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
query || (query = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.destroyAll.call(self, mapper, query, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.destroyAll.call(this, mapper, query, opts)) | ||
}, | ||
@@ -820,3 +824,2 @@ | ||
find (mapper, id, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
@@ -826,5 +829,5 @@ opts.with || (opts.with = []) | ||
const relationList = mapper.relationList || [] | ||
let tasks = [self.waitForTable(mapper, opts)] | ||
let tasks = [this.waitForTable(mapper, opts)] | ||
relationList.forEach(function (def) { | ||
relationList.forEach((def) => { | ||
const relationName = def.relation | ||
@@ -837,11 +840,9 @@ const relationDef = def.getRelation() | ||
if (def.type === 'belongsTo') { | ||
tasks.push(self.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)) | ||
tasks.push(this.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)) | ||
} else { | ||
tasks.push(self.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)) | ||
tasks.push(this.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)) | ||
} | ||
} | ||
}) | ||
return Promise.all(tasks).then(function () { | ||
return __super__.find.call(self, mapper, id, opts) | ||
}) | ||
return Promise.all(tasks).then(() => __super__.find.call(this, mapper, id, opts)) | ||
}, | ||
@@ -872,3 +873,2 @@ | ||
findAll (mapper, query, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
@@ -879,5 +879,5 @@ opts.with || (opts.with = []) | ||
const relationList = mapper.relationList || [] | ||
let tasks = [self.waitForTable(mapper, opts)] | ||
let tasks = [this.waitForTable(mapper, opts)] | ||
relationList.forEach(function (def) { | ||
relationList.forEach((def) => { | ||
const relationName = def.relation | ||
@@ -890,11 +890,9 @@ const relationDef = def.getRelation() | ||
if (def.type === 'belongsTo') { | ||
tasks.push(self.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)) | ||
tasks.push(this.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)) | ||
} else { | ||
tasks.push(self.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)) | ||
tasks.push(this.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)) | ||
} | ||
} | ||
}) | ||
return Promise.all(tasks).then(function () { | ||
return __super__.findAll.call(self, mapper, query, opts) | ||
}) | ||
return Promise.all(tasks).then(() => __super__.findAll.call(this, mapper, query, opts)) | ||
}, | ||
@@ -945,9 +943,7 @@ | ||
sum (mapper, field, query, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
query || (query = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.sum.call(self, mapper, field, query, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.sum.call(this, mapper, field, query, opts)) | ||
}, | ||
@@ -971,9 +967,7 @@ | ||
update (mapper, id, props, opts) { | ||
const self = this | ||
props || (props = {}) | ||
opts || (opts = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.update.call(self, mapper, id, props, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.update.call(this, mapper, id, props, opts)) | ||
}, | ||
@@ -1005,3 +999,2 @@ | ||
updateAll (mapper, props, query, opts) { | ||
const self = this | ||
props || (props = {}) | ||
@@ -1011,5 +1004,4 @@ query || (query = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.updateAll.call(self, mapper, props, query, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.updateAll.call(this, mapper, props, query, opts)) | ||
}, | ||
@@ -1032,9 +1024,7 @@ | ||
updateMany (mapper, records, opts) { | ||
const self = this | ||
records || (records = []) | ||
opts || (opts = {}) | ||
return self.waitForTable(mapper, opts).then(function () { | ||
return __super__.updateMany.call(self, mapper, records, opts) | ||
}) | ||
return this.waitForTable(mapper, opts) | ||
.then(() => __super__.updateMany.call(this, mapper, records, opts)) | ||
} | ||
@@ -1046,2 +1036,10 @@ }) | ||
* | ||
* @example <caption>ES2015 modules import</caption> | ||
* import {version} from 'js-data-rethinkdb' | ||
* console.log(version.full) | ||
* | ||
* @example <caption>CommonJS import</caption> | ||
* var version = require('js-data-rethinkdb').version | ||
* console.log(version.full) | ||
* | ||
* @name module:js-data-rethinkdb.version | ||
@@ -1060,2 +1058,47 @@ * @type {Object} | ||
/** | ||
* {@link RethinkDBAdapter} class. | ||
* | ||
* @example <caption>ES2015 modules import</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter() | ||
* | ||
* @example <caption>CommonJS import</caption> | ||
* var RethinkDBAdapter = require('js-data-rethinkdb').RethinkDBAdapter | ||
* var adapter = new RethinkDBAdapter() | ||
* | ||
* @name module:js-data-rethinkdb.RethinkDBAdapter | ||
* @see RethinkDBAdapter | ||
* @type {Constructor} | ||
*/ | ||
/** | ||
* {@link RethinkDBAdapter} class. | ||
* | ||
* @example <caption>ES2015 modules "default" import</caption> | ||
* import RethinkDBAdapter from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter() | ||
* | ||
* @name module:js-data-rethinkdb.default | ||
* @see RethinkDBAdapter | ||
* @type {Constructor} | ||
*/ | ||
/** | ||
* Registered as `js-data-rethinkdb` in NPM. | ||
* | ||
* @example <caption>Install from NPM</caption> | ||
* npm i --save js-data-rethinkdb@beta js-data@beta rethinkdbdash | ||
* | ||
* @example <caption>ES2015 modules import</caption> | ||
* import {RethinkDBAdapter} from 'js-data-rethinkdb' | ||
* const adapter = new RethinkDBAdapter() | ||
* | ||
* @example <caption>CommonJS import</caption> | ||
* var RethinkDBAdapter = require('js-data-rethinkdb').RethinkDBAdapter | ||
* var adapter = new RethinkDBAdapter() | ||
* | ||
* @module js-data-rethinkdb | ||
*/ | ||
export default RethinkDBAdapter |
Sorry, the diff of this file is not supported yet
135960
1966