Query data
Creating a query is very simple. ng-sapphiredb uses RxJs to provide the values of the collection.
You first have to create a collection object using the method SapphireDb.collection()
.
As a first parameter you have to pass the name of the collection defined in your DbContext.
If you are using a custom context name you can access the collection by prefixing it with the name separated with a dot: default.entries
for example
You can simply query all data of a collection.
Using the method .values()
will create a subscription on the server and the server will notify the client if something has changed.
\f:(typescript:Angular) export class DemoComponent implements OnInit {\n \tvalues$: Observable<Entry[]>;\n\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\tthis.values$ = this.db.collection<Entry>('entries').values();\n \t}\n } \f:(csharp:DemoContext.cs:Server) public class DemoContext : SapphireDbContext\n {\n \tpublic DemoContext(DbContextOptions<DemoContext> options, SapphireDatabaseNotifier notifier) : base(options, notifier)\n \t{\n \t}\n\n \tpublic DbSet<DemoEntry> Entries { get; set; }\n } \f:(csharp:DemoEntry.cs:Server) public class DemoEntry\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 }
If you only want to get the data once without updates use .snapshot()
\f:(typescript:Angular) export class DemoComponent implements OnInit {\n \tvalues$: Observable<Entry[]>;\n\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\tthis.values$ = this.db.collection<Entry>('entries').snapshot();\n \t}\n }
Continue with data management