diff --git a/.changeset/smart-jobs-change.md b/.changeset/smart-jobs-change.md new file mode 100644 index 00000000..c1c2d294 --- /dev/null +++ b/.changeset/smart-jobs-change.md @@ -0,0 +1,5 @@ +--- +'@powersync/diagnostics-app': minor +--- + +Updated schema generated by diagnostics app + increased descriptiveness of the preceding comment" diff --git a/tools/diagnostics-app/public/images/diagnostics-app-schema.png b/tools/diagnostics-app/public/images/diagnostics-app-schema.png index 486ce372..4909ee4f 100644 Binary files a/tools/diagnostics-app/public/images/diagnostics-app-schema.png and b/tools/diagnostics-app/public/images/diagnostics-app-schema.png differ diff --git a/tools/diagnostics-app/src/app/views/schema.tsx b/tools/diagnostics-app/src/app/views/schema.tsx index e51a53a3..c360645f 100644 --- a/tools/diagnostics-app/src/app/views/schema.tsx +++ b/tools/diagnostics-app/src/app/views/schema.tsx @@ -4,10 +4,14 @@ import { Box, Grid, styled } from '@mui/material'; export default function SchemaPage() { const schema = schemaManager.schemaToString(); - const docs = `// This displays the inferred schema currently used by the diagnostics app. -// This is based on downloaded data, rather than the source database. -// If a table is empty, it will not display here. -// Tables and columns are only added here. Nothing is removed until the database is cleared.`; + const docs = `/** + * This is the inferred schema of the data received by the diagnostics app. + * Because this schema is generated on-the-fly based on the data received by the app, it can + * be incomplete and should NOT be relied upon as a source of truth for your app schema. + * If a table is empty, it will not be shown here. + * If a column contains only NULL values, the column will not be shown here. + * Tables and columns are only added here. Nothing is removed until the database is cleared. + */`; return ( diff --git a/tools/diagnostics-app/src/library/powersync/AppSchema.ts b/tools/diagnostics-app/src/library/powersync/AppSchema.ts index 3b5e25ee..34492081 100644 --- a/tools/diagnostics-app/src/library/powersync/AppSchema.ts +++ b/tools/diagnostics-app/src/library/powersync/AppSchema.ts @@ -1,6 +1,6 @@ -import { column, Schema, TableV2 } from '@powersync/web'; +import { column, Schema, Table } from '@powersync/web'; -export const local_bucket_data = new TableV2( +export const local_bucket_data = new Table( { total_operations: column.integer, last_op: column.text, @@ -10,7 +10,7 @@ export const local_bucket_data = new TableV2( { localOnly: true } ); -export const local_schema = new TableV2( +export const local_schema = new Table( { data: column.text }, diff --git a/tools/diagnostics-app/src/library/powersync/JsSchemaGenerator.ts b/tools/diagnostics-app/src/library/powersync/JsSchemaGenerator.ts index f6748791..f71b5ef1 100644 --- a/tools/diagnostics-app/src/library/powersync/JsSchemaGenerator.ts +++ b/tools/diagnostics-app/src/library/powersync/JsSchemaGenerator.ts @@ -1,26 +1,36 @@ -import { Column, Schema } from '@powersync/web'; +import { Column, ColumnsType, Schema, Table } from '@powersync/web'; export class JsSchemaGenerator { generate(schema: Schema): string { const tables = schema.tables; - return `new Schema([ - ${tables.map((table) => this.generateTable(table.name, table.columns)).join(',\n ')} -]) -`; + return this.generateTables(tables) + '\n\n' + this.generateAppSchema(tables); + } + + private generateTables(tables: Table[]): string { + return tables.map((table) => this.generateTable(table.name, table.columns)).join('\n\n'); } private generateTable(name: string, columns: Column[]): string { - return `new Table({ - name: '${name}', - columns: [ - ${columns.map((c) => this.generateColumn(c)).join(',\n ')} - ] - })`; + return `export const ${name} = new Table( + { + // id column (text) is automatically included + ${columns.map((column) => this.generateColumn(column)).join(',\n ')} + }, + { indexes: {} } +);`; } private generateColumn(column: Column) { const t = column.type; - return `new Column({ name: '${column.name}', type: ColumnType.${column.type} })`; + return `${column.name}: column.${column.type!.toLowerCase()}`; + } + + private generateAppSchema(tables: Table[]): string { + return `export const AppSchema = new Schema({ + ${tables.map((table) => table.name).join(',\n ')} +}); + +export type Database = (typeof AppSchema)['types'];`; } }