Skip to content
This repository has been archived by the owner on May 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #30 from jmdobry/lazy-db-create
Browse files Browse the repository at this point in the history
Lazy db create
  • Loading branch information
jmdobry committed Mar 16, 2014
2 parents ac93b1e + 6950afc commit f2f42a4
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
##### 0.10.4 - xx March 2014

###### Backwards compatible bug fixes
- #21 - Add lazy database creation.
26 changes: 24 additions & 2 deletions lib/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,29 @@ function createPool(options) {
port: options.port,
db: options.db,
authKey: options.authKey
}, cb);
}, function (err, connection) {
if (err) {
cb(err);
} else {
r.dbList().run(connection, function (err, dbList) {
if (err) {
cb(err);
} else {
if (!utils.contains(dbList, options.db)) {
r.dbCreate(options.db).run(connection, function (err) {
if (err) {
cb(err);
} else {
cb(null, connection);
}
});
} else {
cb(null, connection);
}
}
});
}
});
},
destroy: function (conn) {
if (conn) {
Expand Down Expand Up @@ -488,7 +510,7 @@ function sanitizeConfigOptions(options) {
*
* - `{number=28015}` `port` - The port to connect on. See [r#connect](http://rethinkdb.com/api/javascript/#connect).
* - `{string="localhost"}` `host` - The host to connect to. See [r#connect](http://rethinkdb.com/api/javascript/#connect).
* - `{string="test"}` `db` - The default db to use. See [r#connect](http://rethinkdb.com/api/javascript/#connect).
* - `{string="test"}` `db` - The default db to use. This db will be created if it doesn't exist. See [r#connect](http://rethinkdb.com/api/javascript/#connect).
* - `{string=""}` `authKey` - The authentication key. See [r#connect](http://rethinkdb.com/api/javascript/#connect).
* - `{string=""}` `name` - The name of the connection. See [generic-pool#name](https://github.com/coopernurse/node-pool#documentation).
* - `{number=1}` `max` - Maximum number of active connections to create at any given time. See [generic-pool#max](https://github.com/coopernurse/node-pool#documentation).
Expand Down
8 changes: 7 additions & 1 deletion lib/support/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ function UnhandledError(error) {
* @description Message and stack trace. Same as `UnhandledError#stack`.
*/
this.message = 'UnhandledError: This is an uncaught exception. Please consider submitting an issue at https://github.com/jmdobry/reheat/issues.\n\n' +
'Original Uncaught Exception:\n' + (error.stack ? error.stack.toString() : error.stack);
'Original Uncaught Exception: ';

if (error && error.constructor && error.constructor.name === 'RqlRuntimeError') {
this.message = this.message + error.name + '\n' + error.message + '\n';
} else if (error instanceof Error) {
this.message = this.message + 'Error' + '\n' + (error.stack ? error.stack.toString() : error.stack);
}

/**
* @doc property
Expand Down
1 change: 1 addition & 0 deletions lib/support/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
forEach: require('mout/array/forEach'),
contains: require('mout/array/contains'),

isString: require('mout/lang/isString'),
isBoolean: require('mout/lang/isBoolean'),
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "reheat",
"description": "A red hot Node.js ORM for RethinkDB.",
"version": "0.10.3",
"version": "0.11.0-SNAPSHOT",
"homepage": "https://github.com/jmdobry/reheat",
"repository": {
"type": "git",
Expand Down Expand Up @@ -42,26 +42,26 @@
"dependencies": {
"generic-pool": "~2.0.4",
"mout": "~0.9.0",
"bluebird": "~1.0.7",
"bluebird": "~1.1.0",
"rethinkdb": "~1.11.0-4",
"robocop.js": "~0.12.0"
},
"devDependencies": {
"grunt": "~0.4.2",
"grunt": "~0.4.4",
"sandboxed-module": "~0.3.0",
"grunt-contrib-clean": "~0.5.0",
"grunt-docular": "~0.1.2",
"grunt-contrib-copy": "~0.5.0",
"grunt-contrib-concat": "~0.3.0",
"grunt-karma-coveralls": "~2.4.0",
"time-grunt": "~0.2.9",
"sinon": "~1.8.2",
"grunt-contrib-uglify": "~0.3.2",
"time-grunt": "~0.2.10",
"sinon": "~1.9.0",
"grunt-contrib-uglify": "~0.4.0",
"load-grunt-tasks": "~0.4.0",
"grunt-cli": "~0.1.13",
"grunt-contrib-jshint": "~0.8.0",
"grunt-contrib-jshint": "~0.9.2",
"coveralls": "~2.8.0",
"grunt-contrib-nodeunit": "~0.3.2",
"grunt-contrib-nodeunit": "~0.3.3",
"grunt-istanbul": "~0.2.4"
},
"bugs": {
Expand Down
7 changes: 7 additions & 0 deletions test/unit/reheat/connection/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ var errors = require('../../../../build/instrument/lib/support/errors'),
}
});
}, 100);
},
dbList: function () {
return {
run: function (conn, cb) {
cb(null, ['test']);
}
};
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/reheat/support/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ exports.errors = {

test.equal(err2.type, 'UnhandledError');
test.equal(err2.message, 'UnhandledError: This is an uncaught exception. Please consider submitting an issue at https://github.com/jmdobry/reheat/issues.\n\n' +
'Original Uncaught Exception:\n' + err1.stack.toString());
'Original Uncaught Exception: Error\n' + err1.stack.toString());

test.equal(err2.stack, 'UnhandledError: This is an uncaught exception. Please consider submitting an issue at https://github.com/jmdobry/reheat/issues.\n\n' +
'Original Uncaught Exception:\n' + err1.stack.toString());
'Original Uncaught Exception: Error\n' + err1.stack.toString());

test.deepEqual(err2.originalError, err1);

Expand Down

0 comments on commit f2f42a4

Please sign in to comment.