public TDocument GetOrCreate <TDocument>( ref bool pre_existing, string uid) where TDocument : DbObj { //return _db.GetDocument<TDocument>( uid ); TDocument doc; if (_db.DocumentExists(uid)) { pre_existing = true; doc = _db.GetDocument <TDocument>(uid); Debug.Assert( doc.Id.Equals(uid), "expected equal ids"); } else { pre_existing = false; doc = (TDocument)Activator.CreateInstance( typeof(TDocument), uid); } return(doc); }
private Yield LoadContactsHelper(Result <ViewResult <string, string, Contact> > aResult) { Result <bool> exists = new Result <bool>(); yield return(theDatabase.DocumentExists("_design/contactview", exists)); if (exists.HasException) { aResult.Throw(exists.Exception); yield break; } if (!exists.Value) { CouchDesignDocument view = new CouchDesignDocument("contactview"); view.Views.Add("all", new CouchView( @"function(doc){ if(doc.type && doc.type == 'contact'){ emit(doc.lastName, doc.firstName) } }" )); Result <CouchDesignDocument> creationResult = new Result <CouchDesignDocument>(); yield return(theDatabase.CreateDocument(view, creationResult)); if (creationResult.HasException) { aResult.Throw(creationResult.Exception); yield break; } } var viewRes = new Result <ViewResult <string, string, Contact> >(); yield return(theDatabase.GetView("contactview", "all", viewRes)); if (viewRes.HasException) { aResult.Throw(viewRes.Exception); yield break; } aResult.Return(viewRes.Value); }
static DbModel GetDbModel( CouchDatabase db, Element projectInfo) { string uid = projectInfo.UniqueId; DbModel dbModel; if (db.DocumentExists(uid)) { dbModel = db.GetDocument <DbModel>(uid); Debug.Assert( dbModel.Id.Equals(projectInfo.UniqueId), "expected equal ids"); dbModel.Description = Util.ElementDescription( projectInfo); dbModel.Name = projectInfo.Document.Title; dbModel = db.UpdateDocument <DbModel>( dbModel); } else { dbModel = new DbModel(uid); dbModel.Description = Util.ElementDescription( projectInfo); dbModel.Name = projectInfo.Name; dbModel = db.CreateDocument <DbModel>(dbModel); } return(dbModel); }