-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (128 loc) · 4.79 KB
/
Copy pathindex.js
File metadata and controls
143 lines (128 loc) · 4.79 KB
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
import 'dotenv/config';
import { Octokit, App } from "octokit";
import { queue } from 'd3-queue';
import hat from "hat";
import { Base64 } from 'js-base64';
export class GithubKV {
constructor(options) {
this.options = options;
this.octokit = new Octokit({ auth: `${options.token}` });
}
async initdb() {
// TODO: create the db branch
}
async list() {
return new Promise(async (resolve, reject) => {
// let { data: { tree } } = await this.octokit.rest.git.getTree({ owner: `${this.options.owner}`, repo: `${this.options.repo}`, tree_sha: `${this.options.branch}`, recursive: 2 });
let { data } = await this.octokit.rest.repos.getContent({ owner: `${this.options.owner}`, repo: `${this.options.repo}`, path: this.options.path, ref: `${this.options.branch}` });
let q = queue(1);
data.filter(item => { return item.path.match(/json$/) }).forEach(item => {
q.defer(async (cb) => {
const { data } = await this.octokit.rest.repos.getContent({
mediaType: {
format: "raw",
},
owner: `${this.options.owner}`,
repo: `${this.options.repo}`,
path: `${item.path}`,
ref: `${this.options.branch}`
});
return cb(null, { path: item.path, data: JSON.parse(data) });
})
})
q.awaitAll((err, res) => {
if (err) reject(err);
resolve(res);
})
});
}
async get(id) {
return new Promise(async (resolve, reject) => {
try {
const { data } = await this.octokit.rest.repos.getContent({
mediaType: {
format: "raw",
},
owner: `${this.options.owner}`,
repo: `${this.options.repo}`,
path: `${id}`,
ref: `${this.options.branch}`
});
resolve(JSON.parse(data));
} catch (error) {
reject(error);
}
})
}
async add(data) {
return new Promise(async (resolve, reject) => {
try {
var id = hat() + '.json';
await this.octokit.rest.repos.createOrUpdateFileContents(
{
owner: this.options.owner,
repo: this.options.repo,
path: `${id}`,
message: `Add ${id}`,
content: Base64.encode(JSON.stringify(data)),
branch: this.options.branch
}
)
resolve(id);
} catch (error) {
reject(error);
}
});
}
async update(id, data) {
return new Promise(async (resolve, reject) => {
try {
const { data: { sha } } = await this.octokit.rest.repos.getContent({
owner: `${this.options.owner}`,
repo: `${this.options.repo}`,
path: `${id}`,
ref: `${this.options.branch}`
});
await this.octokit.rest.repos.createOrUpdateFileContents(
{
owner: this.options.owner,
repo: this.options.repo,
path: `${id}`,
message: `Update ${id}`,
sha: `${sha}`,
content: Base64.encode(JSON.stringify(data)),
branch: this.options.branch
}
)
resolve();
} catch (error) {
reject(error);
}
});
}
async remove(id) {
return new Promise(async (resolve, reject) => {
try {
const { data: { sha } } = await this.octokit.rest.repos.getContent({
owner: `${this.options.owner}`,
repo: `${this.options.repo}`,
path: `${id}`,
ref: `${this.options.branch}`
});
await this.octokit.rest.repos.deleteFile(
{
owner: this.options.owner,
repo: this.options.repo,
path: `${id}`,
message: `Remove ${id}`,
sha: `${sha}`,
branch: this.options.branch
}
)
resolve();
} catch (error) {
reject(error);
}
});
}
}