From 76745d2262712f11beb1c5e700996d92a1ecdd1d Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Tue, 23 Apr 2024 19:56:12 +0100 Subject: [PATCH] Fix codegen tests While working on the new C# codegen, I accidentally noticed that those tests were passing even when they clearly should've been failing due to changed output. After running with `--nocapture`, I found out it's because the tests are silently skipped and reported as successful when `rust_wasm_test.wasm` isn't built. This further led to finding that `rust_wasm_test.wasm` is never built - the relevant module results in `rust_wasm_test_module.wasm` instead - so these tests have been incorrectly passing for ages. This PR changes them to actually build the module as part of testing and updates the snapshots to latest master. --- Cargo.lock | 1 + crates/cli/Cargo.toml | 1 + crates/cli/tests/codegen.rs | 34 +- .../snapshots/codegen__codegen_output.snap | 883 +++++++++++++++--- .../codegen__typescript_codegen_output.snap | 713 +++++++++----- 5 files changed, 1224 insertions(+), 408 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7f6558a41ac..9ba129daba8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4380,6 +4380,7 @@ dependencies = [ "spacetimedb-lib", "spacetimedb-primitives", "spacetimedb-standalone", + "spacetimedb-testing", "syntect", "tabled", "tar", diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 4669e9a7c0b..6a5d885253f 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -62,6 +62,7 @@ wasmtime.workspace = true [dev-dependencies] insta.workspace = true +spacetimedb-testing = { path = "../testing" } [features] standalone = ["spacetimedb-standalone"] diff --git a/crates/cli/tests/codegen.rs b/crates/cli/tests/codegen.rs index 9941932255b..0d04c82d848 100644 --- a/crates/cli/tests/codegen.rs +++ b/crates/cli/tests/codegen.rs @@ -1,19 +1,19 @@ +use spacetimedb_cli::generate; use spacetimedb_data_structures::map::HashMap; +use spacetimedb_testing::modules::{CompilationMode, CompiledModule}; use std::path::Path; +use std::sync::OnceLock; + +fn compiled_module() -> &'static Path { + static COMPILED_MODULE: OnceLock = OnceLock::new(); + COMPILED_MODULE + .get_or_init(|| CompiledModule::compile("rust-wasm-test", CompilationMode::Debug)) + .path() +} #[test] fn test_codegen_output() { - let path = Path::new(concat!( - env!("CARGO_MANIFEST_DIR"), - "/../../target/wasm32-unknown-unknown/release/rust_wasm_test.wasm" - )); - if !path.exists() { - eprintln!("rust_wasm_test isn't built, skipping"); - return; - } - use spacetimedb_cli::generate; - println!("{}", path.to_str().unwrap()); - let outfiles: HashMap<_, _> = generate::generate(path, generate::Language::Csharp, "SpacetimeDB") + let outfiles: HashMap<_, _> = generate::generate(compiled_module(), generate::Language::Csharp, "SpacetimeDB") .unwrap() .into_iter() .collect(); @@ -24,17 +24,7 @@ fn test_codegen_output() { #[test] fn test_typescript_codegen_output() { - let path = Path::new(concat!( - env!("CARGO_MANIFEST_DIR"), - "/../../target/wasm32-unknown-unknown/release/rust_wasm_test.wasm" - )); - if !path.exists() { - eprintln!("rust_wasm_test isn't built, skipping"); - return; - } - use spacetimedb_cli::generate; - println!("{}", path.to_str().unwrap()); - let outfiles: HashMap<_, _> = generate::generate(path, generate::Language::TypeScript, "SpacetimeDB") + let outfiles: HashMap<_, _> = generate::generate(compiled_module(), generate::Language::TypeScript, "SpacetimeDB") .unwrap() .into_iter() .collect(); diff --git a/crates/cli/tests/snapshots/codegen__codegen_output.snap b/crates/cli/tests/snapshots/codegen__codegen_output.snap index 03456a77d4e..1574fbb7099 100644 --- a/crates/cli/tests/snapshots/codegen__codegen_output.snap +++ b/crates/cli/tests/snapshots/codegen__codegen_output.snap @@ -20,11 +20,11 @@ namespace SpacetimeDB public static void AddPlayer(string name) { var _argArray = new object[] {name}; - var _message = new NetworkManager.ReducerCallRequest { + var _message = new SpacetimeDBClient.ReducerCallRequest { fn = "add_player", args = _argArray, }; - NetworkManager.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); + SpacetimeDBClient.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); } [ReducerCallback(FunctionName = "add_player")] @@ -32,15 +32,15 @@ namespace SpacetimeDB { if(OnAddPlayerEvent != null) { - var args = dbEvent.FunctionCall.CallInfo.AddPlayerArgs; - OnAddPlayerEvent(dbEvent.Status, Identity.From(dbEvent.CallerIdentity.ToByteArray()) + var args = ((ReducerEvent)dbEvent.FunctionCall.CallInfo).AddPlayerArgs; + OnAddPlayerEvent((ReducerEvent)dbEvent.FunctionCall.CallInfo ,(string)args.Name ); return true; } return false; } - + [DeserializeEvent(FunctionName = "add_player")] public static void AddPlayerDeserializeEventArgs(ClientApi.Event dbEvent) { @@ -53,7 +53,7 @@ namespace SpacetimeDB using var reader = new System.IO.BinaryReader(ms); var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String), reader); args.Name = args_0_value.AsString(); - dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.AddPlayer, "add_player", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), dbEvent.Message, dbEvent.Status, args); + dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.AddPlayer, "add_player", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), Address.From(dbEvent.CallerAddress.ToByteArray()), dbEvent.Message, dbEvent.Status, args); } } @@ -61,6 +61,193 @@ namespace SpacetimeDB { public string Name; } + +} +''' +"AddPrivateReducer.cs" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +using System; +using ClientApi; +using Newtonsoft.Json.Linq; + +namespace SpacetimeDB +{ + public static partial class Reducer + { + public delegate void AddPrivateHandler(ReducerEvent reducerEvent, string name); + public static event AddPrivateHandler OnAddPrivateEvent; + + public static void AddPrivate(string name) + { + var _argArray = new object[] {name}; + var _message = new SpacetimeDBClient.ReducerCallRequest { + fn = "add_private", + args = _argArray, + }; + SpacetimeDBClient.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); + } + + [ReducerCallback(FunctionName = "add_private")] + public static bool OnAddPrivate(ClientApi.Event dbEvent) + { + if(OnAddPrivateEvent != null) + { + var args = ((ReducerEvent)dbEvent.FunctionCall.CallInfo).AddPrivateArgs; + OnAddPrivateEvent((ReducerEvent)dbEvent.FunctionCall.CallInfo + ,(string)args.Name + ); + return true; + } + return false; + } + + [DeserializeEvent(FunctionName = "add_private")] + public static void AddPrivateDeserializeEventArgs(ClientApi.Event dbEvent) + { + var args = new AddPrivateArgsStruct(); + var bsatnBytes = dbEvent.FunctionCall.ArgBytes; + using var ms = new System.IO.MemoryStream(); + ms.SetLength(bsatnBytes.Length); + bsatnBytes.CopyTo(ms.GetBuffer(), 0); + ms.Position = 0; + using var reader = new System.IO.BinaryReader(ms); + var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String), reader); + args.Name = args_0_value.AsString(); + dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.AddPrivate, "add_private", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), Address.From(dbEvent.CallerAddress.ToByteArray()), dbEvent.Message, dbEvent.Status, args); + } + } + + public partial class AddPrivateArgsStruct + { + public string Name; + } + +} +''' +"DeletePlayerReducer.cs" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +using System; +using ClientApi; +using Newtonsoft.Json.Linq; + +namespace SpacetimeDB +{ + public static partial class Reducer + { + public delegate void DeletePlayerHandler(ReducerEvent reducerEvent, ulong id); + public static event DeletePlayerHandler OnDeletePlayerEvent; + + public static void DeletePlayer(ulong id) + { + var _argArray = new object[] {id}; + var _message = new SpacetimeDBClient.ReducerCallRequest { + fn = "delete_player", + args = _argArray, + }; + SpacetimeDBClient.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); + } + + [ReducerCallback(FunctionName = "delete_player")] + public static bool OnDeletePlayer(ClientApi.Event dbEvent) + { + if(OnDeletePlayerEvent != null) + { + var args = ((ReducerEvent)dbEvent.FunctionCall.CallInfo).DeletePlayerArgs; + OnDeletePlayerEvent((ReducerEvent)dbEvent.FunctionCall.CallInfo + ,(ulong)args.Id + ); + return true; + } + return false; + } + + [DeserializeEvent(FunctionName = "delete_player")] + public static void DeletePlayerDeserializeEventArgs(ClientApi.Event dbEvent) + { + var args = new DeletePlayerArgsStruct(); + var bsatnBytes = dbEvent.FunctionCall.ArgBytes; + using var ms = new System.IO.MemoryStream(); + ms.SetLength(bsatnBytes.Length); + bsatnBytes.CopyTo(ms.GetBuffer(), 0); + ms.Position = 0; + using var reader = new System.IO.BinaryReader(ms); + var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U64), reader); + args.Id = args_0_value.AsU64(); + dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.DeletePlayer, "delete_player", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), Address.From(dbEvent.CallerAddress.ToByteArray()), dbEvent.Message, dbEvent.Status, args); + } + } + + public partial class DeletePlayerArgsStruct + { + public ulong Id; + } + +} +''' +"DeletePlayersByNameReducer.cs" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +using System; +using ClientApi; +using Newtonsoft.Json.Linq; + +namespace SpacetimeDB +{ + public static partial class Reducer + { + public delegate void DeletePlayersByNameHandler(ReducerEvent reducerEvent, string name); + public static event DeletePlayersByNameHandler OnDeletePlayersByNameEvent; + + public static void DeletePlayersByName(string name) + { + var _argArray = new object[] {name}; + var _message = new SpacetimeDBClient.ReducerCallRequest { + fn = "delete_players_by_name", + args = _argArray, + }; + SpacetimeDBClient.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); + } + + [ReducerCallback(FunctionName = "delete_players_by_name")] + public static bool OnDeletePlayersByName(ClientApi.Event dbEvent) + { + if(OnDeletePlayersByNameEvent != null) + { + var args = ((ReducerEvent)dbEvent.FunctionCall.CallInfo).DeletePlayersByNameArgs; + OnDeletePlayersByNameEvent((ReducerEvent)dbEvent.FunctionCall.CallInfo + ,(string)args.Name + ); + return true; + } + return false; + } + + [DeserializeEvent(FunctionName = "delete_players_by_name")] + public static void DeletePlayersByNameDeserializeEventArgs(ClientApi.Event dbEvent) + { + var args = new DeletePlayersByNameArgsStruct(); + var bsatnBytes = dbEvent.FunctionCall.ArgBytes; + using var ms = new System.IO.MemoryStream(); + ms.SetLength(bsatnBytes.Length); + bsatnBytes.CopyTo(ms.GetBuffer(), 0); + ms.Position = 0; + using var reader = new System.IO.BinaryReader(ms); + var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String), reader); + args.Name = args_0_value.AsString(); + dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.DeletePlayersByName, "delete_players_by_name", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), Address.From(dbEvent.CallerAddress.ToByteArray()), dbEvent.Message, dbEvent.Status, args); + } + } + + public partial class DeletePlayersByNameArgsStruct + { + public string Name; + } + } ''' "NamespaceTestC.cs" = ''' @@ -109,6 +296,317 @@ namespace SpacetimeDB } } ''' +"PkMultiIdentity.cs" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +using System; +using System.Collections.Generic; + +namespace SpacetimeDB +{ + [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)] + public partial class PkMultiIdentity : IDatabaseTable + { + [Newtonsoft.Json.JsonProperty("id")] + public uint Id; + [Newtonsoft.Json.JsonProperty("other")] + public uint Other; + + private static Dictionary Id_Index = new Dictionary(16); + private static Dictionary Other_Index = new Dictionary(16); + + private static void InternalOnValueInserted(object insertedValue) + { + var val = (PkMultiIdentity)insertedValue; + Id_Index[val.Id] = val; + Other_Index[val.Other] = val; + } + + private static void InternalOnValueDeleted(object deletedValue) + { + var val = (PkMultiIdentity)deletedValue; + Id_Index.Remove(val.Id); + Other_Index.Remove(val.Other); + } + + public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType() + { + return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[] + { + new SpacetimeDB.SATS.ProductTypeElement("id", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U32)), + new SpacetimeDB.SATS.ProductTypeElement("other", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U32)), + }); + } + + public static explicit operator PkMultiIdentity(SpacetimeDB.SATS.AlgebraicValue value) + { + if (value == null) { + return null; + } + var productValue = value.AsProductValue(); + return new PkMultiIdentity + { + Id = productValue.elements[0].AsU32(), + Other = productValue.elements[1].AsU32(), + }; + } + + public static System.Collections.Generic.IEnumerable Iter() + { + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("PkMultiIdentity")) + { + yield return (PkMultiIdentity)entry.Item2; + } + } + public static int Count() + { + return SpacetimeDBClient.clientDB.Count("PkMultiIdentity"); + } + public static PkMultiIdentity FilterById(uint value) + { + Id_Index.TryGetValue(value, out var r); + return r; + } + + public static PkMultiIdentity FilterByOther(uint value) + { + Other_Index.TryGetValue(value, out var r); + return r; + } + + public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, SpacetimeDB.SATS.AlgebraicValue v1, SpacetimeDB.SATS.AlgebraicValue v2) + { + var primaryColumnValue1 = v1.AsProductValue().elements[0]; + var primaryColumnValue2 = v2.AsProductValue().elements[0]; + return SpacetimeDB.SATS.AlgebraicValue.Compare(t.product.elements[0].algebraicType, primaryColumnValue1, primaryColumnValue2); + } + + public static SpacetimeDB.SATS.AlgebraicValue GetPrimaryKeyValue(SpacetimeDB.SATS.AlgebraicValue v) + { + return v.AsProductValue().elements[0]; + } + + public static SpacetimeDB.SATS.AlgebraicType GetPrimaryKeyType(SpacetimeDB.SATS.AlgebraicType t) + { + return t.product.elements[0].algebraicType; + } + + public delegate void InsertEventHandler(PkMultiIdentity insertedValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void UpdateEventHandler(PkMultiIdentity oldValue, PkMultiIdentity newValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void DeleteEventHandler(PkMultiIdentity deletedValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void RowUpdateEventHandler(SpacetimeDBClient.TableOp op, PkMultiIdentity oldValue, PkMultiIdentity newValue, SpacetimeDB.ReducerEvent dbEvent); + public static event InsertEventHandler OnInsert; + public static event UpdateEventHandler OnUpdate; + public static event DeleteEventHandler OnBeforeDelete; + public static event DeleteEventHandler OnDelete; + public static event RowUpdateEventHandler OnRowUpdate; + + public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent) + { + OnInsert?.Invoke((PkMultiIdentity)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnUpdateEvent(object oldValue, object newValue, ClientApi.Event dbEvent) + { + OnUpdate?.Invoke((PkMultiIdentity)oldValue,(PkMultiIdentity)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent) + { + OnBeforeDelete?.Invoke((PkMultiIdentity)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent) + { + OnDelete?.Invoke((PkMultiIdentity)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnRowUpdateEvent(SpacetimeDBClient.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) + { + OnRowUpdate?.Invoke(op, (PkMultiIdentity)oldValue,(PkMultiIdentity)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + } +} +''' +"Point.cs" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +using System; +using System.Collections.Generic; + +namespace SpacetimeDB +{ + [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)] + public partial class Point : IDatabaseTable + { + [Newtonsoft.Json.JsonProperty("x")] + public long X; + [Newtonsoft.Json.JsonProperty("y")] + public long Y; + + + private static void InternalOnValueInserted(object insertedValue) + { + var val = (Point)insertedValue; + } + + private static void InternalOnValueDeleted(object deletedValue) + { + var val = (Point)deletedValue; + } + + public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType() + { + return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[] + { + new SpacetimeDB.SATS.ProductTypeElement("x", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.I64)), + new SpacetimeDB.SATS.ProductTypeElement("y", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.I64)), + }); + } + + public static explicit operator Point(SpacetimeDB.SATS.AlgebraicValue value) + { + if (value == null) { + return null; + } + var productValue = value.AsProductValue(); + return new Point + { + X = productValue.elements[0].AsI64(), + Y = productValue.elements[1].AsI64(), + }; + } + + public static System.Collections.Generic.IEnumerable Iter() + { + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Point")) + { + yield return (Point)entry.Item2; + } + } + public static int Count() + { + return SpacetimeDBClient.clientDB.Count("Point"); + } + public static System.Collections.Generic.IEnumerable FilterByX(long value) + { + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Point")) + { + var productValue = entry.Item1.AsProductValue(); + var compareValue = (long)productValue.elements[0].AsI64(); + if (compareValue == value) { + yield return (Point)entry.Item2; + } + } + } + + public static System.Collections.Generic.IEnumerable FilterByY(long value) + { + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Point")) + { + var productValue = entry.Item1.AsProductValue(); + var compareValue = (long)productValue.elements[1].AsI64(); + if (compareValue == value) { + yield return (Point)entry.Item2; + } + } + } + + public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, SpacetimeDB.SATS.AlgebraicValue _v1, SpacetimeDB.SATS.AlgebraicValue _v2) + { + return false; + } + + public delegate void InsertEventHandler(Point insertedValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void DeleteEventHandler(Point deletedValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void RowUpdateEventHandler(SpacetimeDBClient.TableOp op, Point oldValue, Point newValue, SpacetimeDB.ReducerEvent dbEvent); + public static event InsertEventHandler OnInsert; + public static event DeleteEventHandler OnBeforeDelete; + public static event DeleteEventHandler OnDelete; + public static event RowUpdateEventHandler OnRowUpdate; + + public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent) + { + OnInsert?.Invoke((Point)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent) + { + OnBeforeDelete?.Invoke((Point)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent) + { + OnDelete?.Invoke((Point)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnRowUpdateEvent(SpacetimeDBClient.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) + { + OnRowUpdate?.Invoke(op, (Point)oldValue,(Point)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + } +} +''' +"QueryPrivateReducer.cs" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +using System; +using ClientApi; +using Newtonsoft.Json.Linq; + +namespace SpacetimeDB +{ + public static partial class Reducer + { + public delegate void QueryPrivateHandler(ReducerEvent reducerEvent); + public static event QueryPrivateHandler OnQueryPrivateEvent; + + public static void QueryPrivate() + { + var _argArray = new object[] {}; + var _message = new SpacetimeDBClient.ReducerCallRequest { + fn = "query_private", + args = _argArray, + }; + SpacetimeDBClient.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); + } + + [ReducerCallback(FunctionName = "query_private")] + public static bool OnQueryPrivate(ClientApi.Event dbEvent) + { + if(OnQueryPrivateEvent != null) + { + var args = ((ReducerEvent)dbEvent.FunctionCall.CallInfo).QueryPrivateArgs; + OnQueryPrivateEvent((ReducerEvent)dbEvent.FunctionCall.CallInfo + ); + return true; + } + return false; + } + + [DeserializeEvent(FunctionName = "query_private")] + public static void QueryPrivateDeserializeEventArgs(ClientApi.Event dbEvent) + { + var args = new QueryPrivateArgsStruct(); + var bsatnBytes = dbEvent.FunctionCall.ArgBytes; + using var ms = new System.IO.MemoryStream(); + ms.SetLength(bsatnBytes.Length); + bsatnBytes.CopyTo(ms.GetBuffer(), 0); + ms.Position = 0; + using var reader = new System.IO.BinaryReader(ms); + dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.QueryPrivate, "query_private", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), Address.From(dbEvent.CallerAddress.ToByteArray()), dbEvent.Message, dbEvent.Status, args); + } + } + + public partial class QueryPrivateArgsStruct + { + } + +} +''' "ReducerEvent.cs" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. @@ -122,47 +620,63 @@ namespace SpacetimeDB public enum ReducerType { None, - Update, AddPlayer, + AddPrivate, + DeletePlayer, + DeletePlayersByName, + QueryPrivate, RepeatingTest, Test, } - public partial class ReducerEvent + public partial class ReducerEvent : ReducerEventBase { public ReducerType Reducer { get; private set; } - public string ReducerName { get; private set; } - public ulong Timestamp { get; private set; } - public SpacetimeDB.Identity Identity { get; private set; } - public string ErrMessage { get; private set; } - public ClientApi.Event.Types.Status Status { get; private set; } - private object Args; - public ReducerEvent(ReducerType reducer, string reducerName, ulong timestamp, SpacetimeDB.Identity identity, string errMessage, ClientApi.Event.Types.Status status, object args) + public ReducerEvent(ReducerType reducer, string reducerName, ulong timestamp, SpacetimeDB.Identity identity, SpacetimeDB.Address? callerAddress, string errMessage, ClientApi.Event.Types.Status status, object args) + : base(reducerName, timestamp, identity, callerAddress, errMessage, status, args) { Reducer = reducer; - ReducerName = reducerName; - Timestamp = timestamp; - Identity = identity; - ErrMessage = errMessage; - Status = status; - Args = args; } - public UpdateArgsStruct UpdateArgs + public AddPlayerArgsStruct AddPlayerArgs { get { - if (Reducer != ReducerType.Update) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "Update"); - return (UpdateArgsStruct)Args; + if (Reducer != ReducerType.AddPlayer) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "AddPlayer"); + return (AddPlayerArgsStruct)Args; } } - public AddPlayerArgsStruct AddPlayerArgs + public AddPrivateArgsStruct AddPrivateArgs { get { - if (Reducer != ReducerType.AddPlayer) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "AddPlayer"); - return (AddPlayerArgsStruct)Args; + if (Reducer != ReducerType.AddPrivate) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "AddPrivate"); + return (AddPrivateArgsStruct)Args; + } + } + public DeletePlayerArgsStruct DeletePlayerArgs + { + get + { + if (Reducer != ReducerType.DeletePlayer) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "DeletePlayer"); + return (DeletePlayerArgsStruct)Args; + } + } + public DeletePlayersByNameArgsStruct DeletePlayersByNameArgs + { + get + { + if (Reducer != ReducerType.DeletePlayersByName) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "DeletePlayersByName"); + return (DeletePlayersByNameArgsStruct)Args; + } + } + public QueryPrivateArgsStruct QueryPrivateArgs + { + get + { + if (Reducer != ReducerType.QueryPrivate) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "QueryPrivate"); + return (QueryPrivateArgsStruct)Args; } } public RepeatingTestArgsStruct RepeatingTestArgs @@ -186,19 +700,40 @@ namespace SpacetimeDB { switch (Reducer) { - case ReducerType.Update: + case ReducerType.AddPlayer: { - var args = UpdateArgs; + var args = AddPlayerArgs; return new object[] { + args.Name, }; } - case ReducerType.AddPlayer: + case ReducerType.AddPrivate: { - var args = AddPlayerArgs; + var args = AddPrivateArgs; + return new object[] { + args.Name, + }; + } + case ReducerType.DeletePlayer: + { + var args = DeletePlayerArgs; + return new object[] { + args.Id, + }; + } + case ReducerType.DeletePlayersByName: + { + var args = DeletePlayersByNameArgs; return new object[] { args.Name, }; } + case ReducerType.QueryPrivate: + { + var args = QueryPrivateArgs; + return new object[] { + }; + } case ReducerType.RepeatingTest: { var args = RepeatingTestArgs; @@ -221,6 +756,22 @@ namespace SpacetimeDB } } ''' +"ReducerJsonSettings.cs" = ''' +using SpacetimeDB; + +namespace SpacetimeDB +{ + [ReducerClass] + public partial class Reducer + { + private static Newtonsoft.Json.JsonSerializerSettings _settings = new Newtonsoft.Json.JsonSerializerSettings + { + Converters = { new SpacetimeDB.SomeWrapperConverter(), new SpacetimeDB.EnumWrapperConverter() }, + ContractResolver = new SpacetimeDB.JsonContractResolver(), + }; + } +} +''' "RepeatingTestReducer.cs" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. @@ -239,11 +790,11 @@ namespace SpacetimeDB public static void RepeatingTest(ulong prevTime) { var _argArray = new object[] {prevTime}; - var _message = new NetworkManager.ReducerCallRequest { + var _message = new SpacetimeDBClient.ReducerCallRequest { fn = "repeating_test", args = _argArray, }; - NetworkManager.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); + SpacetimeDBClient.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); } [ReducerCallback(FunctionName = "repeating_test")] @@ -251,8 +802,8 @@ namespace SpacetimeDB { if(OnRepeatingTestEvent != null) { - var args = dbEvent.FunctionCall.CallInfo.RepeatingTestArgs; - OnRepeatingTestEvent(dbEvent.Status, Identity.From(dbEvent.CallerIdentity.ToByteArray()) + var args = ((ReducerEvent)dbEvent.FunctionCall.CallInfo).RepeatingTestArgs; + OnRepeatingTestEvent((ReducerEvent)dbEvent.FunctionCall.CallInfo ,(ulong)args.PrevTime ); return true; @@ -272,7 +823,7 @@ namespace SpacetimeDB using var reader = new System.IO.BinaryReader(ms); var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U64), reader); args.PrevTime = args_0_value.AsU64(); - dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.RepeatingTest, "repeating_test", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), dbEvent.Message, dbEvent.Status, args); + dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.RepeatingTest, "repeating_test", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), Address.From(dbEvent.CallerAddress.ToByteArray()), dbEvent.Message, dbEvent.Status, args); } } @@ -280,6 +831,7 @@ namespace SpacetimeDB { public ulong PrevTime; } + } ''' "TestA.cs" = ''' @@ -291,6 +843,7 @@ using System.Collections.Generic; namespace SpacetimeDB { + [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)] public partial class TestA : IDatabaseTable { [Newtonsoft.Json.JsonProperty("x")] @@ -300,6 +853,17 @@ namespace SpacetimeDB [Newtonsoft.Json.JsonProperty("z")] public string Z; + + private static void InternalOnValueInserted(object insertedValue) + { + var val = (TestA)insertedValue; + } + + private static void InternalOnValueDeleted(object deletedValue) + { + var val = (TestA)deletedValue; + } + public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType() { return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[] @@ -326,18 +890,18 @@ namespace SpacetimeDB public static System.Collections.Generic.IEnumerable Iter() { - foreach(var entry in NetworkManager.clientDB.GetEntries("TestA")) + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("TestA")) { yield return (TestA)entry.Item2; } } public static int Count() { - return NetworkManager.clientDB.Count("TestA"); + return SpacetimeDBClient.clientDB.Count("TestA"); } public static System.Collections.Generic.IEnumerable FilterByX(uint value) { - foreach(var entry in NetworkManager.clientDB.GetEntries("TestA")) + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("TestA")) { var productValue = entry.Item1.AsProductValue(); var compareValue = (uint)productValue.elements[0].AsU32(); @@ -349,7 +913,7 @@ namespace SpacetimeDB public static System.Collections.Generic.IEnumerable FilterByY(uint value) { - foreach(var entry in NetworkManager.clientDB.GetEntries("TestA")) + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("TestA")) { var productValue = entry.Item1.AsProductValue(); var compareValue = (uint)productValue.elements[1].AsU32(); @@ -361,7 +925,7 @@ namespace SpacetimeDB public static System.Collections.Generic.IEnumerable FilterByZ(string value) { - foreach(var entry in NetworkManager.clientDB.GetEntries("TestA")) + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("TestA")) { var productValue = entry.Item1.AsProductValue(); var compareValue = (string)productValue.elements[2].AsString(); @@ -377,38 +941,31 @@ namespace SpacetimeDB } public delegate void InsertEventHandler(TestA insertedValue, SpacetimeDB.ReducerEvent dbEvent); - public delegate void UpdateEventHandler(TestA oldValue, TestA newValue, SpacetimeDB.ReducerEvent dbEvent); public delegate void DeleteEventHandler(TestA deletedValue, SpacetimeDB.ReducerEvent dbEvent); - public delegate void RowUpdateEventHandler(NetworkManager.TableOp op, TestA oldValue, TestA newValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void RowUpdateEventHandler(SpacetimeDBClient.TableOp op, TestA oldValue, TestA newValue, SpacetimeDB.ReducerEvent dbEvent); public static event InsertEventHandler OnInsert; - public static event UpdateEventHandler OnUpdate; public static event DeleteEventHandler OnBeforeDelete; public static event DeleteEventHandler OnDelete; public static event RowUpdateEventHandler OnRowUpdate; public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent) { - OnInsert?.Invoke((TestA)newValue,dbEvent?.FunctionCall.CallInfo); - } - - public static void OnUpdateEvent(object oldValue, object newValue, ClientApi.Event dbEvent) - { - OnUpdate?.Invoke((TestA)oldValue,(TestA)newValue,dbEvent?.FunctionCall.CallInfo); + OnInsert?.Invoke((TestA)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent) { - OnBeforeDelete?.Invoke((TestA)oldValue,dbEvent?.FunctionCall.CallInfo); + OnBeforeDelete?.Invoke((TestA)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent) { - OnDelete?.Invoke((TestA)oldValue,dbEvent?.FunctionCall.CallInfo); + OnDelete?.Invoke((TestA)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } - public static void OnRowUpdateEvent(NetworkManager.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) + public static void OnRowUpdateEvent(SpacetimeDBClient.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) { - OnRowUpdate?.Invoke(op, (TestA)oldValue,(TestA)newValue,dbEvent?.FunctionCall.CallInfo); + OnRowUpdate?.Invoke(op, (TestA)oldValue,(TestA)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } } } @@ -422,6 +979,7 @@ using System.Collections.Generic; namespace SpacetimeDB { + [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)] public partial class TestB : IDatabaseTable { [Newtonsoft.Json.JsonProperty("foo")] @@ -459,12 +1017,24 @@ using System.Collections.Generic; namespace SpacetimeDB { + [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)] public partial class TestD : IDatabaseTable { [Newtonsoft.Json.JsonProperty("test_c")] [SpacetimeDB.Some] public SpacetimeDB.Namespace.Types.TestC TestC; + + private static void InternalOnValueInserted(object insertedValue) + { + var val = (TestD)insertedValue; + } + + private static void InternalOnValueDeleted(object deletedValue) + { + var val = (TestD)deletedValue; + } + public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType() { return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[] @@ -493,14 +1063,14 @@ namespace SpacetimeDB public static System.Collections.Generic.IEnumerable Iter() { - foreach(var entry in NetworkManager.clientDB.GetEntries("TestD")) + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("TestD")) { yield return (TestD)entry.Item2; } } public static int Count() { - return NetworkManager.clientDB.Count("TestD"); + return SpacetimeDBClient.clientDB.Count("TestD"); } public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, SpacetimeDB.SATS.AlgebraicValue _v1, SpacetimeDB.SATS.AlgebraicValue _v2) { @@ -508,38 +1078,31 @@ namespace SpacetimeDB } public delegate void InsertEventHandler(TestD insertedValue, SpacetimeDB.ReducerEvent dbEvent); - public delegate void UpdateEventHandler(TestD oldValue, TestD newValue, SpacetimeDB.ReducerEvent dbEvent); public delegate void DeleteEventHandler(TestD deletedValue, SpacetimeDB.ReducerEvent dbEvent); - public delegate void RowUpdateEventHandler(NetworkManager.TableOp op, TestD oldValue, TestD newValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void RowUpdateEventHandler(SpacetimeDBClient.TableOp op, TestD oldValue, TestD newValue, SpacetimeDB.ReducerEvent dbEvent); public static event InsertEventHandler OnInsert; - public static event UpdateEventHandler OnUpdate; public static event DeleteEventHandler OnBeforeDelete; public static event DeleteEventHandler OnDelete; public static event RowUpdateEventHandler OnRowUpdate; public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent) { - OnInsert?.Invoke((TestD)newValue,dbEvent?.FunctionCall.CallInfo); - } - - public static void OnUpdateEvent(object oldValue, object newValue, ClientApi.Event dbEvent) - { - OnUpdate?.Invoke((TestD)oldValue,(TestD)newValue,dbEvent?.FunctionCall.CallInfo); + OnInsert?.Invoke((TestD)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent) { - OnBeforeDelete?.Invoke((TestD)oldValue,dbEvent?.FunctionCall.CallInfo); + OnBeforeDelete?.Invoke((TestD)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent) { - OnDelete?.Invoke((TestD)oldValue,dbEvent?.FunctionCall.CallInfo); + OnDelete?.Invoke((TestD)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } - public static void OnRowUpdateEvent(NetworkManager.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) + public static void OnRowUpdateEvent(SpacetimeDBClient.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) { - OnRowUpdate?.Invoke(op, (TestD)oldValue,(TestD)newValue,dbEvent?.FunctionCall.CallInfo); + OnRowUpdate?.Invoke(op, (TestD)oldValue,(TestD)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } } } @@ -549,10 +1112,11 @@ namespace SpacetimeDB // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. using System; +using System.Collections.Generic; namespace SpacetimeDB -using System.Collections.Generic; { + [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)] public partial class TestE : IDatabaseTable { [Newtonsoft.Json.JsonProperty("id")] @@ -560,6 +1124,20 @@ using System.Collections.Generic; [Newtonsoft.Json.JsonProperty("name")] public string Name; + private static Dictionary Id_Index = new Dictionary(16); + + private static void InternalOnValueInserted(object insertedValue) + { + var val = (TestE)insertedValue; + Id_Index[val.Id] = val; + } + + private static void InternalOnValueDeleted(object deletedValue) + { + var val = (TestE)deletedValue; + Id_Index.Remove(val.Id); + } + public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType() { return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[] @@ -584,31 +1162,24 @@ using System.Collections.Generic; public static System.Collections.Generic.IEnumerable Iter() { - foreach(var entry in NetworkManager.clientDB.GetEntries("TestE")) + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("TestE")) { yield return (TestE)entry.Item2; } } public static int Count() { - return NetworkManager.clientDB.Count("TestE"); + return SpacetimeDBClient.clientDB.Count("TestE"); } public static TestE FilterById(ulong value) { - foreach(var entry in NetworkManager.clientDB.GetEntries("TestE")) - { - var productValue = entry.Item1.AsProductValue(); - var compareValue = (ulong)productValue.elements[0].AsU64(); - if (compareValue == value) { - return (TestE)entry.Item2; - } - } - return null; + Id_Index.TryGetValue(value, out var r); + return r; } public static System.Collections.Generic.IEnumerable FilterByName(string value) { - foreach(var entry in NetworkManager.clientDB.GetEntries("TestE")) + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("TestE")) { var productValue = entry.Item1.AsProductValue(); var compareValue = (string)productValue.elements[1].AsString(); @@ -625,10 +1196,20 @@ using System.Collections.Generic; return SpacetimeDB.SATS.AlgebraicValue.Compare(t.product.elements[0].algebraicType, primaryColumnValue1, primaryColumnValue2); } + public static SpacetimeDB.SATS.AlgebraicValue GetPrimaryKeyValue(SpacetimeDB.SATS.AlgebraicValue v) + { + return v.AsProductValue().elements[0]; + } + + public static SpacetimeDB.SATS.AlgebraicType GetPrimaryKeyType(SpacetimeDB.SATS.AlgebraicType t) + { + return t.product.elements[0].algebraicType; + } + public delegate void InsertEventHandler(TestE insertedValue, SpacetimeDB.ReducerEvent dbEvent); public delegate void UpdateEventHandler(TestE oldValue, TestE newValue, SpacetimeDB.ReducerEvent dbEvent); public delegate void DeleteEventHandler(TestE deletedValue, SpacetimeDB.ReducerEvent dbEvent); - public delegate void RowUpdateEventHandler(NetworkManager.TableOp op, TestE oldValue, TestE newValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void RowUpdateEventHandler(SpacetimeDBClient.TableOp op, TestE oldValue, TestE newValue, SpacetimeDB.ReducerEvent dbEvent); public static event InsertEventHandler OnInsert; public static event UpdateEventHandler OnUpdate; public static event DeleteEventHandler OnBeforeDelete; @@ -637,27 +1218,27 @@ using System.Collections.Generic; public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent) { - OnInsert?.Invoke((TestE)newValue,dbEvent?.FunctionCall.CallInfo); + OnInsert?.Invoke((TestE)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } public static void OnUpdateEvent(object oldValue, object newValue, ClientApi.Event dbEvent) { - OnUpdate?.Invoke((TestE)oldValue,(TestE)newValue,dbEvent?.FunctionCall.CallInfo); + OnUpdate?.Invoke((TestE)oldValue,(TestE)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent) { - OnBeforeDelete?.Invoke((TestE)oldValue,dbEvent?.FunctionCall.CallInfo); + OnBeforeDelete?.Invoke((TestE)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent) { - OnDelete?.Invoke((TestE)oldValue,dbEvent?.FunctionCall.CallInfo); + OnDelete?.Invoke((TestE)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } - public static void OnRowUpdateEvent(NetworkManager.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) + public static void OnRowUpdateEvent(SpacetimeDBClient.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) { - OnRowUpdate?.Invoke(op, (TestE)oldValue,(TestE)newValue,dbEvent?.FunctionCall.CallInfo); + OnRowUpdate?.Invoke(op, (TestE)oldValue,(TestE)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } } } @@ -679,12 +1260,12 @@ namespace SpacetimeDB public static void Test(SpacetimeDB.TestA arg, SpacetimeDB.TestB arg2, SpacetimeDB.Namespace.Types.TestC arg3) { - var _argArray = new object[] {arg, arg2, new EnumWrapper(arg3)}; - var _message = new NetworkManager.ReducerCallRequest { + var _argArray = new object[] {arg, arg2, new SpacetimeDB.EnumWrapper(arg3)}; + var _message = new SpacetimeDBClient.ReducerCallRequest { fn = "test", args = _argArray, }; - NetworkManager.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); + SpacetimeDBClient.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); } [ReducerCallback(FunctionName = "test")] @@ -692,8 +1273,8 @@ namespace SpacetimeDB { if(OnTestEvent != null) { - var args = dbEvent.FunctionCall.CallInfo.TestArgs; - OnTestEvent(dbEvent.Status, Identity.From(dbEvent.CallerIdentity.ToByteArray()) + var args = ((ReducerEvent)dbEvent.FunctionCall.CallInfo).TestArgs; + OnTestEvent((ReducerEvent)dbEvent.FunctionCall.CallInfo ,(SpacetimeDB.TestA)args.Arg ,(SpacetimeDB.TestB)args.Arg2 ,(SpacetimeDB.Namespace.Types.TestC)args.Arg3 @@ -719,7 +1300,7 @@ namespace SpacetimeDB args.Arg2 = (SpacetimeDB.TestB)(args_1_value); var args_2_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.Namespace.GetAlgebraicTypeForTestC(), reader); args.Arg3 = SpacetimeDB.Namespace.IntoTestC(args_2_value); - dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.Test, "test", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), dbEvent.Message, dbEvent.Status, args); + dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.Test, "test", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), Address.From(dbEvent.CallerAddress.ToByteArray()), dbEvent.Message, dbEvent.Status, args); } } @@ -729,62 +1310,110 @@ namespace SpacetimeDB public SpacetimeDB.TestB Arg2; public SpacetimeDB.Namespace.Types.TestC Arg3; } + } ''' -"UpdateReducer.cs" = ''' +"_Private.cs" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. using System; -using ClientApi; -using Newtonsoft.Json.Linq; +using System.Collections.Generic; namespace SpacetimeDB { - public static partial class Reducer + [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)] + public partial class _Private : IDatabaseTable { - public delegate void UpdateHandler(ReducerEvent reducerEvent); - public static event UpdateHandler OnUpdateEvent; + [Newtonsoft.Json.JsonProperty("name")] + public string Name; + - public static void Update() + private static void InternalOnValueInserted(object insertedValue) { - var _argArray = new object[] {}; - var _message = new NetworkManager.ReducerCallRequest { - fn = "__update__", - args = _argArray, + var val = (_Private)insertedValue; + } + + private static void InternalOnValueDeleted(object deletedValue) + { + var val = (_Private)deletedValue; + } + + public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType() + { + return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[] + { + new SpacetimeDB.SATS.ProductTypeElement("name", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String)), + }); + } + + public static explicit operator _Private(SpacetimeDB.SATS.AlgebraicValue value) + { + if (value == null) { + return null; + } + var productValue = value.AsProductValue(); + return new _Private + { + Name = productValue.elements[0].AsString(), }; - NetworkManager.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings)); } - [ReducerCallback(FunctionName = "__update__")] - public static bool OnUpdate(ClientApi.Event dbEvent) + public static System.Collections.Generic.IEnumerable<_Private> Iter() { - if(OnUpdateEvent != null) + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("_Private")) { - var args = dbEvent.FunctionCall.CallInfo.UpdateArgs; - OnUpdateEvent(dbEvent.Status, Identity.From(dbEvent.CallerIdentity.ToByteArray()) - ); - return true; + yield return (_Private)entry.Item2; } + } + public static int Count() + { + return SpacetimeDBClient.clientDB.Count("_Private"); + } + public static System.Collections.Generic.IEnumerable<_Private> FilterByName(string value) + { + foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("_Private")) + { + var productValue = entry.Item1.AsProductValue(); + var compareValue = (string)productValue.elements[0].AsString(); + if (compareValue == value) { + yield return (_Private)entry.Item2; + } + } + } + + public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, SpacetimeDB.SATS.AlgebraicValue _v1, SpacetimeDB.SATS.AlgebraicValue _v2) + { return false; } - [DeserializeEvent(FunctionName = "__update__")] - public static void UpdateDeserializeEventArgs(ClientApi.Event dbEvent) + public delegate void InsertEventHandler(_Private insertedValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void DeleteEventHandler(_Private deletedValue, SpacetimeDB.ReducerEvent dbEvent); + public delegate void RowUpdateEventHandler(SpacetimeDBClient.TableOp op, _Private oldValue, _Private newValue, SpacetimeDB.ReducerEvent dbEvent); + public static event InsertEventHandler OnInsert; + public static event DeleteEventHandler OnBeforeDelete; + public static event DeleteEventHandler OnDelete; + public static event RowUpdateEventHandler OnRowUpdate; + + public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent) { - var args = new UpdateArgsStruct(); - var bsatnBytes = dbEvent.FunctionCall.ArgBytes; - using var ms = new System.IO.MemoryStream(); - ms.SetLength(bsatnBytes.Length); - bsatnBytes.CopyTo(ms.GetBuffer(), 0); - ms.Position = 0; - using var reader = new System.IO.BinaryReader(ms); - dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.Update, "update", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), dbEvent.Message, dbEvent.Status, args); + OnInsert?.Invoke((_Private)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } - } - public partial class UpdateArgsStruct - { + public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent) + { + OnBeforeDelete?.Invoke((_Private)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent) + { + OnDelete?.Invoke((_Private)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } + + public static void OnRowUpdateEvent(SpacetimeDBClient.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) + { + OnRowUpdate?.Invoke(op, (_Private)oldValue,(_Private)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); + } } } ''' diff --git a/crates/cli/tests/snapshots/codegen__typescript_codegen_output.snap b/crates/cli/tests/snapshots/codegen__typescript_codegen_output.snap index 891a1e15891..30ac18674c7 100644 --- a/crates/cli/tests/snapshots/codegen__typescript_codegen_output.snap +++ b/crates/cli/tests/snapshots/codegen__typescript_codegen_output.snap @@ -7,38 +7,160 @@ expression: outfiles // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, IDatabaseTable, AlgebraicValue } from "@clockworklabs/spacetimedb-sdk"; +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; -export class AddPlayerReducer +export class AddPlayerReducer extends Reducer { - public static call(name: string) + public static reducerName: string = "AddPlayer"; + public static call(_name: string) { + this.getReducer().call(_name); + } + + public call(_name: string) { + const serializer = this.client.getSerializer(); + let _nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + serializer.write(_nameType, _name); + this.client.call("add_player", serializer); + } + + public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { + let nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + let nameValue = AlgebraicValue.deserialize(nameType, adapter.next()) + let name = nameValue.asString(); + return [name]; + } + + public static on(callback: (reducerEvent: ReducerEvent, _name: string) => void) { + this.getReducer().on(callback); + } + public on(callback: (reducerEvent: ReducerEvent, _name: string) => void) { - if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.call("add_player", [name]); - } + this.client.on("reducer:AddPlayer", callback); } +} + + +export default AddPlayerReducer +''' +"add_private_reducer.ts" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. - public static deserializeArgs(rawArgs: any[]): any[] { +// @ts-ignore +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; + +export class AddPrivateReducer extends Reducer +{ + public static reducerName: string = "AddPrivate"; + public static call(_name: string) { + this.getReducer().call(_name); + } + + public call(_name: string) { + const serializer = this.client.getSerializer(); + let _nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + serializer.write(_nameType, _name); + this.client.call("add_private", serializer); + } + + public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { let nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); - let nameValue = AlgebraicValue.deserialize(nameType, rawArgs[0]) + let nameValue = AlgebraicValue.deserialize(nameType, adapter.next()) let name = nameValue.asString(); return [name]; } - public static on(callback: (status: string, identity: string, reducerArgs: any[]) => void) + public static on(callback: (reducerEvent: ReducerEvent, _name: string) => void) { + this.getReducer().on(callback); + } + public on(callback: (reducerEvent: ReducerEvent, _name: string) => void) { - if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.on("reducer:AddPlayer", callback); - } + this.client.on("reducer:AddPrivate", callback); + } +} + + +export default AddPrivateReducer +''' +"delete_player_reducer.ts" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +// @ts-ignore +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; + +export class DeletePlayerReducer extends Reducer +{ + public static reducerName: string = "DeletePlayer"; + public static call(_id: BigInt) { + this.getReducer().call(_id); + } + + public call(_id: BigInt) { + const serializer = this.client.getSerializer(); + let _idType = AlgebraicType.createPrimitiveType(BuiltinType.Type.U64); + serializer.write(_idType, _id); + this.client.call("delete_player", serializer); + } + + public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { + let idType = AlgebraicType.createPrimitiveType(BuiltinType.Type.U64); + let idValue = AlgebraicValue.deserialize(idType, adapter.next()) + let id = idValue.asBigInt(); + return [id]; + } + + public static on(callback: (reducerEvent: ReducerEvent, _id: BigInt) => void) { + this.getReducer().on(callback); + } + public on(callback: (reducerEvent: ReducerEvent, _id: BigInt) => void) + { + this.client.on("reducer:DeletePlayer", callback); } } -__SPACETIMEDB__.reducers.set("AddPlayer", AddPlayerReducer); -if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.registerReducer("AddPlayer", AddPlayerReducer); + +export default DeletePlayerReducer +''' +"delete_players_by_name_reducer.ts" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +// @ts-ignore +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; + +export class DeletePlayersByNameReducer extends Reducer +{ + public static reducerName: string = "DeletePlayersByName"; + public static call(_name: string) { + this.getReducer().call(_name); + } + + public call(_name: string) { + const serializer = this.client.getSerializer(); + let _nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + serializer.write(_nameType, _name); + this.client.call("delete_players_by_name", serializer); + } + + public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { + let nameType = AlgebraicType.createPrimitiveType(BuiltinType.Type.String); + let nameValue = AlgebraicValue.deserialize(nameType, adapter.next()) + let name = nameValue.asString(); + return [name]; + } + + public static on(callback: (reducerEvent: ReducerEvent, _name: string) => void) { + this.getReducer().on(callback); + } + public on(callback: (reducerEvent: ReducerEvent, _name: string) => void) + { + this.client.on("reducer:DeletePlayersByName", callback); + } } -export default AddPlayerReducer + +export default DeletePlayersByNameReducer ''' "namespace_test_c.ts" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE @@ -63,8 +185,10 @@ export namespace Namespace.testC { return result; } - export type Foo = { tag: "Foo"; value: undefined }; - export type Bar = { tag: "Bar"; value: undefined }; + export type Foo = { tag: "Foo", value: undefined }; + export const Foo = { tag: "Foo", value: undefined }; + export type Bar = { tag: "Bar", value: undefined }; + export const Bar = { tag: "Bar", value: undefined }; export function fromValue(value: AlgebraicValue): Namespace.testC { let sumValue = value.asSumValue(); @@ -77,42 +201,276 @@ export namespace Namespace.testC { export type Namespace.testC = Namespace.testC.Foo | Namespace.testC.Bar; export default Namespace.testC; ''' -"repeating_test_reducer.ts" = ''' +"pk_multi_identity.ts" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +// @ts-ignore +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; + +export class PkMultiIdentity extends DatabaseTable +{ + public static db: ClientDB = __SPACETIMEDB__.clientDB; + public static tableName = "PkMultiIdentity"; + public id: number; + public other: number; + + public static primaryKey: string | undefined = "id"; + + constructor(id: number, other: number) { + super(); + this.id = id; + this.other = other; + } + + public static serialize(value: PkMultiIdentity): object { + return [ + value.id, value.other + ]; + } + + public static getAlgebraicType(): AlgebraicType + { + return AlgebraicType.createProductType([ + new ProductTypeElement("id", AlgebraicType.createPrimitiveType(BuiltinType.Type.U32)), + new ProductTypeElement("other", AlgebraicType.createPrimitiveType(BuiltinType.Type.U32)), + ]); + } + + public static fromValue(value: AlgebraicValue): PkMultiIdentity + { + let productValue = value.asProductValue(); + let __id = productValue.elements[0].asNumber(); + let __other = productValue.elements[1].asNumber(); + return new this(__id, __other); + } + + public static filterById(value: number): PkMultiIdentity | null + { + for(let instance of this.db.getTable("PkMultiIdentity").getInstances()) + { + if (instance.id === value) { + return instance; + } + } + return null; + } + + public static filterByOther(value: number): PkMultiIdentity | null + { + for(let instance of this.db.getTable("PkMultiIdentity").getInstances()) + { + if (instance.other === value) { + return instance; + } + } + return null; + } + + +} + +export default PkMultiIdentity; +''' +"point.ts" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, IDatabaseTable, AlgebraicValue } from "@clockworklabs/spacetimedb-sdk"; +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; -export class RepeatingTestReducer +export class Point extends DatabaseTable { - public static call(prevTime: number) + public static db: ClientDB = __SPACETIMEDB__.clientDB; + public static tableName = "Point"; + public x: BigInt; + public y: BigInt; + + constructor(x: BigInt, y: BigInt) { + super(); + this.x = x; + this.y = y; + } + + public static serialize(value: Point): object { + return [ + value.x, value.y + ]; + } + + public static getAlgebraicType(): AlgebraicType { - if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.call("repeating_test", [prevTime]); + return AlgebraicType.createProductType([ + new ProductTypeElement("x", AlgebraicType.createPrimitiveType(BuiltinType.Type.I64)), + new ProductTypeElement("y", AlgebraicType.createPrimitiveType(BuiltinType.Type.I64)), + ]); + } + + public static fromValue(value: AlgebraicValue): Point + { + let productValue = value.asProductValue(); + let __x = productValue.elements[0].asBigInt(); + let __y = productValue.elements[1].asBigInt(); + return new this(__x, __y); + } + + public static filterByX(value: BigInt): Point[] + { + let result: Point[] = []; + for(let instance of this.db.getTable("Point").getInstances()) + { + if (instance.x === value) { + result.push(instance); + } } + return result; } - public static deserializeArgs(rawArgs: any[]): any[] { - let prevTimeType = AlgebraicType.createPrimitiveType(BuiltinType.Type.U64); - let prevTimeValue = AlgebraicValue.deserialize(prevTimeType, rawArgs[0]) - let prevTime = prevTimeValue.asNumber(); - return [prevTime]; + public static filterByY(value: BigInt): Point[] + { + let result: Point[] = []; + for(let instance of this.db.getTable("Point").getInstances()) + { + if (instance.y === value) { + result.push(instance); + } + } + return result; + } + + +} + +export default Point; +''' +"private.ts" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +// @ts-ignore +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; + +export class Private extends DatabaseTable +{ + public static db: ClientDB = __SPACETIMEDB__.clientDB; + public static tableName = "Private"; + public name: string; + + constructor(name: string) { + super(); + this.name = name; + } + + public static serialize(value: Private): object { + return [ + value.name + ]; } - public static on(callback: (status: string, identity: string, reducerArgs: any[]) => void) + public static getAlgebraicType(): AlgebraicType { - if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.on("reducer:RepeatingTest", callback); + return AlgebraicType.createProductType([ + new ProductTypeElement("name", AlgebraicType.createPrimitiveType(BuiltinType.Type.String)), + ]); + } + + public static fromValue(value: AlgebraicValue): Private + { + let productValue = value.asProductValue(); + let __name = productValue.elements[0].asString(); + return new this(__name); + } + + public static filterByName(value: string): Private[] + { + let result: Private[] = []; + for(let instance of this.db.getTable("_Private").getInstances()) + { + if (instance.name === value) { + result.push(instance); + } } + return result; } + + } -__SPACETIMEDB__.reducers.set("RepeatingTest", RepeatingTestReducer); -if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.registerReducer("RepeatingTest", RepeatingTestReducer); +export default Private; +''' +"query_private_reducer.ts" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +// @ts-ignore +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; + +export class QueryPrivateReducer extends Reducer +{ + public static reducerName: string = "QueryPrivate"; + public static call() { + this.getReducer().call(); + } + + public call() { + const serializer = this.client.getSerializer(); + this.client.call("query_private", serializer); + } + + public static deserializeArgs(_adapter: ReducerArgsAdapter): any[] { + return []; + } + + public static on(callback: (reducerEvent: ReducerEvent, ) => void) { + this.getReducer().on(callback); + } + public on(callback: (reducerEvent: ReducerEvent, ) => void) + { + this.client.on("reducer:QueryPrivate", callback); + } +} + + +export default QueryPrivateReducer +''' +"repeating_test_reducer.ts" = ''' +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. + +// @ts-ignore +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; + +export class RepeatingTestReducer extends Reducer +{ + public static reducerName: string = "RepeatingTest"; + public static call(_prevTime: BigInt) { + this.getReducer().call(_prevTime); + } + + public call(_prevTime: BigInt) { + const serializer = this.client.getSerializer(); + let _prevTimeType = AlgebraicType.createPrimitiveType(BuiltinType.Type.U64); + serializer.write(_prevTimeType, _prevTime); + this.client.call("repeating_test", serializer); + } + + public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { + let prevTimeType = AlgebraicType.createPrimitiveType(BuiltinType.Type.U64); + let prevTimeValue = AlgebraicValue.deserialize(prevTimeType, adapter.next()) + let prevTime = prevTimeValue.asBigInt(); + return [prevTime]; + } + + public static on(callback: (reducerEvent: ReducerEvent, _prevTime: BigInt) => void) { + this.getReducer().on(callback); + } + public on(callback: (reducerEvent: ReducerEvent, _prevTime: BigInt) => void) + { + this.client.on("reducer:RepeatingTest", callback); + } } + export default RepeatingTestReducer ''' "test_a.ts" = ''' @@ -120,10 +478,11 @@ export default RepeatingTestReducer // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, IDatabaseTable, AlgebraicValue, ReducerEvent } from "@clockworklabs/spacetimedb-sdk"; +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; -export class TestA extends IDatabaseTable +export class TestA extends DatabaseTable { + public static db: ClientDB = __SPACETIMEDB__.clientDB; public static tableName = "TestA"; public x: number; public y: number; @@ -160,104 +519,57 @@ export class TestA extends IDatabaseTable return new this(__x, __y, __z); } - public static count(): number - { - return __SPACETIMEDB__.clientDB.getTable("TestA").count(); - } - - public static all(): TestA[] - { - return __SPACETIMEDB__.clientDB.getTable("TestA").getInstances() as unknown as TestA[]; - } - - public static filterByX(value: number): TestA[] | null + public static filterByX(value: number): TestA[] { let result: TestA[] = []; - for(let entry of __SPACETIMEDB__.clientDB.getTable("TestA").getEntries()) + for(let instance of this.db.getTable("TestA").getInstances()) { - var productValue = entry.asProductValue(); - let compareValue = productValue.elements[0].asNumber() as number; - if (compareValue == value) { - result.push(TestA.fromValue(entry)); + if (instance.x === value) { + result.push(instance); } } return result; } - public static filterByY(value: number): TestA[] | null + public static filterByY(value: number): TestA[] { let result: TestA[] = []; - for(let entry of __SPACETIMEDB__.clientDB.getTable("TestA").getEntries()) + for(let instance of this.db.getTable("TestA").getInstances()) { - var productValue = entry.asProductValue(); - let compareValue = productValue.elements[1].asNumber() as number; - if (compareValue == value) { - result.push(TestA.fromValue(entry)); + if (instance.y === value) { + result.push(instance); } } return result; } - public static filterByZ(value: string): TestA[] | null + public static filterByZ(value: string): TestA[] { let result: TestA[] = []; - for(let entry of __SPACETIMEDB__.clientDB.getTable("TestA").getEntries()) + for(let instance of this.db.getTable("TestA").getInstances()) { - var productValue = entry.asProductValue(); - let compareValue = productValue.elements[2].asString() as string; - if (compareValue == value) { - result.push(TestA.fromValue(entry)); + if (instance.z === value) { + result.push(instance); } } return result; } - public static onInsert(callback: (value: TestA, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestA").onInsert(callback); - } - - public static onUpdate(callback: (oldValue: TestA, newValue: TestA, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestA").onUpdate(callback); - } - - public static onDelete(callback: (value: TestA, oldValue: TestA, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestA").onDelete(callback); - } - - public static removeOnInsert(callback: (value: TestA, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestA").removeOnInsert(callback); - } - - public static removeOnUpdate(callback: (oldValue: TestA, newValue: TestA, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestA").removeOnUpdate(callback); - } - - public static removeOnDelete(callback: (value: TestA, oldValue: TestA, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestA").removeOnDelete(callback); - } - } export default TestA; - -__SPACETIMEDB__.registerComponent("TestA", TestA); ''' "test_b.ts" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, IDatabaseTable, AlgebraicValue, ReducerEvent } from "@clockworklabs/spacetimedb-sdk"; +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; -export class TestB extends IDatabaseTable +export class TestB extends DatabaseTable { + public static db: ClientDB = __SPACETIMEDB__.clientDB; public static tableName = "TestB"; public foo: string; @@ -291,20 +603,19 @@ export class TestB extends IDatabaseTable } export default TestB; - -__SPACETIMEDB__.registerComponent("TestB", TestB); ''' "test_d.ts" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, IDatabaseTable, AlgebraicValue, ReducerEvent } from "@clockworklabs/spacetimedb-sdk"; +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; // @ts-ignore import { Namespace.TestC } from "./namespace.test_c"; -export class TestD extends IDatabaseTable +export class TestD extends DatabaseTable { + public static db: ClientDB = __SPACETIMEDB__.clientDB; public static tableName = "TestD"; public testC: Namespace.TestC | null; @@ -322,7 +633,7 @@ export class TestD extends IDatabaseTable public static getAlgebraicType(): AlgebraicType { return AlgebraicType.createProductType([ - new ProductTypeElement("test_c", AlgebraicType.createSumType([ + new ProductTypeElement("testC", AlgebraicType.createSumType([ new SumTypeVariant("some", Namespace.TestC.getAlgebraicType()), new SumTypeVariant("none", AlgebraicType.createProductType([ ])), @@ -337,69 +648,28 @@ export class TestD extends IDatabaseTable return new this(__test_c); } - public static count(): number - { - return __SPACETIMEDB__.clientDB.getTable("TestD").count(); - } - - public static all(): TestD[] - { - return __SPACETIMEDB__.clientDB.getTable("TestD").getInstances() as unknown as TestD[]; - } - - - public static onInsert(callback: (value: TestD, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestD").onInsert(callback); - } - - public static onUpdate(callback: (oldValue: TestD, newValue: TestD, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestD").onUpdate(callback); - } - - public static onDelete(callback: (value: TestD, oldValue: TestD, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestD").onDelete(callback); - } - - public static removeOnInsert(callback: (value: TestD, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestD").removeOnInsert(callback); - } - - public static removeOnUpdate(callback: (oldValue: TestD, newValue: TestD, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestD").removeOnUpdate(callback); - } - - public static removeOnDelete(callback: (value: TestD, oldValue: TestD, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestD").removeOnDelete(callback); - } } export default TestD; - -__SPACETIMEDB__.registerComponent("TestD", TestD); ''' "test_e.ts" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, IDatabaseTable, AlgebraicValue, ReducerEvent } from "@clockworklabs/spacetimedb-sdk"; +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, SumType, SumTypeVariant, DatabaseTable, AlgebraicValue, ReducerEvent, Identity, Address, ClientDB, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; -export class TestE extends IDatabaseTable +export class TestE extends DatabaseTable { + public static db: ClientDB = __SPACETIMEDB__.clientDB; public static tableName = "TestE"; - public id: number; + public id: BigInt; public name: string; public static primaryKey: string | undefined = "id"; - constructor(id: number, name: string) { + constructor(id: BigInt, name: string) { super(); this.id = id; this.name = name; @@ -422,91 +692,45 @@ export class TestE extends IDatabaseTable public static fromValue(value: AlgebraicValue): TestE { let productValue = value.asProductValue(); - let __id = productValue.elements[0].asNumber(); + let __id = productValue.elements[0].asBigInt(); let __name = productValue.elements[1].asString(); return new this(__id, __name); } - public static count(): number - { - return __SPACETIMEDB__.clientDB.getTable("TestE").count(); - } - - public static all(): TestE[] + public static filterById(value: BigInt): TestE | null { - return __SPACETIMEDB__.clientDB.getTable("TestE").getInstances() as unknown as TestE[]; - } - - public static filterById(value: number): TestE | null - { - for(let entry of __SPACETIMEDB__.clientDB.getTable("TestE").getEntries()) + for(let instance of this.db.getTable("TestE").getInstances()) { - var productValue = entry.asProductValue(); - let compareValue = productValue.elements[0].asNumber() as number; - if (compareValue == value) { - return TestE.fromValue(entry); + if (instance.id === value) { + return instance; } } return null; } - public static filterByName(value: string): TestE[] | null + public static filterByName(value: string): TestE[] { let result: TestE[] = []; - for(let entry of __SPACETIMEDB__.clientDB.getTable("TestE").getEntries()) + for(let instance of this.db.getTable("TestE").getInstances()) { - var productValue = entry.asProductValue(); - let compareValue = productValue.elements[1].asString() as string; - if (compareValue == value) { - result.push(TestE.fromValue(entry)); + if (instance.name === value) { + result.push(instance); } } return result; } - public static onInsert(callback: (value: TestE, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestE").onInsert(callback); - } - - public static onUpdate(callback: (oldValue: TestE, newValue: TestE, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestE").onUpdate(callback); - } - - public static onDelete(callback: (value: TestE, oldValue: TestE, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestE").onDelete(callback); - } - - public static removeOnInsert(callback: (value: TestE, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestE").removeOnInsert(callback); - } - - public static removeOnUpdate(callback: (oldValue: TestE, newValue: TestE, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestE").removeOnUpdate(callback); - } - - public static removeOnDelete(callback: (value: TestE, oldValue: TestE, reducerEvent: ReducerEvent | undefined) => void) - { - __SPACETIMEDB__.clientDB.getTable("TestE").removeOnDelete(callback); - } - } export default TestE; - -__SPACETIMEDB__.registerComponent("TestE", TestE); ''' "test_reducer.ts" = ''' // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. // @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, IDatabaseTable, AlgebraicValue } from "@clockworklabs/spacetimedb-sdk"; +import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, DatabaseTable, AlgebraicValue, ReducerArgsAdapter, SumTypeVariant, Serializer, Identity, Address, ReducerEvent, Reducer, SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk"; // @ts-ignore import { TestA } from "./test_a"; // @ts-ignore @@ -514,75 +738,46 @@ import { TestB } from "./test_b"; // @ts-ignore import { Namespace.TestC } from "./namespace.test_c"; -export class TestReducer +export class TestReducer extends Reducer { - public static call(arg: TestA, arg2: TestB, arg3: Namespace.TestC) - { - if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.call("test", [TestA.serialize(arg), TestB.serialize(arg2), Namespace.TestC.serialize(arg3)]); - } + public static reducerName: string = "Test"; + public static call(_arg: TestA, _arg2: TestB, _arg3: Namespace.TestC) { + this.getReducer().call(_arg, _arg2, _arg3); + } + + public call(_arg: TestA, _arg2: TestB, _arg3: Namespace.TestC) { + const serializer = this.client.getSerializer(); + let _argType = TestA.getAlgebraicType(); + serializer.write(_argType, _arg); + let _arg2Type = TestB.getAlgebraicType(); + serializer.write(_arg2Type, _arg2); + let _arg3Type = Namespace.TestC.getAlgebraicType(); + serializer.write(_arg3Type, _arg3); + this.client.call("test", serializer); } - public static deserializeArgs(rawArgs: any[]): any[] { + public static deserializeArgs(adapter: ReducerArgsAdapter): any[] { let argType = TestA.getAlgebraicType(); - let argValue = AlgebraicValue.deserialize(argType, rawArgs[0]) + let argValue = AlgebraicValue.deserialize(argType, adapter.next()) let arg = TestA.fromValue(argValue); let arg2Type = TestB.getAlgebraicType(); - let arg2Value = AlgebraicValue.deserialize(arg2Type, rawArgs[1]) + let arg2Value = AlgebraicValue.deserialize(arg2Type, adapter.next()) let arg2 = TestB.fromValue(arg2Value); let arg3Type = Namespace.TestC.getAlgebraicType(); - let arg3Value = AlgebraicValue.deserialize(arg3Type, rawArgs[2]) + let arg3Value = AlgebraicValue.deserialize(arg3Type, adapter.next()) let arg3 = Namespace.TestC.fromValue(arg3Value); return [arg, arg2, arg3]; } - public static on(callback: (status: string, identity: string, reducerArgs: any[]) => void) - { - if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.on("reducer:Test", callback); - } + public static on(callback: (reducerEvent: ReducerEvent, _arg: TestA, _arg2: TestB, _arg3: Namespace.TestC) => void) { + this.getReducer().on(callback); } -} - -__SPACETIMEDB__.reducers.set("Test", TestReducer); -if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.registerReducer("Test", TestReducer); -} - -export default TestReducer -''' -"update_reducer.ts" = ''' -// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE -// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. - -// @ts-ignore -import { __SPACETIMEDB__, AlgebraicType, ProductType, BuiltinType, ProductTypeElement, IDatabaseTable, AlgebraicValue } from "@clockworklabs/spacetimedb-sdk"; - -export class UpdateReducer -{ - public static call() + public on(callback: (reducerEvent: ReducerEvent, _arg: TestA, _arg2: TestB, _arg3: Namespace.TestC) => void) { - if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.call("__update__", []); - } - } - - public static deserializeArgs(): any[] { - return []; - } - - public static on(callback: (status: string, identity: string, reducerArgs: any[]) => void) - { - if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.on("reducer:Update", callback); - } + this.client.on("reducer:Test", callback); } } -__SPACETIMEDB__.reducers.set("Update", UpdateReducer); -if (__SPACETIMEDB__.spacetimeDBClient) { - __SPACETIMEDB__.spacetimeDBClient.registerReducer("Update", UpdateReducer); -} -export default UpdateReducer +export default TestReducer '''