I am using https://ionicframework.com/docs/native/sqlite for Capacitor and have saved it to the default location. I am making the complete offline app so on every login I want to copy the database to another safe folder in user storage every time. here is my code
import { Injectable } from '@angular/core';
import { SQLitePorter } from '@awesome-cordova-plugins/sqlite-porter/ngx';
import { SQLite, SQLiteObject } from '@awesome-cordova-plugins/sqlite/ngx';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs';
import { SqliteDbCopy } from '@awesome-cordova-plugins/sqlite-db-copy/ngx';
@Injectable({
providedIn: 'root'
})
export class DatabaseServiceService {
storage: SQLiteObject;
isReady: BehaviorSubject<boolean> = new BehaviorSubject(false);
constructor(private sqlitePorter: SQLitePorter, private sqlite: SQLite, private http: HttpClient, private sqliteDbCopy: SqliteDbCopy) {
this.sqlite.create({
name: 'data.db',
location: 'default'
}).then((db: any) => {
this.storage = db._objectInstance;
this.importDb();
});
}
importDb() {
this.http.get('assets/demo.sql', {responseType: 'text'}).subscribe(data => {
this.sqlitePorter.importSqlToDb(this.storage, data).then(() => {
this.isReady.next(true);
console.warn("Our Database Created You Can Check It");
}).catch(error => {
console.error("Error Found when tried to create database", error);
});
});
}
dbState() {
return this.isReady.asObservable();
}
Hope this code will help, I have used sqlPorter to import the database schema and I want a backup that can be saved even user app uninstall. I have no server and want to create a full offline app. Thank you in advance