Skip to content

[POC] SQL.js #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added demos/.DS_Store
Binary file not shown.
19 changes: 14 additions & 5 deletions demos/react-native-supabase-todolist/library/powersync/system.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import '@azure/core-asynciterator-polyfill';

import { createBaseLogger, LogLevel, PowerSyncDatabase, SyncClientImplementation } from '@powersync/react-native';
import { createBaseLogger, LogLevel, PowerSyncDatabase } from '@powersync/react-native';
import React from 'react';
import { SupabaseStorageAdapter } from '../storage/SupabaseStorageAdapter';

import { type AttachmentRecord } from '@powersync/attachments';
import { SQLJSOpenFactory } from '@powersync/dev-adapter';
import { configureFts } from '../fts/fts_setup';
import { KVStorage } from '../storage/KVStorage';
import { AppConfig } from '../supabase/AppConfig';
Expand All @@ -29,9 +30,17 @@ export class System {
this.storage = this.supabaseConnector.storage;
this.powersync = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: 'sqlite.db'
},
// database: {
// dbFilename: 'ddd'
// },
database: new SQLJSOpenFactory({
dbFilename: 'powersync.db',
persister: {
// TODO
readFile: async () => null,
writeFile: async () => {}
}
}),
logger
});
/**
Expand Down Expand Up @@ -68,7 +77,7 @@ export class System {

async init() {
await this.powersync.init();
await this.powersync.connect(this.supabaseConnector, { clientImplementation: SyncClientImplementation.RUST });
await this.powersync.connect(this.supabaseConnector);

if (this.attachmentQueue) {
await this.attachmentQueue.init();
Expand Down
1 change: 1 addition & 0 deletions demos/react-native-supabase-todolist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@powersync/attachments": "workspace:*",
"@powersync/common": "workspace:*",
"@powersync/react": "workspace:*",
"@powersync/dev-adapter": "workspace:*",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put this driver in it's own package for now.

SQL.js is relatively large. About 2Mb. Alternatively we could publish this under a different export e.g. @powersync/common/driver

"@powersync/react-native": "workspace:*",
"@react-native-community/masked-view": "^0.1.11",
"@react-navigation/drawer": "^7.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/db/crud/SyncProgress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BucketProgress } from 'src/client/sync/stream/core-instruction.js';
import { BucketProgress } from '../../client/sync/stream/core-instruction.js';
import type { SyncStatus } from './SyncStatus.js';

// (bucket, progress) pairs
Expand Down
3 changes: 3 additions & 0 deletions packages/dev-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PowerSync SDK Dev Adapter

A very small development DB Adapter for PowerSync which uses [SQL.js](https://sql.js.org/#/)
40 changes: 40 additions & 0 deletions packages/dev-adapter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@powersync/dev-adapter",
"version": "0.0.0",
"private": "true",
"description": "API definitions for JourneyApps PowerSync",
"type": "module",
"main": "dist/bundle.mjs",
"module": "dist/bundle.mjs",
"types": "lib/index.d.ts",
"author": "JOURNEYAPPS",
"license": "Apache-2.0",
"files": [
"lib",
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/powersync-ja/powersync-js.git"
},
"bugs": {
"url": "https://github.com/powersync-ja/powersync-js/issues"
},
"homepage": "https://docs.powersync.com",
"scripts": {
"build": "tsc -b && rollup -c rollup.config.mjs",
"build:prod": "tsc -b --sourceMap false && && rollup -c rollup.config.mjs --sourceMap false",
"clean": "rm -rf lib dist tsconfig.tsbuildinfo",
"test": "vitest"
},
"dependencies": {
"@powersync/common": "workspace:^",
"async-mutex": "^0.4.0"
},
"devDependencies": {
"@rollup/plugin-alias": "^5.1.0",
"@types/sql.js": "^1.4.9",
"rollup": "4.14.3",
"sql.js": "workspace:*"
}
}
40 changes: 40 additions & 0 deletions packages/dev-adapter/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

/**
* @returns {import('rollup').RollupOptions}
*/
export default (commandLineArgs) => {
const sourceMap = (commandLineArgs.sourceMap || 'true') == 'true';

// Clears rollup CLI warning https://github.com/rollup/rollup/issues/2694
delete commandLineArgs.sourceMap;

return {
input: 'lib/index.js',
output: {
file: 'dist/bundle.mjs',
format: 'esm',
sourcemap: sourceMap
},
plugins: [
nodeResolve({ preferBuiltins: false, browser: true }),
commonjs({}),
alias({
entries: [
{ find: 'fs', replacement: path.resolve(__dirname, 'vendored/empty.js') },
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default Emscripten output contains code like require("fs"). This seems to be unreachable, but Metro complains when it detects it. This replaces the requires to return empty modules.

{ find: 'path', replacement: path.resolve(__dirname, 'vendored/empty.js') },
{ find: 'crypto', replacement: path.resolve(__dirname, 'vendored/empty.js') }
// add others as needed
]
})
],
external: ['@powersync/common']
};
};
Loading
Loading