Skip to content

Commit b0ea41d

Browse files
committed
feat: new wrapDb method to use existing native database
1 parent 56e5374 commit b0ea41d

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/sqlite/sqlite.android.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function createDb(dbName: string, flags) {
2121
}
2222

2323
export class SQLiteDatabase extends SQLiteDatabaseBase implements ISQLiteDatabase {
24+
2425
open() {
2526
if (!this.db) {
2627
this.db = createDb(this.filePath, this.flags);
@@ -33,6 +34,19 @@ export class SQLiteDatabase extends SQLiteDatabaseBase implements ISQLiteDatabas
3334
}
3435
}
3536

37+
export function wrapDb(
38+
db: android.database.sqlite.SQLiteDatabase,
39+
options?: {
40+
readOnly?: boolean;
41+
transformBlobs?: boolean;
42+
threading?: boolean;
43+
}
44+
): SQLiteDatabase {
45+
const obj = new SQLiteDatabase(db, options);
46+
obj.open();
47+
return obj;
48+
}
49+
3650
export const openOrCreate = (
3751
filePath: string,
3852
options?: {

src/sqlite/sqlite.ios.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,21 +419,30 @@ async function transactionRaw<T = any>(db: FMDatabase, action: (cancel?: () => v
419419
export class SQLiteDatabase implements ISQLiteDatabase {
420420
db: FMDatabase;
421421
transformBlobs: boolean;
422+
filePath: string;
422423
constructor(
423-
public filePath: string,
424+
filePathOrDb: string | FMDatabase,
424425
options?: {
425426
threading?: boolean;
426427
transformBlobs?: boolean;
427428
readOnly?: boolean;
428429
}
429430
) {
431+
if (filePathOrDb instanceof FMDatabase) {
432+
this.db = filePathOrDb;
433+
this.filePath = filePathOrDb.databaseURL.absoluteString;
434+
this.isOpen = this.db.isOpen;
435+
} else {
436+
this.filePath = filePathOrDb;
437+
}
430438
this.transformBlobs = !options || options.transformBlobs !== false;
431439
}
432440
isOpen = false;
433441
open() {
434442
if (!this.db) {
435443
this.db = FMDatabase.databaseWithPath(getRealPath(this.filePath));
436444
}
445+
this.isOpen = this.db.isOpen;
437446
if (!this.isOpen) {
438447
this.isOpen = this.db.open();
439448
}
@@ -483,11 +492,10 @@ export class SQLiteDatabase implements ISQLiteDatabase {
483492
if (!this._isInTransaction) {
484493
this._isInTransaction = shouldFinishTransaction = true;
485494
res = await transactionRaw(this.db, action, true);
486-
}
487-
else {
495+
} else {
488496
res = await transactionRaw(this.db, action, false);
489497
}
490-
} catch(error) {
498+
} catch (error) {
491499
throw error;
492500
} finally {
493501
if (shouldFinishTransaction) {
@@ -512,6 +520,19 @@ export function openOrCreate(
512520
return obj;
513521
}
514522

523+
export function wrapDb(
524+
db: FMDatabase,
525+
options?: {
526+
readOnly?: boolean;
527+
transformBlobs?: boolean;
528+
threading?: boolean;
529+
}
530+
): SQLiteDatabase {
531+
const obj = new SQLiteDatabase(db, options);
532+
obj.open();
533+
return obj;
534+
}
535+
515536
export const deleteDatabase = (filePath: string) => {
516537
filePath = getRealPath(filePath);
517538
const fileManager = iosProperty(NSFileManager, NSFileManager.defaultManager);

src/sqlite/sqlitedatabase.android.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,25 @@ const transactionRaw = async <T = any>(db: Db, action: (cancel?: () => void) =>
149149
const messagePromises: { [key: string]: { resolve: Function; reject: Function; timeoutTimer: ReturnType<typeof setTimeout> }[] } = {};
150150

151151
export class SQLiteDatabaseBase {
152+
filePath: string;
152153
db: android.database.sqlite.SQLiteDatabase;
153154
flags;
154155
transformBlobs: boolean;
155156
constructor(
156-
public filePath: string,
157+
filePathOrDb: string | android.database.sqlite.SQLiteDatabase,
157158
options?: {
158159
threading?: boolean;
159160
readOnly?: boolean;
160161
flags?: number;
161162
transformBlobs?: boolean;
162163
}
163164
) {
165+
if (filePathOrDb instanceof android.database.sqlite.SQLiteDatabase) {
166+
this.db = filePathOrDb;
167+
this.filePath = filePathOrDb.getPath();
168+
} else {
169+
this.filePath = filePathOrDb;
170+
}
164171
this.threading = options && options.threading === true;
165172
this.flags = options?.flags;
166173
this.transformBlobs = !options || options.transformBlobs !== false;

0 commit comments

Comments
 (0)