-
Notifications
You must be signed in to change notification settings - Fork 1
/
supabase.js
138 lines (107 loc) · 3.7 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
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
// import config from "./config.json" assert { "type": "json" };
import { createClient } from "@supabase/supabase-js";
import fs from 'fs';
import dotenv from "dotenv";
dotenv.config();
const { supabaseUrl, supabaseKey } = process.env;
const supabase = createClient(supabaseUrl, supabaseKey, {
auth: {
persistSession: false,
},
});
const loadInvocations = () => {
try {
const data = fs.readFileSync("./invocations.json", "utf8");
return JSON.parse(data);
} catch (err) {
console.error(err);
return {};
}
};
// upload invocations.json to supabase
const upload = async () => {
const invocations = loadInvocations();
// for each user, upsert to supabase table
// key is userId
const invocationsArr = []
for (const userName in invocations) {
const user = invocations[userName];
invocationsArr.push({
username: userName,
commands: user.commands,
commandInvocations: user.commandInvocations,
tier: user.tier,
lifetimeInvocations: user.lifetimeInvocations,
dayOfSignup: new Date(user.dayOfSignup), // str to date
})
}
uploadToSupabase(invocationsArr);
}
const uploadToSupabase = async (invocations) => {
for (const userIdx in invocations) {
const user = invocations[userIdx];
const { data, error } = await supabase
.from('invocations')
.upsert({
username: user.username,
commands: user.commands,
commandInvocations: user.commandInvocations,
isMember: user.isMember,
dayOfSignup: user.dayOfSignup,
tier: user.tier,
lifetimeInvocations: user.lifetimeInvocations,
}, {
onConflict: 'username', // Handle conflicts
returning: 'minimal', // Don't return the result after the insert
});
if (error) {
console.error('Error uploading data to Supabase:', error);
}
}
}
const downloadFromSupabase = async () => {
const { data, error } = await supabase
.from('invocations')
.select('*');
if (error) {
console.error('Error downloading data from Supabase:', error);
}
return data;
}
const jsonToCsvBooks = (filename) => {
const data = fs.readFileSync(filename, "utf8");
const json = JSON.parse(data).books;
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(json[0])
let csv = json.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
csv.unshift(header.join(','))
csv = csv.join('\r\n')
fs.writeFileSync(`./${filename}.csv`, csv)
return csv
}
const jsonToCsvCommandLimts = (filename) => {
const data = fs.readFileSync(filename, "utf8");
const json = JSON.parse(data)
// convert to array of objects
const arr = []
for (const command in json) {
const obj = {
command: command,
free: json[command].free,
member: json[command].member,
paid: json[command].paid,
}
arr.push(obj)
}
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(arr[0])
let csv = arr.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
csv.unshift(header.join(','))
csv = csv.join('\r\n')
fs.writeFileSync(`./${filename}.csv`, csv)
return csv
}
// (async () => {
// await upload();
// })();
// jsonToCsvCommandLimts('./command_limits.json')