|
| 1 | +import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from './ExpressionType.js'; |
| 2 | +import { SchemaGenerator } from './SchemaGenerator.js'; |
| 3 | +import { SqlSyncRules } from './SqlSyncRules.js'; |
| 4 | +import { SourceSchema } from './types.js'; |
| 5 | + |
| 6 | +export interface TsSchemaGeneratorOptions { |
| 7 | + language?: TsSchemaLanguage; |
| 8 | + imports?: TsSchemaImports; |
| 9 | +} |
| 10 | + |
| 11 | +export enum TsSchemaLanguage { |
| 12 | + ts = 'ts', |
| 13 | + /** Excludes types from the generated schema. */ |
| 14 | + js = 'js' |
| 15 | +} |
| 16 | + |
| 17 | +export enum TsSchemaImports { |
| 18 | + web = 'web', |
| 19 | + reactNative = 'reactNative', |
| 20 | + /** |
| 21 | + * Emits imports for `@powersync/web`, with comments for `@powersync/react-native`. |
| 22 | + */ |
| 23 | + auto = 'auto' |
| 24 | +} |
| 25 | + |
| 26 | +export class TsSchemaGenerator extends SchemaGenerator { |
| 27 | + readonly key: string; |
| 28 | + readonly fileName: string; |
| 29 | + readonly mediaType: string; |
| 30 | + readonly label: string; |
| 31 | + |
| 32 | + readonly language: TsSchemaLanguage; |
| 33 | + |
| 34 | + constructor(public readonly options: TsSchemaGeneratorOptions = {}) { |
| 35 | + super(); |
| 36 | + |
| 37 | + this.language = options.language ?? TsSchemaLanguage.ts; |
| 38 | + this.key = this.language; |
| 39 | + if (this.language == TsSchemaLanguage.ts) { |
| 40 | + this.fileName = 'schema.ts'; |
| 41 | + this.mediaType = 'text/typescript'; |
| 42 | + this.label = 'TypeScript'; |
| 43 | + } else { |
| 44 | + this.fileName = 'schema.js'; |
| 45 | + this.mediaType = 'text/javascript'; |
| 46 | + this.label = 'JavaScript'; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + generate(source: SqlSyncRules, schema: SourceSchema): string { |
| 51 | + const tables = super.getAllTables(source, schema); |
| 52 | + |
| 53 | + return `${this.generateImports()} |
| 54 | +
|
| 55 | +${tables.map((table) => this.generateTable(table.name, table.columns)).join('\n\n')} |
| 56 | +
|
| 57 | +export const AppSchema = new Schema({ |
| 58 | + ${tables.map((table) => table.name).join(',\n ')} |
| 59 | +}); |
| 60 | +
|
| 61 | +${this.generateTypeExports()}`; |
| 62 | + } |
| 63 | + |
| 64 | + private generateTypeExports() { |
| 65 | + if (this.language == TsSchemaLanguage.ts) { |
| 66 | + return `export type Database = (typeof AppSchema)['types'];\n`; |
| 67 | + } else { |
| 68 | + return ``; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private generateImports() { |
| 73 | + const importStyle = this.options.imports ?? 'auto'; |
| 74 | + if (importStyle == TsSchemaImports.web) { |
| 75 | + return `import { column, Schema, TableV2 } from '@powersync/web';`; |
| 76 | + } else if (importStyle == TsSchemaImports.reactNative) { |
| 77 | + return `import { column, Schema, TableV2 } from '@powersync/react-native';`; |
| 78 | + } else { |
| 79 | + return `import { column, Schema, TableV2 } from '@powersync/web'; |
| 80 | +// OR: import { column, Schema, TableV2 } from '@powersync/react-native';`; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private generateTable(name: string, columns: ColumnDefinition[]): string { |
| 85 | + return `const ${name} = new TableV2( |
| 86 | + { |
| 87 | + // id column (text) is automatically included |
| 88 | + ${columns.map((c) => this.generateColumn(c)).join(',\n ')} |
| 89 | + }, |
| 90 | + { indexes: {} } |
| 91 | +);`; |
| 92 | + } |
| 93 | + |
| 94 | + private generateColumn(column: ColumnDefinition) { |
| 95 | + const t = column.type; |
| 96 | + if (t.typeFlags & TYPE_TEXT) { |
| 97 | + return `${column.name}: column.text`; |
| 98 | + } else if (t.typeFlags & TYPE_REAL) { |
| 99 | + return `${column.name}: column.real`; |
| 100 | + } else if (t.typeFlags & TYPE_INTEGER) { |
| 101 | + return `${column.name}: column.integer`; |
| 102 | + } else { |
| 103 | + return `${column.name}: column.text`; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments