Skip to content

Commit 2f8b30c

Browse files
feat: expose table name in schema props (#741)
1 parent 63bdf9d commit 2f8b30c

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

.changeset/four-pans-laugh.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@powersync/common': minor
3+
'@powersync/web': minor
4+
'@powersync/node': minor
5+
'@powersync/react-native': minor
6+
---
7+
8+
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.

packages/common/src/db/schema/Schema.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ export class Schema<S extends SchemaType = SchemaType> {
3535
}
3636
this.tables = tables;
3737
} else {
38-
this.props = tables as S;
39-
this.tables = this.convertToClassicTables(this.props);
38+
// Update the table entries with the provided table name key
39+
this.props = Object.fromEntries(
40+
Object.entries(tables).map(([tableName, table]) => [tableName, table.copyWithName(tableName)])
41+
) as S;
42+
this.tables = Object.values(this.props);
4043
}
4144

4245
this.rawTables = [];
@@ -66,15 +69,8 @@ export class Schema<S extends SchemaType = SchemaType> {
6669

6770
toJSON() {
6871
return {
69-
// This is required because "name" field is not present in TableV2
7072
tables: this.tables.map((t) => t.toJSON()),
7173
raw_tables: this.rawTables
7274
};
7375
}
74-
75-
private convertToClassicTables(props: S) {
76-
return Object.entries(props).map(([name, table]) => {
77-
return table.copyWithName(name);
78-
});
79-
}
8076
}

packages/web/tests/schemav2.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,17 @@ describe('Schema Tests', { sequential: true }, () => {
144144
const aliasedTable = await powersync.getAll('SELECT * FROM test1');
145145
expect(Array.isArray(aliasedTable)).toBe(true);
146146
});
147+
148+
it('should have table names present in schema props', async () => {
149+
// The table names aren't present when creating the Table instances.
150+
// Passing Tables into a Schema should populate the table name based
151+
// off the key of the prop
152+
expect(schema.props.aliased.name).eq('aliased');
153+
expect(schema.props.aliased.internalName).eq('ps_data__aliased');
154+
expect(schema.props.aliased.viewName).eq('test1');
155+
156+
expect(schema.props.assets.name).eq('assets');
157+
expect(schema.props.assets.internalName).eq('ps_data__assets');
158+
expect(schema.props.assets.viewName).eq('assets');
159+
});
147160
});

0 commit comments

Comments
 (0)