-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from LucDeCaf/feat/update-diagnostics-schema
feat(diagnostics): update autogenerated schema and description
- Loading branch information
Showing
5 changed files
with
38 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@powersync/diagnostics-app': minor | ||
--- | ||
|
||
Updated schema generated by diagnostics app + increased descriptiveness of the preceding comment" |
Binary file modified
BIN
+110 KB
(260%)
tools/diagnostics-app/public/images/diagnostics-app-schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
34 changes: 22 additions & 12 deletions
34
tools/diagnostics-app/src/library/powersync/JsSchemaGenerator.ts
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,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<ColumnsType>[]): 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<ColumnsType>[]): string { | ||
return `export const AppSchema = new Schema({ | ||
${tables.map((table) => table.name).join(',\n ')} | ||
}); | ||
export type Database = (typeof AppSchema)['types'];`; | ||
} | ||
} |