kikko
kikko copied to clipboard
Failed to start DB cause it is stopped - ionic angular
there is error showing on ionic angular with the latest version not able to connect the Database.
by doing let isStopped = false; in nativeIonicBackend.ts file
error is resolve let isStopped = true; database is not started yet ` async initialize() { if (isStopped) throw new Error("Failed to start DB- cause it is stopped");
db = await SQLite.create({
name: dbName,
location: "default",
});
if (isStopped) {
await db.close();
}
},
`
is isStopped is always true so its not creating database
I faced the same issue just now, using tauri-backend
. Seems like nothing is setting it to false 🤷
i have made a service in angular
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from '@awesome-cordova-plugins/sqlite/ngx';
import {
buildAsyncQueryRunner,
getTime,
IDbBackend,
initJobsState,
IPrimitiveValue,
IQuery,
IQueryResult,
IRunRes,
ITransactionOpts,
} from "@kikko-land/kikko";
@Injectable({
providedIn: 'root'
})
export class SqliteService {
constructor( public sqlite:SQLite,) { }
async ionicBackend(dbName:string) {
let sqlDB = await this.sqlite.create({
name:`${dbName}.db`,
location: 'default',
});
// console.log('db',JSON.stringify(sqlDB));
return () => {
let isStopped = false;
let db: SQLiteObject | undefined = undefined;
const jobsState = initJobsState();
const runQueries = async (
queries:
| { type: "usual"; values: IQuery[] }
| {
type: "prepared";
query: IQuery;
preparedValues: IPrimitiveValue[][];
}
): Promise<IRunRes[]> => {
if (!db) {
throw new Error(`Failed to run queries, db not initialized`);
}
const res: IRunRes[] = [];
const queriesToRun =
queries.type === "usual"
? queries.values
: queries.preparedValues.map(
(v): IQuery => ({
text: queries.query.text,
values: v,
})
);
for (const q of queriesToRun) {
const startTime = getTime();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const execResult = await (async () => {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return await db.executeSql(q.text, q.values);
} catch (e) {
if (e instanceof Error) {
e.message = `Error while executing query: ${q.text} - ${e.message}`;
}
throw e;
}
})();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const rows: IQueryResult = new Array(execResult.rows.length);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
for (let i = 0; i < execResult.rows.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
rows[i] = execResult.rows.item(i);
}
const end = getTime();
res.push({
rows: rows,
performance: {
execTime: end - startTime,
},
});
}
return res;
};
const queryRunner = buildAsyncQueryRunner({
async execPrepared(query: IQuery, preparedValues: IPrimitiveValue[][]) {
return await runQueries({ type: "prepared", query, preparedValues });
},
async execUsualBatch(queriesToRun: IQuery[]): Promise<IRunRes[]> {
return await runQueries({ type: "usual", values: queriesToRun });
},
async rollback() {
if (!db) {
throw new Error(`Failed to run queries, db not initialized`);
}
await db.executeSql("ROLLBACK");
},
});
return {
async initialize() {
if (isStopped)
throw new Error("Failed to start DB - cause it is stopped");
db = sqlDB;
if (isStopped) {
await db.close();
}
},
async execQueries(
q:
| { type: "usual"; values: IQuery[] }
| {
type: "prepared";
query: IQuery;
preparedValues: IPrimitiveValue[][];
},
transactionOpts?: ITransactionOpts
) {
const startedAt = getTime();
const res = await queryRunner.run(jobsState, q, transactionOpts);
const endAt = getTime();
return {
...res,
performance: {
...res.performance,
totalTime: endAt - startedAt,
},
};
},
async stop() {
isStopped = true;
if (db) {
await db.close();
}
},
};
};
}
}
import this sqliteKikko service and in start function
let ionicBackend = await this.sqliteKikko.ionicBackend(this.dbName);
this.sqliteDb = ionicBackend().initialize();
console.log("sqlite", this.sqliteDb);
console.log("db_start");
await initDbClient({
dbName: this.dbName,
dbBackend: ionicBackend ,
plugins: []
}).then(async (dbConnect)=>{
this.kikkoDb = dbConnect ;
}).catch(e => console.log("db",e));
hope it help