Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/four-pans-laugh.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 5 additions & 9 deletions packages/common/src/db/schema/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ export class Schema<S extends SchemaType = SchemaType> {
}
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 = [];
Expand Down Expand Up @@ -66,15 +69,8 @@ export class Schema<S extends SchemaType = SchemaType> {

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);
});
}
}
13 changes: 13 additions & 0 deletions packages/web/tests/schemav2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});