Skip to content

Commit

Permalink
fixing format
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Apr 17, 2024
1 parent d6f21db commit a1e2719
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 127 deletions.
220 changes: 110 additions & 110 deletions gui/README.md
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),
};
}
```
18 changes: 9 additions & 9 deletions gui/src/driver.ts
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";
12 changes: 6 additions & 6 deletions gui/src/extension.ts
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[];
}
5 changes: 3 additions & 2 deletions gui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
".eslintrc.cjs",
"*.js",
"*.ts",
"jest.config.cjs"
, "../studio/src/components/file-upload.ts" ],
"jest.config.cjs",
"../studio/src/components/file-upload.ts"
],
"exclude": ["node_modules", "dist"]
}

0 comments on commit a1e2719

Please sign in to comment.