@alcalzone/jsonl-db
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -0,1 +1,2 @@ | ||
import * as fs from "fs-extra"; | ||
export declare class DB<V extends unknown = unknown> { | ||
@@ -30,2 +31,6 @@ constructor(filename: string); | ||
set(key: string, value: V): this; | ||
private importJsonFile; | ||
importJson(filename: string): Promise<void>; | ||
importJson(json: Record<string, any>): void; | ||
exportJson(filename: string, options?: fs.WriteOptions): Promise<void>; | ||
private write; | ||
@@ -32,0 +37,0 @@ private entryToLine; |
@@ -11,2 +11,3 @@ "use strict"; | ||
const deferred_promise_1 = require("alcalzone-shared/deferred-promise"); | ||
const objects_1 = require("alcalzone-shared/objects"); | ||
const fs = require("fs-extra"); | ||
@@ -117,2 +118,30 @@ const readline = require("readline"); | ||
} | ||
async importJsonFile(filename) { | ||
const json = await fs.readJSON(filename); | ||
return this.importJson(json); | ||
} | ||
importJson(jsonOrFile) { | ||
if (typeof jsonOrFile === "string") { | ||
if (!this._isOpen) { | ||
return Promise.reject(new Error("The database is not open!")); | ||
} | ||
return this.importJsonFile(jsonOrFile); | ||
} | ||
else { | ||
if (!this._isOpen) { | ||
throw new Error("The database is not open!"); | ||
} | ||
} | ||
for (const [key, value] of Object.entries(jsonOrFile)) { | ||
this._db.set(key, value); | ||
this.write(this.entryToLine(key, value)); | ||
} | ||
} | ||
async exportJson(filename, options) { | ||
if (!this._isOpen) { | ||
return Promise.reject(new Error("The database is not open!")); | ||
} | ||
return fs.writeJSON(filename, objects_1.composeObject([...this._db]), options); | ||
} | ||
// TODO: use cork() and uncork() to throttle filesystem accesses | ||
write(line) { | ||
@@ -169,3 +198,2 @@ /* istanbul ignore else */ | ||
} | ||
// TODO: use cork() and uncork() to throttle filesystem accesses | ||
/** Asynchronously performs all write actions */ | ||
@@ -172,0 +200,0 @@ async writeThread() { |
{ | ||
"name": "@alcalzone/jsonl-db", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Simple JSONL-based key-value store", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
@@ -25,2 +25,3 @@ # jsonl-db | ||
``` | ||
Now, `db.isOpen` is `true`. | ||
@@ -34,2 +35,3 @@ Use the database like you would use a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). | ||
``` | ||
Now, `db.isOpen` is `false`. While the db is not open, any calls that access the data will throw an error. | ||
@@ -50,2 +52,17 @@ To create a compressed copy of the database in `/path/to/file.dump`, use the `dump()` method. If any data is written to the db during the dump, it is appended to the dump but most likely compressed. | ||
Importing JSON files can be done this way: | ||
```ts | ||
// pass a filename, the import will be asynchronous | ||
await db.importJson(filename); | ||
// pass the object directly, the import will be synchronous | ||
db.importJson({key: "value"}); | ||
``` | ||
In both cases, existing entries in the DB will not be deleted but will be overwritten if they exist. | ||
Exporting JSON files is also possible: | ||
```ts | ||
await db.exportJson(filename[, options]); | ||
``` | ||
The file will be overwritten if it exists. The 2nd options argument can be used to control the file formatting. Since `fs-extra`'s `writeJson` is used under the hood, take a look at that [method documentation](https://github.com/jprichardson/node-fs-extra/blob/master/docs/writeJson.md) for details on the options object. | ||
## Changelog | ||
@@ -58,2 +75,5 @@ | ||
### 0.3.0 (2020-04-26) | ||
* Added `importJson` and `exportJson` methods | ||
### 0.2.0 (2020-04-25) | ||
@@ -60,0 +80,0 @@ * Added `isOpen` property |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
31005
338
89