forked from TalaikisInc/json-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.js
193 lines (182 loc) · 4.85 KB
/
mod.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { open, unlink, ftruncate, readDir, readFile, stat, writeFile, close, mkdir, rmdir } from './deps.ts'
import { join } from './deps.ts'
// const { promisify } = require('util')
const closeFile = (descriptor, done) => {
close(descriptor, (err) => {
if (!err) {
done(false, {})
} else {
done(`Error closing file: ${err.message}`)
}
})
}
const write = (descriptor, data, done) => {
writeFile(descriptor, data, (err) => {
if (!err) {
closeFile(descriptor, done)
} else {
done(`Error writing file: ${err.message}`)
}
})
}
const stringToJson = (msg, done) => {
try {
done(false, JSON.parse(msg))
} catch (e) {
done(e.message)
}
}
const createTable = (dir, done) => {
mkdir(dir, (err) => {
if (err) {
if (err.code == 'EEXIST') {
done(false)
} else {
done(err)
}
} else {
done(false)
}
})
}
export const create = async (baseDir, dir, file, data, done) => {
const dataDir = join(baseDir, dir)
const fileName = join(dataDir, `${file}.json`)
createTable(dataDir, (err) => {
if (!err) {
open(fileName, 'wx', (err, fileDescriptor) => {
if (!err && fileDescriptor) {
const dataString = typeof data === 'string' ? data : JSON.stringify(data)
write(fileDescriptor, dataString, done)
} else {
done('Cannot create new item, it may exist already.')
}
})
} else {
done(`Cannot create table: ${err.message}`)
}
})
}
export const read = async (baseDir, dir, file, done) => {
const filePath = join(baseDir, dir, `${file}.json`)
stat(filePath, (err, _) => {
if (!err) {
readFile(filePath, 'utf8', (err, data) => {
if (!err && data) {
stringToJson(data, (err, parsed) => {
if (!err && parsed) {
done(false, parsed)
} else {
done(err)
}
})
} else {
done(err, data)
}
})
} else {
done('No such item or table.')
}
})
}
export const update = async (baseDir, dir, file, data, done) => {
open(join(baseDir, dir, `${file}.json`), 'r+', (err, fileDescriptor) => {
if (!err && fileDescriptor) {
const dataString = typeof data === 'string' ? data : JSON.stringify(data)
ftruncate(fileDescriptor, () => {
if (!err) {
write(fileDescriptor, dataString, done)
} else {
done('Error truncating file.')
}
})
} else {
done('Cannot open file for updating, file may not exist.')
}
})
}
export const destroy = async (baseDir, dir, file, done) => {
const filepath = join(baseDir, dir, `${file}.json`)
stat(filepath, (err, _) => {
if (!err) {
unlink(filepath, (err) => {
if (!err) {
done(false, {})
} else {
done('Error deleting file.')
}
})
} else {
done('File doens\'t exist.')
}
})
}
export const list = async (baseDir, dir, done) => {
const filePath = join(baseDir, dir)
stat(filePath, (err, r) => {
if (err === null) {
readDir(filePath, (err, data) => {
if (!err && data && data.length > 0) {
const out = []
data.forEach((filename) => {
if (filename.indexOf('.json') > -1) {
out.push(filename.replace('.json', ''))
}
})
done(false, out)
} else {
done(err)
}
})
} else {
done(false, [])
}
})
}
export const destroyTable = async (baseDir, dir, done) => {
const path = join(baseDir, dir)
_list(baseDir, dir, (err, toDelete) => {
if (!err && toDelete) {
let errors = 0
let i = 1
toDelete.forEach((el) => {
_destroy(baseDir, dir, el, (err, _) => {
if (err) {
errors++
i++
} else {
if (toDelete.length === i && errors === 0) {
try {
stat(path, (err, _) => {
if (err === null) {
rmdir(path, (err) => {
if (err === null) {
done(false)
} else {
done(`Error deleting table: ${err.message} with ${errors} other errrors.`)
}
})
} else {
done(false)
}
})
} catch (e) {
// console.error(e.message)
done(false)
}
}
i++
}
})
})
} else {
done(err)
}
})
}
// module.exports.create = promisify(_create)
// module.exports.read = promisify(_read)
// module.exports.destroy = promisify(_destroy)
// module.exports.list = promisify(_list)
// module.exports.update = promisify(_update)
// module.exports.destroyTable = promisify(_deleteTable)