SapphireDb logo SapphireDb

Manage data

SapphireDb provides all methods you need to manage your data. You don't have to write Create, Update and Delete methods.

Create data

\f:(typescript:Angular) export class DemoComponent implements OnInit {\n \tcollection: DefaultCollection<Entry>;\n \tvalues$: Observable<Entry[]>;\n\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\t// Use a collection object to easily access the methods of a collection\n \t\tthis.collection = this.db.collection<Entry>('demo.entries');\n \t\tthis.values$ = this.collection.values();\n \t}\n\n \taddValue() {\n \t\t// Use the method .add to add a new object\n \t\tthis.collection.add({\n \t\t\tcontent: 'test value'\n \t\t});\n \t}\n\n \taddValues() {\n \t\t// Use the method .add to add multiple new object\n \t\tthis.collection.add(...[\n \t\t\t{\n \t\t\t\tcontent: 'test value 1'\n \t\t\t},\n \t\t\t{\n \t\t\t\tcontent: 'test value 2'\n \t\t\t}\n \t\t]);\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 }
You can also exclude some fields and make them [NonCreatable] using the attribute. This is useful when using includes for example.

Delete data

\f:(typescript:Angular) export class DemoComponent implements OnInit {\n \tcollection: DefaultCollection<Entry>;\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\tthis.collection = this.db.collection<Entry>('entries', 'demo');\n \t}\n\n \tdeleteValue(value: Entry) {\n \t\tthis.collection.remove(value);\n \t}\n\n \tdeleteValues(values: Entry[]) {\n \t\tthis.collection.remove(...values);\n \t}\n }
The value should contain the value and/or at least the primary keys of the object. (Check out model definition to configure primary keys)

Update data

You can also update values very easy. Make sure to add the [Updatable]-annotation to the model class or to specific fields to make them updatable.

\f:(typescript:Angular) export class DemoComponent implements OnInit {\n \tcollection: DefaultCollection<Entry>;\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\tthis.collection = this.db.collection<Entry>('demo.entries');\n \t}\n\n \tupdateValue(value: Entry) {\n \t\tthis.collection.update([value, {\n \t\t\tcontent: v\n \t\t}]);\n \t}\n\n \tupdateValues(values: [Entry, Partial<Entry>][]) {\n \t\tthis.collection.update(...values);\n \t}\n } \f:(csharp:DemoEntry.cs:Server) [Updatable]\n public class DemoEntry\n {\n \t[Key]\n \tpublic int Id { get; set; }\n\n \tpublic string Content { get; set; }\n }
The api for updates tries to keep immutability. You have to provide a tuple.
The first value should contain the original value and/or at least the primary keys of the object. (Check out model definition to configure primary keys)
The second value should contain the changes to apply.
All manage methods support inserting ranges. You can easily create, update and delete an array of objects.
You just have to pass it to the methods using the spread array syntax.
Continue with prefilters

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