Skip to content

Commit

Permalink
Merge branch 'feat/drizzle-schema' of github.com:powersync-ja/powersy…
Browse files Browse the repository at this point in the history
…nc-js into feat/drizzle-schema
  • Loading branch information
Chriztiaan committed Dec 2, 2024
2 parents e4eddd3 + 31759b7 commit 102f7ba
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 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,23 +47,33 @@ export const drizzleSchema = {
todosRelations
};

// As an alternative to manually defining a PowerSync schema, generate the local PowerSync schema from the Drizzle schema with `toPowerSyncSchema`:
// import { toPowerSyncSchema } from '@powersync/drizzle-driver';
// export const AppSchema = toPowerSyncSchema(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
});
```

## Schema Conversion

The `toPowerSyncSchema` schema function simplifies the process of integrating Drizzle with PowerSync. Define your Drizzle tables and supply the schema to the `toPowerSyncSchema` function for a unified development experience.
The `toPowerSyncSchema` function 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.

As the PowerSync table only supports `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 `toPowerSyncSchema` function:

```js
import { toPowerSyncSchema } from '@powersync/drizzle-driver';
Expand All @@ -81,6 +91,7 @@ export const drizzleSchema = {
lists
};

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

Expand All @@ -103,7 +114,7 @@ export const drizzleSchemaWithOptions = {
export const AppSchema = toPowerSyncSchema(drizzleSchemaWithOptions);
```

### Converting a Single Table From Drizzle to Powersync
### Converting a Single Table From Drizzle to PowerSync

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

Expand All @@ -128,11 +139,7 @@ export const AppSchema = new Schema({
});
```

## Known limitations

- The integration does not currently support nested transactions (also known as `savepoints`).

### Compilable queries
## Compilable queries

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

Expand All @@ -144,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`).

0 comments on commit 102f7ba

Please sign in to comment.