diff --git a/.changeset/four-pans-laugh.md b/.changeset/four-pans-laugh.md new file mode 100644 index 000000000..a12d168f8 --- /dev/null +++ b/.changeset/four-pans-laugh.md @@ -0,0 +1,8 @@ +--- +'@powersync/common': minor +'@powersync/web': minor +'@powersync/node': minor +'@powersync/react-native': minor +--- + +Populate Table `name` values in `schema.props` for Schemas created with typed `Table`s. e.g. `schema.props['some_table'].name` will contain the table name. diff --git a/packages/common/src/db/schema/Schema.ts b/packages/common/src/db/schema/Schema.ts index 9f82ecf80..a5f69d8f8 100644 --- a/packages/common/src/db/schema/Schema.ts +++ b/packages/common/src/db/schema/Schema.ts @@ -35,8 +35,11 @@ export class Schema { } this.tables = tables; } else { - this.props = tables as S; - this.tables = this.convertToClassicTables(this.props); + // Update the table entries with the provided table name key + this.props = Object.fromEntries( + Object.entries(tables).map(([tableName, table]) => [tableName, table.copyWithName(tableName)]) + ) as S; + this.tables = Object.values(this.props); } this.rawTables = []; @@ -66,15 +69,8 @@ export class Schema { toJSON() { return { - // This is required because "name" field is not present in TableV2 tables: this.tables.map((t) => t.toJSON()), raw_tables: this.rawTables }; } - - private convertToClassicTables(props: S) { - return Object.entries(props).map(([name, table]) => { - return table.copyWithName(name); - }); - } } diff --git a/packages/web/tests/schemav2.test.ts b/packages/web/tests/schemav2.test.ts index 1fc54b6aa..d78a06bb3 100644 --- a/packages/web/tests/schemav2.test.ts +++ b/packages/web/tests/schemav2.test.ts @@ -144,4 +144,17 @@ describe('Schema Tests', { sequential: true }, () => { const aliasedTable = await powersync.getAll('SELECT * FROM test1'); expect(Array.isArray(aliasedTable)).toBe(true); }); + + it('should have table names present in schema props', async () => { + // The table names aren't present when creating the Table instances. + // Passing Tables into a Schema should populate the table name based + // off the key of the prop + expect(schema.props.aliased.name).eq('aliased'); + expect(schema.props.aliased.internalName).eq('ps_data__aliased'); + expect(schema.props.aliased.viewName).eq('test1'); + + expect(schema.props.assets.name).eq('assets'); + expect(schema.props.assets.internalName).eq('ps_data__assets'); + expect(schema.props.assets.viewName).eq('assets'); + }); });