-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eedf90e
commit 1b3fc33
Showing
8 changed files
with
692 additions
and
137 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
import js from '@eslint/js' | ||
import globals from 'globals' | ||
import reactHooks from 'eslint-plugin-react-hooks' | ||
import reactRefresh from 'eslint-plugin-react-refresh' | ||
import tseslint from 'typescript-eslint' | ||
import js from "@eslint/js"; | ||
import globals from "globals"; | ||
import reactHooks from "eslint-plugin-react-hooks"; | ||
import reactRefresh from "eslint-plugin-react-refresh"; | ||
import tseslint from "typescript-eslint"; | ||
|
||
export default tseslint.config( | ||
{ ignores: ['dist'] }, | ||
{ ignores: ["dist"] }, | ||
{ | ||
extends: [js.configs.recommended, ...tseslint.configs.recommended], | ||
files: ['**/*.{ts,tsx}'], | ||
files: ["**/*.{ts,tsx}"], | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
globals: globals.browser, | ||
}, | ||
plugins: { | ||
'react-hooks': reactHooks, | ||
'react-refresh': reactRefresh, | ||
"react-hooks": reactHooks, | ||
"react-refresh": reactRefresh, | ||
}, | ||
rules: { | ||
...reactHooks.configs.recommended.rules, | ||
'react-refresh/only-export-components': [ | ||
'warn', | ||
"react-refresh/only-export-components": [ | ||
"warn", | ||
{ allowConstantExport: true }, | ||
], | ||
}, | ||
}, | ||
) | ||
); |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,127 @@ | ||
import { | ||
AsyncDuckDB, | ||
AsyncDuckDBConnection, | ||
} from '@duckdb/duckdb-wasm' | ||
import { useEffect, useState } from 'react' | ||
import { initializeDuckDB, cleanUpDuckDB } from './duckdb' | ||
import './App.css' | ||
import { AsyncDuckDB, AsyncDuckDBConnection } from "@duckdb/duckdb-wasm"; | ||
import { useEffect, useState } from "react"; | ||
import { initializeDuckDB, cleanUpDuckDB } from "./duckdb"; | ||
import SQLEditor from "./SQLEditor.tsx"; | ||
import "./App.css"; | ||
import ResultTable from "./ResultTable.tsx"; | ||
|
||
function useDuckDBConnection() { | ||
const [db, setDb] = useState<AsyncDuckDB | null>(null) | ||
const [conn, setConn] = useState<AsyncDuckDBConnection | null>(null) | ||
const [db, setDb] = useState<AsyncDuckDB | null>(null); | ||
const [conn, setConn] = useState<AsyncDuckDBConnection | null>(null); | ||
|
||
useEffect(() => { | ||
let cleanUpFn = () => {} | ||
let cleanUpFn = () => {}; | ||
|
||
initializeDuckDB().then(async (duck) => { | ||
cleanUpFn = () => { | ||
setDb(null) | ||
setConn(null) | ||
cleanUpDuckDB(duck.conn) | ||
} | ||
setDb(null); | ||
setConn(null); | ||
cleanUpDuckDB(duck.conn); | ||
}; | ||
|
||
setDb(duck.db) | ||
setConn(duck.conn) | ||
}) | ||
setDb(duck.db); | ||
setConn(duck.conn); | ||
}); | ||
|
||
return () => cleanUpFn?.() | ||
}, []) | ||
return () => cleanUpFn?.(); | ||
}, []); | ||
|
||
return { db, conn } | ||
return { db, conn }; | ||
} | ||
|
||
const FRIENDS = [ | ||
'Tamjid', | ||
'Vinesh', | ||
'Samarth', | ||
'Andrew', | ||
'Evan', | ||
'David', | ||
'Joni', | ||
'Keegan', | ||
'Lauren', | ||
'Nancy', | ||
'Shoji', | ||
'James', | ||
'Laura', | ||
] | ||
"Tamjid", | ||
"Vinesh", | ||
"Samarth", | ||
"Andrew", | ||
"Evan", | ||
"David", | ||
"Joni", | ||
"Keegan", | ||
"Lauren", | ||
"Nancy", | ||
"Shoji", | ||
"James", | ||
"Laura", | ||
]; | ||
|
||
async function insertFriends(conn: AsyncDuckDBConnection) { | ||
const name = FRIENDS[Math.floor(Math.random() * FRIENDS.length)] | ||
const score = Math.ceil(Math.random() * 100) | ||
await conn.query(`INSERT INTO friends VALUES ('${name}', ${score})`) | ||
console.log(`🦆 Inserted ('${name}', ${score}) into table [friends].`) | ||
const name = FRIENDS[Math.floor(Math.random() * FRIENDS.length)]; | ||
const score = Math.ceil(Math.random() * 100); | ||
await conn.query(`INSERT INTO friends VALUES ('${name}', ${score})`); | ||
console.log(`🦆 Inserted ('${name}', ${score}) into table [friends].`); | ||
} | ||
|
||
async function getFriends(conn: AsyncDuckDBConnection) { | ||
const result = await conn.query(`SELECT * FROM friends`) | ||
const friends = result.toArray().map((row) => row.toJSON()) | ||
console.log({ friends }) | ||
async function setUpDatabase(conn: AsyncDuckDBConnection) { | ||
const query = `CREATE TABLE IF NOT EXISTS friends (name VARCHAR(255) NOT NULL, score INTEGER NOT NULL)`; | ||
await conn.query(query); | ||
await insertFriends(conn); | ||
} | ||
|
||
async function setUpDatabase(conn: AsyncDuckDBConnection) { | ||
const query = `CREATE TABLE IF NOT EXISTS friends (name VARCHAR(255) NOT NULL, score INTEGER NOT NULL)` | ||
await conn.query(query) | ||
await insertFriends(conn) | ||
function transpileToZQL(sql: string) { | ||
return `transpileToZQL(${sql})`; | ||
} | ||
|
||
function transpileFromZQL(sql: string) { | ||
return `transpileFromZQL(${sql})`; | ||
} | ||
|
||
async function runSQL(conn: AsyncDuckDBConnection, sql: string) { | ||
console.log(`🦆 Running SQL: ${sql}`); | ||
const result = await conn.query(sql); | ||
return result.toArray().map((row) => row.toJSON()); | ||
} | ||
|
||
function App() { | ||
const { db, conn } = useDuckDBConnection() | ||
const { db, conn } = useDuckDBConnection(); | ||
|
||
useEffect(() => { | ||
if (db == null || conn == null) { | ||
return | ||
return; | ||
} | ||
|
||
setUpDatabase(conn) | ||
}, [db, conn]) | ||
setUpDatabase(conn); | ||
}, [db, conn]); | ||
|
||
const [inputSQL, setInputSQL] = useState<string>(""); | ||
const [outputSQL, setOutputSQL] = useState<string>(""); | ||
const [sqlResult, setSqlResult] = useState<any[] | null>(null); | ||
|
||
const handleRunSQL = async () => { | ||
if (conn) { | ||
const result = await runSQL(conn, inputSQL); | ||
setSqlResult(result); | ||
} | ||
}; | ||
return ( | ||
<> | ||
<h1>Web DuckDB</h1> | ||
<h1>zql</h1> | ||
<p>The future is quack.</p> | ||
<button onClick={() => conn && insertFriends(conn)}>Insert</button> | ||
<button onClick={() => conn && getFriends(conn)}>Query</button> | ||
<div style={{ display: "flex", justifyContent: "space-between" }}> | ||
<div style={{ width: "48%" }}> | ||
<SQLEditor | ||
value={inputSQL} | ||
onChange={(v) => { | ||
setInputSQL(v); | ||
setOutputSQL(transpileToZQL(v)); | ||
}} | ||
/> | ||
<button onClick={handleRunSQL}>Run</button> | ||
</div> | ||
<div style={{ width: "48%" }}> | ||
<SQLEditor | ||
value={outputSQL} | ||
onChange={(v) => { | ||
setOutputSQL(v); | ||
setInputSQL(transpileFromZQL(v)); | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
|
||
{sqlResult && <ResultTable result={sqlResult} />} | ||
</> | ||
) | ||
); | ||
} | ||
|
||
export default App | ||
export default App; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from "react"; | ||
|
||
interface ResultTableProps { | ||
result: any[] | null; | ||
} | ||
|
||
const ResultTable: React.FC<ResultTableProps> = ({ result }) => { | ||
console.log("result", result); | ||
if (!result || result.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div style={{ marginTop: "20px" }}> | ||
<h2>SQL Result:</h2> | ||
<div>{result}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ResultTable; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from "react"; | ||
import CodeMirror from "@uiw/react-codemirror"; | ||
import { sql } from "@codemirror/lang-sql"; | ||
|
||
const SQLEditor: React.FC<{ | ||
value: string; | ||
onChange: (value: string) => void; | ||
}> = ({ value, onChange }) => { | ||
return ( | ||
<div style={{ width: "500px", maxWidth: "800px", margin: "0 auto" }}> | ||
<CodeMirror | ||
value={value} | ||
height="300px" | ||
extensions={[sql()]} | ||
onChange={onChange} | ||
theme="dark" | ||
placeholder="Enter your SQL code here..." | ||
style={{ textAlign: "left" }} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SQLEditor; |
This file contains 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
Oops, something went wrong.