Created
November 28, 2024 08:20
-
-
Save younes200/59156f3692ec32d9190a3563d81e9fe0 to your computer and use it in GitHub Desktop.
Connect Expo Drizzle Studio with op-sqlite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useDevToolsPluginClient } from "expo/devtools"; | |
import { useEffect } from "react"; | |
import { type DB } from "@op-engineering/op-sqlite"; | |
export function DrizzleStudio(db:DB) { | |
const client = useDevToolsPluginClient("expo-drizzle-studio-plugin"); | |
const transferData = async (e: { | |
sql: string; | |
params: (string | number)[]; | |
arrayMode: boolean; | |
id: string; | |
}) => { | |
if (!db) return; | |
try { | |
if (e.arrayMode) { | |
const data = await db.executeRaw(e.sql, e.params); | |
client?.sendMessage(`transferData-${e.id}`, { | |
from: "app", | |
data: data, | |
}); | |
} else { | |
const data = await db.execute(e.sql, e.params); | |
client?.sendMessage(`transferData-${e.id}`, { | |
from: "app", | |
data: data.rows, | |
}); | |
} | |
} catch (error) { | |
console.error(error); | |
} | |
}; | |
useEffect(() => { | |
const subscriptions: any[] = []; | |
subscriptions.push(client?.addMessageListener("getData", transferData)); | |
return () => { | |
for (const subscription of subscriptions) { | |
subscription?.remove(); | |
} | |
}; | |
}, [client]); | |
return null; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment