diff --git a/.gitignore b/.gitignore index 2d7ec5c..0e86102 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env node_modules/ +main.js \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ba6cb91..90b6ada 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,8 @@ "license": "ISC", "dependencies": { "@supabase/supabase-js": "^1.29.4" - } + }, + "devDependencies": {} }, "node_modules/@supabase/gotrue-js": { "version": "1.22.0", diff --git a/package.json b/package.json index b81627f..2d74ca7 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,23 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "type": "module", - "keywords": ["supabase", "database", "backup"], + "keywords": [ + "supabase", + "database", + "backup" + ], "author": "Honorable Con", "license": "ISC", "dependencies": { "@supabase/supabase-js": "^1.29.4" - } + }, + "devDependencies": {}, + "repository": { + "type": "git", + "url": "git+https://github.com/honorableCon/supabackup.git" + }, + "bugs": { + "url": "https://github.com/honorableCon/supabackup/issues" + }, + "homepage": "https://github.com/honorableCon/supabackup#readme" } diff --git a/supabackup.js b/supabackup.js index 2247a97..fd0c795 100644 --- a/supabackup.js +++ b/supabackup.js @@ -20,4 +20,17 @@ export const backup = async (supabackup, bucket, tables) => { ); }); } +} + +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)) + }) + }) + } } \ No newline at end of file diff --git a/utils.js b/utils.js index 45e6e25..8b573fa 100644 --- a/utils.js +++ b/utils.js @@ -14,6 +14,21 @@ export class Database { return data } + + async insert(table, dataToInsert){ + const { data, error } = await this.supabase + .from(table) + .insert(dataToInsert) + + return data + } + + async clear(table){ + const { data, error } = await this.supabase + .from(table) + .delete() + return {data, error} + } } export class Storage { @@ -41,4 +56,12 @@ export class Storage { return {data, error}; } -} + + async downloadFile(bucket, folder, file){ + const { data, error } = await this.supastorage + .from(bucket) + .download(`${folder}/${file}.json`) + + return data.text(); + } +} \ No newline at end of file