Skip to content

Commit 4056a5e

Browse files
committed
feat(diagnostics): update autogenerated schema
1 parent afcc9b3 commit 4056a5e

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

tools/diagnostics-app/src/app/views/schema.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import { Box, Grid, styled } from '@mui/material';
44

55
export default function SchemaPage() {
66
const schema = schemaManager.schemaToString();
7-
const docs = `// This displays the inferred schema currently used by the diagnostics app.
8-
// This is based on downloaded data, rather than the source database.
9-
// If a table is empty, it will not display here.
10-
// Tables and columns are only added here. Nothing is removed until the database is cleared.`;
7+
const docs = `/**
8+
* This is the inferred schema of the data received by the diagnostics app.
9+
* Because this schema is generated on-the-fly based on the data received by the app, it can
10+
* be incomplete and should NOT be relied upon as a source of truth for your app schema.
11+
* If a table is empty, it will not be shown here.
12+
* Tables and columns are only added here. Nothing is removed until the database is cleared.
13+
*/`;
1114
return (
1215
<NavigationPage title="Dynamic Schema">
1316
<S.MainContainer>

tools/diagnostics-app/src/library/powersync/AppSchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { column, Schema, TableV2 } from '@powersync/web';
1+
import { column, Schema, Table } from '@powersync/web';
22

3-
export const local_bucket_data = new TableV2(
3+
export const local_bucket_data = new Table(
44
{
55
total_operations: column.integer,
66
last_op: column.text,
@@ -10,7 +10,7 @@ export const local_bucket_data = new TableV2(
1010
{ localOnly: true }
1111
);
1212

13-
export const local_schema = new TableV2(
13+
export const local_schema = new Table(
1414
{
1515
data: column.text
1616
},
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
import { Column, Schema } from '@powersync/web';
1+
import { Column, ColumnsType, Schema, Table } from '@powersync/web';
22

33
export class JsSchemaGenerator {
44
generate(schema: Schema): string {
55
const tables = schema.tables;
66

7-
return `new Schema([
8-
${tables.map((table) => this.generateTable(table.name, table.columns)).join(',\n ')}
9-
])
10-
`;
7+
return this.generateTables(tables) + '\n\n' + this.generateAppSchema(tables);
8+
}
9+
10+
private generateTables(tables: Table<ColumnsType>[]): string {
11+
return tables.map((table) => this.generateTable(table.name, table.columns)).join('\n\n');
1112
}
1213

1314
private generateTable(name: string, columns: Column[]): string {
14-
return `new Table({
15-
name: '${name}',
16-
columns: [
17-
${columns.map((c) => this.generateColumn(c)).join(',\n ')}
18-
]
19-
})`;
15+
return `export const ${name} = new Table(
16+
{
17+
// id column (text) is automatically included
18+
${columns.map((column) => this.generateColumn(column)).join(',\n ')}
19+
},
20+
{ indexes: {} }
21+
);`;
2022
}
2123

2224
private generateColumn(column: Column) {
2325
const t = column.type;
24-
return `new Column({ name: '${column.name}', type: ColumnType.${column.type} })`;
26+
return `${column.name}: column.${column.type!.toLowerCase()}`;
27+
}
28+
29+
private generateAppSchema(tables: Table<ColumnsType>[]): string {
30+
return `export const AppSchema = new Schema({
31+
${tables.map((table) => table.name).join(',\n ')}
32+
});
33+
34+
export type Database = (typeof AppSchema)['types'];`;
2535
}
2636
}

0 commit comments

Comments
 (0)