SapphireDb logo SapphireDb

Server side queries

For security reasons and make development across multiple implementations you may want to define queries on server side.
Those queries almost work like normal client defined queries and give you all features you normally have, enriched with some server only features like custom select-returns and full support of all EF-Core supported where-clauses.

Define query

In place

\f:(csharp:DemoEntry.cs) [Query("custom_query_name", nameof(CustomQueryBuilder))]\n public class DemoEntry\n {\n \tprivate static SapphireQueryBuilderBase<DemoEntry> CustomQueryBuilder(SapphireQueryBuilder<DemoEntry> queryBuilder, HttpInformation httpInformation, JToken[] parameters)\n \t{\n \t\treturn queryBuilder\n \t\t\t.Where(e => e.Content.Length > 5)\n \t\t\t.OrderBy(e => e.Content)\n \t\t\t.ThenOrderBy(e => e.IntegerValue)\n \t\t\t.Select(m => new { m.Content });\n \t}\n\n \t[Key]\n \tpublic Guid Id { get; set; }\n\n \tpublic string Content { get; set; }\n\n \tpublic int IntegerValue { get; set; } = RandomNumberGenerator.GetInt32(1, 100);\n }
The parameters of the model builder are enriched with dependency injection so you are able to use any functionality you want.
You can run auth checks etc. if you need to.

Fluent api

\f:(csharp:MessageConfiguration.cs) public class MessageConfiguration : ISapphireModelConfiguration<Message>\n {\n \tpublic void Configure(SapphireModelBuilder<Message> modelBuilder)\n \t{\n \t\tmodelBuilder.CreateQuery("query1", (builder, information, parameters) =>\n \t\t\tbuilder\n \t\t\t\t.OrderBy(m => m.Content)\n \t\t\t\t.Where(m => m.Content == null)\n \t\t\t);\n \t}\n }

Use query

Subscribe

\f:(typescript:Angular) export class ServerSideQueryComponent {\n \tcollection: QueryCollection<any, any[]>;\n \tvalues$: Observable<any>;\n\n \tconstructor(private db: SapphireDbService) {\n \t\tthis.collection = this.db.query<any>('demo.entries.custom_query_name');\n \t\tthis.values$ = this.collection.values();\n \t}\n\n \taddValue() {\n \t\tthis.collection.add({\n \t\t\tcontent: v,\n \t\t});\n \t}\n }
[]
A query works like a collection. You can use any method on that collection.

Snapshot

\f:(typescript:Angular) export class ServerSideQueryComponent {\n \tcollection: QueryCollection<any, any[]>;\n \tsnapshot$: Observable<any>;\n\n \tconstructor(private db: SapphireDbService) {\n \t\tthis.collection = this.db.query<any>('demo.entries.custom_query_name');\n \t\tthis.snapshot$ = this.collection.snapshot();\n \t}\n }
[]

This website uses Google Analytics. If you want to disable it just unselect the option: