Skip to content

Commit 228ec0c

Browse files
authored
Add the primary key type to typescript's TableRuntimeTypeInfo (#2671)
1 parent 4e896b8 commit 228ec0c

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

crates/cli/src/subcommands/generate/typescript.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,22 @@ removeOnUpdate = (cb: (ctx: EventContext, onRow: {row_type}, newRow: {row_type})
325325
writeln!(out, "tableName: \"{}\",", table.name);
326326
writeln!(out, "rowType: {row_type}.getTypeScriptAlgebraicType(),");
327327
if let Some(pk) = schema.pk() {
328+
// This is left here so we can release the codegen change before releasing a new
329+
// version of the SDK.
330+
//
331+
// Eventually we can remove this and only generate use the `primaryKeyInfo` field.
328332
writeln!(out, "primaryKey: \"{}\",", pk.col_name.to_string().to_case(Case::Camel));
333+
334+
writeln!(out, "primaryKeyInfo: {{");
335+
out.indent(1);
336+
writeln!(out, "colName: \"{}\",", pk.col_name.to_string().to_case(Case::Camel));
337+
writeln!(
338+
out,
339+
"colType: {row_type}.getTypeScriptAlgebraicType().product.elements[{}].algebraicType,",
340+
pk.col_pos.0
341+
);
342+
out.dedent(1);
343+
writeln!(out, "}},");
329344
}
330345
out.dedent(1);
331346
writeln!(out, "}},");

crates/cli/tests/snapshots/codegen__codegen_typescript.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,21 +857,37 @@ const REMOTE_MODULE = {
857857
tableName: "logged_out_player",
858858
rowType: Player.getTypeScriptAlgebraicType(),
859859
primaryKey: "identity",
860+
primaryKeyInfo: {
861+
colName: "identity",
862+
colType: Player.getTypeScriptAlgebraicType().product.elements[0].algebraicType,
863+
},
860864
},
861865
person: {
862866
tableName: "person",
863867
rowType: Person.getTypeScriptAlgebraicType(),
864868
primaryKey: "id",
869+
primaryKeyInfo: {
870+
colName: "id",
871+
colType: Person.getTypeScriptAlgebraicType().product.elements[0].algebraicType,
872+
},
865873
},
866874
pk_multi_identity: {
867875
tableName: "pk_multi_identity",
868876
rowType: PkMultiIdentity.getTypeScriptAlgebraicType(),
869877
primaryKey: "id",
878+
primaryKeyInfo: {
879+
colName: "id",
880+
colType: PkMultiIdentity.getTypeScriptAlgebraicType().product.elements[0].algebraicType,
881+
},
870882
},
871883
player: {
872884
tableName: "player",
873885
rowType: Player.getTypeScriptAlgebraicType(),
874886
primaryKey: "identity",
887+
primaryKeyInfo: {
888+
colName: "identity",
889+
colType: Player.getTypeScriptAlgebraicType().product.elements[0].algebraicType,
890+
},
875891
},
876892
points: {
877893
tableName: "points",
@@ -885,6 +901,10 @@ const REMOTE_MODULE = {
885901
tableName: "repeating_test_arg",
886902
rowType: RepeatingTestArg.getTypeScriptAlgebraicType(),
887903
primaryKey: "scheduledId",
904+
primaryKeyInfo: {
905+
colName: "scheduledId",
906+
colType: RepeatingTestArg.getTypeScriptAlgebraicType().product.elements[0].algebraicType,
907+
},
888908
},
889909
test_a: {
890910
tableName: "test_a",
@@ -898,6 +918,10 @@ const REMOTE_MODULE = {
898918
tableName: "test_e",
899919
rowType: TestE.getTypeScriptAlgebraicType(),
900920
primaryKey: "id",
921+
primaryKeyInfo: {
922+
colName: "id",
923+
colType: TestE.getTypeScriptAlgebraicType().product.elements[0].algebraicType,
924+
},
901925
},
902926
test_f: {
903927
tableName: "test_f",

0 commit comments

Comments
 (0)