SapphireDb logo SapphireDb

Offline support

SapphireDb support offline usage and can store and load data from a local storage.
Changes made offline and online will be synced and automatically merged.

Basic setup

To enable offline support just activate the option offlineSupport in your client application.
By enabling offlineOptimization operations made offline will be stored in an optimized way.
When creating and deleting the same entry without going online nothing is stored.
When editing a newly created object the new object will get stored in the creation and no change operation is stored.
\f:(typescript:JS) var sapphiredb = require('sapphiredb');\n\n var db = new sapphiredb.SapphireDb({\n \tofflineSupport: true,\n \tofflineOptimization: true\n });

Setup storage

To store data locally and benefit from offline support you have to provide a storage. For normal browsers you can use SapphireLocalStorage. For other environments you should implement a custom storage provider.
\f:(typescript:JS) var sapphiredb = require('sapphiredb');\n\n var db = new sapphiredb.SapphireDb({\n \tofflineSupport: true,\n \tofflineOptimization: true\n }, new sapphiredb.SapphireLocalStorage());
In angular you can provide the custom storage as SapphireStorage in providers of your module

Prepare models

Without configuring anything SapphireDb can already store collection state and local operations. But it will not be possible to create new values in a collection and update them without going online again.
To use that you have to generate some sort of id on client side when creating new values. (UUIDs/GUIDs are ideal for that)
Use the class SapphireOfflineEntity in every model that should work offline. It has an auto generated id property and a timestamp for concurrency conflict detection.
\f:(csharp:Entry.cs) // Server side usage\n public class DemoEntry : SapphireOfflineEntity\n {\n \tpublic string Content { get; set; }\n } \f:(typescript:entry.ts) // Client side usage\n class Entry extends SapphireOfflineEntity {\n\n }

Conflict handling

SapphireDb will handle all conflicts that can happen automatically when using SapphireOfflineEntity.
You can optionally control how SapphireDb should behave in case of a merge conflict.
\f:(csharp:Entry.cs) public class DemoEntry : SapphireOfflineEntity\n {\n \t[MergeConflictResolutionMode(MergeConflictResolutionMode.ConflictMarkers)]\n \tpublic string Content { get; set; }\n }
Available merge conflict resolution modes are:
  • Database: Take the value that is already stored in database
  • Last: Take the value of the conflicting operation
  • ConflictMarkers: Take both values and add GIT-Merge markers (only works with strings)


  • Server side model validation will only get executed on server side. It is possible to insert invalid values on client side that are rejected later.
    Add additional client side validation to avoid that
  • Some prefilters like First, Last, Count etc. will online work limited offline. The state before going offline will get stored but operations on the collection will first take effect when going online again.
  • The prefilters Skip and Take will have no effect when offline. If you add new values they are always displayed by the prefilter.
  • Every collection value with different prefilters is stored in a different storage. That can result in sometimes different values when using different prefilters. To avoid that only use one combination of prefilters in one scope of the application.

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