-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupabase.js
51 lines (45 loc) · 1.15 KB
/
supabase.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
require("dotenv").config();
const supabase = require("@supabase/supabase-js");
const { getId } = require("./generatorId");
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;
const supabaseApp = supabase.createClient(supabaseUrl, supabaseKey);
async function addJsonData(data) {
try {
if (typeof data === "object") {
if (JSON.stringify(data) === "{}") {
throw SyntaxError("not json");
}
const id = getId();
const { status, error } = await supabaseApp
.from("jsons_tb")
.insert([{ id, data }]);
if (error) {
throw Error("no data added");
}
return { status, id };
} else {
throw SyntaxError("received no json data");
}
} catch (e) {
throw e;
}
}
async function getJsonData(id) {
try {
const { data, error } = await supabaseApp
.from("jsons_tb")
.select("data")
.eq("id", id)
.limit(1)
.single();
if (error) {
throw Error("not object");
}
return data.data;
} catch (e) {
throw e;
}
}
module.exports.addJsonData = addJsonData;
module.exports.getJsonData = getJsonData;