/
index.ts
131 lines (117 loc) 路 3.21 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { LinkAlreadyExists } from './errors/chain/link-already-exists';
import { NoLinkFound } from './errors/chain/no-link';
import { NoChainLoaded } from './errors/chainfile/no-chain-loaded';
import { Chainfile } from './models/chainfile';
import { ChainQueue } from './modules/queue';
import { loadFromDisk } from './modules/storage';
import { runValidateChecks } from './modules/validate-file';
class Chainson {
private chainLocation: string;
private output: string;
private chainQueue: ChainQueue;
private chain: Chainfile | undefined;
constructor(chainLocation = 'chainfile', output = 'NORMAL') {
this.chainLocation = chainLocation + '.json';
this.output = output;
this.init();
this.chainQueue = new ChainQueue(this.chainLocation);
}
/**
* Initializes the database, runs checks and sets everything up.
*/
private async init() {
runValidateChecks(this.chainLocation);
this.chain = loadFromDisk(this.chainLocation);
}
// TODO: Move add, get, has, contains, full, remove, update, forceStore to module
/**
* Add a link to the chain
* @param key key
* @param value data
*/
public add(key: string, value: any) {
if (!this.chain) throw new NoChainLoaded();
if (this.has(key)) throw new LinkAlreadyExists(key);
this.chain.chain.set(key, value);
this.store();
}
/**
* Get the value from the chain
* @param key key
*/
public get(key: string): any {
if (!this.chain) throw new NoChainLoaded();
if (!this.has(key)) throw new NoLinkFound(key);
return this.chain.chain.get(key);
}
/**
* Check if the chain contains a certain key
* @param key key
*/
public has(key: string): boolean {
if (!this.chain) throw new NoChainLoaded();
return this.chain.chain.has(key);
}
/**
* Returns the full chain as a map
*/
public full(): Map<string, any> {
if (!this.chain) throw new NoChainLoaded();
return this.chain.chain;
}
/**
* Update a value in the chain
* @param key key
* @param value value
*/
public set(key: any, value: any) {
if (!this.chain) throw new NoChainLoaded();
this.chain.chain.set(key, value);
this.store();
}
/**
* Update item in the chain, only if it exists
* @param key key
* @param value value
*/
public update(key: any, value: any) {
if (!this.chain) throw new NoChainLoaded();
if (!this.has(key)) throw new NoLinkFound(key);
this.chain.chain.set(key, value);
this.store();
}
/**
* Remove a value from the chain
* @param key key
*/
public delete(key: any) {
if (!this.chain) throw new NoChainLoaded();
if (!this.has(key)) throw new NoLinkFound(key);
this.chain.chain.delete(key);
this.store();
}
/**
* Add the updated chain to the queue to be made persistan
*/
private store() {
if (!this.chain) throw new NoChainLoaded();
this.chainQueue.add(this.chain);
}
/**
* Forcefully adds the memory chain to the disk
*/
public forceStore() {
if (!this.chain) throw new NoChainLoaded();
this.chainQueue.add(this.chain);
}
/*
Aliasses
*/
public contains(key: any) {
return this.has(key);
}
public remove(key: any) {
return this.delete(key);
}
}
export = Chainson;