From 989f4cb8318e364f46d7e444621e6510c94b073b Mon Sep 17 00:00:00 2001 From: stevensJourney Date: Tue, 21 Oct 2025 13:28:25 +0200 Subject: [PATCH 1/2] feat: expose table name in schema props --- .changeset/four-pans-laugh.md | 8 ++++++++ packages/common/src/db/schema/Schema.ts | 14 +++++--------- packages/web/tests/schemav2.test.ts | 13 +++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 .changeset/four-pans-laugh.md diff --git a/.changeset/four-pans-laugh.md b/.changeset/four-pans-laugh.md new file mode 100644 index 000000000..7fda2c91e --- /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'); + }); }); From 0e38dc78611555b6ecd4b94a763f5b613e2c8406 Mon Sep 17 00:00:00 2001 From: stevensJourney Date: Wed, 22 Oct 2025 10:42:30 +0200 Subject: [PATCH 2/2] update wording --- .changeset/four-pans-laugh.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/four-pans-laugh.md b/.changeset/four-pans-laugh.md index 7fda2c91e..a12d168f8 100644 --- a/.changeset/four-pans-laugh.md +++ b/.changeset/four-pans-laugh.md @@ -5,4 +5,4 @@ '@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). +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.