Skip to content

Commit

Permalink
wip: split db classes/interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Nov 28, 2024
1 parent 18dbd7f commit e937099
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
25 changes: 25 additions & 0 deletions packages/web/src/db/adapters/AsyncDatabaseConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { BatchedUpdateNotification, QueryResult } from '@powersync/common';

/**
* Proxied query result does not contain a function for accessing row values
*/
export type ProxiedQueryResult = Omit<QueryResult, 'rows'> & {
rows: {
_array: any[];
length: number;
};
};
export type OnTableChangeCallback = (event: BatchedUpdateNotification) => void;

/**
* @internal
* An async Database connection which provides basic async SQL methods.
* This is usually a proxied through a web worker.
*/
export interface AsyncDatabaseConnection {
init(): Promise<void>;
close(): Promise<void>;
execute(sql: string, params?: any[]): Promise<ProxiedQueryResult>;
executeBatch(sql: string, params?: any[]): Promise<ProxiedQueryResult>;
registerOnTableChange(callback: OnTableChangeCallback): () => void;
}
8 changes: 8 additions & 0 deletions packages/web/src/db/adapters/ProxiedDatabaseConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AsyncDatabaseConnection } from './AsyncDatabaseConnection';

export interface ProxiedDatabaseConnection extends AsyncDatabaseConnection {
/**
* Get a MessagePort which can be used to share the internals of this connection.
*/
getMessagePort(): MessagePort;
}
7 changes: 5 additions & 2 deletions packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as SQLite from '@journeyapps/wa-sqlite';
import { BaseObserver, BatchedUpdateNotification } from '@powersync/common';
import { Mutex } from 'async-mutex';
import { OnTableChangeCallback, WASQLExecuteResult } from '../../../shared/types';
import { AsyncDatabaseConnection } from '../AsyncDatabaseConnection';

/**
* List of currently tested virtual filesystems
Expand Down Expand Up @@ -95,8 +96,10 @@ export const DEFAULT_MODULE_FACTORIES = {

/**
* @internal
* WA-SQLite connection which directly interfaces with WA-SQLite.
* This is usually instantiated inside a worker.
*/
export class WASqliteConnection extends BaseObserver<WASQLiteConnectionListener> {
export class WASqliteConnection extends BaseObserver<WASQLiteConnectionListener> implements AsyncDatabaseConnection {
private _sqliteAPI: SQLiteAPI | null = null;
private _dbP: number | null = null;
private _moduleFactory: WASQLiteModuleFactory;
Expand Down Expand Up @@ -236,7 +239,7 @@ export class WASqliteConnection extends BaseObserver<WASQLiteConnectionListener>
}

async close() {
return this.sqliteAPI.close(this.dbP);
await this.sqliteAPI.close(this.dbP);
}

registerOnTableChange(callback: OnTableChangeCallback) {
Expand Down

0 comments on commit e937099

Please sign in to comment.