Skip to content

Commit

Permalink
chore: update readme and update to PowerSyncDatabase (#220)
Browse files Browse the repository at this point in the history
Co-authored-by: DominicGBauer <[email protected]>
  • Loading branch information
DominicGBauer and DominicGBauer authored Jul 9, 2024
1 parent 86cfa71 commit f60fbed
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
20 changes: 10 additions & 10 deletions packages/kysely-driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ Table column object type definitions are not yet available in JavaScript.

```js
import { wrapPowerSyncWithKysely } from '@powersync/kysely-driver';
import { WASQLitePowerSyncDatabaseOpenFactory } from '@powersync/web';
import { PowerSyncDatabase } from '@powersync/web';
import { appSchema } from './schema';

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

export const powerSyncDb = factory.getInstance();

export const db = wrapPowerSyncWithKysely(powerSyncDb);
```

Expand All @@ -32,7 +32,7 @@ See an [example](https://github.com/powersync-ja/powersync-js/blob/main/demos/re

```TypeScript
import { wrapPowerSyncWithKysely } from '@powersync/kysely-driver';
import { WASQLitePowerSyncDatabaseOpenFactory } from "@powersync/web";
import { PowerSyncDatabase } from "@powersync/web";

// Define schema as in: https://docs.powersync.com/usage/installation/client-side-setup/define-your-schema
import { appSchema } from "./schema";
Expand All @@ -46,13 +46,13 @@ export type Database = {
lists: ListsRecord; // Interface defined externally for list item object
};

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

export const powerSyncDb = factory.getInstance();

// `db` now automatically contains types for defined tables
export const db = wrapPowerSyncWithKysely<Database>(powerSyncDb)
```
Expand Down
10 changes: 6 additions & 4 deletions packages/kysely-driver/tests/setup/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, TableV2, WASQLitePowerSyncDatabaseOpenFactory, column } from '@powersync/web';
import { Schema, TableV2, PowerSyncDatabase, column } from '@powersync/web';
import { wrapPowerSyncWithKysely } from '../../src/sqlite/db';
import { Database } from './types';

Expand All @@ -9,12 +9,14 @@ const users = new TableV2({
export const TestSchema = new Schema({ users });

export const getPowerSyncDb = () => {
const factory = new WASQLitePowerSyncDatabaseOpenFactory({
dbFilename: 'test.db',
const database = new PowerSyncDatabase({
database: {
dbFilename: 'test.db'
},
schema: TestSchema
});

return factory.getInstance();
return database;
};

export const getKyselyDb = wrapPowerSyncWithKysely<Database>(getPowerSyncDb());
4 changes: 3 additions & 1 deletion packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Configure a PowerSync DB connection and add it to a context provider.

```JSX
// App.jsx
import { PowerSyncDatabase } from '@powersync/react-native';
import { PowerSyncDatabase } from '@powersync/web';
// or for React Native
// import { PowerSyncDatabase } from '@powersync/react-native';
import { PowerSyncContext } from "@powersync/react";
export const App = () => {
const powerSync = React.useMemo(() => {
Expand Down
10 changes: 6 additions & 4 deletions packages/web/tests/crud.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { AbstractPowerSyncDatabase, Column, ColumnType, CrudEntry, Schema, Table, UpdateType } from '@powersync/common';
import { WASQLitePowerSyncDatabaseOpenFactory } from '@powersync/web';
import { PowerSyncDatabase } from '@powersync/web';
import { v4 as uuid } from 'uuid';
import { generateTestDb } from './utils/testDb';

Expand Down Expand Up @@ -165,13 +165,15 @@ describe('CRUD Tests', () => {
it('INSERT-only tables', async () => {
await powersync.disconnectAndClear();

powersync = new WASQLitePowerSyncDatabaseOpenFactory({
powersync = new PowerSyncDatabase({
/**
* Deleting the IndexDB seems to freeze the test.
* Use a new DB for each run to keep CRUD counters
* consistent
*/
dbFilename: 'test.db' + uuid(),
database: {
dbFilename: 'test.db' + uuid()
},
schema: new Schema([
new Table({
name: 'logs',
Expand All @@ -185,7 +187,7 @@ describe('CRUD Tests', () => {
flags: {
enableMultiTabs: false
}
}).getInstance();
});

expect(await powersync.getAll('SELECT * FROM ps_crud')).empty;

Expand Down
10 changes: 6 additions & 4 deletions tools/diagnostics-app/src/library/powersync/ConnectionManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
BaseListener,
BaseObserver,
WASQLitePowerSyncDatabaseOpenFactory,
PowerSyncDatabase,
WebRemote,
WebStreamingSyncImplementation,
WebStreamingSyncImplementationOptions
Expand All @@ -16,10 +16,12 @@ Logger.setLevel(Logger.DEBUG);

export const schemaManager = new DynamicSchemaManager();

export const db = new WASQLitePowerSyncDatabaseOpenFactory({
dbFilename: 'example.db',
export const db = new PowerSyncDatabase({
database: {
dbFilename: 'example.db'
},
schema: schemaManager.buildSchema()
}).getInstance();
});
export const connector = new TokenConnector();

const remote = new WebRemote(connector);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
Column,
ColumnType,
DBAdapter,
OpTypeEnum,
Schema,
SyncDataBatch,
Table
} from '@powersync/web';
import { Column, ColumnType, DBAdapter, OpTypeEnum, Schema, SyncDataBatch, Table } from '@powersync/web';
import { AppSchema } from './AppSchema';
import { JsSchemaGenerator } from './JsSchemaGenerator';

Expand All @@ -30,14 +22,14 @@ export class DynamicSchemaManager {

async updateFromOperations(batch: SyncDataBatch) {
let schemaDirty = false;
for (let bucket of batch.buckets) {
for (const bucket of batch.buckets) {
// Build schema
for (let op of bucket.data) {
for (const op of bucket.data) {
if (op.op.value == OpTypeEnum.PUT && op.data != null) {
this.tables[op.object_type!] ??= {};
const table = this.tables[op.object_type!];
const data = JSON.parse(op.data);
for (let [key, value] of Object.entries(data)) {
for (const [key, value] of Object.entries(data)) {
if (key == 'id') {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class RecordingStorageAdapter extends SqliteBucketStorage {
async setTargetCheckpoint(checkpoint: Checkpoint) {
await super.setTargetCheckpoint(checkpoint);
await this.rdb.writeTransaction(async (tx) => {
for (let bucket of checkpoint.buckets) {
for (const bucket of checkpoint.buckets) {
await tx.execute(
`INSERT OR REPLACE INTO local_bucket_data(id, total_operations, last_op, download_size, downloading)
VALUES (
Expand Down Expand Up @@ -53,7 +53,7 @@ export class RecordingStorageAdapter extends SqliteBucketStorage {
await super.saveSyncData(batch);

await this.rdb.writeTransaction(async (tx) => {
for (let bucket of batch.buckets) {
for (const bucket of batch.buckets) {
// Record metrics
const size = JSON.stringify(bucket.data).length;
await tx.execute(
Expand Down

0 comments on commit f60fbed

Please sign in to comment.