-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add abstract StorageProvider, JSONProvider, Util.assignNested
Still so much to do T-T
- Loading branch information
Showing
3 changed files
with
80 additions
and
3 deletions.
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
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,43 @@ | ||
import { StorageProvider } from './StorageProvider'; | ||
import DB = require('node-json-db'); | ||
|
||
export class JSONProvider extends StorageProvider | ||
{ | ||
private _name: string; | ||
private _db: DB; | ||
public constructor(name: string) | ||
{ | ||
super(); | ||
this._name = name; | ||
} | ||
|
||
public async init(): Promise<void> | ||
{ | ||
this._db = new DB(`storage/${this._name}`); | ||
} | ||
|
||
public async keys(): Promise<string[]> | ||
{ | ||
return Object.keys(this._db.getData('/')); | ||
} | ||
|
||
public async get(key: string): Promise<string> | ||
{ | ||
return this._db.getData(`/${key}`); | ||
} | ||
|
||
public async set(key: string, value: string): Promise<void> | ||
{ | ||
this._db.push(`/${key}`, value); | ||
} | ||
|
||
public async remove(key: string): Promise<void> | ||
{ | ||
this._db.delete(`/${key}`); | ||
} | ||
|
||
public async clear(): Promise<void> | ||
{ | ||
for (const key of await this.keys()) this._db.delete(`/${key}`); | ||
} | ||
} |
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,9 @@ | ||
export abstract class StorageProvider | ||
{ | ||
public abstract async init(): Promise<void>; | ||
public abstract async keys(): Promise<string[]>; | ||
public abstract async get(key: string): Promise<string>; | ||
public abstract async set(key: string, value: string): Promise<void>; | ||
public abstract async remove(key: string): Promise<void>; | ||
public abstract async clear(): Promise<void>; | ||
} |