-
Notifications
You must be signed in to change notification settings - Fork 3
/
minidb.js
58 lines (57 loc) · 1.44 KB
/
minidb.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
import { CSV } from "https://js.sabae.cc/CSV.js";
export const minidb = async (fn, cmd, param) => {
if (cmd == "load") {
try {
return await Deno.readTextFile(fn);
} catch (e) {
}
return null;
} else if (cmd == "get") {
const id = param.id;
const csv = await CSV.fetch(fn);
for (let i = 1; i < csv.length; i++) {
if (csv[i][0] == id) {
return csv[i].join(",");
}
}
return id.toString();
} else if (cmd == "set") {
const ss = param.data.split(",");
const id = param.id || ss[0];
console.log("set", id, fn, param)
const csv = await CSV.fetch(fn);
for (let i = 1; i < csv.length; i++) {
if (csv[i][0] == id) {
csv[i] = ss;
await Deno.writeTextFile(fn, CSV.encode(csv));
return 1;
}
}
csv.push(ss);
await Deno.writeTextFile(fn, CSV.encode(csv));
return 1;
} else if (cmd == "remove") {
const id = param.id || ss[0];
console.log("remove", id, fn, param)
const csv = await CSV.fetch(fn);
for (let i = 1; i < csv.length; i++) {
if (csv[i][0] == id) {
csv.splice(i, 1);
await Deno.writeTextFile(fn, CSV.encode(csv));
return 1;
}
}
return 1;
} else if (cmd == "delete") {
try {
await Deno.remove(fn);
return 1;
} catch (e) {
}
return 0;
} else if (cmd == "create") {
await Deno.writeTextFile(fn, param.data);
return 1;
}
return null;
};