feat(persistance): add additional ops for sqlite

This commit is contained in:
2026-01-17 14:43:40 -03:00
parent fcee6e0b19
commit ab2aca1d97
2 changed files with 23 additions and 1 deletions

View File

@@ -1,14 +1,34 @@
import { inject, Injectable } from '@angular/core'; import { inject, Injectable } from '@angular/core';
import { WebSqlite } from 'angular-web-sqlite'; import { WebSqlite } from 'angular-web-sqlite';
import { BatchOp } from '../types/sqlite.type';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class Sqlite { export class Sqlite {
private readonly webSqlite = inject(WebSqlite) private readonly webSqlite = inject(WebSqlite);
initializeDatabase(dbName: string) { initializeDatabase(dbName: string) {
return this.webSqlite.init(dbName); 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);
}
} }

View File

@@ -0,0 +1,2 @@
export type QueryResult<T> = { rows: T[]};
export type BatchOp = [string, any[]];