Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP-SQLite support #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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