Data Store CRUD
READ
this article provides an overview of essential methods for data retrieval just like with conventional database querying, you execute query operations to fetch one or more documents that satisfy specific criteria and conditions to perform a single execution query on the ditto store, call the execute api method on the store namespace as follows 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");let query result = ditto store() execute("select from cars", none) await?;final result = await ditto store execute("select from cars"); using args to query dynamic values when dealing with data that may change dynamically during runtime, instead of defining the changing values directly in the query string , encapsulate them in a top level args object you can use to reference the values in your queries to pass an argument to the execute function, use the \[argument] syntax with dql where the \[argument] maps to the field in the provided args object for example, here color is passed as an argument to the execute function, and, within the query string, \ color placeholder references the color defined in a top level args object let result = await ditto store execute( query "select from cars where color = \ color", arguments \[ "color" "blue" ])val result = ditto store execute( "select from cars where color = \ color", mapof("color" to "blue"))const result = await ditto store execute( "select from cars where color = \ color", { color "blue" } );dittoqueryresult result = (dittoqueryresult) ditto store execute( "select from cars where color = \ color", collections singletonmap("color", "blue"), new continuation<>() { @nonnull @override public coroutinecontext getcontext() { return emptycoroutinecontext instance; } @override public void resumewith(@nonnull object o) { if (o instanceof result failure) { // handle failure } } } );const result = await ditto store executeasync( "select from cars where color = \ color", new dictionary\<string, object> { "color", "blue" });auto result = ditto get store() execute( "select from cars where color = \ color", {{"color", "blue"}});let query result = ditto store() execute( "select from cars where color = \ color", some(serde json json!({ "color" "blue" }) into()), ) await?;final result = await ditto store execute( "select from cars where color = \ color", arguments {"color" "blue"}); once the previous example operation executes, the query becomes select from cars where color = blue managing query results after executing a query, the result object that is returned includes both the overall content retrieved and individual items each item is encapsulated in independent queryresultitem objects that you can use to access them either directly or as raw cbor or json data rather than retrieving the items as part of the query execution and making them available immediately after execution, each item is lazy loaded lazy loading involves postponing fetching and storing in memory until requested on demand or accessed later here is an example query execution to select all documents from the cars collection the result is stored in the variable result then, each item is lazy loaded from the result object and stored in the items let result = await ditto store execute(query "select from cars") let items = result itemsval result = ditto store execute("select from cars") const items = result itemsconst result = await ditto store execute("select from cars") const items = result itemsdittoqueryresult 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 } } } ); dittoqueryresultitems items = result items;var result = await ditto store executeasync("select from cars"); var items = result items;auto result = ditto get store() execute("select from cars"); auto items = result items();let query result = ditto store() execute("select from cars", none) await?; let items = query result items();final result = await ditto store execute("select from cars"); final items = result items; working with a queryresultitem the result items object is a collection of queryresultitem each item's value can be independently managed to meet the needs of your scenario value to retrieve the value, call the value property on an item let result = await ditto store execute(query "select from cars") let item = result items\[0] let itemvalue = item value let itemvaluecolor = item value\["color"]val result = ditto store execute("select from cars") val item = result items first() val itemvalue = item value val itemvaluecolor = item value\["color"]const result = await ditto store execute(`select from cars`) const item = result items\[0] const itemvalue = item value const itemvaluecolor = item value\["color"]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 } } } ); dittoqueryresultitem item = result items\[0] map\<string, object> itemvalue = item value string itemvaluecolor = item value\["color"] tostring()var result = await ditto store executeasync("select from cars"); var item = result items\[0]; var itemvalue = item value; var itemvaluecolor = item value\["color"] as string;const auto result = ditto store execute(`select from cars`); auto item = result get item(0); auto item value = item value(); auto item value color = item value\["color"];let query result = ditto store() execute("select from cars", none) await?; let item = query result items(0); let itemvalue = item materialize();final result = await ditto store execute("select from cars"); final item = result items\[0]; final itemvalue = item value; final itemvaluecolor = item value\["color"]; materializing the value to help manage memory usage more efficiently, content associated with an item is lazily loaded, meaning it materializes — loads into memory — only upon the initial call to value to load the item's value into memory, use any of the following methods as appropriate // returns `true` if value is currently held materialized in memory, otherwise returns `false` item ismaterialized // loads the item's value into memory item materialize() // release item's value from memory item dematerialize()// returns `true` if value is currently held materialized in memory, otherwise returns `false` item ismaterialized // loads the item's value into memory item materialize() // release item's value from memory item dematerialize()// returns `true` if value is currently held materialized in memory, otherwise returns `false` item ismaterialized // loads the item's value into memory item materialize() // release item's value from memory item dematerialize()// returns `true` if value is currently held materialized in memory, otherwise returns `false` item ismaterialized // loads the item's value into memory item materialize() // release item's value from memory item dematerialize()// returns `true` if value is currently held materialized in memory, otherwise returns `false` item ismaterialized; // loads the item's value into memory item materialize(); // release item's value from memory item dematerialize();// returns `true` if value is currently held materialized in memory, otherwise returns `false` item is materialized() // loads the item's value into memory item materialize() // release item's value from memory item dematerialize()// loads the item's value into memory item materialize();// query results are always materialized in dart raw cbor value to access the result items as cbor data the result of this method call is not cached let result = await ditto store execute(query "select from cars") let cborserializeditem = result items\[0] cbordata()val result = ditto store execute("select from cars") val cboritem = result items first() cbordata()const result = await ditto store execute(`select from cars`) const cboritem = result items\[0] cbordata()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 } } } ); byte\[] cboritem = result items\[0] cbordata()var result = await ditto store executeasync("select from cars"); var cboritem = result items\[0] cbordata();auto result = ditto get store() execute("select from cars"); auto cboritem = result get item(0) cbor data();let query result = ditto store() execute("select from cars", none) await?; let cboritem = query result items(0) cbor();// coming soon raw json value to access the result items as a json string the result of this method call is not cached let result = await ditto store execute(query "select from cars") let jsonserializeditem string = result items\[0] jsonstring() let jsonasdata data = data(jsonserializeditem utf8)val result = ditto store execute("select from cars") val jsonitem = result items first() jsonstring()const result = await ditto store execute(`select from cars`) const jsonitem = result items\[0] jsonstring()dittoqueryresult result = (dittoqueryresult) ditto store execute( "select from cars", collections singletonmap("color", "blue"), new continuation<>() { @nonnull @override public coroutinecontext getcontext() { return emptycoroutinecontext instance; } @override public void resumewith(@nonnull object o) { if (o instanceof result failure) { // handle failure } } } ); string cboritem = result items\[0] jsondata()var result = await ditto store executeasync("select from cars"); var jsonitem = result items\[0] jsonstring();auto result = ditto get store() execute("select from cars"); auto jsonitem = result get item(0) json string();let query result = ditto store() execute("select from cars", none) await?; let jsonitem = query result items(0) json();// coming soon