Skip to content

Commit

Permalink
[AUTO]Updating snippets (#426)
Browse files Browse the repository at this point in the history
Files changed:
M	snippets.json

Co-authored-by: danielhenrymantilla <[email protected]>
  • Loading branch information
dittotester and danielhenrymantilla authored Jan 17, 2023
1 parent 09aa1f8 commit aa73d31
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"query-sort": "let sort_param = ffi_sdk::COrderByParam {\n query_c_str: c!(\"miles\"),\n direction: ffi_sdk::QuerySortDirection::Ascending,\n};\ncollection\n .find(\"color == \\'red\\'\")\n .sort(vec![sort_param])\n .exec()?;\n\n",
"query-limit": "let sort_param = ffi_sdk::COrderByParam {\n query_c_str: c!(\"rank\"),\n direction: ffi_sdk::QuerySortDirection::Ascending,\n};\ncollection\n .find(\"color == \\'red\\'\")\n .sort(vec![sort_param])\n .limit(100)\n .exec()?;\n\n",
"subscribe": "let store = ditto.store(); // Ditto must have a longer lifetime than all live queries\nlet live_query = store\n .collection(\"cars\")?\n .find(\"color == \\'red\\'\")\n .subscribe();\n\n",
"sync-observe": "let store = ditto.store(); // Ditto must have a longer lifetime than all live queries\nlet (tx, rx) = channel();\n{\n let live_query = store\n .collection(\"cars\")?\n .find(\"color == \\'red\\'\")\n .observe_local(move |mut docs: Vec<BoxedDocument>, event| {\n match event {\n LiveQueryEvent::Initial { .. } => { /* handle if appropriate */ }\n LiveQueryEvent::Update { mut insertions, .. } => {\n insertions.sort_by(|a, b| b.cmp(a));\n for idx in insertions.iter() {\n let doc = docs.remove(*idx);\n let _ = tx.send(doc).unwrap();\n }\n }\n }\n })?;\n store\n .collection(\"cars\")?\n .upsert(json!({\"color\": \"red\"}))\n .unwrap();\n for doc in rx.iter() {\n println!(\"New doc {:?}\", doc);\n }\n} // IMPORTANT: LiveQuery goes out of scope and is Dropped and terminated here.\n\n",
"sync-observe": "let store = ditto.store(); // Ditto must have a longer lifetime than all live queries\nlet (tx, rx) = channel();\n{\n let live_query = store\n .collection(\"cars\")?\n .find(\"color == \\'red\\'\")\n .observe_local(move |mut docs: Vec<BoxedDocument>, event| {\n match event {\n LiveQueryEvent::Initial { .. } => { /* handle if appropriate */ }\n LiveQueryEvent::Update { mut insertions, .. } => {\n insertions.sort_by(|a, b| b.cmp(a));\n for idx in insertions.iter() {\n let doc = docs.remove(*idx);\n tx.send(doc).unwrap();\n }\n }\n }\n })?;\n store\n .collection(\"cars\")?\n .upsert(json!({\"color\": \"red\"}))\n .unwrap();\n for doc in rx.iter() {\n println!(\"New doc {:?}\", doc);\n }\n} // IMPORTANT: LiveQuery goes out of scope and is Dropped and terminated here.\n\n",
"sync-observe-local": "// Some action in your app ...\nlet store = ditto.store();\nstore.collection(\"cars\")?.upsert(json!({\"color\": \"red\"}))?;\n// Elsewhere register handlers for data changes\n{\n let live_query = store\n .collection(\"cars\")?\n .find(\"color == \\'red\\'\")\n .observe_local(move |cars, event| {\n println!(\"cars {:?}, event {:?}\", cars, event);\n // do something when data changes\n // BUT this closure must be permitted to take ownership\n })?;\n // stash your live query in something with a long lifetime\n // or it will be dropped\n}\n\n",
"shared-key": "// This is just an example. You should use OpenSSL to generate a unique shared key for every\n// application.\nlet p256_der_b64: &str = \"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgFUUrOkOH52QN+Rr6uDSDsk4hUTcD1eW4mT0UnGGptFehRANCAATJ3fG8TVLQcDwUV18BJJI8efK0hQAjzB3VJeYOVbfOlqnfukVId0V25r/abxwjD3HfHuPsCGEiefzzmkMbjPo9\";\nlet app_id = AppId::from_env(\"app\")?;\nlet ditto = Ditto::builder()\n .with_root(Arc::new(PersistentRoot::from_current_exe()?))\n .with_identity(|ditto_root| identity::SharedKey::new(ditto_root, app_id, p256_der_b64))?\n .with_minimum_log_level(CLogLevel::Info)\n .build()?;\nlet res = ditto.set_offline_only_license_token(&license_token);\nditto.start_sync()?;\n\n",
"online-playground": "let ditto = Ditto::builder()\n // creates a `ditto_data` folder in the directory containing the executing process\n .with_root(Arc::new(PersistentRoot::from_current_exe()?))\n .with_identity(|ditto_root| {\n // Provided as an env var, may also be provided as hardcoded string\n let app_id = AppId::from_env(\"00000000-0000-4000-0000-000000000000\")?;\n let shared_token = std::env::var(\"REPLACE_ME_WITH_A_SHARED_TOKEN\").unwrap();\n let enable_cloud_sync = true;\n let custom_auth_url = None;\n OnlinePlayground::new(\n ditto_root,\n app_id,\n shared_token,\n enable_cloud_sync,\n custom_auth_url,\n )\n })?\n .build()?;\n\nditto.start_sync()?;\n\n",
Expand Down Expand Up @@ -71,9 +71,9 @@
"upsert-default-data": "do {\n let docID = try ditto.store[\"people\"].upsert([\n \"name\": \"Susan\",\n \"age\": 31\n ], writeStrategy: .insertDefaultIfAbsent)\n} catch {\n //handle error\n print(error)\n}\n\n",
"upsert-composite-primary-key": "do {\n let docID = try ditto.store[\"people\"].upsert([\n \"_id\": [ \"userId\": \"456abc\", \"workId\": 789 ],\n \"name\": \"Susan\",\n \"age\": 31\n ])\n print(docID) // \"[ \"userId\": \"456abc\", \"workId\": 789 ]\"\n} catch {\n //handle error\n print(error)\n}\n\n",
"upsert-datatypes": "do {\n // Insert JSON-compatible data into Ditto\n try ditto.store[\"foo\"].upsert([\n \"boolean\": true,\n \"string\": \"Hello World\",\n \"number\": 10,\n \"map\": [\"key\": \"value\"],\n \"array\": [1,2,3],\n \"null\": nil\n ])\n}\ncatch {\n //handle error\n print(error)\n}\n\n",
"counter": "do {\n let docId = try ditto.store[\"people\"].upsert([\n \"name\": \"Frank\",\n \"ownedCars\": 0 // here 0 is a number\n ])\n \n ditto.store[\"people\"].findByID(docId).update({ mutableDoc in\n mutableDoc?[\"ownedCars\"].set(DittoCounter())\n mutableDoc?[\"ownedCars\"].counter?.increment(by: 1)\n })\n} catch {\n //handle error\n print(error)\n}\n\n",
"counter": "do {\n let docId = try ditto.store[\"people\"].upsert([\n \"name\": \"Frank\",\n \"ownedCars\": 0 // here 0 is a number\n ])\n\n ditto.store[\"people\"].findByID(docId).update({ mutableDoc in\n mutableDoc?[\"ownedCars\"].set(DittoCounter())\n mutableDoc?[\"ownedCars\"].counter?.increment(by: 1)\n })\n} catch {\n //handle error\n print(error)\n}\n\n",
"update": "do {\n let docID = try ditto.store[\"people\"].upsert([\n \"name\": \"Frank\",\n \"age\": 31,\n \"ownedCars\": 0\n ])\n\n ditto.store[\"people\"].findByID(docID).update { mutableDoc in\n mutableDoc?[\"age\"] = 32\n mutableDoc?[\"ownedCars\"].set(DittoCounter())\n mutableDoc?[\"ownedCars\"].counter?.increment(by: 1)\n }\n} catch {\n //handle error\n print(error)\n}\n\n",
"array-to-map": "collection.findByID(docID).update { doc in\n guard let doc = doc else {\n print(\"Document with id=\\(docID) not found\")\n return\n }\n \n let names = doc[\"friends\"].arrayValue\n var dict = Dictionary<String, Any>()\n \n _ = names.map { name in\n let friend: NSMutableDictionary = [:]\n let id = UUID().uuidString\n friend[\"id\"] = id\n friend[\"name\"] = name\n dict.updateValue(friend, forKey: id)\n }\n doc[\"friendsMap\"].set(dict)\n}\n\n",
"array-to-map": "collection.findByID(docID).update { doc in\n guard let doc = doc else {\n print(\"Document with id=\\(docID) not found\")\n return\n }\n\n let names = doc[\"friends\"].arrayValue\n var dict = Dictionary<String, Any>()\n\n _ = names.map { name in\n let friend: NSMutableDictionary = [:]\n let id = UUID().uuidString\n friend[\"id\"] = id\n friend[\"name\"] = name\n dict.updateValue(friend, forKey: id)\n }\n doc[\"friendsMap\"].set(dict)\n}\n\n",
"upsert": "do {\n // upsert JSON-compatible data into Ditto\n let docID = try ditto.store[\"people\"].upsert([\n \"name\": \"Susan\",\n \"age\": 31\n ])\n} catch {\n //handle error\n print(error)\n}\n\n",
"upsert-id": "do {\n // upsert JSON-compatible data into Ditto\n let docID = try ditto.store[\"people\"].upsert([\n \"_id\": \"abc123\",\n \"name\": \"Susan\",\n \"age\": 31\n ])\n XCTAssertEqual(docID, \"abc123\")\n} catch {\n //handle error\n print(error)\n}\n\n",
"query-basic": "let collection = ditto.store[\"people\"]\n .find(\"favoriteBook.title == 'The Great Gatsby'\")\n .exec()\n\n",
Expand Down Expand Up @@ -261,4 +261,4 @@
"network-multiple-transports": "DittoTransportConfig config = new DittoTransportConfig();\n\n// 1. Enable Peer to Peer Connections\nconfig.enableAllPeerToPeer();\n\n// 2. Listen for incoming connections on port 4000\nDittoListen listen = new DittoListen();\nDittoTcpListenConfig tcpListenConfig = new DittoTcpListenConfig();\ntcpListenConfig.setEnabled(true);\ntcpListenConfig.setInterfaceIp(\"0.0.0.0\");\ntcpListenConfig.setPort(4000);\nlisten.setTcp(tcpListenConfig);\nconfig.setListen(listen);\n// 3. Connect explicitly to remote devices\nDittoConnect connect = new DittoConnect();\nconnect.setTcpServers(Sets.newHashSet(\"135.1.5.5:12345\", \"185.1.5.5:12345\"));\nconfig.setConnect(connect);\n\ntry {\n ditto.startSync();\n} catch(DittoError error) {\n // handle error\n}\n\n",
"network-monitor-conditions": "// Setting up inside an Activity\nDefaultAndroidDittoDependencies androidDependencies = new DefaultAndroidDittoDependencies(getApplicationContext());\nDitto ditto = new Ditto(androidDependencies, new DittoIdentity.OnlinePlayground(androidDependenciesOne, \"REPLACE_WITH_APP_ID\"));\nditto.callback = this;\nditto.startSync();\n\n// Now you can observe real time changes to the transport conditions:\npublic class MainActivity extends AppCompatActivity implements DittoCallback {\n @Override\n public void transportConditionDidChange(@NotNull DittoTransportCondition condition, @NotNull DittoConditionSource transportId) {\n String toastText = null;\n if (condition == DittoTransportCondition.BleDisabled) {\n toastText = \"BLE disabled\";\n } else if (condition == DittoTransportCondition.NoBleCentralPermission) {\n toastText = \"Permission missing for BLE\";\n } else if (condition == DittoTransportCondition.NoBlePeripheralPermission) {\n toastText = \"Permission missing for BLE\";\n }\n\n if (toastText != null) {\n String finalToastText = toastText;\n runOnUiThread(new Runnable() {\n @Override\n public void run() {\n Toast.makeText(MainActivity.this, finalToastText, Toast.LENGTH_LONG).show();\n }\n });\n }\n }\n}\n\n"
}
}
}

0 comments on commit aa73d31

Please sign in to comment.