feat(persistance): add additional ops for sqlite
This commit is contained in:
@@ -1,14 +1,34 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { WebSqlite } from 'angular-web-sqlite';
|
||||
import { BatchOp } from '../types/sqlite.type';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class Sqlite {
|
||||
private readonly webSqlite = inject(WebSqlite)
|
||||
private readonly webSqlite = inject(WebSqlite);
|
||||
|
||||
initializeDatabase(dbName: string) {
|
||||
return this.webSqlite.init(dbName);
|
||||
}
|
||||
|
||||
async executeQuery(sql: string, params: any[] = []) {
|
||||
const result = await this.webSqlite.executeSql(sql, params);
|
||||
return result;
|
||||
}
|
||||
|
||||
async batchSqlOperations(ops: BatchOp[]) {
|
||||
const result = await this.webSqlite.batchSql(ops);
|
||||
return result;
|
||||
}
|
||||
|
||||
async dropAllTables() {
|
||||
const tables: { rows: { name: string }[] } = await this.executeQuery(
|
||||
"SELECT * FROM sqlite_master WHERE type='table';"
|
||||
);
|
||||
const dropQueries: BatchOp[] = tables.rows
|
||||
.filter((r) => !r.name.startsWith('sqlite_'))
|
||||
.map((table) => [`DROP TABLE ${table.name};`, []]);
|
||||
return this.batchSqlOperations(dropQueries);
|
||||
}
|
||||
}
|
||||
|
||||
2
src/app/types/sqlite.type.ts
Normal file
2
src/app/types/sqlite.type.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export type QueryResult<T> = { rows: T[]};
|
||||
export type BatchOp = [string, any[]];
|
||||
Reference in New Issue
Block a user