-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: theanmolsharma <[email protected]>
- Loading branch information
1 parent
abca6ff
commit bf9ca8e
Showing
8 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ coverage | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
# test | ||
test/wallet-db |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './db.interface.ts'; | ||
export * from './level'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Level } from 'level'; | ||
import { wdb } from './layout.ts'; | ||
import { DbInterface } from '../db.interface.ts'; | ||
|
||
export type LevelDBConfigOptions = { | ||
location: string; | ||
}; | ||
|
||
export class WalletDB implements DbInterface { | ||
private readonly db; | ||
|
||
constructor(config: LevelDBConfigOptions) { | ||
this.db = new Level(config.location, { | ||
valueEncoding: 'json', | ||
keyEncoding: 'utf-8', | ||
createIfMissing: true, | ||
}); | ||
} | ||
|
||
public async open(): Promise<void> { | ||
await this.db.open(); | ||
} | ||
|
||
public async close(): Promise<void> { | ||
await this.db.close(); | ||
} | ||
|
||
public getStatus(): string { | ||
return this.db.status; | ||
} | ||
|
||
public async getVersion(): Promise<number> { | ||
return parseInt(await this.db.get(wdb.V)); | ||
} | ||
|
||
public async setVersion(version: number): Promise<void> { | ||
this.db.put(wdb.V, version.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './db.ts'; | ||
export * from './layout.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Database layout | ||
export const wdb = { | ||
V: 'V', // Version | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { WalletDB } from '../src/wallet'; | ||
|
||
describe('Wallet DB', () => { | ||
let walletDB: WalletDB; | ||
|
||
beforeAll(async () => { | ||
walletDB = new WalletDB({ | ||
location: './test/wallet-db', | ||
}); | ||
|
||
await walletDB.open(); | ||
}); | ||
|
||
it('should open the wallet database', async () => { | ||
expect(walletDB.getStatus()).toBe('open'); | ||
}); | ||
|
||
it('should set the wallet database version', async () => { | ||
await walletDB.setVersion(1); | ||
}); | ||
|
||
it('should get the wallet database version', async () => { | ||
expect(await walletDB.getVersion()).toBe(1); | ||
}); | ||
|
||
afterAll(async () => { | ||
await walletDB.close(); | ||
}); | ||
}); |