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:demo.component.ts) export class DemoComponent implements OnInit {\n \tcollection: DefaultCollection<Entry>;\n \tvalues$: Observable<Entry[]>;\n\n \tconstructor(private db: SapphireDb) { }\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) 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) public class DemoEntry\n {\n \t[Key]\n \tpublic int Id { get; set; }\n\n \tpublic string Content { get; set; }\n\n \t[NonCreatable]\n \tpublic string NonCreatableField { get; set; }\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:demo.component.ts) export class DemoComponent implements OnInit {\n \tcollection: DefaultCollection<Entry>;\n \tconstructor(private db: SapphireDb) { }\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 }

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:demo.component.ts) export class DemoComponent implements OnInit {\n \tcollection: DefaultCollection<Entry>;\n \tconstructor(private db: SapphireDb) { }\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({\n \t\t\t...value,\n \t\t\tcontent: v\n \t\t});\n \t}\n\n \tupdateValues(values: Entry[]) {\n \t\tthis.collection.update(...values);\n \t}\n } \f:(csharp:DemoEntry.cs) [Updatable]\n public class DemoEntry\n {\n \t[Key]\n \tpublic int Id { get; set; }\n\n \tpublic string Content { get; set; }\n }
Make sure to include at least the primary keys in the object when calling update or delete.
You should use the spread-syntax used in the example. This ensures that all data are included in the model.
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: