spacetimedb-cli 0.8.2

A command line interface for SpacetimeDB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
---
source: crates/cli/tests/codegen.rs
expression: outfiles
---
"AddPlayerReducer.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 AddPlayerHandler(ReducerEvent reducerEvent, string name);
		public static event AddPlayerHandler OnAddPlayerEvent;

		public static void AddPlayer(string name)
		{
			var _argArray = new object[] {name};
			var _message = new NetworkManager.ReducerCallRequest {
				fn = "add_player",
				args = _argArray,
			};
			NetworkManager.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings));
		}

		[ReducerCallback(FunctionName = "add_player")]
		public static bool OnAddPlayer(ClientApi.Event dbEvent)
		{
			if(OnAddPlayerEvent != null)
			{
				var args = dbEvent.FunctionCall.CallInfo.AddPlayerArgs;
				OnAddPlayerEvent(dbEvent.Status, Identity.From(dbEvent.CallerIdentity.ToByteArray())
					,(string)args.Name
				);
				return true;
			}
			return false;
		}
		
		[DeserializeEvent(FunctionName = "add_player")]
		public static void AddPlayerDeserializeEventArgs(ClientApi.Event dbEvent)
		{
			var args = new AddPlayerArgsStruct();
			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.AddPlayer, "add_player", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), dbEvent.Message, dbEvent.Status, args);
		}
	}

	public partial class AddPlayerArgsStruct
	{
		public string Name;
	}
}
'''
"NamespaceTestC.cs" = '''
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

using System;

namespace SpacetimeDB
{
	public partial class Namespace
	{
		public partial class Types
		{
			public enum TestC
			{
				Foo,
				Bar,
			}
		}
		public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicTypeForTestC()
		{
			return SpacetimeDB.SATS.AlgebraicType.CreateSumType(new System.Collections.Generic.List<SpacetimeDB.SATS.SumTypeVariant>
			{
				new SpacetimeDB.SATS.SumTypeVariant("Foo", SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
			{
			})),
				new SpacetimeDB.SATS.SumTypeVariant("Bar", SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
			{
			})),
			});
		}
		public static Namespace.Types.TestC IntoTestC(SpacetimeDB.SATS.AlgebraicValue value)
		{
			var sumValue = value.AsSumValue();
			switch(sumValue.tag)
			{
				case 0:
					return Namespace.Types.TestC.Foo;
				case 1:
					return Namespace.Types.TestC.Bar;
			}

			return default;
		}
	}
}
'''
"ReducerEvent.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 enum ReducerType
	{
		None,
		Update,
		AddPlayer,
		RepeatingTest,
		Test,
	}

	public partial class ReducerEvent
	{
		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)
		{
			Reducer = reducer;
			ReducerName = reducerName;
			Timestamp = timestamp;
			Identity = identity;
			ErrMessage = errMessage;
			Status = status;
			Args = args;
		}

		public UpdateArgsStruct UpdateArgs
		{
			get
			{
				if (Reducer != ReducerType.Update) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "Update");
				return (UpdateArgsStruct)Args;
			}
		}
		public AddPlayerArgsStruct AddPlayerArgs
		{
			get
			{
				if (Reducer != ReducerType.AddPlayer) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "AddPlayer");
				return (AddPlayerArgsStruct)Args;
			}
		}
		public RepeatingTestArgsStruct RepeatingTestArgs
		{
			get
			{
				if (Reducer != ReducerType.RepeatingTest) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "RepeatingTest");
				return (RepeatingTestArgsStruct)Args;
			}
		}
		public TestArgsStruct TestArgs
		{
			get
			{
				if (Reducer != ReducerType.Test) throw new SpacetimeDB.ReducerMismatchException(Reducer.ToString(), "Test");
				return (TestArgsStruct)Args;
			}
		}

		public object[] GetArgsAsObjectArray()
		{
			switch (Reducer)
			{
				case ReducerType.Update:
				{
					var args = UpdateArgs;
					return new object[] {
					};
				}
				case ReducerType.AddPlayer:
				{
					var args = AddPlayerArgs;
					return new object[] {
						args.Name,
					};
				}
				case ReducerType.RepeatingTest:
				{
					var args = RepeatingTestArgs;
					return new object[] {
						args.PrevTime,
					};
				}
				case ReducerType.Test:
				{
					var args = TestArgs;
					return new object[] {
						args.Arg,
						args.Arg2,
						args.Arg3,
					};
				}
				default: throw new System.Exception($"Unhandled reducer case: {Reducer}. Please run SpacetimeDB code generator");
			}
		}
	}
}
'''
"RepeatingTestReducer.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 RepeatingTestHandler(ReducerEvent reducerEvent, ulong prevTime);
		public static event RepeatingTestHandler OnRepeatingTestEvent;

		public static void RepeatingTest(ulong prevTime)
		{
			var _argArray = new object[] {prevTime};
			var _message = new NetworkManager.ReducerCallRequest {
				fn = "repeating_test",
				args = _argArray,
			};
			NetworkManager.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings));
		}

		[ReducerCallback(FunctionName = "repeating_test")]
		public static bool OnRepeatingTest(ClientApi.Event dbEvent)
		{
			if(OnRepeatingTestEvent != null)
			{
				var args = dbEvent.FunctionCall.CallInfo.RepeatingTestArgs;
				OnRepeatingTestEvent(dbEvent.Status, Identity.From(dbEvent.CallerIdentity.ToByteArray())
					,(ulong)args.PrevTime
				);
				return true;
			}
			return false;
		}

		[DeserializeEvent(FunctionName = "repeating_test")]
		public static void RepeatingTestDeserializeEventArgs(ClientApi.Event dbEvent)
		{
			var args = new RepeatingTestArgsStruct();
			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.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);
		}
	}

	public partial class RepeatingTestArgsStruct
	{
		public ulong PrevTime;
	}
}
'''
"TestA.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
{
	public partial class TestA : IDatabaseTable
	{
		[Newtonsoft.Json.JsonProperty("x")]
		public uint X;
		[Newtonsoft.Json.JsonProperty("y")]
		public uint Y;
		[Newtonsoft.Json.JsonProperty("z")]
		public string Z;

		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.U32)),
				new SpacetimeDB.SATS.ProductTypeElement("y", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U32)),
				new SpacetimeDB.SATS.ProductTypeElement("z", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String)),
			});
		}

		public static explicit operator TestA(SpacetimeDB.SATS.AlgebraicValue value)
		{
			if (value == null) {
				return null;
			}
			var productValue = value.AsProductValue();
			return new TestA
			{
				X = productValue.elements[0].AsU32(),
				Y = productValue.elements[1].AsU32(),
				Z = productValue.elements[2].AsString(),
			};
		}

		public static System.Collections.Generic.IEnumerable<TestA> Iter()
		{
			foreach(var entry in NetworkManager.clientDB.GetEntries("TestA"))
			{
				yield return (TestA)entry.Item2;
			}
		}
		public static int Count()
		{
			return NetworkManager.clientDB.Count("TestA");
		}
		public static System.Collections.Generic.IEnumerable<TestA> FilterByX(uint value)
		{
			foreach(var entry in NetworkManager.clientDB.GetEntries("TestA"))
			{
				var productValue = entry.Item1.AsProductValue();
				var compareValue = (uint)productValue.elements[0].AsU32();
				if (compareValue == value) {
					yield return (TestA)entry.Item2;
				}
			}
		}

		public static System.Collections.Generic.IEnumerable<TestA> FilterByY(uint value)
		{
			foreach(var entry in NetworkManager.clientDB.GetEntries("TestA"))
			{
				var productValue = entry.Item1.AsProductValue();
				var compareValue = (uint)productValue.elements[1].AsU32();
				if (compareValue == value) {
					yield return (TestA)entry.Item2;
				}
			}
		}

		public static System.Collections.Generic.IEnumerable<TestA> FilterByZ(string value)
		{
			foreach(var entry in NetworkManager.clientDB.GetEntries("TestA"))
			{
				var productValue = entry.Item1.AsProductValue();
				var compareValue = (string)productValue.elements[2].AsString();
				if (compareValue == value) {
					yield return (TestA)entry.Item2;
				}
			}
		}

		public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, SpacetimeDB.SATS.AlgebraicValue _v1, SpacetimeDB.SATS.AlgebraicValue _v2)
		{
			return false;
		}

		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 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);
		}

		public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent)
		{
			OnBeforeDelete?.Invoke((TestA)oldValue,dbEvent?.FunctionCall.CallInfo);
		}

		public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent)
		{
			OnDelete?.Invoke((TestA)oldValue,dbEvent?.FunctionCall.CallInfo);
		}

		public static void OnRowUpdateEvent(NetworkManager.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent)
		{
			OnRowUpdate?.Invoke(op, (TestA)oldValue,(TestA)newValue,dbEvent?.FunctionCall.CallInfo);
		}
	}
}
'''
"TestB.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
{
	public partial class TestB : IDatabaseTable
	{
		[Newtonsoft.Json.JsonProperty("foo")]
		public string Foo;

		public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType()
		{
			return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
			{
				new SpacetimeDB.SATS.ProductTypeElement("foo", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String)),
			});
		}

		public static explicit operator TestB(SpacetimeDB.SATS.AlgebraicValue value)
		{
			if (value == null) {
				return null;
			}
			var productValue = value.AsProductValue();
			return new TestB
			{
				Foo = productValue.elements[0].AsString(),
			};
		}

	}
}
'''
"TestD.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
{
	public partial class TestD : IDatabaseTable
	{
		[Newtonsoft.Json.JsonProperty("test_c")]
		[SpacetimeDB.Some]
		public SpacetimeDB.Namespace.Types.TestC TestC;

		public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType()
		{
			return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
			{
				new SpacetimeDB.SATS.ProductTypeElement("test_c", SpacetimeDB.SATS.AlgebraicType.CreateSumType(new System.Collections.Generic.List<SpacetimeDB.SATS.SumTypeVariant>
			{
				new SpacetimeDB.SATS.SumTypeVariant("some", SpacetimeDB.Namespace.GetAlgebraicTypeForTestC()),
				new SpacetimeDB.SATS.SumTypeVariant("none", SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
			{
			})),
			})),
			});
		}

		public static explicit operator TestD(SpacetimeDB.SATS.AlgebraicValue value)
		{
			if (value == null) {
				return null;
			}
			var productValue = value.AsProductValue();
			return new TestD
			{
				TestC = SpacetimeDB.Namespace.IntoTestC(productValue.elements[0].AsSumValue().tag == 1 ? null : productValue.elements[0].AsSumValue().value),
			};
		}

		public static System.Collections.Generic.IEnumerable<TestD> Iter()
		{
			foreach(var entry in NetworkManager.clientDB.GetEntries("TestD"))
			{
				yield return (TestD)entry.Item2;
			}
		}
		public static int Count()
		{
			return NetworkManager.clientDB.Count("TestD");
		}
		public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, SpacetimeDB.SATS.AlgebraicValue _v1, SpacetimeDB.SATS.AlgebraicValue _v2)
		{
			return false;
		}

		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 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);
		}

		public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent)
		{
			OnBeforeDelete?.Invoke((TestD)oldValue,dbEvent?.FunctionCall.CallInfo);
		}

		public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent)
		{
			OnDelete?.Invoke((TestD)oldValue,dbEvent?.FunctionCall.CallInfo);
		}

		public static void OnRowUpdateEvent(NetworkManager.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent)
		{
			OnRowUpdate?.Invoke(op, (TestD)oldValue,(TestD)newValue,dbEvent?.FunctionCall.CallInfo);
		}
	}
}
'''
"TestE.cs" = '''
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

using System;

namespace SpacetimeDB
using System.Collections.Generic;
{
	public partial class TestE : IDatabaseTable
	{
		[Newtonsoft.Json.JsonProperty("id")]
		public ulong Id;
		[Newtonsoft.Json.JsonProperty("name")]
		public string Name;

		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.U64)),
				new SpacetimeDB.SATS.ProductTypeElement("name", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String)),
			});
		}

		public static explicit operator TestE(SpacetimeDB.SATS.AlgebraicValue value)
		{
			if (value == null) {
				return null;
			}
			var productValue = value.AsProductValue();
			return new TestE
			{
				Id = productValue.elements[0].AsU64(),
				Name = productValue.elements[1].AsString(),
			};
		}

		public static System.Collections.Generic.IEnumerable<TestE> Iter()
		{
			foreach(var entry in NetworkManager.clientDB.GetEntries("TestE"))
			{
				yield return (TestE)entry.Item2;
			}
		}
		public static int Count()
		{
			return NetworkManager.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;
		}

		public static System.Collections.Generic.IEnumerable<TestE> FilterByName(string value)
		{
			foreach(var entry in NetworkManager.clientDB.GetEntries("TestE"))
			{
				var productValue = entry.Item1.AsProductValue();
				var compareValue = (string)productValue.elements[1].AsString();
				if (compareValue == value) {
					yield return (TestE)entry.Item2;
				}
			}
		}

		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 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 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((TestE)newValue,dbEvent?.FunctionCall.CallInfo);
		}

		public static void OnUpdateEvent(object oldValue, object newValue, ClientApi.Event dbEvent)
		{
			OnUpdate?.Invoke((TestE)oldValue,(TestE)newValue,dbEvent?.FunctionCall.CallInfo);
		}

		public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent)
		{
			OnBeforeDelete?.Invoke((TestE)oldValue,dbEvent?.FunctionCall.CallInfo);
		}

		public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent)
		{
			OnDelete?.Invoke((TestE)oldValue,dbEvent?.FunctionCall.CallInfo);
		}

		public static void OnRowUpdateEvent(NetworkManager.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent)
		{
			OnRowUpdate?.Invoke(op, (TestE)oldValue,(TestE)newValue,dbEvent?.FunctionCall.CallInfo);
		}
	}
}
'''
"TestReducer.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 TestHandler(ReducerEvent reducerEvent, SpacetimeDB.TestA arg, SpacetimeDB.TestB arg2, SpacetimeDB.Namespace.Types.TestC arg3);
		public static event TestHandler OnTestEvent;

		public static void Test(SpacetimeDB.TestA arg, SpacetimeDB.TestB arg2, SpacetimeDB.Namespace.Types.TestC arg3)
		{
			var _argArray = new object[] {arg, arg2, new EnumWrapper<SpacetimeDB.Namespace.Types.TestC>(arg3)};
			var _message = new NetworkManager.ReducerCallRequest {
				fn = "test",
				args = _argArray,
			};
			NetworkManager.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings));
		}

		[ReducerCallback(FunctionName = "test")]
		public static bool OnTest(ClientApi.Event dbEvent)
		{
			if(OnTestEvent != null)
			{
				var args = dbEvent.FunctionCall.CallInfo.TestArgs;
				OnTestEvent(dbEvent.Status, Identity.From(dbEvent.CallerIdentity.ToByteArray())
					,(SpacetimeDB.TestA)args.Arg
					,(SpacetimeDB.TestB)args.Arg2
					,(SpacetimeDB.Namespace.Types.TestC)args.Arg3
				);
				return true;
			}
			return false;
		}

		[DeserializeEvent(FunctionName = "test")]
		public static void TestDeserializeEventArgs(ClientApi.Event dbEvent)
		{
			var args = new TestArgsStruct();
			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.TestA.GetAlgebraicType(), reader);
			args.Arg = (SpacetimeDB.TestA)(args_0_value);
			var args_1_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.TestB.GetAlgebraicType(), reader);
			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);
		}
	}

	public partial class TestArgsStruct
	{
		public SpacetimeDB.TestA Arg;
		public SpacetimeDB.TestB Arg2;
		public SpacetimeDB.Namespace.Types.TestC Arg3;
	}
}
'''
"UpdateReducer.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 UpdateHandler(ReducerEvent reducerEvent);
		public static event UpdateHandler OnUpdateEvent;

		public static void Update()
		{
			var _argArray = new object[] {};
			var _message = new NetworkManager.ReducerCallRequest {
				fn = "__update__",
				args = _argArray,
			};
			NetworkManager.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings));
		}

		[ReducerCallback(FunctionName = "__update__")]
		public static bool OnUpdate(ClientApi.Event dbEvent)
		{
			if(OnUpdateEvent != null)
			{
				var args = dbEvent.FunctionCall.CallInfo.UpdateArgs;
				OnUpdateEvent(dbEvent.Status, Identity.From(dbEvent.CallerIdentity.ToByteArray())
				);
				return true;
			}
			return false;
		}

		[DeserializeEvent(FunctionName = "__update__")]
		public static void UpdateDeserializeEventArgs(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);
		}
	}

	public partial class UpdateArgsStruct
	{
	}
}
'''