Skip to content

Commit 46b0ca1

Browse files
authored
[TypeScript] Remove entries from filtering (#31)
* [TypeScript] Remove entries from filtering In the TypeScript SDK we keep "entries" - an algebraic value of records. I added them because they were also used in the C# SDK, but we don't really need them. This commit changes generated filtering functions so that they compare actual instances instead of product values.
1 parent 3d3d117 commit 46b0ca1

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,9 @@ fn autogen_typescript_access_funcs_for_struct(
10121012
let field_name = field.name.as_ref().expect("autogen'd tuples should have field names");
10131013
let field_type = &field.algebraic_type;
10141014
let typescript_field_name_pascal = field_name.replace("r#", "").to_case(Case::Pascal);
1015+
let typescript_field_name_camel = field_name.replace("r#", "").to_case(Case::Camel);
10151016

1016-
let (field_type, typescript_field_type) = match field_type {
1017+
let typescript_field_type = match field_type {
10171018
AlgebraicType::Product(_) | AlgebraicType::Ref(_) => {
10181019
// TODO: We don't allow filtering on tuples right now, its possible we may consider it for the future.
10191020
continue;
@@ -1023,11 +1024,11 @@ fn autogen_typescript_access_funcs_for_struct(
10231024
continue;
10241025
}
10251026
AlgebraicType::Builtin(b) => match maybe_primitive(b) {
1026-
MaybePrimitive::Primitive(ty) => (typescript_as_type(b).to_string(), ty),
1027+
MaybePrimitive::Primitive(ty) => ty,
10271028
MaybePrimitive::Array(ArrayType { elem_ty }) => {
10281029
if let Some(BuiltinType::U8) = elem_ty.as_builtin() {
10291030
// Do allow filtering for byte arrays
1030-
("Bytes".into(), "Uint8Array")
1031+
"Uint8Array"
10311032
} else {
10321033
// TODO: We don't allow filtering based on an array type, but we might want other functionality here in the future.
10331034
continue;
@@ -1042,15 +1043,15 @@ fn autogen_typescript_access_funcs_for_struct(
10421043

10431044
let filter_return_type = fmt_fn(|f| {
10441045
if is_unique {
1045-
f.write_str(struct_name_pascal_case)
1046+
write!(f, "{struct_name_pascal_case} | null")
10461047
} else {
10471048
write!(f, "{struct_name_pascal_case}[]")
10481049
}
10491050
});
10501051

10511052
writeln!(
10521053
output,
1053-
"public static filterBy{typescript_field_name_pascal}(value: {typescript_field_type}): {filter_return_type} | null"
1054+
"public static filterBy{typescript_field_name_pascal}(value: {typescript_field_type}): {filter_return_type}"
10541055
)
10551056
.unwrap();
10561057

@@ -1062,53 +1063,51 @@ fn autogen_typescript_access_funcs_for_struct(
10621063
}
10631064
writeln!(
10641065
output,
1065-
"for(let entry of __SPACETIMEDB__.clientDB.getTable(\"{table_name}\").getEntries())"
1066+
"for(let instance of __SPACETIMEDB__.clientDB.getTable(\"{table_name}\").getInstances())"
10661067
)
10671068
.unwrap();
10681069
writeln!(output, "{{").unwrap();
10691070
{
10701071
indent_scope!(output);
1071-
writeln!(output, "var productValue = entry.asProductValue();").unwrap();
1072-
writeln!(
1073-
output,
1074-
"let compareValue = productValue.elements[{col_i}].as{field_type}() as {typescript_field_type};"
1075-
)
1076-
.unwrap();
10771072
if typescript_field_type == "Uint8Array" {
10781073
writeln!(
10791074
output,
10801075
"let byteArrayCompare = function (a1: Uint8Array, a2: Uint8Array)
10811076
{{
1082-
if (a1.length != a2.length)
1077+
if (a1.length !== a2.length)
10831078
return false;
10841079
10851080
for (let i=0; i<a1.length; i++)
1086-
if (a1[i]!=a2[i])
1081+
if (a1[i]!==a2[i])
10871082
return false;
10881083
10891084
return true;
10901085
}}"
10911086
)
10921087
.unwrap();
10931088
writeln!(output).unwrap();
1094-
writeln!(output, "if (byteArrayCompare(compareValue, value)) {{").unwrap();
1089+
writeln!(
1090+
output,
1091+
"if (byteArrayCompare(instance.{typescript_field_name_camel}, value)) {{"
1092+
)
1093+
.unwrap();
10951094
{
10961095
indent_scope!(output);
10971096
if is_unique {
1098-
writeln!(output, "return {struct_name_pascal_case}.fromValue(entry);").unwrap();
1097+
writeln!(output, "return instance;").unwrap();
10991098
} else {
1100-
writeln!(output, "result.push({struct_name_pascal_case}.fromValue(entry));").unwrap();
1099+
writeln!(output, "result.push(instance);").unwrap();
11011100
}
11021101
}
11031102
writeln!(output, "}}").unwrap();
11041103
} else {
1105-
writeln!(output, "if (compareValue == value) {{").unwrap();
1104+
writeln!(output, "if (instance.{typescript_field_name_camel} === value) {{").unwrap();
11061105
{
11071106
indent_scope!(output);
11081107
if is_unique {
1109-
writeln!(output, "return {struct_name_pascal_case}.fromValue(entry);").unwrap();
1108+
writeln!(output, "return instance;").unwrap();
11101109
} else {
1111-
writeln!(output, "result.push({struct_name_pascal_case}.fromValue(entry));").unwrap();
1110+
writeln!(output, "result.push(instance);").unwrap();
11121111
}
11131112
}
11141113
writeln!(output, "}}").unwrap();

0 commit comments

Comments
 (0)