Skip to content

Commit 3fccdca

Browse files
added delete/update api
1 parent 0cf4a14 commit 3fccdca

2 files changed

Lines changed: 109 additions & 31 deletions

File tree

index.js

Lines changed: 95 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,103 @@
1-
const axios= require("axios");
1+
const axios = require("axios");
22

3-
const url= `https://github-db.glitch.me/db/api`;
3+
const url = `https://github-db.glitch.me/db/api`;
44

5-
function Client({db,token}){
6-
const options={db,token};
7-
let validated=false;
8-
async function validate(){
9-
if(!validated){
10-
const {data}=await axios.get(`${url}/validate`,{params:options});
11-
validated=data.valid
12-
}
5+
function Client({ db, token }) {
6+
const options = { db, token };
7+
let validated = false;
8+
async function validate() {
9+
try {
10+
if (!validated) {
11+
const { data } = await axios.get(`${url}/validate`, {
12+
params: options
13+
});
14+
validated = data.valid;
15+
}
16+
} catch (error) {
17+
hanlderError(error);
1318
}
14-
async function add(document,payload){
15-
await validate();
16-
const resp=await axios.post(`${url}/add`,{db:options.db,token,document,payload});
17-
return resp.data.identifier;
19+
}
20+
async function add(document, payload) {
21+
try {
22+
await validate();
23+
const resp = await axios.post(`${url}/add`, {
24+
db: options.db,
25+
token,
26+
document,
27+
payload
28+
});
29+
return resp.data;
30+
} catch (error) {
31+
hanlderError(error);
1832
}
19-
async function fetchOne(document,identifier){
20-
await validate();
21-
const resp=await axios.get(`${url}/fetchOne`,{params:{db:options.db,token,document,identifier}});
22-
return resp.data;
33+
}
34+
async function update(document, identifier, payload) {
35+
try {
36+
await validate();
37+
const resp = await axios.post(`${url}/update`, {
38+
db: options.db,
39+
token,
40+
document,
41+
identifier,
42+
payload
43+
});
44+
return resp.data;
45+
} catch (error) {
46+
hanlderError(error);
2347
}
24-
async function fetchAll(document){
25-
await validate();
26-
const resp=await axios.get(`${url}/fetchAll`,{params:{db:options.db,token,document}});
27-
return resp.data;
48+
}
49+
async function _delete(document, identifier) {
50+
try {
51+
await validate();
52+
const resp = await axios.post(`${url}/delete`, {
53+
db: options.db,
54+
token,
55+
document,
56+
identifier
57+
});
58+
return resp.data;
59+
} catch (error) {
60+
hanlderError(error);
2861
}
29-
return {
30-
add,fetchOne,fetchAll
62+
}
63+
async function fetchOne(document, identifier) {
64+
try {
65+
await validate();
66+
const resp = await axios.get(`${url}/fetchOne`, {
67+
params: { db: options.db, token, document, identifier }
68+
});
69+
return resp.data;
70+
} catch (error) {
71+
hanlderError(error);
3172
}
73+
}
74+
async function fetchAll(document) {
75+
try {
76+
await validate();
77+
const resp = await axios.get(`${url}/fetchAll`, {
78+
params: { db: options.db, token, document }
79+
});
80+
return resp.data;
81+
} catch (error) {
82+
hanlderError(error);
83+
}
84+
}
85+
return {
86+
add,
87+
fetchOne,
88+
fetchAll,
89+
delete: _delete,
90+
update
91+
};
92+
}
93+
function hanlderError(error) {
94+
if (error.response) {
95+
throw new Error(error.response.data);
96+
} else if (error.request) {
97+
throw error.request;
98+
} else {
99+
throw new Error(error.message);
100+
}
32101
}
33-
module.exports= Client;
102+
103+
module.exports = Client;

test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11

2-
const GithubDb=require("github-db");
2+
const GithubDb=require("./index");
33

4-
const db= GithubDb({db:"test-db",token:"test"});
4+
const db= GithubDb({db:process.env.db,token:process.env.token});
55
(async function(){
66
try {
7-
const id=await db.add("user",{name:"John"});
8-
console.log("added user", id);
7+
const {identifier:id1}=await db.add("user",{name:"John"});
8+
console.log("added user", id1);
99

10-
const id2=await db.add("user",{name:"Will"});
10+
const {identifier:id2}=await db.add("user",{name:"Will"});
1111
console.log("added user", id2);
1212

13-
const user=await db.fetchOne("user",id);
13+
const user=await db.fetchOne("user",id1);
1414
console.log("fetched user", user);
15+
16+
const updated=await db.update("user",id1,{name:"John Cena"});
17+
console.log("updated user", updated);
18+
1519
const users=await db.fetchAll("user");
1620
console.log("fetched users", users);
21+
for (const {identifier} of users) {
22+
const result=await db.delete("user",identifier);
23+
console.log("delete users", result);
24+
}
1725
} catch (error) {
1826
console.error(error);
1927
}

0 commit comments

Comments
 (0)