CosmosQuery 0.1.0
See the version list below for details.
dotnet add package CosmosQuery --version 0.1.0
NuGet\Install-Package CosmosQuery -Version 0.1.0
<PackageReference Include="CosmosQuery" Version="0.1.0" />
paket add CosmosQuery --version 0.1.0
#r "nuget: CosmosQuery, 0.1.0"
// Install CosmosQuery as a Cake Addin #addin nuget:?package=CosmosQuery&version=0.1.0 // Install CosmosQuery as a Cake Tool #tool nuget:?package=CosmosQuery&version=0.1.0
CosmosQuery
Creates LINQ expressions from ODataQueryOptions
and executes the query.
This library uses AutoMapper's Queryable Extentions to project from your DTO
(Data Transfer Object) to your entities.
Usage
<b>Step1</b>
<hr>
Set up your DTO
to Entity
mappings for AutoMapper so that it can correctly project the incoming OData
query from your DTO
type(s) to your Entity
type(s).
<br>
If you only want to include properties explicitly (E.g., the consumer of the API has to explicitly $select
which properties they want included in the results) then make sure to enable Explicit expansion on your properties.
public class Mappings : AutoMapper.Profile
{
public Mappings()
{
CreateMap<Entity, DTO>()
.ForMember(d => d.Name, o => o.MapFrom(s => s.FullName))
.ForAllMembers(o => o.ExplicitExpansion());
}
}
<b>Step2</b>
<hr>
Set up the actions on your controller(s) to accept a ODataQueryOptions
.
<br>
Do not decorate your actions with the [EnableQuery]
attribute as this will result in some operations being applied more than once.
public class MyController : ODataController
{
private readonly CosmosClient _client;
private readonly IMapper _mapper;
public MyController(CosmosClient client, IMapper mapper)
{
_client = client;
_mapper = mapper;
}
[HttpGet]
public async Task<IActionResult> Get(ODataQueryOptions<DTO> options)
{
var container = _client.GetContainer("DatabaseID", "ContainerID");
var query = container.GetItemLinqQueryable<Entity>();
return Ok(await query.GetQueryAsync(_mapper, options));
}
}
And thats it, you're done. The query is mapped to your entitiy type before query execution.
<br>
The query and results are correctly mapped to and from your DTO
type(s) and your Entity
type(s). This keeps your entities from beging publically exposed via the API.
Functions
There are four main functions you can use depending on your usecase.
Get
- Executes the query aginst the database synchronouslyGetAsync
- Executes the query against the database asynchronouslyGetQuery
- Synchronously builds theIQueryable
but does not execute it against the databaseGetQueryAsync
- Asynchronously builds theIQueryable
but does not execute it against the database
<b>If you plan on using the synchronous versions of the functions above make sure you've enabled synchronous execution:</b>
var container = _client.GetContainer("DatabaseID", "ContainerID");
var query = container.GetItemLinqQueryable<Entity>(allowSynchronousQueryExecution: true);
Function Signatures
public static ICollection<TModel> Get<TModel, TData>(
this IQueryable<TData> query,
IMapper mapper,
ODataQueryOptions<TModel> options,
QuerySettings? querySettings = null);
public static IQueryable<TModel> GetQuery<TModel, TData>(
this IQueryable<TData> query,
IMapper mapper,
ODataQueryOptions<TModel> options,
QuerySettings? querySettings = null)
public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(
this IQueryable<TData> query,
IMapper mapper,
ODataQueryOptions<TModel> options,
QuerySettings? querySettings = null);
public static async Task<IQueryable<TModel>> GetQueryAsync<TModel, TData>(
this IQueryable<TData> query,
IMapper mapper,
ODataQueryOptions<TModel> options,
QuerySettings? querySettings = null);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- AutoMapper.Extensions.ExpressionMapping (>= 6.0.3)
- LogicBuilder.Expressions.Utils (>= 5.0.6 && < 6.0.0)
- Microsoft.AspNetCore.OData (>= 8.0.6)
- Microsoft.Azure.Cosmos (>= 3.31.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.