public static WriteJson ( ICanJson doc ) : string | ||
doc | ICanJson | |
return | string |
/// <summary> /// This is a bulk key request, not to be confused with requests using complex keys, see Key(). /// </summary> public CouchQuery Keys(IList <object> keys) { var bulk = new CouchBulkKeys(keys.ToArray()); Data(CouchDocument.WriteJson(bulk)); return(this); }
/// <summary> /// Create or update a list of ICouchDocuments in CouchDB. Uses POST and CouchDB will /// allocate new ids if the documents lack them. /// </summary> /// <param name="documents">List of documents to store.</param> /// <remarks>POST may be problematic in some environments.</remarks> public void SaveDocuments(IEnumerable <ICouchDocument> documents, bool allOrNothing) { var bulk = new CouchBulkDocuments(documents); try { var result = Request("_bulk_docs") .Data(CouchDocument.WriteJson(bulk)) .Query("?all_or_nothing=" + allOrNothing.ToString().ToLower()) .PostJson() .Parse <JArray>(); int index = 0; foreach (var document in documents) { document.Id = (result[index])["id"].Value <string>(); document.Rev = (result[index])["rev"].Value <string>(); ++index; } } catch (WebException e) { throw CouchException.Create("Failed to create bulk documents", e); } }
/// <summary> /// This is a bulk key request, not to be confused with requests using complex keys, see Key(). /// </summary> public CouchQuery Keys(object[] keys) { var bulk = new CouchBulkKeys(keys); Data(CouchDocument.WriteJson(bulk)); return(this); }
/// <summary> /// Create a given ICouchDocument in CouchDB. Uses POST and CouchDB will allocate a new id and overwrite any existing id. /// </summary> /// <param name="document">Document to store.</param> /// <returns>Document with Id and Rev set.</returns> /// <remarks>POST which may be problematic in some environments.</remarks> public ICouchDocument CreateDocument(ICouchDocument document) { try { JObject result = Request().Data(CouchDocument.WriteJson(document)).PostJson().Check("Failed to create document").Result(); document.Id = result["id"].Value <string>(); document.Rev = result["rev"].Value <string>(); return(document); } catch (WebException e) { throw CouchException.Create("Failed to create document", e); } }
/// <summary> /// Write a CouchDocument or ICouchDocument, it may already exist in db and will then be overwritten. /// </summary> /// <param name="document">Couch document</param> /// <param name="batch">True if we don't want to wait for flush (commit).</param> /// <returns>Couch Document with new Rev set.</returns> /// <remarks>This relies on the document to already have an id.</remarks> public ICouchDocument WriteDocument(ICouchDocument document, bool batch) { if (document.Id == null) { throw CouchException.Create( "Failed to write document using PUT because it lacks an id, use CreateDocument instead to let CouchDB generate an id"); } JObject result = Request(document.Id).Query(batch ? "?batch=ok" : null).Data(CouchDocument.WriteJson(document)).Put().Check("Failed to write document").Result(); document.Id = result["id"].Value <string>(); // Not really needed document.Rev = result["rev"].Value <string>(); return(document); }
/// <summary> /// Delete documents in bulk fashion. /// </summary> public void DeleteDocuments(ICanJson bulk) { try { var result = Request("_bulk_docs").Data(CouchDocument.WriteJson(bulk)).PostJson().Parse <JArray>(); for (int i = 0; i < result.Count(); i++) { //documents[i].id = (result[i])["id"].Value<string>(); //documents[i].rev = (result[i])["rev"].Value<string>(); if ((result[i])["error"] != null) { throw CouchException.Create(string.Format(CultureInfo.InvariantCulture, "Document with id {0} was not deleted: {1}: {2}", (result[i])["id"].Value <string>(), (result[i])["error"], (result[i])["reason"])); } } } catch (WebException e) { throw CouchException.Create("Failed to bulk delete documents", e); } }
public IEnumerable <T> GetArbitraryDocuments <T>(IEnumerable <string> documentIds, Func <T> ctor) { var bulk = new CouchBulkKeys(documentIds.Cast <object>()); return(QueryAllDocuments().Data(CouchDocument.WriteJson(bulk)).IncludeDocuments().GetResult().ArbitraryDocuments(ctor)); }
public IEnumerable <T> GetDocuments <T>(IEnumerable <string> documentIds) where T : ICouchDocument, new() { var bulk = new CouchBulkKeys(documentIds.Cast <object>()); return(QueryAllDocuments().Data(CouchDocument.WriteJson(bulk)).IncludeDocuments().GetResult().Documents <T>()); }