Skip to content

Commit

Permalink
Merge pull request #2 from tiagorangel2011/glitch
Browse files Browse the repository at this point in the history
Updated with Glitch
  • Loading branch information
tiagorangel2011 committed Oct 16, 2022
2 parents 22d91f1 + d4d9ce1 commit 4265821
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1,096 deletions.
38 changes: 6 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,14 @@ A fork of [nmaggioni/Simple-JSONdb](https://github.com/nmaggioni/Simple-JSONdb),

## Usage

### Start
```javascript
const qjson = require('qjson-db');
const db = new JSONdb('/path/to/your/storage.json');
```

You can supply the optional `options` object by giving it as second parameter:
Start by creating your database:

```javascript
const db = new qjson('/path/to/your/storage.json', { ... });
const qjson = require('qjson-db');
const db = new qjson('/path/to/your/storage.json');
```

See the [Options](#options) section for more details.

#### Options

All keys are optional and will default to a safe value.

| **Key** | **Value type** | **Description** | **Default value** |
|-------------|------------------------------|-------------------------------------------------------------------|-------------------------------------|
| asyncWrite | _Boolean_ | Enables the storage to be asynchronously written to disk. | _**false**_ (synchronous behaviour) |
| syncOnWrite | _Boolean_ | Makes the storage be written to disk after every modification. | _**true**_ |
| jsonSpaces | _Number_ | The number of spaces used for indentation in the output JSON. | _**4**_ |
| stringify | _Function(object) => string_ | A stringifier function to serialize JS objects into JSON strings. | _**JSON.stringify**_ |
| parse | _Function(string) => object_ | A parser function to deserialize JSON strings into JS objects. | _**JSON.parse**_ |
Then, you can use the functions:

### Set a key
`db.set('key', 'value');`
Expand All @@ -55,21 +38,12 @@ The `key` parameter must be a string. If the key exists `true` is returned, if i

The `key` parameter must be a string. The function returns [as per the _delete_ operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Return_value) if the key exists, else it returns `undefined`.

### Sync to disk
`db.sync();`

This function writes the JSON storage object to the file path specified as the parameter of the main constructor. Consult the [Options](#options) section for usage details; on default options there is no need to manually invoke it.

### Access JSON storage
`db.JSON();`

This will return **a copy** of the internal JSON storage object, for you to tinker with and loop over.
This will return a copy of the internal JSON storage object.

### Replace JSON storage
`db.JSON({ data });`

Giving a parameter to the `JSON` function makes the object passed replace the internal one. _Be careful, as there's no way to recover the old object if the changes have already been written to disk._

## Tests

Run `npm ci` to install the testing dependencies and `npm test` to start the test suite.
Giving a parameter to the `JSON` function makes the object passed replace the internal one. _Be careful, as there's no way to recover the old object if the changes have already been written to disk._
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ function JSONdb(filePath, options) {
// Mandatory arguments check
if (!filePath || !filePath.length) {
throw new Error('Missing file path argument.');
} else {
} else if (filePath.endsWith(".json")) {
this.filePath = filePath;
} else {
this.filePath = filePath + ".json";
}

// Options parsing
Expand Down Expand Up @@ -85,7 +87,7 @@ function JSONdb(filePath, options) {
try {
data = fs.readFileSync(filePath);
} catch (err) {
throw err; // TODO: Do something meaningful
throw err;
}
if (validateJSON.bind(this)(data)) this.storage = this.options.parse(data);
}
Expand Down
Loading

0 comments on commit 4265821

Please sign in to comment.