Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add restore feature #2

Merged
merged 1 commit into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
node_modules/
main.js
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
13 changes: 13 additions & 0 deletions supabackup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
}
}
25 changes: 24 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}