// Specify types of keys and values (any, in the case of json). import {Level} from 'level'; import {Account, Bank} from './bank'; import {Option, fromNullable} from 'fp-ts/lib/Option'; export class LevelBank implements Bank { db: Level; file: string; constructor(file: string) { this.file = file; this.db = new Level(file, { valueEncoding: 'json', }); } async populateWith( initialAccounts: Map, ): Promise { await this.db.open({createIfMissing: true}); const puts: Promise[] = []; initialAccounts.forEach((v, k) => { puts.push(this.db.put(k, v)); }); await Promise.all(puts); return this; } async balance(account: string): Promise> { const result = await this.db.get(account); return fromNullable(result); } async deposit(account: string, amt: number): Promise { const current = await this.db.get(account); if (current) { await this.db.put(account, current + amt); } } async withdraw(account: string, amt: number): Promise { const current = await this.db.get(account); if (current) { await this.db.put(account, current + amt); } } }