What is Salesforce SmartStore?
SmartStore is an encrypted NoSQL-style JSON document data store. In fact, it is the only cross-platform encrypted NoSQL mobile database on the market that works with both hybrid and native development models
- Salesforce Mobile SDK SmartStore to store encrypted data in a NoSQL-style database on both iOS and Android devices.
- The app’s connectivity should be transparent to the user, and switching between online and offline should be seamless.
- This allows users to focus on using the app to do their job rather than about with online / offline settings.
- The app will first attempt to load records from SmartStore. If none exist, it will check if the device is online. If it is, finally it will attempt to load the records from Salesforce.com.
The above method is good for reading data from the Salesforce
What if the user needs to be able to Create, Update, or Delete data?
- Any time a record is upserted or deleted in the app, the local copy of that record is upserted or deleted and a queue record is created too.
- If the app is online, the record can be upserted or deleted in Salesforce.com immediately and the queue record can be deleted.
- Since the queue record isn’t deleted until a successful response is received from the Salesforce API, this ensures that no data will be lost regardless of whether the device is online, offline, or has a bad connection and a synchronization error occurred during data transfer.
- Salesforce can also calculate values for some fields by using formula fields, workflow rules, or triggers. It’s a good practice to query for any recently upserted records just to make sure the data is in sync across Salesforce and the mobile device.
- “NoSQL” databases are a class of database that, unlike their “relational” brethren, are designed to be inherently schemaless. They take care of storing data and let the application worry about how to structure it.
- Some typical benefits of this style of database is their raw speed, a dynamic schema where fields and tables can be added by the application at will, and easy of use for the developer.
- Force.com Rest API and Chatter API both return JSON documents from the Salesforce.com server.
- The code you wrote to process the data you’re getting from the Rest API is the same code you’ll use to process the data you’re getting from SmartStore, and you don’t have to set up a schema offline.
- In the typical relational database, data is stored in “tables,” where data is segmented across many tables in order to avoid things like duplicating data.
- With SmartStore, data is stored in “soups,” Soups are then saved in Stores, which are essentially separate database files on a disk.
- Unless you have some strong need for multiple database files, most apps will have only one Store and any number of Soups.
Register a Soup
You can register a new soup with an IndexSpec. An IndexSpec is a simple JSON document that tells SmartStore which fields need to be indexed for searching, upserting, deleting, and sorting.
Query a Soup
Let’s query the registered soup with NoSQL database. Four different types of QuerySpec to query
- BuildAllQuerySpec = function(path, order, pageSize).
- This QuerySpec is used to query all records from the object in the given sorted order (or null order). This is the one to use if you want to load all records from a Soup into app memory and work with it there. This isn’t the best option if you have a lot of data in a soup.
- BuildExactQuerySpec = function(path, matchKey, pageSize).
- Use this QuerySpec if you know exactly what record you want.
- BuildRangeQuerySpec = function(path, beginKey, endKey, order, pageSize).
This QuerySpec is similar to the ExactQuerySpec, but it allows you to specify a range of values with the beginKey and endKey parameters. For instance, if you wanted to query all Names between “Adams” and “Zaphod”, you would want to use this QuerySpec.
- BuildLikeQuerySpec = function(path, likeKey, order, pageSize)
The query is similar to the LIKE clause and allows you to search for a substring within string indexes
You just need to specify which soup you want to write to and pass in a JSON document for the entries parameter. It will be automatically indexed using the indexes you specified when you registered the soup earlier.
UpsertSoupEntries = function (soupName, entries, successCB, errorCB)
No database would be complete without some way to delete data, and SmartStore provides the RemoveFromSoup method for just that reason.
RemoveFromSoup = function (soupName, entryIds, successCB, errorCB)