Skip to content

Commit 3f586f8

Browse files
committed
Closes #1.
Stable Version 1.0.0-alpha.1.
1 parent 2ec4700 commit 3f586f8

File tree

3 files changed

+82
-23
lines changed

3 files changed

+82
-23
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
##### 1.0.0-alpha.1 - 01 November 2014
2+
3+
###### Backwards compatible API changes
4+
- #1 - auto create db and tables if they don't exist
5+
16
##### 0.2.0 - 03 October 2014
27

38
###### Breaking API changes

package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "js-data-rethinkdb",
33
"description": "RethinkDB adapter for js-data.",
4-
"version": "0.2.0",
5-
"homepage": "http://www.js-data.io/js-data-rethinkdb",
4+
"version": "1.0.0-alpha.1",
5+
"homepage": "http://www.js-data.io/docs/dsrethinkdbadapter",
66
"repository": {
77
"type": "git",
8-
"url": "git://github.com/js-data/js-data-rethinkdb.git"
8+
"url": "https://github.com/js-data/js-data-rethinkdb.git"
99
},
1010
"author": {
1111
"name": "Jason Dobry",
@@ -33,16 +33,16 @@
3333
"grunt-contrib-jshint": "0.10.0",
3434
"grunt-contrib-watch": "0.6.1",
3535
"grunt-karma-coveralls": "2.5.2",
36-
"grunt-mocha-test": "0.12.1",
37-
"jit-grunt": "0.8.0",
38-
"sinon": "1.10.3",
36+
"grunt-mocha-test": "0.12.2",
37+
"jit-grunt": "0.9.0",
38+
"sinon": "1.11.1",
3939
"time-grunt": "1.0.0"
4040
},
4141
"scripts": {
4242
"test": "grunt test"
4343
},
4444
"dependencies": {
45-
"js-data": "~0.4.x",
45+
"js-data": "~1.0.x",
4646
"mout": "0.10.0",
4747
"rethinkdbdash": "~1.15.x"
4848
}

src/index.js

+70-16
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ var reserved = [
3030
'where'
3131
];
3232

33-
function filterQuery(resourceConfig, params) {
33+
function filterQuery(resourceConfig, params, options) {
3434
var r = this.r;
3535
params = params || {};
36+
options = options || {};
3637
params.where = params.where || {};
3738
params.orderBy = params.orderBy || params.sort;
3839
params.skip = params.skip || params.offset;
@@ -51,7 +52,7 @@ function filterQuery(resourceConfig, params) {
5152
}
5253
});
5354

54-
var query = r.db(this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name));
55+
var query = r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name));
5556
var subQuery;
5657

5758
forOwn(params.where, function (criteria, field) {
@@ -131,12 +132,41 @@ function DSRethinkDBAdapter(options) {
131132
this.defaults = new Defaults();
132133
deepMixIn(this.defaults, options);
133134
this.r = rethinkdbdash(this.defaults);
135+
this.databases = {};
136+
this.tables = {};
134137
}
135138

136139
var dsRethinkDBAdapterPrototype = DSRethinkDBAdapter.prototype;
137140

138-
dsRethinkDBAdapterPrototype.find = function find(resourceConfig, id) {
139-
return this.r.db(this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).run().then(function (item) {
141+
dsRethinkDBAdapterPrototype.waitForDb = function waitForDb(options) {
142+
var _this = this;
143+
options = options || {};
144+
var db = options.db || _this.defaults.db;
145+
if (!_this.databases[db]) {
146+
_this.databases[db] = _this.r.branch(_this.r.dbList().contains(db), true, _this.r.dbCreate(db)).run();
147+
}
148+
return _this.databases[db];
149+
};
150+
151+
dsRethinkDBAdapterPrototype.waitForTable = function waitForTable(table, options) {
152+
var _this = this;
153+
options = options || {};
154+
var db = options.db || _this.defaults.db;
155+
return _this.waitForDb(options).then(function () {
156+
_this.tables[db] = _this.tables[db] || {};
157+
if (!_this.tables[db][table]) {
158+
_this.tables[db][table] = _this.r.branch(_this.r.db(db).tableList().contains(table), true, _this.r.db(db).tableCreate(table)).run();
159+
}
160+
return _this.tables[db][table];
161+
});
162+
};
163+
164+
dsRethinkDBAdapterPrototype.find = function find(resourceConfig, id, options) {
165+
var _this = this;
166+
options = options || {};
167+
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
168+
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).run();
169+
}).then(function (item) {
140170
if (!item) {
141171
throw new Error('Not Found!');
142172
} else {
@@ -145,25 +175,41 @@ dsRethinkDBAdapterPrototype.find = function find(resourceConfig, id) {
145175
});
146176
};
147177

148-
dsRethinkDBAdapterPrototype.findAll = function (resourceConfig, params) {
149-
return filterQuery.call(this, resourceConfig, params).run();
178+
dsRethinkDBAdapterPrototype.findAll = function (resourceConfig, params, options) {
179+
var _this = this;
180+
options = options || {};
181+
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
182+
return filterQuery.call(_this, resourceConfig, params, options).run();
183+
});
150184
};
151185

152-
dsRethinkDBAdapterPrototype.create = function (resourceConfig, attrs) {
153-
return this.r.db(this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).insert(attrs, { returnChanges: true }).run().then(function (cursor) {
186+
dsRethinkDBAdapterPrototype.create = function (resourceConfig, attrs, options) {
187+
var _this = this;
188+
options = options || {};
189+
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
190+
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).insert(attrs, { returnChanges: true }).run();
191+
}).then(function (cursor) {
154192
return cursor.changes[0].new_val;
155193
});
156194
};
157195

158-
dsRethinkDBAdapterPrototype.update = function (resourceConfig, id, attrs) {
159-
return this.r.db(this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run().then(function (cursor) {
196+
dsRethinkDBAdapterPrototype.update = function (resourceConfig, id, attrs, options) {
197+
var _this = this;
198+
options = options || {};
199+
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
200+
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run();
201+
}).then(function (cursor) {
160202
return cursor.changes[0].new_val;
161203
});
162204
};
163205

164-
dsRethinkDBAdapterPrototype.updateAll = function (resourceConfig, attrs, params) {
206+
dsRethinkDBAdapterPrototype.updateAll = function (resourceConfig, attrs, params, options) {
207+
var _this = this;
208+
options = options || {};
165209
params = params || {};
166-
return filterQuery.call(this, resourceConfig, params).update(attrs, { returnChanges: true }).run().then(function (cursor) {
210+
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
211+
return filterQuery.call(_this, resourceConfig, params, options).update(attrs, { returnChanges: true }).run();
212+
}).then(function (cursor) {
167213
var items = [];
168214
cursor.changes.forEach(function (change) {
169215
items.push(change.new_val);
@@ -172,15 +218,23 @@ dsRethinkDBAdapterPrototype.updateAll = function (resourceConfig, attrs, params)
172218
});
173219
};
174220

175-
dsRethinkDBAdapterPrototype.destroy = function (resourceConfig, id) {
176-
return this.r.db(this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).delete().run().then(function () {
221+
dsRethinkDBAdapterPrototype.destroy = function (resourceConfig, id, options) {
222+
var _this = this;
223+
options = options || {};
224+
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
225+
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).delete().run();
226+
}).then(function () {
177227
return undefined;
178228
});
179229
};
180230

181-
dsRethinkDBAdapterPrototype.destroyAll = function (resourceConfig, params) {
231+
dsRethinkDBAdapterPrototype.destroyAll = function (resourceConfig, params, options) {
232+
var _this = this;
233+
options = options || {};
182234
params = params || {};
183-
return filterQuery.call(this, resourceConfig, params).delete().run().then(function () {
235+
return _this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () {
236+
return filterQuery.call(_this, resourceConfig, params, options).delete().run();
237+
}).then(function () {
184238
return undefined;
185239
});
186240
};

0 commit comments

Comments
 (0)