Skip to content

Commit 178f0b1

Browse files
Merge pull request #32 from matteobortolazzo/StringQuery
Support for direct queries without using the LINQ api
2 parents fbf6bad + 552da7f commit 178f0b1

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ await rebels.CreateOrUpdateAsync(rebel);
205205
await rebels.DeleteAsync(rebel);
206206
var rebel = await rebels.FindAsync(id);
207207
var rebel = await rebels.FindAsync(id, withConflicts: true);
208-
var rebels = await rebels.FindManyAsync(ids);
208+
var list = await rebels.FindManyAsync(ids);
209+
var list = await rebels.QueryAsync(someMangoJson);
210+
var list = await rebels.QueryAsync(someMangoObject);
209211
// Bulk
210212
await rebels.CreateOrUpdateRangeAsync(moreRebels);
211213
// Utils

src/CouchDB.Driver/CouchDatabase.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Linq;
1212
using System.Linq.Expressions;
13+
using System.Net.Http;
1314
using System.Threading.Tasks;
1415

1516
namespace CouchDB.Driver
@@ -237,6 +238,43 @@ public async Task<TSource> FindAsync(string docId, bool withConflicts = false)
237238
}
238239

239240
/// <summary>
241+
/// Finds all documents matching the MangoQuery.
242+
/// </summary>
243+
/// <param name="mangoQueryJson">The JSON representing the Mango query.</param>
244+
/// <retuns>A task that represents the asynchronous operation. The task result contains a List<T> that contains elements from the database.</retuns>
245+
public Task<List<TSource>> QueryAsync(string mangoQueryJson)
246+
{
247+
return SendQueryAsync(r => r
248+
.WithHeader("Content-Type", "application/json")
249+
.PostStringAsync(mangoQueryJson));
250+
}
251+
252+
/// <summary>
253+
/// Finds all documents matching the MangoQuery.
254+
/// </summary>
255+
/// <param name="mangoQuery">The object representing the Mango query.</param>
256+
/// <retuns>A task that represents the asynchronous operation. The task result contains a List<T> that contains elements from the database.</retuns>
257+
public Task<List<TSource>> QueryAsync(object mangoQuery)
258+
{
259+
return SendQueryAsync(r => r
260+
.PostJsonAsync(mangoQuery));
261+
}
262+
263+
private async Task<List<TSource>> SendQueryAsync(Func<IFlurlRequest, Task<HttpResponseMessage>> requestFunc)
264+
{
265+
var request = NewRequest()
266+
.AppendPathSegment("_find");
267+
268+
var message = requestFunc(request);
269+
270+
var findResult = await message
271+
.ReceiveJson<FindResult<TSource>>()
272+
.SendRequestAsync()
273+
.ConfigureAwait(false);
274+
275+
return findResult.Docs.ToList();
276+
}
277+
240278
/// Finds all documents with given IDs.
241279
/// </summary>
242280
/// <param name="docIds">The collection of documents IDs.</param>

tests/CouchDB.Driver.UnitTests/Database_Tests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,43 @@ public async Task CouchList()
153153
}
154154
}
155155

156+
[Fact]
157+
public async Task QueryJson()
158+
{
159+
using (var httpTest = new HttpTest())
160+
{
161+
var expected = new List<Rebel>() { new Rebel { Id = Guid.NewGuid().ToString() } };
162+
httpTest.RespondWithJson(new { Docs = expected });
163+
164+
var query = @"{""selector"":{""age"":19}}";
165+
var result = await _rebels.QueryAsync(query);
166+
httpTest
167+
.ShouldHaveCalled("http://localhost/rebels/_find")
168+
.WithVerb(HttpMethod.Post)
169+
.WithRequestBody(@"{""selector"":{""age"":19}}");
170+
Assert.Equal(expected.Count, result.Count);
171+
Assert.Equal(expected[0].Id, result[0].Id);
172+
}
173+
}
174+
[Fact]
175+
public async Task QueryObject()
176+
{
177+
using (var httpTest = new HttpTest())
178+
{
179+
var expected = new List<Rebel>() { new Rebel { Id = Guid.NewGuid().ToString() } };
180+
httpTest.RespondWithJson(new { Docs = expected });
181+
182+
var query = new { selector = new { age = 19 } };
183+
var result = await _rebels.QueryAsync(query);
184+
httpTest
185+
.ShouldHaveCalled("http://localhost/rebels/_find")
186+
.WithVerb(HttpMethod.Post)
187+
.WithRequestBody(@"{""selector"":{""age"":19}}");
188+
Assert.Equal(expected.Count, result.Count);
189+
Assert.Equal(expected[0].Id, result[0].Id);
190+
}
191+
}
192+
156193
#endregion
157194

158195
#region Bulk

0 commit comments

Comments
 (0)