private void SetDocument(T entity, MutableDocument mutableDocument) { var properties = ObjectToDictionaryHelper.ToDictionary(entity); foreach (var prop in properties) { if (prop.Value is int) { mutableDocument.SetInt(prop.Key, (int)prop.Value); } else if (prop.Value is long) { mutableDocument.SetLong(prop.Key, (long)prop.Value); } else if (prop.Value is bool) { mutableDocument.SetBoolean(prop.Key, (bool)prop.Value); } else if (prop.Value is DateTimeOffset) { if ((DateTimeOffset)prop.Value != default(DateTimeOffset)) { mutableDocument.SetDate(prop.Key, (DateTimeOffset)prop.Value); } } else if (prop.Value is double) { mutableDocument.SetDouble(prop.Key, (double)prop.Value); } else if (prop.Value is float) { mutableDocument.SetFloat(prop.Key, (float)prop.Value); } else if (prop.Value is string) { mutableDocument.SetString(prop.Key, (string)prop.Value); } else { mutableDocument.SetValue(prop.Key, prop.Value); } } }
public void TestGetFragmentFromFloat() { var doc = new MutableDocument("doc1"); doc.SetFloat("float", 100.10f); SaveDocument(doc, d => { var fragment = d["float"]; fragment.Exists.Should().BeTrue("because this portion of the data exists"); fragment.String.Should().BeNull("because this fragment is not of this type"); fragment.Array.Should().BeNull("because this fragment is not of this type"); fragment.Dictionary.Should().BeNull("because this fragment is not of this type"); fragment.Int.Should().Be(100, "because that is the stored value"); fragment.Long.Should().Be(100L, "because that is the converted value"); fragment.Float.Should().Be(100.10f, "because that is the stored value"); fragment.Double.Should().BeApproximately(100.10, 0.0001, "because that is the converted value"); fragment.Boolean.Should().Be(true, "because that is the converted value"); fragment.Date.Should().Be(DateTimeOffset.MinValue, "because that is the default value"); fragment.Value.Should().NotBeNull("because this fragment has a value"); }); }
private static void GettingStarted() { // # tag::getting-started[] // Get the database (and create it if it doesn't exist) var database = new Database("mydb"); // Create a new document (i.e. a record) in the database string id = null; using (var mutableDoc = new MutableDocument()) { mutableDoc.SetFloat("version", 2.0f) .SetString("type", "SDK"); // Save it to the database database.Save(mutableDoc); id = mutableDoc.Id; } // Update a document using (var doc = database.GetDocument(id)) using (var mutableDoc = doc.ToMutable()) { mutableDoc.SetString("language", "C#"); database.Save(mutableDoc); using (var docAgain = database.GetDocument(id)) { Console.WriteLine($"Document ID :: {docAgain.Id}"); Console.WriteLine($"Learning {docAgain.GetString("language")}"); } } // Create a query to fetch documents of type SDK // i.e. SELECT * FROM database WHERE type = "SDK" using (var query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(database)) .Where(Expression.Property("type").EqualTo(Expression.String("SDK")))) { // Run the query var result = query.Execute(); Console.WriteLine($"Number of rows :: {result.Count()}"); } // Create replicator to push and pull changes to and from the cloud var targetEndpoint = new URLEndpoint(new Uri("ws://localhost:4984/example_sg_db")); var replConfig = new ReplicatorConfiguration(database, targetEndpoint); // Add authentication replConfig.Authenticator = new BasicAuthenticator("john", "pass"); // Create replicator var replicator = new Replicator(replConfig); replicator.AddChangeListener((sender, args) => { if (args.Status.Error != null) { Console.WriteLine($"Error :: {args.Status.Error}"); } }); replicator.Start(); // Later, stop and dispose the replicator *before* closing/disposing the database // # end::getting-started[] }
void Start() { Database.SetLogLevel(LogDomain.All, LogLevel.Verbose); // Get the database (and create it if it doesn't exist) database = new Database(databaseName, new DatabaseConfiguration() { Directory = Path.Combine(Application.persistentDataPath, "CouchBase") }); // Create a new document (i.e. a record) in the database string id = null; using (MutableDocument mutableDoc = new MutableDocument("meta")) { mutableDoc.SetFloat("version", 2.0f) .SetString("type", "SDK"); // Save it to the database database.Save(mutableDoc); id = mutableDoc.Id; } statusLabel.text += "\n- Saved meta doc"; // Update a document using (Document doc = database.GetDocument("meta")) using (MutableDocument mutableDoc = doc.ToMutable()) { mutableDoc.SetFloat("version", 2.0f); database.Save(mutableDoc); using (Document docAgain = database.GetDocument("meta")) { Debug.Log($"Document ID :: {docAgain.Id}"); Debug.Log($"Version {docAgain.GetFloat("version")}"); statusLabel.text += $"\n- Retrieved: {docAgain.Id}"; } } // Create a query to fetch documents of type SDK // i.e. SELECT * FROM database WHERE type = "SDK" using (IWhere query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(database)) .Where(Expression.Property("version").EqualTo(Expression.Float(2.0f)))) { // Run the query IResultSet result = query.Execute(); int count = result.AllResults().Count; Debug.Log($"Number of rows :: {count}"); statusLabel.text += $"\n- Query result: {count}"; } #if UNITY_EDITOR Debug.LogWarning("CouchBase: In Unity Editor – Not replicating!"); return; #endif // Create replicator to push and pull changes to and from the cloud // NOTE: if Continuous is set to true, make sure to stop it and to clean up everything before exiting the application! URLEndpoint targetEndpoint = new URLEndpoint(new Uri(syncUrl)); ReplicatorConfiguration replConfig = new ReplicatorConfiguration(database, targetEndpoint) { // Add authentication Authenticator = new BasicAuthenticator(username, password), ReplicatorType = ReplicatorType.PushAndPull, Continuous = false }; // Create replicator replicator = new Replicator(replConfig); replicator.AddChangeListener((sender, args) => { if (args.Status.Error != null) { Debug.Log($"Error :: {args.Status.Error}"); } else { if (replicator.Status.Activity != ReplicatorActivityLevel.Busy && replicator.Status.Activity != ReplicatorActivityLevel.Connecting) { syncing = false; Debug.Log($"Done syncing: {replicator.Status.Activity}"); doneSyncing = true; } else { Debug.Log($"Progress: {args.Status.Progress.Completed} / {args.Status.Progress.Total}"); } } }); Debug.Log("Starting syncing..."); statusLabel.text += "\n- Starting syncing..."; replicator.Start(); syncing = true; // Later, stop and dispose the replicator *before* closing/disposing the database }
private static void GettingStarted() { // tag::getting-started[] // Get the database (and create it if it doesn't exist) var database = new Database("mydb"); // Create a new document (i.e. a record) in the database string id = null; using (var mutableDoc = new MutableDocument()) { mutableDoc.SetFloat("version", 2.0f) .SetString("type", "SDK"); // Save it to the database database.Save(mutableDoc); id = mutableDoc.Id; } // Update a document // Retrieve doc from database using (var doc = database.GetDocument(id)) // set the doc to mutable using (var mutableDoc = doc.ToMutable()) { // apply some modifications : // add a field & value mutableDoc.SetString("language", "C#"); // save the doc database.Save(mutableDoc); // retrieve again from Lite DB to check the updated doc // with the new field using (var docAgain = database.GetDocument(id)) { Console.WriteLine($"Document ID :: {docAgain.Id}"); Console.WriteLine($"Learning {docAgain.GetString("language")}"); } } // Create a query to fetch documents of type SDK // i.e. SELECT * FROM database WHERE type = "SDK" using (var query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(database)) .Where(Expression.Property("type").EqualTo(Expression.String("SDK")))) { // Run the query var result = query.Execute(); Console.WriteLine($"Number of rows :: {result.Count()}"); } // Create replicator to push and pull changes to and from the remote couchbase DB // Through Sync Gateway & WebSockets (ws) var targetEndpoint = new URLEndpoint(new Uri("ws://localhost:4984/getting-started-db")); var replConfig = new ReplicatorConfiguration(database, targetEndpoint); // Add authentication replConfig.Authenticator = new BasicAuthenticator("admin", "password"); // Create replicator (make sure to add an instance or static variable // named _Replicator) _Replicator = new Replicator(replConfig); // Manage replication error handling _Replicator.AddChangeListener((sender, args) => { if (args.Status.Error != null) { Console.WriteLine($"Error :: {args.Status.Error}"); } }); _Replicator.Start(); // Later, stop and dispose the replicator *before* closing/disposing the database // end::getting-started[] }
static void Main(string[] args) { // Get the database (and create it if it doesn't exist) var database = new Database("mydb"); // Create a new document (i.e. a record) in the database string id = null; using (var mutableDoc = new MutableDocument()) { mutableDoc.SetFloat("version", 2.0f) .SetString("type", "SDK"); // Save it to the database database.Save(mutableDoc); id = mutableDoc.Id; } // Update a document using (var doc = database.GetDocument(id)) using (var mutableDoc = doc.ToMutable()) { mutableDoc.SetString("language", "python"); database.Save(mutableDoc); using (var docAgain = database.GetDocument(id)) { Console.WriteLine($"Document ID :: {docAgain.Id}"); Console.WriteLine($"Learning {docAgain.GetString("language")}"); } } // Create a query to fetch documents of type SDK // i.e. SELECT * FROM database WHERE type = "SDK" using (var query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(database)) .Where(Expression.Property("type").EqualTo(Expression.String("SDK")))) { // Run the query var result = query.Execute(); Console.WriteLine($"Number of rows :: {result.Count()}"); } using (var query = QueryBuilder.Select( SelectResult.Expression(Meta.ID), SelectResult.Property("language")) .From(DataSource.Database(database))) { foreach (var result in query.Execute()) { Console.WriteLine($"Document Name :: {result.GetString("language")}"); } } // Create replicator to push and pull changes to and from the cloud var targetEndpoint = new URLEndpoint(new Uri("ws://localhost:4984/getting-started-db")); var replConfig = new ReplicatorConfiguration(database, targetEndpoint); // Add authentication replConfig.Authenticator = new BasicAuthenticator("john", "pass"); // Create replicator (make sure to add an instance or static variable // named _Replicator) var _Replicator = new Replicator(replConfig); _Replicator.AddChangeListener((sender, args) => { if (args.Status.Error != null) { Console.WriteLine($"Error :: {args.Status.Error}"); } }); //Path.Combine(AppContext.BaseDirectory, "CouchbaseLite"); _Replicator.Start(); }