Skip to content

Commit e937099

Browse files
wip: split db classes/interfaces
1 parent 18dbd7f commit e937099

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { BatchedUpdateNotification, QueryResult } from '@powersync/common';
2+
3+
/**
4+
* Proxied query result does not contain a function for accessing row values
5+
*/
6+
export type ProxiedQueryResult = Omit<QueryResult, 'rows'> & {
7+
rows: {
8+
_array: any[];
9+
length: number;
10+
};
11+
};
12+
export type OnTableChangeCallback = (event: BatchedUpdateNotification) => void;
13+
14+
/**
15+
* @internal
16+
* An async Database connection which provides basic async SQL methods.
17+
* This is usually a proxied through a web worker.
18+
*/
19+
export interface AsyncDatabaseConnection {
20+
init(): Promise<void>;
21+
close(): Promise<void>;
22+
execute(sql: string, params?: any[]): Promise<ProxiedQueryResult>;
23+
executeBatch(sql: string, params?: any[]): Promise<ProxiedQueryResult>;
24+
registerOnTableChange(callback: OnTableChangeCallback): () => void;
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { AsyncDatabaseConnection } from './AsyncDatabaseConnection';
2+
3+
export interface ProxiedDatabaseConnection extends AsyncDatabaseConnection {
4+
/**
5+
* Get a MessagePort which can be used to share the internals of this connection.
6+
*/
7+
getMessagePort(): MessagePort;
8+
}

packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as SQLite from '@journeyapps/wa-sqlite';
22
import { BaseObserver, BatchedUpdateNotification } from '@powersync/common';
33
import { Mutex } from 'async-mutex';
44
import { OnTableChangeCallback, WASQLExecuteResult } from '../../../shared/types';
5+
import { AsyncDatabaseConnection } from '../AsyncDatabaseConnection';
56

67
/**
78
* List of currently tested virtual filesystems
@@ -95,8 +96,10 @@ export const DEFAULT_MODULE_FACTORIES = {
9596

9697
/**
9798
* @internal
99+
* WA-SQLite connection which directly interfaces with WA-SQLite.
100+
* This is usually instantiated inside a worker.
98101
*/
99-
export class WASqliteConnection extends BaseObserver<WASQLiteConnectionListener> {
102+
export class WASqliteConnection extends BaseObserver<WASQLiteConnectionListener> implements AsyncDatabaseConnection {
100103
private _sqliteAPI: SQLiteAPI | null = null;
101104
private _dbP: number | null = null;
102105
private _moduleFactory: WASQLiteModuleFactory;
@@ -236,7 +239,7 @@ export class WASqliteConnection extends BaseObserver<WASQLiteConnectionListener>
236239
}
237240

238241
async close() {
239-
return this.sqliteAPI.close(this.dbP);
242+
await this.sqliteAPI.close(this.dbP);
240243
}
241244

242245
registerOnTableChange(callback: OnTableChangeCallback) {

0 commit comments

Comments
 (0)