Skip to content

Commit

Permalink
Merge branch 'main' into chore/clean-up-connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicGBauer authored Dec 12, 2024
2 parents c8f6e97 + 6ad09ca commit 8ba49d6
Show file tree
Hide file tree
Showing 47 changed files with 1,619 additions and 101 deletions.
2 changes: 1 addition & 1 deletion demos/angular-supabase-todolist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This demo is currently in an alpha release.

Demo app demonstrating use of the [PowerSync SDK for Web](https://www.npmjs.com/package/@powersync/web) together with Supabase.

A step-by-step guide on Supabase<>PowerSync integration is available [here](https://docs.powersync.com/integration-guides/supabase).
A step-by-step guide on Supabase<>PowerSync integration is available [here](https://docs.powersync.com/integration-guides/supabase-+-powersync).

## Quick Start

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { column, Schema, TableV2 } from '@powersync/react-native';
import { column, Schema, Table } from '@powersync/react-native';

export const LIST_TABLE = 'lists';
export const TODO_TABLE = 'todos';

const todos = new TableV2(
const todos = new Table(
{
list_id: column.text,
created_at: column.text,
Expand All @@ -17,7 +17,7 @@ const todos = new TableV2(
{ indexes: { list: ['list_id'] } }
);

const lists = new TableV2({
const lists = new Table({
created_at: column.text,
name: column.text,
owner_id: column.text
Expand Down
4 changes: 2 additions & 2 deletions demos/example-capacitor/src/library/powersync/AppSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { column, Schema, TableV2 } from '@powersync/web';
import { column, Schema, Table } from '@powersync/web';

const customers = new TableV2({
const customers = new Table({
name: column.text,
created_at: column.text
});
Expand Down
4 changes: 2 additions & 2 deletions demos/example-electron/src/library/powersync/AppSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { column, Schema, TableV2 } from '@powersync/web';
import { column, Schema, Table } from '@powersync/web';

const customers = new TableV2({
const customers = new Table({
name: column.text,
created_at: column.text
});
Expand Down
4 changes: 2 additions & 2 deletions demos/example-nextjs/src/library/powersync/AppSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { column, Schema, TableV2 } from '@powersync/web';
import { column, Schema, Table } from '@powersync/web';

const customers = new TableV2({
const customers = new Table({
name: column.text,
created_at: column.text
});
Expand Down
3 changes: 1 addition & 2 deletions demos/react-native-supabase-todolist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Demo app demonstrating use of the [PowerSync SDK for React Native](https://www.npmjs.com/package/@powersync/react-native) together with Supabase.

A step-by-step guide on Supabase<>PowerSync integration is available [here](https://docs.powersync.com/integration-guides/supabase).
A step-by-step guide on Supabase<>PowerSync integration is available [here](https://docs.powersync.com/integration-guides/supabase-+-powersync).
Follow all the steps until, but not including, [Test Everything (Using Our Demo App)](https://docs.powersync.com/integration-guides/supabase-+-powersync#test-everything-using-our-demo-app).

## Getting Started
Expand Down Expand Up @@ -51,4 +51,3 @@ General information on defining environment variables with Expo can be found her
Check out [the PowerSync SDK for React Native on GitHub](https://github.com/powersync-ja/powersync-js/tree/main/packages/react-native) - your feedback and contributions are welcome!

To learn more about PowerSync, see the [PowerSync docs](https://docs.powersync.com).

2 changes: 1 addition & 1 deletion demos/react-native-web-supabase-todolist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pnpm build:packages

### Set up Supabase Project

Detailed instructions for integrating PowerSync with Supabase can be found in the [integration guide](https://docs.powersync.com/integration-guides/supabase). Below are the main steps required to get this demo running.
Detailed instructions for integrating PowerSync with Supabase can be found in the [integration guide](https://docs.powersync.com/integration-guides/supabase-+-powersync). Below are the main steps required to get this demo running.

Create a new Supabase project, and paste and run the contents of [database.sql](./database.sql) in the Supabase SQL editor.

Expand Down
2 changes: 1 addition & 1 deletion demos/react-supabase-todolist-optional-sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Create Supabase and PowerSync projects, and add their credentials to `.env` to e

## Set up your Supabase project

Detailed instructions for integrating PowerSync with Supabase can be found in [the integration guide](https://docs.powersync.com/integration-guides/supabase). Below are the main steps required to get this demo running.
Detailed instructions for integrating PowerSync with Supabase can be found in [the integration guide](https://docs.powersync.com/integration-guides/supabase-+-powersync). Below are the main steps required to get this demo running.

Create a new Supabase project, and paste an run the contents of [database.sql](./database.sql) in the Supabase SQL editor.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractPowerSyncDatabase, column, ColumnsType, Schema, Table, TableV2Options } from '@powersync/web';
import { AbstractPowerSyncDatabase, column, Schema, Table } from '@powersync/web';
import { setSyncEnabled } from './SyncMode';

/**
Expand Down
2 changes: 1 addition & 1 deletion demos/react-supabase-todolist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Demo app demonstrating use of the [PowerSync SDK for Web](https://www.npmjs.com/package/@powersync/web) together with Supabase.

A step-by-step guide on Supabase<>PowerSync integration is available [here](https://docs.powersync.com/integration-guides/supabase).
A step-by-step guide on Supabase<>PowerSync integration is available [here](https://docs.powersync.com/integration-guides/supabase-+-powersync).

## Getting Started

Expand Down
2 changes: 1 addition & 1 deletion demos/vue-supabase-todolist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The `powersync/vue` package is currently in a beta release.

Demo app demonstrating use of the [PowerSync Vue](https://www.npmjs.com/package/@powersync/vue) together with Supabase.

A step-by-step guide through the Supabase<>PowerSync integration is available [here](https://docs.powersync.com/integration-guides/supabase).
A step-by-step guide through the Supabase<>PowerSync integration is available [here](https://docs.powersync.com/integration-guides/supabase-+-powersync).

## Recommended Setup

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { column, Schema, TableV2 } from '@powersync/web';
import { column, Schema, Table } from '@powersync/web';

export const LISTS_TABLE = 'lists';
export const TODOS_TABLE = 'todos';

const todos = new TableV2(
const todos = new Table(
{
list_id: column.text,
created_at: column.text,
Expand All @@ -16,7 +16,7 @@ const todos = new TableV2(
{ indexes: { list: ['list_id'] } }
);

const lists = new TableV2({
const lists = new Table({
created_at: column.text,
name: column.text,
owner_id: column.text
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { column, Schema, TableV2 } from '@powersync/web';
import { column, Schema, Table } from '@powersync/web';

const documents = new TableV2({
const documents = new Table({
title: column.text,
created_at: column.text
});

const document_updates = new TableV2(
const document_updates = new Table(
{
document_id: column.text,
created_at: column.text,
Expand Down
6 changes: 6 additions & 0 deletions packages/attachments/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @powersync/attachments

## 2.2.0

### Minor Changes

- 0094e52: Added option to download attachments

## 2.1.2

### Patch Changes
Expand Down
16 changes: 14 additions & 2 deletions packages/attachments/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powersync/attachments",
"version": "2.1.2",
"version": "2.2.0",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
Expand All @@ -25,9 +25,21 @@
"build": "tsc -b",
"build:prod": "tsc -b --sourceMap false",
"clean": "rm -rf lib tsconfig.tsbuildinfo",
"watch": "tsc -b -w"
"watch": "tsc -b -w",
"test": "pnpm build && vitest"
},
"peerDependencies": {
"@powersync/common": "workspace:^1.18.1"
},
"devDependencies": {
"@types/node": "^20.17.6",
"@vitest/browser": "^2.1.4",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
"vite": "^5.4.10",
"vite-plugin-top-level-await": "^1.4.4",
"vitest": "^2.1.4",
"webdriverio": "^9.2.8"
}
}
16 changes: 15 additions & 1 deletion packages/attachments/src/AbstractAttachmentQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface AttachmentQueueOptions {
* Whether to mark the initial watched attachment IDs to be synced
*/
performInitialSync?: boolean;
/**
* Should attachments be downloaded
*/
downloadAttachments?: boolean;
/**
* How to handle download errors, return { retry: false } to ignore the download
*/
Expand All @@ -35,7 +39,8 @@ export const DEFAULT_ATTACHMENT_QUEUE_OPTIONS: Partial<AttachmentQueueOptions> =
attachmentDirectoryName: 'attachments',
syncInterval: 30_000,
cacheLimit: 100,
performInitialSync: true
performInitialSync: true,
downloadAttachments: true
};

export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions = AttachmentQueueOptions> {
Expand Down Expand Up @@ -295,6 +300,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
}

async downloadRecord(record: AttachmentRecord) {
if (!this.options.downloadAttachments) {
return false;
}
if (!record.local_uri) {
record.local_uri = this.getLocalFilePathSuffix(record.filename);
}
Expand Down Expand Up @@ -426,6 +434,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
}

watchDownloads() {
if (!this.options.downloadAttachments) {
return;
}
this.idsToDownload(async (ids) => {
ids.map((id) => this.downloadQueue.add(id));
// No need to await this, the lock will ensure only one loop is running at a time
Expand All @@ -434,6 +445,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
}

private async downloadRecords() {
if (!this.options.downloadAttachments) {
return;
}
if (this.downloading) {
return;
}
Expand Down
95 changes: 95 additions & 0 deletions packages/attachments/tests/attachments/AttachmentQueue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import * as commonSdk from '@powersync/common';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { AbstractAttachmentQueue } from '../../src/AbstractAttachmentQueue';
import { AttachmentRecord, AttachmentState } from '../../src/Schema';
import { AbstractPowerSyncDatabase } from '@powersync/common';
import { StorageAdapter } from '../../src/StorageAdapter';

const record = {
id: 'test-1',
filename: 'test.jpg',
state: AttachmentState.QUEUED_DOWNLOAD
}

const mockPowerSync = {
currentStatus: { status: 'initial' },
registerListener: vi.fn(() => {}),
resolveTables: vi.fn(() => ['table1', 'table2']),
onChangeWithCallback: vi.fn(),
getAll: vi.fn(() => Promise.resolve([{id: 'test-1'}, {id: 'test-2'}])),
execute: vi.fn(() => Promise.resolve()),
getOptional: vi.fn((_query, params) => Promise.resolve(record)),
watch: vi.fn((query, params, callbacks) => {
callbacks?.onResult?.({ rows: { _array: [{id: 'test-1'}, {id: 'test-2'}] } });
}),
writeTransaction: vi.fn(async (callback) => {
await callback({
execute: vi.fn(() => Promise.resolve())
});
})
};

const mockStorage: StorageAdapter = {
downloadFile: vi.fn(),
uploadFile: vi.fn(),
deleteFile: vi.fn(),
writeFile: vi.fn(),
readFile: vi.fn(),
fileExists: vi.fn(),
makeDir: vi.fn(),
copyFile: vi.fn(),
getUserStorageDirectory: vi.fn()
};

class TestAttachmentQueue extends AbstractAttachmentQueue {
onAttachmentIdsChange(onUpdate: (ids: string[]) => void): void {
throw new Error('Method not implemented.');
}
newAttachmentRecord(record?: Partial<AttachmentRecord>): Promise<AttachmentRecord> {
throw new Error('Method not implemented.');
}
}

describe('attachments', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('should not download attachments when downloadRecord is called with downloadAttachments false', async () => {
const queue = new TestAttachmentQueue({
powersync: mockPowerSync as any,
storage: mockStorage,
downloadAttachments: false
});

await queue.downloadRecord(record);

expect(mockStorage.downloadFile).not.toHaveBeenCalled();
});

it('should download attachments when downloadRecord is called with downloadAttachments true', async () => {
const queue = new TestAttachmentQueue({
powersync: mockPowerSync as any,
storage: mockStorage,
downloadAttachments: true
});

await queue.downloadRecord(record);

expect(mockStorage.downloadFile).toHaveBeenCalled();
});

// Testing the inverse of this test, i.e. when downloadAttachments is false, is not required as you can't wait for something that does not happen
it('should not download attachments with watchDownloads is called with downloadAttachments false', async () => {
const queue = new TestAttachmentQueue({
powersync: mockPowerSync as any,
storage: mockStorage,
downloadAttachments: true
});

queue.watchDownloads();
await vi.waitFor(() => {
expect(mockStorage.downloadFile).toBeCalledTimes(2);
});
});
});
19 changes: 19 additions & 0 deletions packages/attachments/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import topLevelAwait from 'vite-plugin-top-level-await';
import { defineConfig, UserConfigExport } from 'vitest/config';

const config: UserConfigExport = {
plugins: [topLevelAwait()],
test: {
isolate: false,
globals: true,
include: ['tests/**/*.test.ts'],
browser: {
enabled: true,
headless: true,
provider: 'webdriverio',
name: 'chrome' // browser name is required
}
}
};

export default defineConfig(config);
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
Loading

0 comments on commit 8ba49d6

Please sign in to comment.