Skip to content

Commit

Permalink
add: support op-sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
slvssb committed Dec 22, 2024
1 parent adb0167 commit 4a5e7b4
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import useDrizzleStudio from "./useDrizzleStudio";
declare const useDrizzleStudio: any;
export { useDrizzleStudio };
//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion build/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion build/useDrizzleStudio.d.ts
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion build/useDrizzleStudio.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions build/useDrizzleStudio.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/useDrizzleStudio.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
35 changes: 27 additions & 8 deletions src/useDrizzleStudio.tsx
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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);
Expand Down

0 comments on commit 4a5e7b4

Please sign in to comment.