Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
IClient.cs
Go to the documentation of this file.
1using System.Runtime.CompilerServices;
2using Fauna.Core;
4using Fauna.Mapping;
6using Fauna.Types;
7using static Fauna.Query;
8
9namespace Fauna;
10
14public interface IClient
15{
35 public Task<QuerySuccess<T>> QueryAsync<T>(
36 Query query,
37 QueryOptions? queryOptions = null,
38 CancellationToken cancel = default)
39 where T : notnull;
40
59 public Task<QuerySuccess<object?>> QueryAsync(
60 Query query,
61 QueryOptions? queryOptions = null,
62 CancellationToken cancel = default);
63
84 public Task<QuerySuccess<T>> QueryAsync<T>(
85 Query query,
86 ISerializer<T> serializer,
87 QueryOptions? queryOptions = null,
88 CancellationToken cancel = default);
89
109 public Task<QuerySuccess<object?>> QueryAsync(
110 Query query,
111 ISerializer serializer,
112 QueryOptions? queryOptions = null,
113 CancellationToken cancel = default);
114
137 public IAsyncEnumerable<Page<T>> PaginateAsync<T>(
138 Query query,
139 QueryOptions? queryOptions = null,
140 CancellationToken cancel = default)
141 where T : notnull;
142
166 public IAsyncEnumerable<Page<T>> PaginateAsync<T>(
167 Page<T> page,
168 QueryOptions? queryOptions = null,
169 CancellationToken cancel = default)
170 where T : notnull;
171
193 public IAsyncEnumerable<Page<object?>> PaginateAsync(
194 Query query,
195 QueryOptions? queryOptions = null,
196 CancellationToken cancel = default);
197
220 public IAsyncEnumerable<Page<object?>> PaginateAsync(
221 Page<object?> page,
222 QueryOptions? queryOptions = null,
223 CancellationToken cancel = default);
224
248 public IAsyncEnumerable<Page<T>> PaginateAsync<T>(
249 Query query,
250 ISerializer<T> elemSerializer,
251 QueryOptions? queryOptions = null,
252 CancellationToken cancel = default);
253
278 public IAsyncEnumerable<Page<T>> PaginateAsync<T>(
279 Page<T> page,
280 ISerializer<T> elemSerializer,
281 QueryOptions? queryOptions = null,
282 CancellationToken cancel = default);
283
306 public IAsyncEnumerable<Page<object?>> PaginateAsync(
307 Query query,
308 ISerializer elemSerializer,
309 QueryOptions? queryOptions = null,
310 CancellationToken cancel = default);
311
335 public IAsyncEnumerable<Page<object?>> PaginateAsync(
336 Page<object?> page,
337 ISerializer elemSerializer,
338 QueryOptions? queryOptions = null,
339 CancellationToken cancel = default);
340
360 public Task<T> LoadRefAsync<T>(
361 BaseRef<T> reference,
362 CancellationToken cancel = default)
363 where T : notnull;
364}
365
369public abstract class BaseClient : IClient
370{
371 internal BaseClient()
372 {
373 }
374
375 internal abstract MappingContext MappingCtx { get; }
376
377 internal abstract Task<QuerySuccess<T>> QueryAsyncInternal<T>(
378 Query query,
379 ISerializer<T> serializer,
380 MappingContext ctx,
381 QueryOptions? queryOptions,
382 CancellationToken cancel
383 );
384
385 #region IClient
386
388 public Task<QuerySuccess<T>> QueryAsync<T>(
389 Query query,
390 QueryOptions? queryOptions = null,
391 CancellationToken cancel = default)
392 where T : notnull =>
393 QueryAsync<T>(query, Serializer.Generate<T>(MappingCtx), queryOptions, cancel);
394
396 public Task<QuerySuccess<object?>> QueryAsync(
397 Query query,
398 QueryOptions? queryOptions = null,
399 CancellationToken cancel = default) =>
400 QueryAsync<object?>(query, Serializer.Dynamic, queryOptions, cancel);
401
403 public Task<QuerySuccess<T>> QueryAsync<T>(
404 Query query,
405 ISerializer<T> serializer,
406 QueryOptions? queryOptions = null,
407 CancellationToken cancel = default) =>
408 QueryAsyncInternal(query, serializer, MappingCtx, queryOptions, cancel);
409
411 public Task<QuerySuccess<object?>> QueryAsync(
412 Query query,
413 ISerializer serializer,
414 QueryOptions? queryOptions = null,
415 CancellationToken cancel = default) =>
416 QueryAsync<object?>(query, (ISerializer<object?>)serializer, queryOptions, cancel);
417
419 public IAsyncEnumerable<Page<T>> PaginateAsync<T>(
420 Query query,
421 QueryOptions? queryOptions = null,
422 CancellationToken cancel = default)
423 where T : notnull =>
424 PaginateAsync(query, Serializer.Generate<T>(MappingCtx), queryOptions, cancel);
425
427 public IAsyncEnumerable<Page<T>> PaginateAsync<T>(
428 Page<T> page,
429 QueryOptions? queryOptions = null,
430 CancellationToken cancel = default)
431 where T : notnull =>
432 PaginateAsync(page, Serializer.Generate<T>(MappingCtx), queryOptions, cancel);
433
435 public IAsyncEnumerable<Page<object?>> PaginateAsync(
436 Query query,
437 QueryOptions? queryOptions = null,
438 CancellationToken cancel = default) =>
439 PaginateAsync(query, Serializer.Dynamic, queryOptions, cancel);
440
442 public IAsyncEnumerable<Page<object?>> PaginateAsync(
443 Page<object?> page,
444 QueryOptions? queryOptions = null,
445 CancellationToken cancel = default) =>
446 PaginateAsync(page, Serializer.Dynamic, queryOptions, cancel);
447
449 public IAsyncEnumerable<Page<T>> PaginateAsync<T>(
450 Query query,
451 ISerializer<T> elemSerializer,
452 QueryOptions? queryOptions = null,
453 CancellationToken cancel = default)
454 {
455 var serializer = new PageSerializer<T>(elemSerializer);
456 return PaginateAsyncInternal(query, serializer, queryOptions, cancel);
457 }
458
460 public IAsyncEnumerable<Page<T>> PaginateAsync<T>(
461 Page<T> page,
462 ISerializer<T> elemSerializer,
463 QueryOptions? queryOptions = null,
464 CancellationToken cancel = default)
465 {
466 var serializer = new PageSerializer<T>(elemSerializer);
467 return PaginateAsyncInternal(page, serializer, queryOptions, cancel);
468 }
469
471 public IAsyncEnumerable<Page<object?>> PaginateAsync(
472 Query query,
473 ISerializer elemSerializer,
474 QueryOptions? queryOptions = null,
475 CancellationToken cancel = default)
476 {
477 var elemObjSer = (ISerializer<object?>)elemSerializer;
478 var serializer = new PageSerializer<object?>(elemObjSer);
479 return PaginateAsyncInternal(query, serializer, queryOptions, cancel);
480 }
481
483 public IAsyncEnumerable<Page<object?>> PaginateAsync(
484 Page<object?> page,
485 ISerializer elemSerializer,
486 QueryOptions? queryOptions = null,
487 CancellationToken cancel = default)
488 {
489 var elemObjSer = (ISerializer<object?>)elemSerializer;
490 var serializer = new PageSerializer<object?>(elemObjSer);
491 return PaginateAsyncInternal(page, serializer, queryOptions, cancel);
492 }
493
495 public async Task<T> LoadRefAsync<T>(
496 BaseRef<T> reference,
497 CancellationToken cancel = default) where T : notnull
498 {
499 if (reference.IsLoaded)
500 {
501 return reference.Get();
502 }
503
504 var q = FQL($"{reference}");
505 var res = await QueryAsync(q, Serializer.Generate<BaseRef<T>>(MappingCtx), null, cancel);
506 return res.Data.Get();
507 }
508
509 #endregion
510
511 // Internally accessible for QuerySource use
512 internal async IAsyncEnumerable<Page<T>> PaginateAsyncInternal<T>(
513 Query query,
514 PageSerializer<T> serializer,
515 QueryOptions? queryOptions,
516 [EnumeratorCancellation] CancellationToken cancel = default)
517 {
518 var p = await QueryAsyncInternal(query,
519 serializer,
520 MappingCtx,
521 queryOptions,
522 cancel);
523
524 await foreach (var page in PaginateAsyncInternal(p.Data, serializer, queryOptions, cancel))
525 {
526 yield return page;
527 }
528 }
529
530 private async IAsyncEnumerable<Page<T>> PaginateAsyncInternal<T>(
531 Page<T> page,
532 PageSerializer<T> serializer,
533 QueryOptions? queryOptions,
534 [EnumeratorCancellation] CancellationToken cancel = default)
535 {
536 yield return page;
537
538 while (page.After is not null)
539 {
540 var q = new QueryExpr(new QueryLiteral($"Set.paginate('{page.After}')"));
541
542 var response = await QueryAsyncInternal(q,
543 serializer,
544 MappingCtx,
545 queryOptions,
546 cancel);
547
548 page = response.Data;
549 yield return page;
550 }
551 }
552
553 #region Streaming
554
564 internal abstract IAsyncEnumerator<Event<T>> SubscribeStreamInternal<T>(
565 EventSource eventSource,
566 MappingContext ctx,
567 CancellationToken cancel = default) where T : notnull;
568
578 public async Task<StreamEnumerable<T>> EventStreamAsync<T>(
579 Query query,
580 QueryOptions? queryOptions = null,
581 StreamOptions? streamOptions = null,
582 CancellationToken cancellationToken = default) where T : notnull
583 {
584 EventSource eventSource = streamOptions?.Token != null
585 ? new EventSource(streamOptions.Token) { Options = streamOptions }
586 : await GetEventSourceFromQueryAsync(query, queryOptions, cancellationToken);
587
588 return new StreamEnumerable<T>(this, eventSource, cancellationToken);
589 }
590
598 public async Task<StreamEnumerable<T>> EventStreamAsync<T>(
599 EventSource eventSource,
600 CancellationToken cancellationToken = default) where T : notnull
601 {
602 await Task.CompletedTask;
603
604 return new StreamEnumerable<T>(this, eventSource, cancellationToken);
605 }
606
607
617 internal abstract IAsyncEnumerator<FeedPage<T>> SubscribeFeedInternal<T>(
618 EventSource eventSource,
619 MappingContext ctx,
620 CancellationToken cancel = default) where T : notnull;
621
630 public async Task<FeedEnumerable<T>> EventFeedAsync<T>(
631 EventSource eventSource,
632 FeedOptions? feedOptions = null,
633 CancellationToken cancellationToken = default) where T : notnull
634 {
635 await Task.CompletedTask;
636
637 if (feedOptions != null) eventSource.Options = feedOptions;
638
639 return new FeedEnumerable<T>(this, eventSource, cancellationToken);
640 }
641
650 public async Task<FeedEnumerable<T>> EventFeedAsync<T>(
651 Query query,
652 FeedOptions? feedOptions = null,
653 CancellationToken cancellationToken = default) where T : notnull
654 {
655 EventSource eventSource = await GetEventSourceFromQueryAsync(query, null, cancellationToken);
656 if (feedOptions != null) eventSource.Options = feedOptions;
657
658 return new FeedEnumerable<T>(this, eventSource, cancellationToken);
659 }
660
668 private async Task<EventSource> GetEventSourceFromQueryAsync(
669 Query query,
670 QueryOptions? queryOptions,
671 CancellationToken cancellationToken)
672 {
673 try
674 {
675 var response = await QueryAsync<EventSource>(
676 query,
677 queryOptions,
678 cancellationToken);
679 return response.Data;
680 }
681 catch (SerializationException ex)
682 {
683 throw new InvalidOperationException("Query must return an EventSource.", ex);
684 }
685 }
686
695 public IAsyncEnumerator<Event<T>> SubscribeStream<T>(
696 EventSource eventSource,
697 MappingContext ctx,
698 CancellationToken cancel = default) where T : notnull
699 {
700 return SubscribeStreamInternal<T>(eventSource, ctx, cancel);
701 }
702
711 public IAsyncEnumerator<FeedPage<T>> SubscribeFeed<T>(
712 EventSource eventSource,
713 MappingContext ctx,
714 CancellationToken cancel = default) where T : notnull
715 {
716 return SubscribeFeedInternal<T>(eventSource, ctx, cancel);
717 }
718
719 #endregion
720}
The base class for Client and DataContext.
Definition IClient.cs:370
IAsyncEnumerable< Page< T > > PaginateAsync< T >(Query query, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
IAsyncEnumerator< Event< T > > SubscribeStream< T >(EventSource eventSource, MappingContext ctx, CancellationToken cancel=default)
Opens the stream with Fauna and returns an enumerator for the stream events.
Definition IClient.cs:695
async Task< FeedEnumerable< T > > EventFeedAsync< T >(EventSource eventSource, FeedOptions? feedOptions=null, CancellationToken cancellationToken=default)
Opens the event feed with Fauna and returns an enumerator for the events.
Definition IClient.cs:630
IAsyncEnumerable< Page< object?> > PaginateAsync(Query query, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
IAsyncEnumerable< Page< object?> > PaginateAsync(Query query, ISerializer elemSerializer, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
Definition IClient.cs:471
async Task< StreamEnumerable< T > > EventStreamAsync< T >(Query query, QueryOptions? queryOptions=null, StreamOptions? streamOptions=null, CancellationToken cancellationToken=default)
Retrieves a Stream token from Fauna and returns a StreamEnumerable for the stream events.
Definition IClient.cs:578
async Task< T > LoadRefAsync< T >(BaseRef< T > reference, CancellationToken cancel=default)
Asynchronously executes a specified FQL query against the Fauna database and returns the typed result...
Definition IClient.cs:495
Task< QuerySuccess< object?> > QueryAsync(Query query, ISerializer serializer, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously executes a specified FQL query against the Fauna database and returns the typed result...
IAsyncEnumerable< Page< object?> > PaginateAsync(Page< object?> page, ISerializer elemSerializer, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
Definition IClient.cs:483
Task< QuerySuccess< object?> > QueryAsync(Query query, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously executes a specified FQL query against the Fauna database.A Task representing the asyn...
Task< QuerySuccess< T > > QueryAsync< T >(Query query, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously executes a specified FQL query against the Fauna database and returns the typed result...
IAsyncEnumerable< Page< object?> > PaginateAsync(Page< object?> page, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
IAsyncEnumerator< FeedPage< T > > SubscribeFeed< T >(EventSource eventSource, MappingContext ctx, CancellationToken cancel=default)
Opens an event feed with Fauna and returns an enumerator for the events.
Definition IClient.cs:711
Represents a Fauna Event Feed.
Represents the options when subscribing to Fauna Event Feeds.
Definition FeedOptions.cs:9
Represents the options for customizing Fauna queries.
A class representing a Fauna Event Stream. Additional queries will be made during enumeration.
Represents error that occur during serialization and deserialization of Fauna data.
A class representing the mapping context to be used during serialization and deserialization.
Represents an FQL query expression. This class encapsulates a list of IQueryFragment instances,...
Definition QueryExpr.cs:11
Represents the abstract base class for constructing FQL queries.
Definition Query.cs:11
Represents a literal part of an FQL query. This class is used for embedding raw string values directl...
Represents the options when subscribing to Fauna Event Streams.
An abstract class representing a reference that can wrap an instance of the referenced document.
Definition BaseRef.cs:10
Represents a Fauna EventSource for initializing Streams and Feeds.
Definition EventSource.cs:9
Represents a client for interacting with a Fauna.
Definition IClient.cs:15
IAsyncEnumerable< Page< T > > PaginateAsync< T >(Query query, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
IAsyncEnumerable< Page< object?> > PaginateAsync(Page< object?> page, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
IAsyncEnumerable< Page< object?> > PaginateAsync(Page< object?> page, ISerializer elemSerializer, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
Task< QuerySuccess< object?> > QueryAsync(Query query, ISerializer serializer, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously executes a specified FQL query against the Fauna database and returns the typed result...
Task< QuerySuccess< T > > QueryAsync< T >(Query query, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously executes a specified FQL query against the Fauna database and returns the typed result...
IAsyncEnumerable< Page< object?> > PaginateAsync(Query query, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
Task< T > LoadRefAsync< T >(BaseRef< T > reference, CancellationToken cancel=default)
Asynchronously executes a specified FQL query against the Fauna database and returns the typed result...
IAsyncEnumerable< Page< object?> > PaginateAsync(Query query, ISerializer elemSerializer, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously iterates over pages of a Fauna query result, automatically fetching subsequent pages u...
Task< QuerySuccess< object?> > QueryAsync(Query query, QueryOptions? queryOptions=null, CancellationToken cancel=default)
Asynchronously executes a specified FQL query against the Fauna database.
A generic interface that defines serialize and deserialize behavior for a specific type,...
record Page< T >(IReadOnlyList< T > Data, string? After)
Represents a page in a dataset for pagination.