Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active July 4, 2021 09:31
Show Gist options
  • Save gistlyn/0726548ab7ca7112f90d5dca9d97163d to your computer and use it in GitHub Desktop.
Save gistlyn/0726548ab7ca7112f90d5dca9d97163d to your computer and use it in GitHub Desktop.
autoquery-custom
// Connect your database
container.AddSingleton<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(
MapProjectPath("~/northwind.sqlite"), SqliteDialect.Provider));
// Add the AutoQuery Plugin
Plugins.Add(new AutoQueryFeature {
MaxLimit = 1000
});
// Return CustomRockstar result to control what the service returns
// Join with other tables
[Route("/rockstar-albums")]
public class QueryRockstarAlbums
: QueryDb<Rockstar,CustomRockstar>, IJoin<Rockstar,RockstarAlbum>
{
public int? Age { get; set; }
public string RockstarAlbumName { get; set; }
}
// Custom result
public class CustomRockstar
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
// Comes from joined table
public string RockstarAlbumName { get; set; }
}
// Override with custom implementation
public class MyQueryServices : Service
{
public IAutoQueryDb AutoQuery { get; set; }
public async Task<object> Any(QueryRockstarAlbums query)
{
using var db = AutoQuery.GetDb(query, base.Request);
var q = AutoQuery.CreateQuery(query, base.Request, db);
return await AutoQuery.ExecuteAsync(query, q, base.Request, db);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment