@@ -7,8 +7,10 @@ import prompt from 'react-native-prompt-android';
77import { router , Stack } from 'expo-router' ;
88import { LIST_TABLE , TODO_TABLE , ListRecord } from '../../../library/powersync/AppSchema' ;
99import { useSystem } from '../../../library/powersync/system' ;
10- import { useQuery , useStatus } from '@powersync/react-native' ;
10+ import { usePowerSync , useQuery , useStatus } from '@powersync/react-native' ;
1111import { ListItemWidget } from '../../../library/widgets/ListItemWidget' ;
12+ import { drizzleSchema , lists } from '../../../library/powersync/DrizzleSchema' ;
13+ import { wrapPowerSyncWithDrizzle , toCompilableQuery } from "@powersync/drizzle-driver" ;
1214
1315const description = ( total : number , completed : number = 0 ) => {
1416 return `${ total - completed } pending, ${ completed } completed` ;
@@ -17,16 +19,8 @@ const description = (total: number, completed: number = 0) => {
1719const ListsViewWidget : React . FC = ( ) => {
1820 const system = useSystem ( ) ;
1921 const status = useStatus ( ) ;
20- const { data : listRecords } = useQuery < ListRecord & { total_tasks : number ; completed_tasks : number } > ( `
21- SELECT
22- ${ LIST_TABLE } .*, COUNT(${ TODO_TABLE } .id) AS total_tasks, SUM(CASE WHEN ${ TODO_TABLE } .completed = true THEN 1 ELSE 0 END) as completed_tasks
23- FROM
24- ${ LIST_TABLE }
25- LEFT JOIN ${ TODO_TABLE }
26- ON ${ LIST_TABLE } .id = ${ TODO_TABLE } .list_id
27- GROUP BY
28- ${ LIST_TABLE } .id;
29- ` ) ;
22+ const powersync = usePowerSync ( ) ;
23+ const drizzleDb = wrapPowerSyncWithDrizzle ( powersync , { schema : drizzleSchema } ) ;
3024
3125 const createNewList = async ( name : string ) => {
3226 const { userID } = await system . supabaseConnector . fetchCredentials ( ) ;
@@ -35,21 +29,19 @@ const ListsViewWidget: React.FC = () => {
3529 `INSERT INTO ${ LIST_TABLE } (id, created_at, name, owner_id) VALUES (uuid(), datetime(), ?, ?) RETURNING *` ,
3630 [ name , userID ]
3731 ) ;
38-
3932 const resultRecord = res . rows ?. item ( 0 ) ;
33+ console . log ( "insert" , resultRecord )
4034 if ( ! resultRecord ) {
4135 throw new Error ( 'Could not create list' ) ;
4236 }
4337 } ;
4438
45- const deleteList = async ( id : string ) => {
46- await system . powersync . writeTransaction ( async ( tx ) => {
47- // Delete associated todos
48- await tx . execute ( `DELETE FROM ${ TODO_TABLE } WHERE list_id = ?` , [ id ] ) ;
49- // Delete list record
50- await tx . execute ( `DELETE FROM ${ LIST_TABLE } WHERE id = ?` , [ id ] ) ;
51- } ) ;
52- } ;
39+ const query = drizzleDb . select ( ) . from ( lists ) ;
40+ // const query = drizzleDb.query.lists.findMany();
41+
42+
43+ const { data : listRecords , isLoading } = useQuery ( toCompilableQuery ( query ) ) ;
44+ console . log ( 'ssaaz' , listRecords [ 0 ] ) ;
5345
5446 return (
5547 < View style = { { flex : 1 , flexGrow : 1 } } >
@@ -58,6 +50,11 @@ const ListsViewWidget: React.FC = () => {
5850 headerShown : false
5951 } }
6052 />
53+ < View >
54+ { listRecords . map ( ( r ) => (
55+ < Text key = { r . id } > { r . id } -{ r . name } </ Text >
56+ ) ) }
57+ </ View >
6158 < FAB
6259 style = { { zIndex : 99 , bottom : 0 } }
6360 icon = { { name : 'add' , color : 'white' } }
@@ -78,26 +75,6 @@ const ListsViewWidget: React.FC = () => {
7875 ) ;
7976 } }
8077 />
81- < ScrollView key = { 'lists' } style = { { maxHeight : '90%' } } >
82- { ! status . hasSynced ? (
83- < Text > Busy with sync...</ Text >
84- ) : (
85- listRecords . map ( ( r ) => (
86- < ListItemWidget
87- key = { r . id }
88- title = { r . name }
89- description = { description ( r . total_tasks , r . completed_tasks ) }
90- onDelete = { ( ) => deleteList ( r . id ) }
91- onPress = { ( ) => {
92- router . push ( {
93- pathname : 'views/todos/edit/[id]' ,
94- params : { id : r . id }
95- } ) ;
96- } }
97- />
98- ) )
99- ) }
100- </ ScrollView >
10178
10279 < StatusBar style = { 'light' } />
10380 </ View >
0 commit comments