Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/add-conditional-…
Browse files Browse the repository at this point in the history
…attachment-download
  • Loading branch information
HeinrichvonStein committed Dec 3, 2024
2 parents 32f4ddb + 3513514 commit 1cb4be4
Show file tree
Hide file tree
Showing 53 changed files with 1,831 additions and 602 deletions.
5 changes: 0 additions & 5 deletions .changeset/curly-poets-explode.md

This file was deleted.

2 changes: 1 addition & 1 deletion demos/django-react-native-todolist/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
buildscript {
ext {
buildToolsVersion = findProperty('android.buildToolsVersion') ?: '34.0.0'
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '23')
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '24')
compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '34')
targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '34')
kotlinVersion = findProperty('android.kotlinVersion') ?: '1.9.23'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ EX_DEV_CLIENT_NETWORK_INSPECTOR=true
# Use legacy packaging to compress native libraries in the resulting APK.
expo.useLegacyPackaging=false

android.minSdkVersion=23
android.minSdkVersion=24
android.compileSdkVersion=34
android.targetSdkVersion=34
android.buildToolsVersion=34.0.0
Expand Down
2 changes: 1 addition & 1 deletion demos/django-react-native-todolist/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"newArchEnabled": false
},
"android": {
"minSdkVersion": 23,
"minSdkVersion": 24,
"compileSdkVersion": 34,
"targetSdkVersion": 34,
"buildToolsVersion": "34.0.0",
Expand Down
2 changes: 1 addition & 1 deletion demos/django-react-native-todolist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@azure/core-asynciterator-polyfill": "^1.0.2",
"@expo/vector-icons": "^14.0.0",
"@journeyapps/react-native-quick-sqlite": "^2.1.2",
"@journeyapps/react-native-quick-sqlite": "^2.2.0",
"@powersync/common": "workspace:*",
"@powersync/react": "workspace:*",
"@powersync/react-native": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
buildscript {
ext {
buildToolsVersion = findProperty('android.buildToolsVersion') ?: '34.0.0'
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '23')
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '24')
compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '34')
targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '34')
kotlinVersion = findProperty('android.kotlinVersion') ?: '1.9.23'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ EX_DEV_CLIENT_NETWORK_INSPECTOR=false
# Use legacy packaging to compress native libraries in the resulting APK.
expo.useLegacyPackaging=false

android.minSdkVersion=23
android.minSdkVersion=24
android.compileSdkVersion=34
android.targetSdkVersion=34
android.buildToolsVersion=34.0.0
Expand Down
2 changes: 1 addition & 1 deletion demos/react-native-supabase-group-chat/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const config: ExpoConfig = {
newArchEnabled: false
},
android: {
minSdkVersion: 23,
minSdkVersion: 24,
compileSdkVersion: 34,
targetSdkVersion: 34,
buildToolsVersion: '34.0.0',
Expand Down
2 changes: 1 addition & 1 deletion demos/react-native-supabase-group-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@azure/core-asynciterator-polyfill": "^1.0.2",
"@faker-js/faker": "8.3.1",
"@journeyapps/react-native-quick-sqlite": "^2.1.2",
"@journeyapps/react-native-quick-sqlite": "^2.2.0",
"@powersync/common": "workspace:*",
"@powersync/react": "workspace:*",
"@powersync/react-native": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion demos/react-native-supabase-todolist/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
buildscript {
ext {
buildToolsVersion = findProperty('android.buildToolsVersion') ?: '34.0.0'
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '23')
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '24')
compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '34')
targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '34')
kotlinVersion = findProperty('android.kotlinVersion') ?: '1.9.23'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ EX_DEV_CLIENT_NETWORK_INSPECTOR=true
# Use legacy packaging to compress native libraries in the resulting APK.
expo.useLegacyPackaging=false

android.minSdkVersion=23
android.minSdkVersion=24
android.compileSdkVersion=34
android.targetSdkVersion=34
android.buildToolsVersion=34.0.0
Expand Down
2 changes: 1 addition & 1 deletion demos/react-native-supabase-todolist/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const config: ExpoConfig = {
newArchEnabled: true
},
android: {
minSdkVersion: 23,
minSdkVersion: 24,
compileSdkVersion: 34,
targetSdkVersion: 34,
buildToolsVersion: '34.0.0',
Expand Down
2 changes: 1 addition & 1 deletion demos/react-native-supabase-todolist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@azure/core-asynciterator-polyfill": "^1.0.2",
"@expo/vector-icons": "^14.0.3",
"@journeyapps/react-native-quick-sqlite": "^2.1.2",
"@journeyapps/react-native-quick-sqlite": "^2.2.0",
"@powersync/attachments": "workspace:*",
"@powersync/common": "workspace:*",
"@powersync/react": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
buildscript {
ext {
buildToolsVersion = findProperty('android.buildToolsVersion') ?: '34.0.0'
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '23')
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '24')
compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '34')
targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '34')
kotlinVersion = findProperty('android.kotlinVersion') ?: '1.9.23'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ EX_DEV_CLIENT_NETWORK_INSPECTOR=true
# Use legacy packaging to compress native libraries in the resulting APK.
expo.useLegacyPackaging=false

android.minSdkVersion=23
android.minSdkVersion=24
android.compileSdkVersion=34
android.targetSdkVersion=34
android.buildToolsVersion=34.0.0
Expand Down
2 changes: 1 addition & 1 deletion demos/react-native-web-supabase-todolist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@azure/core-asynciterator-polyfill": "^1.0.2",
"@expo/metro-runtime": "^3.2.1",
"@expo/vector-icons": "^14.0.0",
"@journeyapps/react-native-quick-sqlite": "^2.1.2",
"@journeyapps/react-native-quick-sqlite": "^2.2.0",
"@powersync/attachments": "workspace:*",
"@powersync/common": "workspace:*",
"@powersync/react": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion demos/vue-supabase-todolist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"vite-plugin-top-level-await": "^1.4.1",
"vite-plugin-vuetify": "^2.0.3",
"vite-plugin-wasm": "^3.3.0",
"vue-tsc": "^2.0.6"
"vue-tsc": "2.0.6"
}
}
6 changes: 6 additions & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @powersync/common

## 1.22.0

### Minor Changes

- 77a9ed2: Added `compilableQueryWatch()` utility function which allows any compilable query to be watched.

## 1.21.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powersync/common",
"version": "1.21.0",
"version": "1.22.0",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
Expand Down
55 changes: 55 additions & 0 deletions packages/common/src/client/compilableQueryWatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { CompilableQuery } from './../types/types.js';
import { AbstractPowerSyncDatabase, SQLWatchOptions } from './AbstractPowerSyncDatabase.js';
import { runOnSchemaChange } from './runOnSchemaChange.js';

export interface CompilableQueryWatchHandler<T> {
onResult: (results: T[]) => void;
onError?: (error: Error) => void;
}

export function compilableQueryWatch<T>(
db: AbstractPowerSyncDatabase,
query: CompilableQuery<T>,
handler: CompilableQueryWatchHandler<T>,
options?: SQLWatchOptions
): void {
const { onResult, onError = (e: Error) => {} } = handler ?? {};
if (!onResult) {
throw new Error('onResult is required');
}

const watchQuery = async (abortSignal: AbortSignal) => {
try {
const toSql = query.compile();
const resolvedTables = await db.resolveTables(toSql.sql, toSql.parameters as [], options);

// Fetch initial data
const result = await query.execute();
onResult(result);

db.onChangeWithCallback(
{
onChange: async () => {
try {
const result = await query.execute();
onResult(result);
} catch (error: any) {
onError(error);
}
},
onError
},
{
...(options ?? {}),
tables: resolvedTables,
// Override the abort signal since we intercept it
signal: abortSignal
}
);
} catch (error: any) {
onError(error);
}
};

runOnSchemaChange(watchQuery, db, options);
}
2 changes: 1 addition & 1 deletion packages/common/src/db/schema/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RowType, Table } from './Table.js';

type SchemaType = Record<string, Table<any>>;

type SchemaTableType<S extends SchemaType> = {
export type SchemaTableType<S extends SchemaType> = {
[K in keyof S]: RowType<S[K]>;
};

Expand Down
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './client/connection/PowerSyncBackendConnector.js';
export * from './client/connection/PowerSyncCredentials.js';
export * from './client/sync/bucket/BucketStorageAdapter.js';
export { runOnSchemaChange } from './client/runOnSchemaChange.js';
export { CompilableQueryWatchHandler, compilableQueryWatch } from './client/compilableQueryWatch.js';
export { UpdateType, CrudEntry, OpId } from './client/sync/bucket/CrudEntry.js';
export * from './client/sync/bucket/SqliteBucketStorage.js';
export * from './client/sync/bucket/CrudBatch.js';
Expand Down
12 changes: 12 additions & 0 deletions packages/drizzle-driver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @powersync/drizzle-driver

## 0.2.0

### Minor Changes

- 77a9ed2: Added `watch()` function to Drizzle wrapper to support watched queries. This function invokes `execute()` on the Drizzle query which improves support for complex queries such as those which are relational.
- 4a70624: Added helper `toPowersyncTable` function and `DrizzleAppSchema` constructor to convert a Drizzle schema into a PowerSync app schema.

### Patch Changes

- Updated dependencies [77a9ed2]
- @powersync/common@1.22.0

## 0.1.0

### Minor Changes
Expand Down
91 changes: 85 additions & 6 deletions packages/drizzle-driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { wrapPowerSyncWithDrizzle } from '@powersync/drizzle-driver';
import { PowerSyncDatabase } from '@powersync/web';
import { relations } from 'drizzle-orm';
import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { appSchema } from './schema';
import { AppSchema } from './schema';

export const lists = sqliteTable('lists', {
id: text('id'),
Expand Down Expand Up @@ -47,24 +47,99 @@ export const drizzleSchema = {
todosRelations
};

// As an alternative to manually defining a PowerSync schema, generate the local PowerSync schema from the Drizzle schema with the `DrizzleAppSchema` constructor:
// import { DrizzleAppSchema } from '@powersync/drizzle-driver';
// export const AppSchema = new DrizzleAppSchema(drizzleSchema);
//
// This is optional, but recommended, since you will only need to maintain one schema on the client-side
// Read on to learn more.

export const powerSyncDb = new PowerSyncDatabase({
database: {
dbFilename: 'test.sqlite'
},
schema: appSchema
schema: AppSchema
});

// This is the DB you will use in queries
export const db = wrapPowerSyncWithDrizzle(powerSyncDb, {
schema: drizzleSchema
});
```

## Known limitations
## Schema Conversion

- The integration does not currently support nested transactions (also known as `savepoints`).
- The Drizzle schema needs to be created manually, and it should match the table definitions of your PowerSync schema.
The `DrizzleAppSchema` constructor simplifies the process of integrating Drizzle with PowerSync. It infers the local [PowerSync schema](https://docs.powersync.com/installation/client-side-setup/define-your-schema) from your Drizzle schema definition, providing a unified development experience.

As the PowerSync schema only supports SQLite types (`text`, `integer`, and `real`), the same limitation extends to the Drizzle table definitions.

To use it, define your Drizzle tables and supply the schema to the `DrizzleAppSchema` function:

```js
import { DrizzleAppSchema } from '@powersync/drizzle-driver';
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';

// Define a Drizzle table
const lists = sqliteTable('lists', {
id: text('id').primaryKey().notNull(),
created_at: text('created_at'),
name: text('name').notNull(),
owner_id: text('owner_id')
});

export const drizzleSchema = {
lists
};

// Infer the PowerSync schema from your Drizzle schema
export const AppSchema = new DrizzleAppSchema(drizzleSchema);
```

### Defining PowerSync Options

### Compilable queries
The PowerSync table definition allows additional options supported by PowerSync's app schema beyond that which are supported by Drizzle.
They can be specified as follows. Note that these options exclude indexes as they can be specified in a Drizzle table.

```js
import { DrizzleAppSchema } from '@powersync/drizzle-driver';
// import { DrizzleAppSchema, type DrizzleTableWithPowerSyncOptions} from '@powersync/drizzle-driver'; for TypeScript

const listsWithOptions = { tableDefinition: logs, options: { localOnly: true } };
// const listsWithOptions: DrizzleTableWithPowerSyncOptions = { tableDefinition: logs, options: { localOnly: true } }; for TypeScript

export const drizzleSchemaWithOptions = {
lists: listsWithOptions
};

export const AppSchema = new DrizzleAppSchema(drizzleSchemaWithOptions);
```

### Converting a Single Table From Drizzle to PowerSync

Drizzle tables can also be converted on a table-by-table basis with `toPowerSyncTable`.

```js
import { toPowerSyncTable } from '@powersync/drizzle-driver';
import { Schema } from '@powersync/web';
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';

// Define a Drizzle table
const lists = sqliteTable('lists', {
id: text('id').primaryKey().notNull(),
created_at: text('created_at'),
name: text('name').notNull(),
owner_id: text('owner_id')
});

const psLists = toPowerSyncTable(lists); // converts the Drizzle table to a PowerSync table
// toPowerSyncTable(lists, { localOnly: true }); - allows for PowerSync table configuration

export const AppSchema = new Schema({
lists: psLists // names the table `lists` in the PowerSync schema
});
```

## Compilable queries

To use Drizzle queries in your hooks and composables, queries need to be converted using `toCompilableQuery`.

Expand All @@ -76,3 +151,7 @@ const { data: listRecords, isLoading } = useQuery(toCompilableQuery(query));
```

For more information on how to use Drizzle queries in PowerSync, see [here](https://docs.powersync.com/client-sdk-references/javascript-web/javascript-orm/drizzle#usage-examples).

## Known limitations

- The integration does not currently support nested transactions (also known as `savepoints`).
4 changes: 2 additions & 2 deletions packages/drizzle-driver/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powersync/drizzle-driver",
"version": "0.1.0",
"version": "0.2.0",
"description": "Drizzle driver for PowerSync",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
Expand All @@ -26,7 +26,7 @@
"test": "vitest"
},
"peerDependencies": {
"@powersync/common": "workspace:^1.19.0",
"@powersync/common": "workspace:^1.22.0",
"drizzle-orm": "<1.0.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 1cb4be4

Please sign in to comment.