-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsupabackup.js
36 lines (29 loc) · 1.06 KB
/
supabackup.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
import {Database, Storage} from "./utils.js";
export const initializeClient = (url, servicekey) => {
const database = new Database(url, servicekey);
const storage = new Storage(url, servicekey);
return {database, storage};
}
export const backup = async (supabackup, bucket, tables) => {
supabackup.storage.creatBucket(bucket);
const instant = new Date().toISOString();
for (const table of tables) {
supabackup.database.get(table).then( (data,error) => {
supabackup.storage.uploadFile(`Backup ${instant}/${table}.json`, data)
.then(
() => console.log(`+[${table}] succesfully backup`)
);
});
}
}
export const restore = async (supabackup, bucket, folder, tables) => {
for (const table of tables) {
supabackup.storage.downloadFile(bucket, folder, table)
.then( data => {
supabackup.database.clear(table)
.then( () => {
supabackup.database.insert(table, JSON.parse(data))
})
})
}
}