From b2aba215c8524408356707363811e564d1dcf087 Mon Sep 17 00:00:00 2001 From: Dominic Gunther Bauer <46312751+DominicGBauer@users.noreply.github.com> Date: Tue, 27 Aug 2024 15:21:14 +0200 Subject: [PATCH] Update LocalFirstDataWithPowerSync.md (#174 by @DominicGBauer) * Update LocalFirstDataWithPowerSync.md update to latest schema and database instantiation * Update LocalFirstDataWithPowerSync.md --- docs/recipes/LocalFirstDataWithPowerSync.md | 107 ++++++++------------ 1 file changed, 43 insertions(+), 64 deletions(-) diff --git a/docs/recipes/LocalFirstDataWithPowerSync.md b/docs/recipes/LocalFirstDataWithPowerSync.md index c1161c5f..ce108a3b 100644 --- a/docs/recipes/LocalFirstDataWithPowerSync.md +++ b/docs/recipes/LocalFirstDataWithPowerSync.md @@ -789,65 +789,41 @@ Here is the schema we'll be using for our todo app: ```ts // app/services/database/schema.ts -import { - Column, - ColumnType, - Index, - IndexedColumn, - Schema, - Table -} from '@powersync/react-native' - -export const TODO_TABLE = 'todos' -export const LIST_TABLE = 'lists' - -export interface ListRecord { - id: string - name: string - created_at: string - owner_id?: string -} +import { column, Schema, TableV2 } from '@powersync/react-native'; + +export const LISTS_TABLE = 'lists'; +export const TODOS_TABLE = 'todos'; + +const todos = new TableV2( + { + list_id: column.text, + created_at: column.text, + completed_at: column.text, + description: column.text, + created_by: column.text, + completed_by: column.text, + completed: column.integer + }, + { indexes: { list: ['list_id'] } } +); -export interface TodoRecord { - id: string - created_at: string - completed: boolean - description: string - completed_at?: string - created_by: string - completed_by?: string - list_id: string - photo_id?: string -} +const lists = new TableV2({ + created_at: column.text, + name: column.text, + owner_id: column.text +}); -export const AppSchema = new Schema([ - new Table({ - name: 'todos', - columns: [ - new Column({ name: 'list_id', type: ColumnType.TEXT }), - new Column({ name: 'created_at', type: ColumnType.TEXT }), - new Column({ name: 'completed_at', type: ColumnType.TEXT }), - new Column({ name: 'description', type: ColumnType.TEXT }), - new Column({ name: 'completed', type: ColumnType.INTEGER }), - new Column({ name: 'created_by', type: ColumnType.TEXT }), - new Column({ name: 'completed_by', type: ColumnType.TEXT }) - ], - indexes: [ - new Index({ - name: 'list', - columns: [new IndexedColumn({ name: 'list_id' })] - }) - ] - }), - new Table({ - name: 'lists', - columns: [ - new Column({ name: 'created_at', type: ColumnType.TEXT }), - new Column({ name: 'name', type: ColumnType.TEXT }), - new Column({ name: 'owner_id', type: ColumnType.TEXT }) - ] - }), -]) +export const AppSchema = new Schema({ + todos, + lists +}); + +export type Database = (typeof AppSchema)['types']; +export type TodoRecord = Database['todos']; +// OR: +// export type Todo = RowType; + +export type ListRecord = Database['lists']; ``` @@ -905,8 +881,10 @@ In `app/services/database/supabase.ts`, we'll add the two methods, and then expo // app/services/database/supabase.ts import { AbstractPowerSyncDatabase, - CrudEntry, PowerSyncBackendConnector, - UpdateType, PowerSyncCredentials + CrudEntry, + PowerSyncBackendConnector, + UpdateType, + PowerSyncCredentials } from "@powersync/react-native" import AsyncStorage from '@react-native-async-storage/async-storage' import { createClient } from "@supabase/supabase-js" @@ -1033,7 +1011,7 @@ import React, { PropsWithChildren, useEffect } from "react" import { AbstractPowerSyncDatabase, PowerSyncContext, - RNQSPowerSyncDatabaseOpenFactory, + PowerSyncDatabase, } from "@powersync/react-native" import { supabase, supabaseConnector } from "./supabase" // Adjust the path as needed import { AppSchema } from "./schema" // Adjust the path as needed @@ -1047,11 +1025,12 @@ export class Database { * Initialize the Database class with a new PowerSync instance */ constructor() { - const factory = new RNQSPowerSyncDatabaseOpenFactory({ - schema: AppSchema, - dbFilename: "sqlite.db", + this.powersync = new PowerSyncDatabase({ + database: { + dbFilename: "sqlite.db" + }, + schema: AppSchema }) - this.powersync = factory.getInstance() } /**