Skip to content

Commit aad9ba5

Browse files
authored
Merge pull request #328 from LucDeCaf/feat/update-diagnostics-schema
feat(diagnostics): update autogenerated schema and description
2 parents cf399df + 89dd672 commit aad9ba5

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

.changeset/smart-jobs-change.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/diagnostics-app': minor
3+
---
4+
5+
Updated schema generated by diagnostics app + increased descriptiveness of the preceding comment"
Loading

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ 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+
* If a column contains only NULL values, the column will not be shown here.
13+
* Tables and columns are only added here. Nothing is removed until the database is cleared.
14+
*/`;
1115
return (
1216
<NavigationPage title="Dynamic Schema">
1317
<S.MainContainer>

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

+3-3
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
},
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)