Skip to content

Commit aa04921

Browse files
committed
Minor polish.
1 parent 4f1e221 commit aa04921

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

packages/dev/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# PowerSync Dev
22

3-
A development package for PowerSync which uses [SQL.js](https://sql.js.org/#/)
3+
A development package for PowerSync which uses [SQL.js](https://sql.js.org/#/) to provide a pure JavaScript SQLite implementation.
4+
This eliminates the need for native dependencies and enabling seamless development with Expo Go and other JavaScript-only environments.
5+
6+
This package is specifically designed to streamline the development workflow and will be much slower than DB adapters that use native dependencies.
7+
For example when building React Native apps we recommend switching to our OP-sqlite or RNQS adapters when making production builds as they give substantially better performance.
48

59
## Note: Alpha Release
610

packages/dev/rollup.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ export default (commandLineArgs) => {
2828
commonjs({}),
2929
alias({
3030
entries: [
31+
// The default Emscripten output contains code like `require("fs")`. This seems
32+
// to be unreachable, but Metro complains when it detects it.
3133
{ find: 'fs', replacement: path.resolve(__dirname, 'vendored/empty.js') },
3234
{ find: 'path', replacement: path.resolve(__dirname, 'vendored/empty.js') },
3335
{ find: 'crypto', replacement: path.resolve(__dirname, 'vendored/empty.js') }
34-
// add others as needed
3536
]
3637
})
3738
],

packages/dev/src/SQLJSAdapter.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import {
33
BaseObserver,
44
BatchedUpdateNotification,
55
ControlledExecutor,
6+
createLogger,
67
DBAdapter,
78
DBAdapterListener,
89
DBLockOptions,
10+
ILogger,
911
LockContext,
1012
QueryResult,
1113
SQLOpenFactory,
@@ -23,10 +25,12 @@ export interface SQLJSPersister {
2325

2426
export interface SQLJSOpenOptions extends SQLOpenOptions {
2527
persister?: SQLJSPersister;
28+
logger?: ILogger;
2629
}
2730

2831
export interface ResolvedSQLJSOpenOptions extends SQLJSOpenOptions {
2932
persister: SQLJSPersister;
33+
logger: ILogger;
3034
}
3135

3236
export class SQLJSOpenFactory implements SQLOpenFactory {
@@ -101,30 +105,29 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
101105
readFile: async () => null,
102106
writeFile: async () => {}
103107
};
108+
109+
const logger = options.logger ?? createLogger('SQLJSDBAdapter');
110+
104111
return {
105112
...options,
106-
persister
113+
persister,
114+
logger
107115
};
108116
}
109117

110118
protected async init(): Promise<SQLJs.Database> {
111119
const SQL = await SQLJs({
112120
locateFile: (filename: any) => `../dist/${filename}`,
113121
print: (text) => {
114-
console.log('[stdout]', text);
122+
this.options.logger.info(text);
115123
},
116124
printErr: (text) => {
117-
console.error('[stderr]', text);
125+
this.options.logger.error('[stderr]', text);
118126
}
119127
});
120128
const existing = await this.options.persister.readFile();
121129
const db = new SQL.Database(existing);
122130
this.dbP = db['db'];
123-
// debugger;
124-
// (db as any).updateHook(function (operation, database, table, rowId) {
125-
// console.log(`Update Hook: ${operation} on ${table}`);
126-
// this.tableUpdateCache.add(table);
127-
// });
128131
this._db = db;
129132
return db;
130133
}
@@ -225,24 +228,24 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
225228
let totalRowsAffected = 0;
226229
const db = await this.getDB();
227230

231+
const stmt = db.prepare(query);
228232
try {
229-
const stmt = db.prepare(query);
230-
231233
for (const paramSet of params) {
232234
stmt.run(paramSet);
233235
totalRowsAffected += db.getRowsModified();
234236
}
235237

236-
stmt.free();
237-
238238
return {
239239
rowsAffected: totalRowsAffected
240240
};
241-
} catch (error) {
242-
throw error;
241+
} finally {
242+
stmt.free();
243243
}
244244
}
245245

246+
/**
247+
* We're not using separate read/write locks here because we can't implement connection pools on top of SQL.js.
248+
*/
246249
readLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {
247250
return this.writeLock(fn, options);
248251
}

0 commit comments

Comments
 (0)