https://github.com/thiagodp/database-js-json
📜 Query your JSON files with SQL
https://github.com/thiagodp/database-js-json
database database-js database-js-json driver javascript json node query sql
Last synced: 26 days ago
JSON representation
📜 Query your JSON files with SQL
- Host: GitHub
- URL: https://github.com/thiagodp/database-js-json
- Owner: thiagodp
- License: mit
- Created: 2018-01-25T00:26:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-01-25T22:01:07.000Z (over 4 years ago)
- Last Synced: 2025-03-24T19:01:42.638Z (about 1 month ago)
- Topics: database, database-js, database-js-json, driver, javascript, json, node, query, sql
- Language: JavaScript
- Homepage:
- Size: 18.6 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# database-js-json
A [database-js](https://github.com/mlaanderson/database-js) driver for JSON files.
## About
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.
Our [releases](https://github.com/thiagodp/database-js-json/releases) adopt [Semantic Versioning](https://semver.org/).
## Install
```shell
npm install database-js-json --save
```Note: `database-js` must also be installed.
## Basic Usage
```javascript
const Connection = require( 'database-js' ).Connection;( async () => {
const connection = new Connection( 'json:///test.json' );
try {
const statement = await connection.prepareStatement("SELECT * WHERE user_name = ?");
const rows = await statement.query('not_so_secret_user');
console.log(rows);
} catch (error) {
console.log(error);
} finally {
await connection.close();
}
} )();
```## Example
`people.json`
```json
[
{ "name": "Alice", "age": 21 },
{ "name": "Bob", "age": 53 },
{ "name": "Jack", "age": 16 }
]
````main.js`
```javascript
const dbjs = require( 'database-js' );
( async () => {
let conn;
try {
conn = new dbjs.Connection( 'json:///people.json' );const st1 = conn.prepareStatement( 'SELECT *' );
const r1 = await st1.query();
console.log( r1 ); // same as people.json's contentconst st2 = conn.prepareStatement( 'SELECT name ORDER BY name DESC' );
const r2 = await st2.query();
console.log( r2 ); // [ { name: 'Jack' }, { name: 'Bob' }, { name: 'Alice' } ]const st3 = conn.prepareStatement( 'SELECT MAX(age) AS older' );
const r3 = await st3.query();
console.log( r3 ); // [ { older: 53 } ]// Inserting a row
const st4 = conn.prepareStatement( 'INSERT VALUES { "name": "Mary", "age": 18 }' );
await st4.execute();// Updating a row
const st5 = conn.prepareStatement( 'UPDATE SET name = "Robert" WHERE name = "Bob"' );
await st5.execute();// Deleting a row
const st6 = conn.prepareStatement( 'DELETE WHERE name = "Alice"' );
await st6.execute();// Now people.json is
// [{"name":"Robert","age":53},{"name":"Jack","age":16},{"name":"Mary","age":18}]} catch ( err ) {
console.error( err );
} finally {
if ( conn ) {
await conn.close();
}
}
} )();
```## Basic Options
Options can be passed as arguments to the database connection string, in URL-format.
- `charset`: defines the charset (encoding) used to handle the JSON file
- Defaults to `utf-8`
- Example: `const connection = new Connection( 'json:///test.json?charset=utf-16' );`
- Available in database-js-json version `1.0.0` or later- `checkOnConnect`: whether it should check if the file exists when connecting to it
- Defaults to `true`
- Example: `const connection = new Connection( 'json:///test.json?checkOnConnect=false' );`
- Accepts `false`, `no` or `0` as false
- Available in database-js-json version `1.1.0` or later## Additional Options
Options from [jl-sql-api](https://github.com/avz/node-jl-sql-api) can also be passed as arguments to the database connection.
Example: `{ tmpDir: "/path/to/dir" }`
```javascript
const connection = new Connection( 'json:///test.json?tmpDir=path/to/dir' );
```When an option that belongs to a group is informed, it must have a dot.
Example: `{ tmpDir: "/path/to/dir", sortOptions: { inMemoryBufferSize: 32000 } }`
```javascript
const connection = new Connection( 'json:///test.json?tmpDir=path/to/dir&sortOptions.inMemoryBufferSize=32000' );
```## License
[MIT](https://github.com/thiagodp/database-js-json/blob/master/LICENSE) (c) [Thiago Delgado Pinto](https://github.com/thiagodp)