Skip to content

Commit 6eb4c73

Browse files
committed
Makes checkOnConnect to accept 'false', 'no' or '0'. Tests added.
1 parent 89df203 commit 6eb4c73

File tree

6 files changed

+265
-3
lines changed

6 files changed

+265
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ Options can be passed as arguments to the database connection string, in URL-for
4141
- `charset`: defines the charset (encoding) used to handle the JSON file
4242
- Defaults to `utf-8`
4343
- Example: `const connection = new Database( 'json:///test.json?charset=utf-16' );`
44-
- Available in database-json-json version `1.0.0` or later
44+
- Available in database-js-json version `1.0.0` or later
4545

4646
- `checkOnConnect`: whether it should check if the file exists when connecting to it
4747
- Defaults to `true`
4848
- Example: `const connection = new Database( 'json:///test.json?checkOnConnect=0' );`
49-
- Available in database-json-json version `1.1.0` or later
49+
- Available in database-js-json version `1.1.0` or later
5050

5151

5252
## Additional Options

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ module.exports = {
173173
}
174174
options[ key ][ subKey ] = val;
175175
}
176+
177+
// Adjust 'checkOnConnect'
178+
if ( options[ 'checkOnConnect' ] ) {
179+
var isFalse = function ( v ) { return 'false' === v || 'no' === v || '0' == v; };
180+
options[ 'checkOnConnect' ] = ! isFalse( val );
181+
}
176182
});
177183
}
178184

package-lock.json

Lines changed: 206 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Database-js driver for JSON files",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "mocha"
88
},
99
"keywords": [
1010
"database-js",
@@ -14,5 +14,10 @@
1414
"license": "MIT",
1515
"dependencies": {
1616
"jl-sql-api": "^2.8.1"
17+
},
18+
"devDependencies": {
19+
"jl-sql-api": "^2.8.1",
20+
"database-js": "^3.0.1",
21+
"mocha": "^5.0.0"
1722
}
1823
}

test/states.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{ "state": "Ohio" },
3+
{ "state": "Dakota" },
4+
{ "state": "New York" }
5+
]

test/test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var dbjs = require( 'database-js' );
2+
var assert = require( 'assert' );
3+
4+
describe( 'database-js-json', function () {
5+
6+
var obj = {
7+
driverName: '../../../index', // hack to load the index as the json module of database-js
8+
database: './test/states.json'
9+
};
10+
11+
it( 'queries an existing file correctly', function( done ) {
12+
var conn = new dbjs.Connection( obj );
13+
var st = conn.prepareStatement( 'SELECT * WHERE state = ?' );
14+
var search = 'Dakota';
15+
st.query( search )
16+
.then( function( data ) {
17+
assert.equal( data[ 0 ].state, search );
18+
done();
19+
} )
20+
.catch( function( err ) {
21+
done( err );
22+
} );
23+
} );
24+
25+
it( 'throws when an inexisting file is required', function() {
26+
var obj2 = Object.assign( obj, { database: 'not-found.json' } );
27+
assert.throws(
28+
function() {
29+
new dbjs.Connection( obj2 );
30+
},
31+
/not exist/
32+
);
33+
} );
34+
35+
it( 'does not throw when an inexisting file is not required', function() {
36+
var obj2 = Object.assign( obj, { database: 'not-found.json', parameters: 'checkOnConnect=false' } );
37+
new dbjs.Connection( obj2 );
38+
} );
39+
40+
} );

0 commit comments

Comments
 (0)