Skip to content

Commit

Permalink
feat: added leveldb
Browse files Browse the repository at this point in the history
Signed-off-by: theanmolsharma <[email protected]>
  • Loading branch information
theanmolsharma committed Dec 30, 2023
1 parent abca6ff commit bf9ca8e
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ coverage
*.njsproj
*.sln
*.sw?

# test
test/wallet-db
147 changes: 146 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"bech32": "^2.0.0",
"buffer": "^6.0.3",
"create-hash": "^1.2.0",
"level": "^8.0.0",
"secp256k1": "^5.0.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/wallet/db/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './db.interface.ts';
export * from './level';
39 changes: 39 additions & 0 deletions src/wallet/db/level/db.ts
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());
}
}
2 changes: 2 additions & 0 deletions src/wallet/db/level/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './db.ts';
export * from './layout.ts';
4 changes: 4 additions & 0 deletions src/wallet/db/level/layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Database layout
export const wdb = {
V: 'V', // Version
};
29 changes: 29 additions & 0 deletions test/wallet-db.spec.ts
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();
});
});

0 comments on commit bf9ca8e

Please sign in to comment.