Navigation Menu

Skip to content

Commit

Permalink
Added pretty-printed JSON stringification
Browse files Browse the repository at this point in the history
>
> This enables databases to be efficiently diffed by Git by including them in a repo.
  • Loading branch information
o0101 committed Sep 26, 2020
1 parent 86e3157 commit 69a20f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
18 changes: 16 additions & 2 deletions README.md
Expand Up @@ -6,13 +6,27 @@ Uses [discohash](https://github.com/cris691/discohash) for hashing key values.

**Around 550 source lines of code** (see stats folder)

# get
## features

- Store any JSON-able object
- Index on any property (only top-level properties)
- Auto ID or custom ID
- Diffable by git
- All records and indexes and table information is just JSON files
- 1 file per record, 1 file per unique index value, 1 file per table info
- 1 sub-directory per table, 1 sub-directory (nested inside table) per indexed property
- ACID guarantee (as long as accessed by single node thread)
- Can expand ACID to multi-threaded access with a request queue.

All in all this makes the database easy to understand and inspect. As well as making the code easy to read and maintain.

## get

```console
npm i --save stubdb
```

# api
## api

There's only a couple of handful of calls in this api: `config`, `dropTable`, `getIndexedTable`, `getTable`, `put`, `get`, `getAllMatchingKeysFromIndex` and `getAllMatchingRecordsFromIndex`

Expand Down
4 changes: 2 additions & 2 deletions api.js
Expand Up @@ -32,7 +32,7 @@ export function getTable(name) {
name,
createdAt: Date.now()
};
fs.writeFileSync(tableBase, JSON.stringify(tableInfo));
fs.writeFileSync(tableBase, JSON.stringify(tableInfo,null,2));
}
return new Table(tableInfo);
}
Expand Down Expand Up @@ -63,7 +63,7 @@ export function getIndexedTable(name, indexed_properties = []) {
property_name: prop,
createdAt: Date.now()
};
fs.writeFileSync(indexInfoBase, JSON.stringify(indexInfo));
fs.writeFileSync(indexInfoBase, JSON.stringify(indexInfo,null,2));
}
}

Expand Down
6 changes: 3 additions & 3 deletions table.js
Expand Up @@ -27,7 +27,7 @@ export class Table {
put(key, record, greenlights = null) {
const keyHash = discohash(key).toString(16);
const keyFileName = path.resolve(this.base, `${keyHash}.json`);
const recordString = JSON.stringify(record);
const recordString = JSON.stringify(record,null,2);

guardGreenLights(greenlights, {key, record, recordString});

Expand Down Expand Up @@ -168,7 +168,7 @@ function index(value, key, propIndex) {

indexRecord[value64] = [...keysWithValue.keys()];

fs.writeFileSync(indexRecordFileName, JSON.stringify(indexRecord));
fs.writeFileSync(indexRecordFileName, JSON.stringify(indexRecord,null,2));

indexUpdated = true;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ function deindex(value, key, propIndex) {

indexRecord[value64] = [...keysWithValue.keys()];

fs.writeFileSync(indexRecordFileName, JSON.stringify(indexRecord));
fs.writeFileSync(indexRecordFileName, JSON.stringify(indexRecord,null,2));

indexUpdated = true;
}
Expand Down

0 comments on commit 69a20f0

Please sign in to comment.