Skip to content

Commit a7053cb

Browse files
committed
feat: crypto can be passed in DB.start, this enables restarting the DB with a new crypto object
1 parent 4245ae8 commit a7053cb

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ npm install --save @matrixai/db
2121

2222
## Usage
2323

24-
2524
```ts
2625
import { DB } from '@matrixai/db';
2726

@@ -76,6 +75,8 @@ async function main () {
7675
main();
7776
```
7877

78+
If you created the `DB` with a `crypto` object, then upon restarting the `DB`, you must pass in the same `crypto` object.
79+
7980
## Development
8081

8182
Run `nix-shell`, and once you're inside, you can use:

src/DB.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ class DB {
5454
logger.info(`Creating ${this.name}`);
5555
const db = new this({
5656
dbPath,
57-
crypto,
5857
fs,
5958
logger,
6059
});
61-
await db.start({ fresh, ...dbOptions });
60+
await db.start({
61+
crypto,
62+
fresh,
63+
...dbOptions,
64+
});
6265
logger.info(`Created ${this.name}`);
6366
return db;
6467
}
@@ -106,28 +109,27 @@ class DB {
106109

107110
constructor({
108111
dbPath,
109-
crypto,
110112
fs,
111113
logger,
112114
}: {
113115
dbPath: string;
114-
crypto?: {
115-
key: Buffer;
116-
ops: Crypto;
117-
};
118116
fs: FileSystem;
119117
logger: Logger;
120118
}) {
121119
this.logger = logger;
122120
this.dbPath = dbPath;
123-
this.crypto = crypto;
124121
this.fs = fs;
125122
}
126123

127124
public async start({
125+
crypto,
128126
fresh = false,
129127
...dbOptions
130128
}: {
129+
crypto?: {
130+
key: Buffer;
131+
ops: Crypto;
132+
};
131133
fresh?: boolean;
132134
} & DBOptions = {}) {
133135
this.logger.info(`Starting ${this.constructor.name}`);
@@ -142,6 +144,7 @@ class DB {
142144
throw new errors.ErrorDBDelete(e.message, { cause: e });
143145
}
144146
}
147+
this.crypto = crypto;
145148
const db = await this.setupDb(this.dbPath, {
146149
...dbOptions,
147150
createIfMissing: true,
@@ -179,6 +182,7 @@ class DB {
179182
}
180183
}
181184
await rocksdbP.dbClose(this._db);
185+
delete this.crypto;
182186
this.logger.info(`Stopped ${this.constructor.name}`);
183187
}
184188

tests/DB.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe(DB.name, () => {
5757
await db.start();
5858
await db.put('a', 'value0');
5959
await db.stop();
60-
await db.start();
60+
await db.start({ crypto });
6161
expect(await db.get('a')).toBe('value0');
6262
await db.stop();
6363
});
@@ -98,7 +98,7 @@ describe(DB.name, () => {
9898
await db._put(['canary'], 'bad ju ju');
9999
await db.stop();
100100
// Start will fail, the DB will still be stopped
101-
await expect(db.start()).rejects.toThrow(errors.ErrorDBKey);
101+
await expect(db.start({ crypto })).rejects.toThrow(errors.ErrorDBKey);
102102
// DB is still corrupted at this point
103103
await expect(DB.createDB({ dbPath, crypto, logger })).rejects.toThrow(
104104
errors.ErrorDBKey,

0 commit comments

Comments
 (0)