-
Notifications
You must be signed in to change notification settings - Fork 24
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
Showing
4 changed files
with
128 additions
and
127 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,110 +1,110 @@ | ||
## 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 = ""; | ||
|
||
constructor(url: string, authToken: string) { | ||
super(); | ||
this.endpoint = url; | ||
this.authToken = authToken; | ||
|
||
this.client = createClient({ | ||
url: this.endpoint, | ||
authToken: this.authToken, | ||
}); | ||
} | ||
|
||
supportBigInt(): boolean { | ||
return false; | ||
} | ||
|
||
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), | ||
}; | ||
} | ||
``` | ||
## 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 = ""; | ||
|
||
constructor(url: string, authToken: string) { | ||
super(); | ||
this.endpoint = url; | ||
this.authToken = authToken; | ||
|
||
this.client = createClient({ | ||
url: this.endpoint, | ||
authToken: this.authToken, | ||
}); | ||
} | ||
|
||
supportBigInt(): boolean { | ||
return false; | ||
} | ||
|
||
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), | ||
}; | ||
} | ||
``` |
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,9 +1,9 @@ | ||
export * from "./drivers/base-driver"; | ||
export { CollaborationDriver } from "./drivers/collaboration-driver"; | ||
export { SqliteLikeBaseDriver } from "./drivers/sqlite-base-driver"; | ||
export { | ||
convertSqliteType, | ||
escapeIdentity, | ||
escapeSqlValue, | ||
} from "./sqlite/sql-helper"; | ||
export { default as parseSafeJson } from "./lib/json-safe"; | ||
export * from "./drivers/base-driver"; | ||
export { CollaborationDriver } from "./drivers/collaboration-driver"; | ||
export { SqliteLikeBaseDriver } from "./drivers/sqlite-base-driver"; | ||
export { | ||
convertSqliteType, | ||
escapeIdentity, | ||
escapeSqlValue, | ||
} from "./sqlite/sql-helper"; | ||
export { default as parseSafeJson } from "./lib/json-safe"; |
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,6 +1,6 @@ | ||
import OptimizeTableState from "./components/table-optimized/OptimizeTableState"; | ||
import { StudioContextMenuItem } from "./messages/open-context-menu"; | ||
|
||
export interface StudioExtension { | ||
contextMenu?: (state: OptimizeTableState) => StudioContextMenuItem[]; | ||
} | ||
import OptimizeTableState from "./components/table-optimized/OptimizeTableState"; | ||
import { StudioContextMenuItem } from "./messages/open-context-menu"; | ||
|
||
export interface StudioExtension { | ||
contextMenu?: (state: OptimizeTableState) => StudioContextMenuItem[]; | ||
} |
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