File tree 3 files changed +16
-11
lines changed
3 files changed +16
-11
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,6 @@ npm install --save @matrixai/db
21
21
22
22
## Usage
23
23
24
-
25
24
``` ts
26
25
import { DB } from ' @matrixai/db' ;
27
26
@@ -76,6 +75,8 @@ async function main () {
76
75
main ();
77
76
```
78
77
78
+ If you created the ` DB ` with a ` crypto ` object, then upon restarting the ` DB ` , you must pass in the same ` crypto ` object.
79
+
79
80
## Development
80
81
81
82
Run ` nix-shell ` , and once you're inside, you can use:
Original file line number Diff line number Diff line change @@ -54,11 +54,14 @@ class DB {
54
54
logger . info ( `Creating ${ this . name } ` ) ;
55
55
const db = new this ( {
56
56
dbPath,
57
- crypto,
58
57
fs,
59
58
logger,
60
59
} ) ;
61
- await db . start ( { fresh, ...dbOptions } ) ;
60
+ await db . start ( {
61
+ crypto,
62
+ fresh,
63
+ ...dbOptions ,
64
+ } ) ;
62
65
logger . info ( `Created ${ this . name } ` ) ;
63
66
return db ;
64
67
}
@@ -106,28 +109,27 @@ class DB {
106
109
107
110
constructor ( {
108
111
dbPath,
109
- crypto,
110
112
fs,
111
113
logger,
112
114
} : {
113
115
dbPath : string ;
114
- crypto ?: {
115
- key : Buffer ;
116
- ops : Crypto ;
117
- } ;
118
116
fs : FileSystem ;
119
117
logger : Logger ;
120
118
} ) {
121
119
this . logger = logger ;
122
120
this . dbPath = dbPath ;
123
- this . crypto = crypto ;
124
121
this . fs = fs ;
125
122
}
126
123
127
124
public async start ( {
125
+ crypto,
128
126
fresh = false ,
129
127
...dbOptions
130
128
} : {
129
+ crypto ?: {
130
+ key : Buffer ;
131
+ ops : Crypto ;
132
+ } ;
131
133
fresh ?: boolean ;
132
134
} & DBOptions = { } ) {
133
135
this . logger . info ( `Starting ${ this . constructor . name } ` ) ;
@@ -142,6 +144,7 @@ class DB {
142
144
throw new errors . ErrorDBDelete ( e . message , { cause : e } ) ;
143
145
}
144
146
}
147
+ this . crypto = crypto ;
145
148
const db = await this . setupDb ( this . dbPath , {
146
149
...dbOptions ,
147
150
createIfMissing : true ,
@@ -179,6 +182,7 @@ class DB {
179
182
}
180
183
}
181
184
await rocksdbP . dbClose ( this . _db ) ;
185
+ delete this . crypto ;
182
186
this . logger . info ( `Stopped ${ this . constructor . name } ` ) ;
183
187
}
184
188
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ describe(DB.name, () => {
57
57
await db . start ( ) ;
58
58
await db . put ( 'a' , 'value0' ) ;
59
59
await db . stop ( ) ;
60
- await db . start ( ) ;
60
+ await db . start ( { crypto } ) ;
61
61
expect ( await db . get ( 'a' ) ) . toBe ( 'value0' ) ;
62
62
await db . stop ( ) ;
63
63
} ) ;
@@ -98,7 +98,7 @@ describe(DB.name, () => {
98
98
await db . _put ( [ 'canary' ] , 'bad ju ju' ) ;
99
99
await db . stop ( ) ;
100
100
// 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 ) ;
102
102
// DB is still corrupted at this point
103
103
await expect ( DB . createDB ( { dbPath, crypto, logger } ) ) . rejects . toThrow (
104
104
errors . ErrorDBKey ,
You can’t perform that action at this time.
0 commit comments