Skip to content

Commit ae0641d

Browse files
committed
feat: allow specifying a different dump target
1 parent 77abe93 commit ae0641d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ If the directory does not exist, it will be created when opening the DB.
9393
### Copying and compressing the database
9494

9595
```ts
96-
await db.dump();
96+
await db.dump(); // To use the default dump filename `/path/to/file.dump`
97+
await db.dump("/somewhere/else.jsonl"); // To use a different filename
9798
```
9899

99100
After a while, the main db file may contain unnecessary entries. The raw number of entries can be read using the `uncompressedSize` property. To remove unnecessary entries, use the `compress()` method.
@@ -141,6 +142,9 @@ The file will be overwritten if it exists. The 2nd options argument can be used
141142
Placeholder for next release:
142143
### __WORK IN PROGRESS__
143144
-->
145+
### __WORK IN PROGRESS__
146+
* Add the ability to dump the database to a different location
147+
144148
### 2.2.0 (2021-10-15)
145149
* Add the ability to specify where the lockfile is created
146150

@@ -213,4 +217,4 @@ Added an optional reviver function to transform non-primitive objects while load
213217
* Fixed some race conditions
214218

215219
### 0.1.0 (2020-04-25)
216-
First official release
220+
First official release

src/lib/db.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,16 @@ export class JsonlDB<V extends unknown = unknown> {
616616
}
617617
}
618618

619-
/** Saves a compressed copy of the DB into `<filename>.dump` */
620-
public async dump(): Promise<void> {
619+
/**
620+
* Saves a compressed copy of the DB into the given path.
621+
* @param targetFilename Where the compressed copy should be written. Default: `<filename>.dump`
622+
*/
623+
public async dump(
624+
targetFilename: string = this.dumpFilename,
625+
): Promise<void> {
621626
this._dumpPromise = createDeferredPromise();
622627
// Open the file for writing (or truncate if it exists)
623-
this._dumpFd = await fs.open(this.dumpFilename, "w+");
628+
this._dumpFd = await fs.open(targetFilename, "w+");
624629
// And start dumping the DB
625630
// Start by creating a dump backlog, so parallel writes will be remembered
626631
this._dumpBacklog = new stream.PassThrough({ objectMode: true });
@@ -685,9 +690,12 @@ export class JsonlDB<V extends unknown = unknown> {
685690

686691
private compressPromise: DeferredPromise<void> | undefined;
687692
private async compressInternal(): Promise<void> {
688-
if (this.compressPromise) return;
689-
693+
if (this.compressPromise) return this.compressPromise;
690694
this.compressPromise = createDeferredPromise();
695+
696+
// If someone else is currently dumping the DB, wait for them to finish
697+
if (this._dumpPromise) await this._dumpPromise;
698+
691699
// Immediately remember the database size or writes while compressing
692700
// will be incorrectly reflected
693701
this._uncompressedSize = this.size;

0 commit comments

Comments
 (0)