Skip to content

Commit 382a458

Browse files
committed
Demo of drizzle in react-native.
1 parent 5d7c48c commit 382a458

File tree

4 files changed

+98
-40
lines changed

4 files changed

+98
-40
lines changed

demos/react-native-supabase-todolist/app/views/todos/lists.tsx

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import prompt from 'react-native-prompt-android';
77
import { router, Stack } from 'expo-router';
88
import { LIST_TABLE, TODO_TABLE, ListRecord } from '../../../library/powersync/AppSchema';
99
import { useSystem } from '../../../library/powersync/system';
10-
import { useQuery, useStatus } from '@powersync/react-native';
10+
import { usePowerSync, useQuery, useStatus } from '@powersync/react-native';
1111
import { ListItemWidget } from '../../../library/widgets/ListItemWidget';
12+
import { drizzleSchema, lists } from '../../../library/powersync/DrizzleSchema';
13+
import { wrapPowerSyncWithDrizzle, toCompilableQuery } from "@powersync/drizzle-driver";
1214

1315
const description = (total: number, completed: number = 0) => {
1416
return `${total - completed} pending, ${completed} completed`;
@@ -17,16 +19,8 @@ const description = (total: number, completed: number = 0) => {
1719
const 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>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { relations } from 'drizzle-orm';
2+
import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
3+
4+
export const lists = sqliteTable('lists', {
5+
id: text('id').primaryKey().notNull(),
6+
created_at: text('created_at'),
7+
name: text('name').notNull(),
8+
owner_id: text('owner_id')
9+
});
10+
11+
export const todos = sqliteTable(
12+
'todos',
13+
{
14+
id: text('id').primaryKey().notNull(),
15+
list_id: text('list_id')
16+
.notNull()
17+
.references(() => lists.id),
18+
created_at: text('created_at'),
19+
completed_at: text('completed_at'),
20+
description: text('description').notNull(),
21+
created_by: text('created_by'),
22+
completed_by: text('completed_by'),
23+
completed: integer('completed')
24+
},
25+
(todos) => ({
26+
listIdx: index('listIdx').on(todos.list_id)
27+
})
28+
);
29+
30+
// Define relations for 'lists' (one-to-many with 'todos')
31+
export const listsRelations = relations(lists, ({ many }) => ({
32+
todos: many(todos)
33+
}));
34+
35+
// Define relations for 'todos' (many-to-one with 'lists')
36+
const todosRelations = relations(todos, ({ one }) => ({
37+
list: one(lists, {
38+
fields: [todos.list_id],
39+
references: [lists.id]
40+
})
41+
}));
42+
43+
export const drizzleSchema = {
44+
lists,
45+
todos,
46+
listsRelations,
47+
todosRelations
48+
};

demos/react-native-supabase-todolist/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
"@azure/core-asynciterator-polyfill": "^1.0.2",
1212
"@expo/vector-icons": "^14.0.3",
1313
"@journeyapps/react-native-quick-sqlite": "^2.0.0",
14+
"@op-engineering/op-sqlite": "^9.2.1",
1415
"@powersync/attachments": "workspace:*",
1516
"@powersync/common": "workspace:*",
17+
"@powersync/drizzle-driver": "workspace:*",
18+
"@powersync/op-sqlite": "^0.0.6",
1619
"@powersync/react": "workspace:*",
1720
"@powersync/react-native": "workspace:*",
1821
"@react-native-community/masked-view": "^0.1.11",

pnpm-lock.yaml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)