Ditto Basics
CRUD Fundamentals
ditto query language (dql) — the dedicated query language you'll use to perform various filter operations to carry out traditional create , read , update , and delete (crud) database operations — is a familiar sql‑like syntax designed specifically for ditto's edge sync features that enable the platform's offline first capabilities for a list of api references by language, see the docid\ k0f9i2pxyjm3k9uwmaoal in the platform manual to execute crud operations, call the ditto sdk's execute api method as follows all modification operations in ditto are performed using the execute api method against the ditto store let result = try await ditto store execute(query / query /, arguments / arguments /);var result = ditto store execute(/ query /, / arguments /)const result = await ditto store execute(/ query /, / arguments /)dittoqueryresult result = (dittoqueryresult) ditto store execute( / query /, / arguments /, / continuation /);var result = await ditto store executeasync(/ query /, / arguments /);auto result = ditto get store() execute(/ query /, / arguments /) get();let result = ditto store() execute(/ query /, / arguments /); overview the following table provides a high level overview of the different ways you can perform crud in ditto operation description create using the insert statement, either insert a new document or update an existing document for a given document id (see /#creating ) read using the select statement , retrieve documents based on the specific criteria you pass as parameters in your function (see /#reading ) update using the update method, write changes to ditto (see /#updating ) delete using the evict method, remove local data from your store (see /#deleting ) in addition, using a soft delete pattern , indicate data as deleted without physically removing it from ditto for detailed information on crud, see docid\ lbrsippkezdqtgbm4i5nn for an overview of the various operators and path navigations you can use to construct sophisticated queries in your app, see docid\ e3uqqeqvzvotjls2xfdfl creating to create a new document in your local ditto store, call insert following is an example of how to perform an insert operation using ditto's sdk await ditto store execute( query "insert into cars documents (\ newcar)", arguments \["newcar" \["color" "blue"]]);ditto store execute( "insert into cars documents (\ newcar)", mapof("newcar" to mapof("color" to "blue")))await ditto store execute( "insert into cars documents (\ newcar)", { newcar { color "blue" } });dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars documents (\ newcar)", collections singletonmap("newcar", collections singletonmap("color", "blue")), );var args = new dictionary\<string, object>(); args add("newcar", new { color = "blue" }); await ditto store executeasync( "insert into cars documents (\ newcar)", args);std map\<std string, std map\<std string, std string>> args; args\["newcar"] = {{"color", "blue"}}; auto result = ditto get store() execute( "insert into cars documents (\ newcar)", args) get();use serde serialize; \#\[derive(serialize)] struct args { newcar car, } \#\[derive(serialize)] struct car { color string } // let args = args { newcar car { color "blue" to string() }, }; ditto store() execute( "insert into cars documents (\ newcar)", some(args into())); for more information and how to instructions, see docid\ lbrsippkezdqtgbm4i5nn > docid\ qsoasgyrr0l0dydxy77ca reading to retrieve data in ditto, depending on your goals and use case, use any of the following query types single execution query — using the execute method and a select query, execute a single read operation (see /#single execution query ) store observer query — using the addobserver method, establish a listener to watch your local changes in realtime (see /#store observer query ) single execution query with the execute api method and select query, search for documents within your local ditto store let result = await ditto store execute(query "select from cars")val result = ditto store execute("select from cars")const result = await ditto store execute("select from cars");dittoqueryresult result = (dittoqueryresult) ditto store execute( "select from cars", new continuation<>() { @nonnull @override public coroutinecontext getcontext() { return emptycoroutinecontext instance; } @override public void resumewith(@nonnull object o) { if (o instanceof result failure) { // handle failure } } } );var result = await ditto store executeasync("select from cars");auto result = ditto get store() execute("select from cars") get();let result = ditto store() execute("select from cars", none); for more information and how to instructions, see docid\ lbrsippkezdqtgbm4i5nn > docid\ yt3vdnkwaehbu1pjtjzvl store observer query when you need to actively monitor changes within your local ditto store and respond to them immediately; for example, watching updates to end user profiles, use a store observer a store observer is a dql query that runs continuously and, once ditto detects relevant changes, asynchronously triggers the callback function you defined when you set up your store observer for instance, when the end user updates their profile, display the profile changes to the end user in realtime following is a snippet demonstrating how to establish a store observer in ditto let observer = ditto store registerobserver( query "select from cars"){ result in / handle change / };val observer = ditto store registerobserver("select from cars") { result > / handle change / };const changehandler = (result) => { // handle change } const observer = ditto store registerobserver( "select from cars", changehandler);dittostoreobserver observer = ditto store registerobserver( "select from cars", result > { // handle change } );// without arguments var result = await ditto store registerobserver( "select from cars", (result) => { // handle change }); // with arguments var result = ditto store registerobserver( "select from cars", (result) => { // handle change }); auto observer = ditto get store() register observer( "select from cars", \[&]\(queryresult result) { / handle change / });let observer = ditto store() register observer( "select from cars", none, move |result queryresult| { // handle change }) for more information and how to instructions, see docid\ lbrsippkezdqtgbm4i5nn > docid\ yt3vdnkwaehbu1pjtjzvl updating with the update statement, you can update fields within one or more documents in your local ditto store for example, executing an update operation within the cars collection, changing the color to 'blue' and the mileage to 3001 in documents where the id field is '123' try await ditto store execute(""" update cars set color = 'blue' where id = '123' """);ditto store execute(""" update cars set color = 'blue' where id = '123' """)await ditto store execute(` update cars set color = 'blue' where id = '123'`)dittoqueryresult result = (dittoqueryresult) ditto store execute( "update cars set color = 'blue' where id = '123'", new continuation<>() { @nonnull @override public coroutinecontext getcontext() { return emptycoroutinecontext instance; } @override public void resumewith(@nonnull object o) { if (o instanceof result failure) { // handle failure } } } );await ditto store executeasync( "update cars set color = 'blue' where id = '123'");ditto get store() execute( "update cars set color = 'blue' where id = '123'") get();ditto store() execute( "update cars set color = 'blue' where id = '123'", none); for more information and how to instructions, see docid\ lbrsippkezdqtgbm4i5nn > docid\ wbvb hd0z6ebzelk5zm9y deleting data storage management is essential for preventing unnecessary resource usage, which affects not only performance but also battery life and overall end user experience call the evict method to clear one or more documents from the local ditto store once invoked, the documents are no longer accessible by local queries; however, they remain accessible from other peers connected in the mesh evict can be used with soft delete patterns to safely coordinate the deletion of data across peers the following snippet shows how to write a basic evict operation to purge the document with an id field of '123' from the local ditto store await ditto store execute("evict from cars where id = '123'");ditto store execute("evict from cars where id = '123'")await ditto store execute("evict from cars where id = '123'");dittoqueryresult result = (dittoqueryresult) ditto store execute( "evict from cars where id = '123'", new continuation<>() { @nonnull @override public coroutinecontext getcontext() { return emptycoroutinecontext instance; } @override public void resumewith(@nonnull object o) { if (o instanceof result failure) { // handle failure } } } );ditto store executeasync("evict from cars where id = '123'");ditto get store() execute("evict from cars where id = '123'") get();ditto store() execute("evict from cars where id = '123'", none); for more information and how to instructions, see docid\ lbrsippkezdqtgbm4i5nn > docid\ f 5iy165eofkhdbat1zd7