-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathWASQLiteDBAdapter.ts
More file actions
85 lines (78 loc) · 2.82 KB
/
WASQLiteDBAdapter.ts
File metadata and controls
85 lines (78 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { type PowerSyncOpenFactoryOptions } from '@powersync/common';
import * as Comlink from 'comlink';
import { resolveWebPowerSyncFlags } from '../../PowerSyncDatabase';
import { OpenAsyncDatabaseConnection } from '../AsyncDatabaseConnection';
import { LockedAsyncDatabaseAdapter } from '../LockedAsyncDatabaseAdapter';
import {
DEFAULT_CACHE_SIZE_KB,
ResolvedWebSQLOpenOptions,
TemporaryStorageOption,
WebSQLFlags
} from '../web-sql-flags';
import { WorkerWrappedAsyncDatabaseConnection } from '../WorkerWrappedAsyncDatabaseConnection';
import { WASQLiteVFS } from './WASQLiteConnection';
import { WASQLiteOpenFactory } from './WASQLiteOpenFactory';
/**
* These flags are the same as {@link WebSQLFlags}.
* This export is maintained only for API consistency
*/
export type WASQLiteFlags = WebSQLFlags;
export interface WASQLiteDBAdapterOptions extends Omit<PowerSyncOpenFactoryOptions, 'schema'> {
flags?: WASQLiteFlags;
/**
* Use an existing port to an initialized worker.
* A worker will be initialized if none is provided
*/
workerPort?: MessagePort;
worker?: string | URL | ((options: ResolvedWebSQLOpenOptions) => Worker | SharedWorker);
vfs?: WASQLiteVFS;
temporaryStorage?: TemporaryStorageOption;
cacheSizeKb?: number;
/**
* Encryption key for the database.
* If set, the database will be encrypted using multiple-ciphers.
*/
encryptionKey?: string;
}
/**
* Adapter for WA-SQLite SQLite connections.
*/
export class WASQLiteDBAdapter extends LockedAsyncDatabaseAdapter {
constructor(options: WASQLiteDBAdapterOptions) {
super({
name: options.dbFilename,
openConnection: async () => {
const { workerPort, temporaryStorage, cacheSizeKb } = options;
if (workerPort) {
const remote = Comlink.wrap<OpenAsyncDatabaseConnection>(workerPort);
return new WorkerWrappedAsyncDatabaseConnection({
remote,
identifier: options.dbFilename,
baseConnection: await remote({
...options,
temporaryStorage: temporaryStorage ?? TemporaryStorageOption.MEMORY,
cacheSizeKb: cacheSizeKb ?? DEFAULT_CACHE_SIZE_KB,
flags: resolveWebPowerSyncFlags(options.flags),
encryptionKey: options.encryptionKey
})
});
}
const openFactory = new WASQLiteOpenFactory({
dbFilename: options.dbFilename,
dbLocation: options.dbLocation,
debugMode: options.debugMode,
flags: options.flags,
temporaryStorage,
cacheSizeKb,
logger: options.logger,
vfs: options.vfs,
encryptionKey: options.encryptionKey,
worker: options.worker
});
return openFactory.openConnection();
},
debugMode: options.debugMode,
logger: options.logger
});
}
}