diff --git a/build/index.d.ts b/build/index.d.ts index 7620c10..e5ab190 100644 --- a/build/index.d.ts +++ b/build/index.d.ts @@ -1,3 +1,3 @@ -import useDrizzleStudio from "./useDrizzleStudio"; +declare const useDrizzleStudio: any; export { useDrizzleStudio }; //# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/build/index.d.ts.map b/build/index.d.ts.map index 941c8ed..75b6a45 100644 --- a/build/index.d.ts.map +++ b/build/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,CAAC"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,gBAAgB,KAGR,CAAC;AAEf,OAAO,EAAE,gBAAgB,EAAE,CAAC"} \ No newline at end of file diff --git a/build/index.js b/build/index.js index 82ed781..f538850 100644 --- a/build/index.js +++ b/build/index.js @@ -1,3 +1,5 @@ -import useDrizzleStudio from "./useDrizzleStudio"; +const useDrizzleStudio = process.env.NODE_ENV !== "production" + ? require("./useDrizzleStudio").default + : () => { }; export { useDrizzleStudio }; //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/build/index.js.map b/build/index.js.map index 129b323..2185335 100644 --- a/build/index.js.map +++ b/build/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,CAAC","sourcesContent":["import useDrizzleStudio from \"./useDrizzleStudio\";\nexport { useDrizzleStudio };\n"]} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,gBAAgB,GACpB,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;IACnC,CAAC,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC,OAAO;IACvC,CAAC,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;AAEf,OAAO,EAAE,gBAAgB,EAAE,CAAC","sourcesContent":["const useDrizzleStudio =\n process.env.NODE_ENV !== \"production\"\n ? require(\"./useDrizzleStudio\").default\n : () => {};\n\nexport { useDrizzleStudio };\n"]} \ No newline at end of file diff --git a/build/useDrizzleStudio.d.ts b/build/useDrizzleStudio.d.ts index 202382e..8422af6 100644 --- a/build/useDrizzleStudio.d.ts +++ b/build/useDrizzleStudio.d.ts @@ -1,3 +1,12 @@ +import * as opSQLite from "@op-engineering/op-sqlite"; import * as SQLite from "expo-sqlite"; -export default function useDrizzleStudio(db: SQLite.SQLiteDatabase | null): void; +type Props = { + driver: "expo"; + db: SQLite.SQLiteDatabase | null; +} | { + driver: "opsqlite"; + db: opSQLite.DB | null; +}; +export default function useDrizzleStudio(props: Props): void; +export {}; //# sourceMappingURL=useDrizzleStudio.d.ts.map \ No newline at end of file diff --git a/build/useDrizzleStudio.d.ts.map b/build/useDrizzleStudio.d.ts.map index fc7de89..d5d5fb3 100644 --- a/build/useDrizzleStudio.d.ts.map +++ b/build/useDrizzleStudio.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"useDrizzleStudio.d.ts","sourceRoot":"","sources":["../src/useDrizzleStudio.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAItC,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,EAAE,EAAE,MAAM,CAAC,cAAc,GAAG,IAAI,QAqCxE"} \ No newline at end of file +{"version":3,"file":"useDrizzleStudio.d.ts","sourceRoot":"","sources":["../src/useDrizzleStudio.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,2BAA2B,CAAC;AACtD,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAItC,KAAK,KAAK,GACJ;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC,cAAc,GAAG,IAAI,CAAA;CAAE,GACpD;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,QAAQ,CAAC,EAAE,GAAG,IAAI,CAAA;CAAE,CAAC;AAErD,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,KAAK,EAAE,KAAK,QAmDpD"} \ No newline at end of file diff --git a/build/useDrizzleStudio.js b/build/useDrizzleStudio.js index de7b02d..e89a63c 100644 --- a/build/useDrizzleStudio.js +++ b/build/useDrizzleStudio.js @@ -1,20 +1,32 @@ import { useDevToolsPluginClient } from "expo/devtools"; import { useEffect } from "react"; -export default function useDrizzleStudio(db) { +export default function useDrizzleStudio(props) { const client = useDevToolsPluginClient("expo-drizzle-studio-plugin"); const transferData = async (e) => { - if (!db) + if (!props.db) return; + let data = []; try { - const statement = await db.prepareAsync(e.sql); - let executed; - if (e.arrayMode) { - executed = await statement.executeForRawResultAsync(e.params); + if (props.driver === "expo") { + const statement = await props.db.prepareAsync(e.sql); + let executed; + if (e.arrayMode) { + executed = await statement.executeForRawResultAsync(e.params); + } + else { + executed = await statement.executeAsync(e.params); + } + data = await executed.getAllAsync(); } else { - executed = await statement.executeAsync(e.params); + if (e.arrayMode) { + data = await props.db.executeRaw(e.sql, e.params); + } + else { + const executed = await props.db.execute(e.sql, e.params); + data = executed.rows; + } } - const data = await executed.getAllAsync(); client?.sendMessage(`transferData-${e.id}`, { from: "app", data }); } catch (error) { diff --git a/build/useDrizzleStudio.js.map b/build/useDrizzleStudio.js.map index 9331765..70e2d5a 100644 --- a/build/useDrizzleStudio.js.map +++ b/build/useDrizzleStudio.js.map @@ -1 +1 @@ -{"version":3,"file":"useDrizzleStudio.js","sourceRoot":"","sources":["../src/useDrizzleStudio.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MA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ACjB,CAAC","sourcesContent":["import * as SQLite from \"expo-sqlite\";\nimport { useDevToolsPluginClient } from \"expo/devtools\";\nimport { useEffect } from \"react\";\n\nexport default function useDrizzleStudio(db: SQLite.SQLiteDatabase | null) {\n const client = useDevToolsPluginClient(\"expo-drizzle-studio-plugin\");\n\n const transferData = async (e: {\n sql: string;\n params: (string | number)[];\n arrayMode: boolean;\n id: string;\n }) => {\n if (!db) return;\n try {\n const statement = await db.prepareAsync(e.sql);\n let executed;\n if (e.arrayMode) {\n executed = await statement.executeForRawResultAsync(e.params);\n } else {\n executed = await statement.executeAsync(e.params);\n }\n\n const data = await executed.getAllAsync();\n client?.sendMessage(`transferData-${e.id}`, { from: \"app\", data });\n } catch (error) {\n console.error(error);\n }\n };\n\n useEffect(() => {\n const subscriptions: any[] = [];\n\n subscriptions.push(client?.addMessageListener(\"getData\", transferData));\n\n return () => {\n for (const subscription of subscriptions) {\n subscription?.remove();\n }\n };\n }, [client]);\n}\n"]} \ No newline at end of file +{"version":3,"file":"useDrizzleStudio.js","sourceRoot":"","sources":["../src/useDrizzleStudio.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAMlC,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,KAAY;IACjD,MAAM,MAAM,GAAG,uBAAuB,CAAC,4BAA4B,CAAC,CAAC;IAErE,MAAM,YAAY,GAAG,KAAK,EAAE,CAK3B,EAAE,EAAE;QACD,IAAI,CAAC,KAAK,CAAC,EAAE;YAAE,OAAO;QACtB,IAAI,IAAI,GAAU,EAAE,CAAC;QAErB,IAAI,CAAC;YACD,IAAI,KAAK,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;gBAC1B,MAAM,SAAS,GAAG,MAAM,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;gBACrD,IAAI,QAAQ,C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C,CAAC;IACN,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;AACjB,CAAC","sourcesContent":["import * as opSQLite from \"@op-engineering/op-sqlite\";\nimport * as SQLite from \"expo-sqlite\";\nimport { useDevToolsPluginClient } from \"expo/devtools\";\nimport { useEffect } from \"react\";\n\ntype Props =\n | { driver: \"expo\"; db: SQLite.SQLiteDatabase | null }\n | { driver: \"opsqlite\"; db: opSQLite.DB | null };\n\nexport default function useDrizzleStudio(props: Props) {\n const client = useDevToolsPluginClient(\"expo-drizzle-studio-plugin\");\n\n const transferData = async (e: {\n sql: string;\n params: (string | number)[];\n arrayMode: boolean;\n id: string;\n }) => {\n if (!props.db) return;\n let data: any[] = [];\n\n try {\n if (props.driver === \"expo\") {\n const statement = await props.db.prepareAsync(e.sql);\n let executed;\n if (e.arrayMode) {\n executed = await statement.executeForRawResultAsync(\n e.params,\n );\n } else {\n executed = await statement.executeAsync(e.params);\n }\n\n data = await executed.getAllAsync();\n } else {\n if (e.arrayMode) {\n data = await props.db.executeRaw(e.sql, e.params);\n } else {\n const executed = await props.db.execute(e.sql, e.params);\n data = executed.rows;\n }\n }\n\n client?.sendMessage(`transferData-${e.id}`, { from: \"app\", data });\n } catch (error) {\n console.error(error);\n }\n };\n\n useEffect(() => {\n const subscriptions: any[] = [];\n\n subscriptions.push(client?.addMessageListener(\"getData\", transferData));\n\n return () => {\n for (const subscription of subscriptions) {\n subscription?.remove();\n }\n };\n }, [client]);\n}\n"]} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 60a27cd..159ff5a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,23 +1,22 @@ { "name": "expo-drizzle-studio-plugin", - "version": "0.0.2", + "version": "0.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "expo-drizzle-studio-plugin", - "version": "0.0.2", + "version": "0.1.1", "license": "MIT", - "dependencies": { - "expo-sqlite": "^15.0.3" - }, "devDependencies": { "expo": "~52.0.0", "expo-module-scripts": "^3.1.0", "typescript": "^5.1.3" }, "peerDependencies": { - "expo": "*" + "@op-engineering/op-sqlite": "~10.1.0", + "expo": "*", + "expo-sqlite": "~15.0.3" } }, "node_modules/@0no-co/graphql.web": { @@ -4076,6 +4075,20 @@ "node": "^14.17.0 || ^16.13.0 || >=18.0.0" } }, + "node_modules/@op-engineering/op-sqlite": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/@op-engineering/op-sqlite/-/op-sqlite-10.1.0.tgz", + "integrity": "sha512-h72JQ8zK3ULiEp3Tngnl2LNsfMuxpxddhRrt3XYyeoQGeh7vj5B6nQwkpKXR02Hl/EKPmbRsmxue8V7bhXr7ug==", + "license": "MIT", + "peer": true, + "workspaces": [ + "example" + ], + "peerDependencies": { + "react": "*", + "react-native": ">0.73.0" + } + }, "node_modules/@pkgjs/parseargs": { "version": "0.11.0", "resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz", @@ -8240,6 +8253,7 @@ "version": "15.0.3", "resolved": "https://registry.npmjs.org/expo-sqlite/-/expo-sqlite-15.0.3.tgz", "integrity": "sha512-Vu4dzkf/TKE+G5m7M0HTz2DP3ef/f5Wh7tpRU9hLonL58LkajFTpQbzKlwEWdb8UEW/c2GqPrV9j8Os6JMbxZg==", + "peer": true, "peerDependencies": { "expo": "*", "react": "*", diff --git a/package.json b/package.json index 3d68bde..a41a2ef 100644 --- a/package.json +++ b/package.json @@ -25,15 +25,15 @@ "expo-module.config.json" ], "license": "MIT", - "dependencies": { - "expo-sqlite": "^15.0.3" - }, + "dependencies": {}, "devDependencies": { "expo": "~52.0.0", "expo-module-scripts": "^3.1.0", "typescript": "^5.1.3" }, "peerDependencies": { - "expo": "*" + "@op-engineering/op-sqlite": "~10.1.0", + "expo": "*", + "expo-sqlite": "~15.0.3" } } diff --git a/src/useDrizzleStudio.tsx b/src/useDrizzleStudio.tsx index 1091bd3..e8c5b4c 100644 --- a/src/useDrizzleStudio.tsx +++ b/src/useDrizzleStudio.tsx @@ -1,8 +1,13 @@ +import * as opSQLite from "@op-engineering/op-sqlite"; import * as SQLite from "expo-sqlite"; import { useDevToolsPluginClient } from "expo/devtools"; import { useEffect } from "react"; -export default function useDrizzleStudio(db: SQLite.SQLiteDatabase | null) { +type Props = + | { driver: "expo"; db: SQLite.SQLiteDatabase | null } + | { driver: "opsqlite"; db: opSQLite.DB | null }; + +export default function useDrizzleStudio(props: Props) { const client = useDevToolsPluginClient("expo-drizzle-studio-plugin"); const transferData = async (e: { @@ -11,17 +16,31 @@ export default function useDrizzleStudio(db: SQLite.SQLiteDatabase | null) { arrayMode: boolean; id: string; }) => { - if (!db) return; + if (!props.db) return; + let data: any[] = []; + try { - const statement = await db.prepareAsync(e.sql); - let executed; - if (e.arrayMode) { - executed = await statement.executeForRawResultAsync(e.params); + if (props.driver === "expo") { + const statement = await props.db.prepareAsync(e.sql); + let executed; + if (e.arrayMode) { + executed = await statement.executeForRawResultAsync( + e.params, + ); + } else { + executed = await statement.executeAsync(e.params); + } + + data = await executed.getAllAsync(); } else { - executed = await statement.executeAsync(e.params); + if (e.arrayMode) { + data = await props.db.executeRaw(e.sql, e.params); + } else { + const executed = await props.db.execute(e.sql, e.params); + data = executed.rows; + } } - const data = await executed.getAllAsync(); client?.sendMessage(`transferData-${e.id}`, { from: "app", data }); } catch (error) { console.error(error);