Skip to content

Commit

Permalink
doc: add gui README
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Apr 16, 2024
1 parent 1b1a310 commit c5741eb
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
108 changes: 108 additions & 0 deletions gui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
## LibSQL Studio GUI

LibSQL Studio GUI is a standalone database GUI React component for SQLite.

```typescript
import { Studio } from "@libsqlstudio/gui";
import TursoDriver from "./driver";

export default App() {
const driver = useMemo(() => {
return new TursoDriver("url_here", "token");
}, [])

return <Studio
driver={driver}
name="Turso Connection"
theme="light"
color="blue"
/>
}
```

Implementing SQLite Driver

```typescript
// driver.ts
import {
createClient,
Client,
InStatement,
ResultSet,
} from "@libsql/client/web";
import {
SqliteLikeBaseDriver,
DatabaseHeader,
DatabaseResultSet,
DatabaseRow,
convertSqliteType,
} from "@libsqlstudio/gui/driver";

export default class TursoDriver extends SqliteLikeBaseDriver {
protected client: Client;
protected endpoint: string = "";
protected authToken = "";
protected bigInt = false;

constructor(url: string, authToken: string) {
super();
this.endpoint = url;
this.authToken = authToken;
this.bigInt = bigInt;

this.client = createClient({
url: this.endpoint,
authToken: this.authToken,
});
}

async query(stmt: InStatement) {
const r = await this.client.execute(stmt);
return transformRawResult(r);
}

async transaction(stmt: InStatement[]) {
return (await this.client.batch(stmt, "write")).map(transformRawResult);
}
}

function transformRawResult(raw: ResultSet): DatabaseResultSet {
const headerSet = new Set();

const headers: DatabaseHeader[] = raw.columns.map((colName, colIdx) => {
const colType = raw.columnTypes[colIdx];
let renameColName = colName;

for (let i = 0; i < 20; i++) {
if (!headerSet.has(renameColName)) break;
renameColName = `__${colName}_${i}`;
}

headerSet.add(renameColName);

return {
name: renameColName,
displayName: colName,
originalType: colType,
type: convertSqliteType(colType),
};
});

const rows = raw.rows.map((r) =>
headers.reduce((a, b, idx) => {
a[b.name] = r[idx];
return a;
}, {} as DatabaseRow)
);

return {
rows,
rowsAffected: raw.rowsAffected,
headers,
lastInsertRowid:
raw.lastInsertRowid === undefined
? undefined
: Number(raw.lastInsertRowid),
};
}
```
2 changes: 1 addition & 1 deletion gui/src/contexts/config-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createContext, useContext, useMemo } from "react";
interface ConfigContextProps {
color: string;
name: string;
onBack: () => void;
onBack?: () => void;
sideBarFooterComponent?: ReactElement;
extensions?: StudioExtension[];
}
Expand Down
2 changes: 1 addition & 1 deletion gui/src/studio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface StudioProps {
name: string;
color: string;

onBack: () => void;
onBack?: () => void;
sideBarFooterComponent?: ReactElement;

theme?: "dark" | "light";
Expand Down

0 comments on commit c5741eb

Please sign in to comment.