Skip to content

Commit c8cdb5c

Browse files
committed
New connection option: checkOnConnect
1 parent 3a73fed commit c8cdb5c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ A [database-js](https://github.com/mlaanderson/database-js) driver for JSON file
66

77
This is a wrapper around the [jl-sql-api](https://github.com/avz/node-jl-sql-api), intended to be used with [database-js](https://github.com/mlaanderson/database-js) for handling JSON files.
88

9+
Our [releases](https://github.com/thiagodp/database-js-json/releases) adopt [Semantic Versioning](https://semver.org/).
10+
911
## Install
1012

1113
```shell
@@ -32,9 +34,24 @@ var Connection = require( 'database-js2' ).Connection;
3234
} )();
3335
```
3436

35-
### Options
37+
## Basic Options
38+
39+
Options can be passed as arguments to the database connection string, in URL-format.
40+
41+
- `charset`: defines the charset (encoding) used to handle the JSON file
42+
- Defaults to `utf-8`
43+
- Example: `const connection = new Database( 'json:///test.json?charset=utf-16' );`
44+
- Available in database-json-json version `1.0.0` or later
45+
46+
- `checkOnConnect`: whether it should check if the file exists when connecting to it
47+
- Defaults to `true`
48+
- Example: `const connection = new Database( 'json:///test.json?checkOnConnect=0' );`
49+
- Available in database-json-json version `1.1.0` or later
50+
51+
52+
## Additional Options
3653

37-
Options from [jl-sql-api](https://github.com/avz/node-jl-sql-api) can be passed as arguments to the database connection, in the format of a URL.
54+
Options from [jl-sql-api](https://github.com/avz/node-jl-sql-api) can also be passed as arguments to the database connection.
3855

3956
Example: `{ tmpDir: "/path/to/dir" }`
4057
```javascript

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class JsonDriver {
1212
* Constructor
1313
*
1414
* @param {string} filename File name
15-
* @param {object} options Options. Defaults to { charset: 'utf-8' }.
15+
* @param {object} options Options. Defaults to { charset: 'utf-8', checkOnConnect: true }.
1616
*
1717
* @see https://github.com/avz/node-jl-sql-api#options-object for more options.
1818
*/
@@ -23,10 +23,15 @@ class JsonDriver {
2323
this._filename = filename;
2424

2525
this._options = Object.assign( {
26-
charset: 'utf-8'
26+
charset: 'utf-8',
27+
checkOnConnect: true
2728
}, options || {} );
2829

2930
this._data = null;
31+
32+
if ( this._options.checkOnConnect && ! fs.existsSync( this._filename ) ) {
33+
throw new Error( 'File "' + this._filename + '" does not exist.' );
34+
}
3035
}
3136

3237
/**

0 commit comments

Comments
 (0)