-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsdb.ts
150 lines (132 loc) · 3.82 KB
/
tsdb.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Copyright (C) devsimsek.
* TSDB - Fast, Reliable database port of the original sdb database (php)
* This is my first typescript package! Please leave comment on my website or social media!
* @github: https://github.com/devsimsek
* @web: https://beta.smsk.me
* @package: tsdb (https://github.com/devsimsek/tsdb)
* @version: v1.0
*/
import * as fs from 'fs'
import * as path from 'path'
class Debug {
constructor(protected mode: boolean = false) {
}
log(message: string, flag: string = "DEBUG"): boolean | void {
if (this.mode) console.info(`[${new Date()} ${flag}]: ${message}`)
}
error(message: string, code: string = "unknown", flag: string = "ERROR") {
console.error(`[${new Date} ${flag}]: ${message} (err code: ${code})`)
}
}
export interface Data {
id: any
key: string
value: any
}
export class TSDB {
debug: Debug = new Debug(true)
data: any
tempObject: Array<Data>
file: string = ''
constructor() {
this.debug.log('Initializing tsdb...')
this.tempObject = []
}
load(file: string, directory: string = ""): boolean {
let load: boolean = false
this.file = __dirname + "/" + directory + "/" + file + ".tsdb"
this.debug.log(`Loading ${file} database. (dir: ${__dirname + "/" + (directory ?? "")})`)
if (fs.existsSync(__dirname + "/" + directory + "/" + file + ".tsdb")) {
try {
let d = fs.readFileSync(path.join(__dirname, directory, file + ".tsdb"), 'utf8')
this.data = JSON.parse(d)
if (Array.isArray(this.data))
load = true
else {
this.debug.error('Datasource is corrupted.')
load = false
}
} catch (e) {
// @ts-ignore
this.debug.error(e)
}
} else this.debug.error('Data source does not exists.')
if (load) this.tempObject = this.data
return load
}
save(): boolean {
if (this.tempObject) {
if (this.tempObject.length > 0) {
try {
fs.writeFileSync(this.file, JSON.stringify(this.tempObject))
return true
} catch (e) {
// @ts-ignore
this.debug.error(e)
return false
}
}
return false
}
return false
}
createFile(file: string, directory: string = ""): boolean {
try {
file = __dirname + "/" + directory + "/" + file + ".tsdb"
fs.writeFileSync(file, "[]")
return true
} catch (e) {
// @ts-ignore
this.debug.error(e)
return false
}
}
create(key: string, value: any): number {
if (this.tempObject) {
let id = this.tempObject.length > 0 ? this.tempObject.length + 1 : 1;
return this.tempObject.push({ id: id , ...{key, value}})
}
return -1
}
readById(id: number): Data | undefined {
if (this.tempObject)
return this.tempObject.find(s => s.id === id)
return undefined
}
read(key: string): Data | undefined {
if (this.tempObject)
return this.tempObject.find(s => s.key === key)
return undefined
}
updateById(id: number, data: Partial<Data>): void {
if (this.tempObject) {
let i = this.tempObject.findIndex(s => s.id === id)
if (i !== -1) this.tempObject[i] = { ...this.tempObject[i], ...data }
}
}
update(key: string, data: Partial<Data>): void {
if (this.tempObject) {
let i = this.tempObject.findIndex(s => s.key === key)
if (i !== -1) this.tempObject[i] = { ...this.tempObject[i], ...data }
}
}
delete(key: string): void {
if (this.tempObject) {
this.tempObject = this.tempObject.filter(v => v.key !== key)
}
}
has(key: string): boolean {
return this.read(key) !== null
}
map(): string | number {
if (this.tempObject)
return JSON.stringify(this.tempObject)
return -1
}
mapObject(): Data[] | number {
if (this.tempObject)
return this.tempObject
return -1
}
}