generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCosmosDbContainer.cs
965 lines (852 loc) · 64.5 KB
/
CosmosDbContainer.cs
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Abstractions;
using CoreEx.Cosmos.Model;
using CoreEx.Entities;
using CoreEx.Json;
using CoreEx.Mapping;
using CoreEx.Results;
using Microsoft.Azure.Cosmos;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Stj = System.Text.Json;
namespace CoreEx.Cosmos
{
/// <summary>
/// Provides <see cref="CosmosDb"/> <see cref="Container"/> capabilities.
/// </summary>
/// <remarks>The <see cref="Model"/> property (<see cref="CosmosDbModelContainer"/>) provides the underlying capabilities for direct model-based access.</remarks>
public partial class CosmosDbContainer
{
private readonly Lazy<CosmosDbModelContainer> _model;
private Func<CosmosDbArgs>? _dbArgsFactory;
private readonly ConcurrentDictionary<(Type, Type), object> _containers = new();
private readonly ConcurrentDictionary<(Type, Type), object> _valueContainers = new();
/// <summary>
/// Initializes a new instance of the <see cref="CosmosDbContainer"/>.
/// </summary>
/// <param name="cosmosDb">The <see cref="ICosmosDb"/>.</param>
/// <param name="containerId">The <see cref="Container"/> identifier.</param>
public CosmosDbContainer(ICosmosDb cosmosDb, string containerId)
{
CosmosDb = cosmosDb.ThrowIfNull(nameof(cosmosDb));
CosmosContainer = cosmosDb.GetCosmosContainer(containerId.ThrowIfNullOrEmpty(nameof(containerId)));
_model = new(() => new(this));
}
/// <summary>
/// Gets the owning <see cref="ICosmosDb"/>.
/// </summary>
public ICosmosDb CosmosDb { get; }
/// <summary>
/// Gets the <see cref="Container"/>.
/// </summary>
public Container CosmosContainer { get; }
/// <summary>
/// Gets the Container-specific <see cref="CosmosDbArgs"/>.
/// </summary>
/// <remarks>Defaults to <see cref="UseDbArgs(Func{CosmosDbArgs})"/>; otherwise, <see cref="CosmosDb.DbArgs"/>.</remarks>
public CosmosDbArgs DbArgs => _dbArgsFactory?.Invoke() ?? new CosmosDbArgs(CosmosDb.DbArgs);
/// <summary>
/// Sets the container-specific <see cref="CosmosDbArgs"/>.
/// </summary>
/// <param name="dbArgsFactory">The <see cref="CosmosDbArgs"/> creation factory.</param>
/// <remarks>This can only be set once; otherwise, a <see cref="InvalidOperationException"/> will be thrown.</remarks>
public CosmosDbContainer UseDbArgs(Func<CosmosDbArgs> dbArgsFactory)
{
dbArgsFactory.ThrowIfNull(nameof(dbArgsFactory));
if (_dbArgsFactory is not null)
throw new InvalidOperationException($"The {nameof(UseDbArgs)} can only be specified once.");
_dbArgsFactory = dbArgsFactory;
return this;
}
/// <summary>
/// Gets the <see cref="CosmosDbModelContainer"/> that encapsulates the direct-to-model operations.
/// </summary>
public CosmosDbModelContainer Model => _model.Value;
/// <summary>
/// Gets or sets the SQL statement format for the <b>MultiSet</b> operation.
/// </summary>
/// <remarks>The SQL statement format must have the <c>{0}</c>> place holder for the list of types represented as comma-separated strings; e.g. <c>"Customer", "Address"</c>.</remarks>
public string MultiSetSqlStatementFormat { get; private set; } = "SELECT * FROM c WHERE c.type in ({0})";
/// <summary>
/// Sets the <see cref="MultiSetSqlStatementFormat"/> for the <b>MultiSet</b> operations.
/// </summary>
/// <param name="format">The SQL statement format.</param>
public CosmosDbContainer UseMultiSetSqlStatement(string format)
{
if (!format.ThrowIfNullOrEmpty(nameof(format)).Contains("{0}"))
throw new ArgumentException("The format must contain '{0}' to insert the 'in' list (contents).", nameof(format));
return this;
}
/// <summary>
/// Gets the <b>CosmosDb</b> identifier from the <see cref="CompositeKey"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <returns>The <b>CosmosDb</b> identifier.</returns>
/// <remarks>Uses the <see cref="CosmosDbArgs.FormatIdentifier"/> to format the <paramref name="key"/> as a string (as required).</remarks>
public virtual string GetCosmosId(CompositeKey key) => DbArgs.FormatIdentifier(key) ?? throw new InvalidOperationException("The CompositeKey formatting into an identifier must not result in a null.");
/// <summary>
/// Gets the <b>CosmosDb</b> identifier from the <paramref name="model"/> <see cref="IEntityKey.EntityKey"/>.
/// </summary>
/// <param name="model">The model value.</param>
/// <returns>The <b>CosmosDb</b> identifier.</returns>
public string GetCosmosId<TModel>(TModel model) where TModel : class, IEntityKey => GetCosmosId(model.ThrowIfNull(nameof(model)).EntityKey);
/// <summary>
/// Gets the <see cref="PartitionKey"/>.
/// </summary>
public PartitionKey GetPartitionKey(PartitionKey? partitionKey) => partitionKey ?? DbArgs.PartitionKey ?? PartitionKey.None;
/// <summary>
/// Sets the function to get the <see cref="PartitionKey"/> from the model used by the <see cref="CosmosDbModelContainer.GetPartitionKey{TModel}(TModel, CosmosDbArgs)"/> (used by only by the <b>Create</b> and <b>Update</b> operations).
/// </summary>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="getPartitionKey">The function to get the <see cref="PartitionKey"/> from the model.</param>
/// <remarks>This can only be set once; otherwise, a <see cref="InvalidOperationException"/> will be thrown.</remarks>
public CosmosDbContainer UsePartitionKey<TModel>(Func<TModel, PartitionKey>? getPartitionKey) where TModel : class, IEntityKey, new()
{
Model.UsePartitionKey(getPartitionKey);
return this;
}
/// <summary>
/// Sets the function to get the <see cref="PartitionKey"/> from the <see cref="CosmosDbValue{TModel}"/> used by the <see cref="CosmosDbModelContainer.GetPartitionKey{TModel}(CosmosDbValue{TModel}, CosmosDbArgs)"/> (used by only by the <b>Create</b> and <b>Update</b> operations).
/// </summary>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="getPartitionKey">The function to get the <see cref="PartitionKey"/> from the model.</param>
/// <remarks>This can only be set once; otherwise, a <see cref="InvalidOperationException"/> will be thrown.</remarks>
public CosmosDbContainer UseValuePartitionKey<TModel>(Func<CosmosDbValue<TModel>, PartitionKey>? getPartitionKey) where TModel : class, IEntityKey, new()
{
Model.UsePartitionKey(getPartitionKey);
return this;
}
/// <summary>
/// Sets (overrides) the name for the model <see cref="Type"/>.
/// </summary>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="name">The model name.</param>
public CosmosDbContainer UseModelName<TModel>(string name) where TModel : class, IEntityKey, new()
{
Model.UseModelName<TModel>(name);
return this;
}
/// <summary>
/// Sets the filter for all operations performed on the <typeparamref name="TModel"/> to ensure authorisation is applied. Applies automatically to all queries, plus create, update, delete and get operations.
/// </summary>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="filter">The authorization filter query.</param>
public CosmosDbContainer UseAuthorizeFilter<TModel>(Func<IQueryable<TModel>, IQueryable<TModel>>? filter) where TModel : class, IEntityKey, new()
{
Model.UseAuthorizeFilter(filter);
return this;
}
/// <summary>
/// Sets the filter for all operations performed on the <see cref="CosmosDbValue{TModel}"/> to ensure authorisation is applied. Applies automatically to all queries, plus create, update, delete and get operations.
/// </summary>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="filter">The authorization filter query.</param>
public CosmosDbContainer UseValueAuthorizeFilter<TModel>(Func<IQueryable<CosmosDbValue<TModel>>, IQueryable<CosmosDbValue<TModel>>>? filter) where TModel : class, IEntityKey, new()
{
Model.UseAuthorizeFilter(filter);
return this;
}
/// <summary>
/// Maps <paramref name="model"/> to the entity <b>value</b> formatting/updating any special properties as required.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="model">The model value.</param>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <returns>The entity value.</returns>
[return: NotNullIfNotNull(nameof(model))]
public T? MapToValue<T, TModel>(TModel? model, CosmosDbArgs dbArgs) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
if (model is null)
return default;
// Map the model to the entity value.
var val = CosmosDb.Mapper.Map<TModel, T>(model, OperationTypes.Get)!;
if (dbArgs.AutoMapETag && val is IETag et && et.ETag != null)
et.ETag = ETagGenerator.ParseETag(et.ETag);
return dbArgs.CleanUpResult ? Cleaner.Clean(val) : val;
}
/// <summary>
/// Maps <paramref name="model"/> to the entity <b>value</b> formatting/updating any special properties as required.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="model">The <see cref="CosmosDbValue{TModel}"/> value.</param>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <returns>The entity value.</returns>
[return: NotNullIfNotNull(nameof(model))]
public T? MapToValue<T, TModel>(CosmosDbValue<TModel>? model, CosmosDbArgs dbArgs) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
if (model is null)
return default;
((ICosmosDbValue)model).PrepareAfter(dbArgs);
var val = CosmosDb.Mapper.Map<TModel, T>(model.Value, OperationTypes.Get)!;
if (dbArgs.AutoMapETag && val is IETag et)
{
if (et.ETag is not null)
et.ETag = ETagGenerator.ParseETag(et.ETag);
else
et.ETag = ETagGenerator.ParseETag(model.ETag);
}
return DbArgs.CleanUpResult ? Cleaner.Clean(val) : val;
}
/// <summary>
/// Gets (or adds) the typed <see cref="CosmosDbContainer{T, TModel}"/> for the specified <typeparamref name="T"/> and <typeparamref name="TModel"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="configure">An optional action to perform one-off configuration on initial access.</param>
/// <returns>The typed <see cref="CosmosDbContainer{T, TModel}"/></returns>
public CosmosDbContainer<T, TModel> AsTyped<T, TModel>(Action<CosmosDbContainer<T, TModel>>? configure = null) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (CosmosDbContainer<T, TModel>)_containers.GetOrAdd((typeof(T), typeof(TModel)), _ =>
{
var c = new CosmosDbContainer<T, TModel>(this);
configure?.Invoke(c);
return c;
});
/// <summary>
/// Gets (or adds) the typed <see cref="CosmosDbValueContainer{T, TModel}"/> for the specified <typeparamref name="T"/> and <typeparamref name="TModel"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="configure">An optional action to perform one-off configuration on initial access.</param>
/// <returns>The typed <see cref="CosmosDbValueContainer{T, TModel}"/></returns>
public CosmosDbValueContainer<T, TModel> AsValueTyped<T, TModel>(Action<CosmosDbValueContainer<T, TModel>>? configure = null) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (CosmosDbValueContainer<T, TModel>)_valueContainers.GetOrAdd((typeof(T), typeof(TModel)), _ =>
{
var c = new CosmosDbValueContainer<T, TModel>(this);
configure?.Invoke(c);
return c;
});
#region Query
/// <summary>
/// Gets (creates) a <see cref="CosmosDbQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbQuery{T, TModel}"/>.</returns>
public CosmosDbQuery<T, TModel> Query<T, TModel>(Func<IQueryable<TModel>, IQueryable<TModel>>? query) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> Query<T, TModel>(new CosmosDbArgs(DbArgs), query);
/// <summary>
/// Gets (creates) a <see cref="CosmosDbQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbQuery{T, TModel}"/>.</returns>
public CosmosDbQuery<T, TModel> Query<T, TModel>(PartitionKey? partitionKey = null, Func<IQueryable<TModel>, IQueryable<TModel>>? query = null) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> Query<T, TModel>(new CosmosDbArgs(DbArgs, partitionKey), query);
/// <summary>
/// Gets (creates) a <see cref="CosmosDbQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbQuery{T, TModel}"/>.</returns>
public CosmosDbQuery<T, TModel> Query<T, TModel>(CosmosDbArgs dbArgs, Func<IQueryable<TModel>, IQueryable<TModel>>? query = null) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> new(this, dbArgs, query);
/// <summary>
/// Gets (creates) a <see cref="CosmosDbValueQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbValueQuery{T, TModel}"/>.</returns>
public CosmosDbValueQuery<T, TModel> ValueQuery<T, TModel>(Func<IQueryable<CosmosDbValue<TModel>>, IQueryable<CosmosDbValue<TModel>>>? query) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> ValueQuery<T, TModel>(new CosmosDbArgs(DbArgs), query);
/// <summary>
/// Gets (creates) a <see cref="CosmosDbValueQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbValueQuery{T, TModel}"/>.</returns>
public CosmosDbValueQuery<T, TModel> ValueQuery<T, TModel>(PartitionKey? partitionKey = null, Func<IQueryable<CosmosDbValue<TModel>>, IQueryable<CosmosDbValue<TModel>>>? query = null) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> ValueQuery<T, TModel>(new CosmosDbArgs(DbArgs, partitionKey), query);
/// <summary>
/// Gets (creates) a <see cref="CosmosDbValueQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbValueQuery{T, TModel}"/>.</returns>
public CosmosDbValueQuery<T, TModel> ValueQuery<T, TModel>(CosmosDbArgs dbArgs, Func<IQueryable<CosmosDbValue<TModel>>, IQueryable<CosmosDbValue<TModel>>>? query = null) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> new(this, dbArgs, query);
#endregion
#region Get
/// <summary>
/// Gets the entity for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public async Task<T?> GetAsync<T, TModel>(CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await GetWithResultAsync<T, TModel>(key, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Gets the entity for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<Result<T?>> GetWithResultAsync<T, TModel>(CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> GetWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs), key, cancellationToken);
/// <summary>
/// Gets the entity for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public async Task<T?> GetAsync<T, TModel>(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await GetWithResultAsync<T, TModel>(key, partitionKey, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Gets the entity for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<Result<T?>> GetWithResultAsync<T, TModel>(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> GetWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs, partitionKey), key, cancellationToken);
/// <summary>
/// Gets the entity for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public async Task<T?> GetAsync<T, TModel>(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await GetWithResultAsync<T, TModel>(dbArgs, key, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Gets the entity for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public async Task<Result<T?>> GetWithResultAsync<T, TModel>(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
var result = await Model.GetWithResultAsync<TModel>(dbArgs, key, cancellationToken).ConfigureAwait(false);
return result.ThenAs(m => MapToValue<T, TModel>(m, dbArgs));
}
/// <summary>
/// Gets the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public async Task<T?> GetValueAsync<T, TModel>(CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await GetValueWithResultAsync<T, TModel>(key, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Gets the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<Result<T?>> GetValueWithResultAsync<T, TModel>(CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> GetValueWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs), key, cancellationToken);
/// <summary>
/// Gets the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public async Task<T?> GetValueAsync<T, TModel>(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await GetValueWithResultAsync<T, TModel>(key, partitionKey, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Gets the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<Result<T?>> GetValueWithResultAsync<T, TModel>(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> GetValueWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs, partitionKey), key, cancellationToken);
/// <summary>
/// Gets the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/>..
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public async Task<T?> GetValueAsync<T, TModel>(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await GetValueWithResultAsync<T, TModel>(dbArgs, key, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Gets the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public async Task<Result<T?>> GetValueWithResultAsync<T, TModel>(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
var result = await Model.GetValueWithResultAsync<TModel>(dbArgs, key, cancellationToken).ConfigureAwait(false);
return result.ThenAs(m => MapToValue<T, TModel>(m, dbArgs));
}
#endregion
#region Create
/// <summary>
/// Creates the entity.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public async Task<T> CreateAsync<T, TModel>(T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await CreateWithResultAsync<T, TModel>(value, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Creates the entity with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public Task<Result<T>> CreateWithResultAsync<T, TModel>(T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> CreateWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs), value, cancellationToken);
/// <summary>
/// Creates the entity.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public async Task<T> CreateAsync<T, TModel>(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await CreateWithResultAsync<T, TModel>(dbArgs, value, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Creates the entity with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public async Task<Result<T>> CreateWithResultAsync<T, TModel>(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
TModel model = CosmosDb.Mapper.Map<T, TModel>(Cleaner.PrepareCreate(value.ThrowIfNull(nameof(value))), OperationTypes.Create)!;
var result = await Model.CreateWithResultAsync(dbArgs, Cleaner.PrepareCreate(model), cancellationToken).ConfigureAwait(false);
return result.ThenAs(model => MapToValue<T, TModel>(model, dbArgs)!);
}
/// <summary>
/// Creates the entity (using underlying <see cref="CosmosDbValue{TModel}"/>).
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public async Task<T> CreateValueAsync<T, TModel>(T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await CreateValueWithResultAsync<T, TModel>(value, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Creates the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public Task<Result<T>> CreateValueWithResultAsync<T, TModel>(T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> CreateValueWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs), value, cancellationToken);
/// <summary>
/// Creates the entity (using underlying <see cref="CosmosDbValue{TModel}"/>).
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public async Task<T> CreateValueAsync<T, TModel>(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await CreateValueWithResultAsync<T, TModel>(dbArgs, value, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Creates the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public async Task<Result<T>> CreateValueWithResultAsync<T, TModel>(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
TModel model = CosmosDb.Mapper.Map<T, TModel>(Cleaner.PrepareCreate(value.ThrowIfNull(nameof(value))), OperationTypes.Create);
var cdv = new CosmosDbValue<TModel>(Model.GetModelName<TModel>(), Cleaner.PrepareCreate(model)!);
var result = await Model.CreateValueWithResultAsync(dbArgs, cdv, cancellationToken).ConfigureAwait(false);
return result.ThenAs(model => MapToValue<T, TModel>(model, dbArgs)!);
}
#endregion
#region Update
/// <summary>
/// Updates the entity.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public async Task<T> UpdateAsync<T, TModel>(T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await UpdateWithResultAsync<T, TModel>(value, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Updates the entity with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public Task<Result<T>> UpdateWithResultAsync<T, TModel>(T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> UpdateWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs), value, cancellationToken);
/// <summary>
/// Updates the entity.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public async Task<T> UpdateAsync<T, TModel>(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await UpdateWithResultAsync<T, TModel>(dbArgs, value, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Updates the entity with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public async Task<Result<T>> UpdateWithResultAsync<T, TModel>(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
var model = CosmosDb.Mapper.Map<T, TModel>(Cleaner.PrepareUpdate(value.ThrowIfNull(nameof(value))), OperationTypes.Update);
var result = await Model.UpdateWithResultInternalAsync(dbArgs, Cleaner.PrepareUpdate(model), m => CosmosDb.Mapper.Map(value, m, OperationTypes.Update), cancellationToken).ConfigureAwait(false);
return result.ThenAs(model => MapToValue<T, TModel>(model, dbArgs)!);
}
/// <summary>
/// Updates the entity (using underlying <see cref="CosmosDbValue{TModel}"/>).
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public async Task<T> UpdateValueAsync<T, TModel>(T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await UpdateValueWithResultAsync<T, TModel>(value, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Updates the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public Task<Result<T>> UpdateValueWithResultAsync<T, TModel>(T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> UpdateValueWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs), value, cancellationToken);
/// <summary>
/// Updates the entity (using underlying <see cref="CosmosDbValue{TModel}"/>).
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public async Task<T> UpdateValueAsync<T, TModel>(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await UpdateValueWithResultAsync<T, TModel>(dbArgs, value, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Updates the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public async Task<Result<T>> UpdateValueWithResultAsync<T, TModel>(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
var model = CosmosDb.Mapper.Map<T, TModel>(Cleaner.PrepareUpdate(value.ThrowIfNull(nameof(value))), OperationTypes.Update)!;
var cdv = new CosmosDbValue<TModel>(Model.GetModelName<TModel>(), Cleaner.PrepareUpdate(model!));
var result = await Model.UpdateValueWithResultInternalAsync<TModel>(dbArgs, cdv, cdv => CosmosDb.Mapper.Map(value, cdv.Value, OperationTypes.Update), cancellationToken).ConfigureAwait(false);
return result.ThenAs(model => MapToValue<T, TModel>(model, dbArgs)!);
}
#endregion
#region Delete
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public async Task DeleteAsync<T, TModel>(CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await DeleteWithResultAsync<T, TModel>(key, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteWithResultAsync<T, TModel>(CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> DeleteWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs), key, cancellationToken);
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public async Task DeleteAsync<T, TModel>(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await DeleteWithResultAsync<T, TModel>(key, partitionKey, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteWithResultAsync<T, TModel>(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> DeleteWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs, partitionKey), key, cancellationToken);
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>..</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public async Task DeleteAsync<T, TModel>(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await DeleteWithResultAsync<T, TModel>(dbArgs, key, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>..</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteWithResultAsync<T, TModel>(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> Model.DeleteWithResultAsync<TModel>(dbArgs, key, cancellationToken);
/// <summary>
/// Deletes the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public async Task DeleteValueAsync<T, TModel>(CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await DeleteValueWithResultAsync<T, TModel>(key, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Deletes the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteValueWithResultAsync<T, TModel>(CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> DeleteValueWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs), key, cancellationToken);
/// <summary>
/// Deletes the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public async Task DeleteValueAsync<T, TModel>(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await DeleteValueWithResultAsync<T, TModel>(key, partitionKey, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Deletes the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteValueWithResultAsync<T, TModel>(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> DeleteValueWithResultAsync<T, TModel>(new CosmosDbArgs(DbArgs, partitionKey), key, cancellationToken);
/// <summary>
/// Deletes the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>..</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public async Task DeleteValueAsync<T, TModel>(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> (await DeleteValueWithResultAsync<T, TModel>(dbArgs, key, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Deletes the entity (using underlying <see cref="CosmosDbValue{TModel}"/>) for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>..</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteValueWithResultAsync<T, TModel>(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
=> Model.DeleteValueWithResultAsync<TModel>(dbArgs, key, cancellationToken);
#endregion
#region MultiSet
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetValueArgs"/>.
/// </summary>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetValueArgs"/>.</param>
/// <remarks>See <see cref="SelectValueMultiSetWithResultAsync(PartitionKey, string?, IEnumerable{IMultiSetValueArgs}, CancellationToken)"/> for further details.</remarks>
public Task SelectValueMultiSetAsync(PartitionKey partitionKey, params IMultiSetValueArgs[] multiSetArgs) => SelectValueMultiSetAsync(partitionKey, multiSetArgs, default);
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetValueArgs"/>.
/// </summary>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetValueArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <remarks>See <see cref="SelectValueMultiSetWithResultAsync(PartitionKey, string?, IEnumerable{IMultiSetValueArgs}, CancellationToken)"/> for further details.</remarks>
public Task SelectValueMultiSetAsync(PartitionKey partitionKey, IEnumerable<IMultiSetValueArgs> multiSetArgs, CancellationToken cancellationToken = default) => SelectValueMultiSetAsync(partitionKey, null, multiSetArgs, cancellationToken);
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetValueArgs"/>.
/// </summary>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="sql">The override SQL statement; will default where not specified.</param>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetValueArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <remarks>See <see cref="SelectValueMultiSetWithResultAsync(PartitionKey, string?, IEnumerable{IMultiSetValueArgs}, CancellationToken)"/> for further details.</remarks>
public async Task SelectValueMultiSetAsync(PartitionKey partitionKey, string? sql, IEnumerable<IMultiSetValueArgs> multiSetArgs, CancellationToken cancellationToken = default)
=> (await SelectValueMultiSetWithResultAsync(partitionKey, sql, multiSetArgs, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetValueArgs"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetValueArgs"/>.</param>
/// <remarks>See <see cref="SelectValueMultiSetWithResultAsync(PartitionKey, string?, IEnumerable{IMultiSetValueArgs}, CancellationToken)"/> for further details.</remarks>
public Task<Result> SelectValueMultiSetWithResultAsync(PartitionKey partitionKey, params IMultiSetValueArgs[] multiSetArgs) => SelectValueMultiSetWithResultAsync(partitionKey, multiSetArgs, default);
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetValueArgs"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetValueArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <remarks>See <see cref="SelectValueMultiSetWithResultAsync(PartitionKey, string?, IEnumerable{IMultiSetValueArgs}, CancellationToken)"/> for further details.</remarks>
public Task<Result> SelectValueMultiSetWithResultAsync(PartitionKey partitionKey, IEnumerable<IMultiSetValueArgs> multiSetArgs, CancellationToken cancellationToken = default) => SelectValueMultiSetWithResultAsync(partitionKey, null, multiSetArgs, cancellationToken);
/// <summary>
/// Executes a multi-dataset query command with one or more <see cref="IMultiSetValueArgs"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="sql">The override SQL statement; will default to <see cref="MultiSetSqlStatementFormat"/> where not specified.</param>
/// <param name="multiSetArgs">One or more <see cref="IMultiSetValueArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <remarks>The <paramref name="multiSetArgs"/> must be of type <see cref="CosmosDbValue{TModel}"/>. Each <paramref name="multiSetArgs"/> is verified and executed in the order specified.
/// <para>The underlying SQL will be automatically created from the specified <paramref name="multiSetArgs"/> where not explicitly supplied. Essentially, it is a simple query where all <i>types</i> inferred from the <paramref name="multiSetArgs"/>
/// are included, for example: <c>SELECT * FROM c WHERE c.type in ("TypeNameA", "TypeNameB")</c></para>
/// </remarks>
public async Task<Result> SelectValueMultiSetWithResultAsync(PartitionKey partitionKey, string? sql, IEnumerable<IMultiSetValueArgs> multiSetArgs, CancellationToken cancellationToken = default)
{
// Verify that the multi set arguments are valid for this type of get query.
var multiSetList = multiSetArgs?.ToArray() ?? null;
if (multiSetList == null || multiSetList.Length == 0)
throw new ArgumentException($"At least one {nameof(IMultiSetValueArgs)} must be supplied.", nameof(multiSetArgs));
// Build the Cosmos SQL statement.
var name = multiSetList[0].GetModelName(this);
var types = new Dictionary<string, IMultiSetValueArgs>([new KeyValuePair<string, IMultiSetValueArgs>(name, multiSetList[0])]);
var sb = string.IsNullOrEmpty(sql) ? new StringBuilder($"\"{name}\"") : null;
if (sb is not null)
{
for (int i = 1; i < multiSetList.Length; i++)
{
name = multiSetList[i].GetModelName(this);
if (!types.TryAdd(name, multiSetList[i]))
throw new ArgumentException($"All {nameof(IMultiSetValueArgs)} must be of different model type.", nameof(multiSetArgs));
sb.Append($", \"{name}\"");
}
sql = string.Format(MultiSetSqlStatementFormat, sb.ToString());
}
// Execute the Cosmos DB query.
var result = await CosmosDb.Invoker.InvokeAsync(CosmosDb, this, sql, types, async (_, container, sql, types, ct) =>
{
// Set up for work.
var da = new CosmosDbArgs(container.DbArgs, partitionKey);
var qsi = container.CosmosContainer.GetItemQueryStreamIterator(sql, requestOptions: da.GetQueryRequestOptions());
IJsonSerializer js = ExecutionContext.GetService<IJsonSerializer>() ?? CoreEx.Json.JsonSerializer.Default;
var isStj = js is Text.Json.JsonSerializer;
while (qsi.HasMoreResults)
{
var rm = await qsi.ReadNextAsync(ct).ConfigureAwait(false);
if (!rm.IsSuccessStatusCode)
return Result.Fail(new InvalidOperationException(rm.ErrorMessage));
var json = Stj.JsonDocument.Parse(rm.Content);
if (!json.RootElement.TryGetProperty("Documents", out var jds) || jds.ValueKind != Stj.JsonValueKind.Array)
return Result.Fail(new InvalidOperationException("Cosmos response JSON 'Documents' property either not found in result or is not an array."));
foreach (var jd in jds.EnumerateArray())
{
if (!jd.TryGetProperty("type", out var jt) || jt.ValueKind != Stj.JsonValueKind.String)
return Result.Fail(new InvalidOperationException("Cosmos response documents item 'type' property either not found in result or is not a string."));
if (!types.TryGetValue(jt.GetString()!, out var msa))
continue; // Ignore any unexpected type.
var model = isStj
? jd.Deserialize(msa.Type, (Stj.JsonSerializerOptions)js.Options)
: js.Deserialize(jd.ToString(), msa.Type);
if (model is null)
return Result.Fail(new InvalidOperationException($"Cosmos response documents item type '{jt.GetRawText()}' deserialization resulted in a null."));
var result = msa.AddItem(container, da, model);
if (result.IsFailure)
return result;
}
}
return Result.Success;
}, cancellationToken).ConfigureAwait(false);
if (result.IsFailure)
return result;
// Validate the multi-set args and action each accordingly.
foreach (var msa in multiSetList)
{
var r = msa.Verify();
if (r.IsFailure)
return r.AsResult();
if (!r.Value && msa.StopOnNull)
break;
msa.Invoke();
}
return Result.Success;
}
#endregion
}
}