Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

several fixes #57

Merged
merged 6 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ services:
before_install:
- docker pull yglukhov/nim-base
script:
- docker run -v "$(pwd):/project" -w /project yglukhov/nim-base run "nimble install -y && nim c -r nimongo/bson && nim c -r nimongo/bsontest"
- docker run -v "$(pwd):/project" -w /project yglukhov/nim-base bash -c "nimble install -y && nimble test_ci"
20 changes: 18 additions & 2 deletions nimongo.nimble
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
# Package
description = "Pure Nim driver for MongoDB with support of synchronous and asynchronous I/O modes"
version = "0.1"
version = "0.2"
license = "MIT"
author = "Rostyslav Dzinko <rostislav.dzinko@gmail.com>"

# Dependencies
requires "scram >= 0.1.2"
requires "scram >= 0.1.7"

proc runTest(input: string) =
let cmd = "nim c -r " & input
echo "running: " & cmd
exec cmd

proc testNoMongod() =
runTest "nimongo/bson.nim"
runTest "tests/bsontest.nim"

task test, "tests":
testNoMongod()
runTest "tests/nimongotest.nim"

task test_ci, "tests for CI":
testNoMongod()
2 changes: 0 additions & 2 deletions nimongo/bson.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import strutils
import times
import tables

import timeit

# ------------- type: BsonKind -------------------#

type BsonKind* = char
Expand Down
20 changes: 12 additions & 8 deletions nimongo/mongo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import uri
import os

import bson except `()`
import timeit

import scram/client

Expand Down Expand Up @@ -376,7 +375,6 @@ proc performFindAsync(f: Cursor[AsyncMongo], numberToReturn, numberToSkip: int32

proc all*(f: Cursor[Mongo]): seq[Bson] =
## Perform MongoDB query and return all matching documents
result = @[]
for doc in f.performFind(f.nlimit, f.nskip):
result.add(doc)

Expand Down Expand Up @@ -415,6 +413,18 @@ iterator items*(f: Cursor): Bson =
for doc in f.performFind(f.nlimit, f.nskip):
yield doc

iterator itemsForceSync*(f: Cursor[AsyncMongo]): Bson =
var count = 0'i32
var limit = f.nlimit
if limit == 0: # pending https://github.com/SSPkrolik/nimongo/issues/64
limit = type(f.nlimit).high
while count < limit:
let docs = waitFor f.performFindAsync(1, f.nskip + count)
if docs.len == 0:
break
count.inc
yield docs[0]

proc isMaster*(sm: Mongo): bool =
## Perform query in order to check if connected Mongo instance is a master
return sm["admin"]["$cmd"].makeQuery(%*{"isMaster": 1}).one()["ismaster"]
Expand All @@ -428,15 +438,13 @@ proc isMaster*(am: AsyncMongo): Future[bool] {.async.} =
proc listDatabases*(sm: Mongo): seq[string] =
## Return list of databases on the server
let response = sm["admin"]["$cmd"].makeQuery(%*{"listDatabases": 1}).one()
result = @[]
if response.isReplyOk:
for db in response["databases"].items():
result.add(db["name"].toString())

proc listDatabases*(am: AsyncMongo): Future[seq[string]] {.async.} =
## Return list of databases on the server via async client
let response = await am["admin"]["$cmd"].makeQuery(%*{"listDatabases": 1}).one()
result = @[]
if response.isReplyOk:
for db in response["databases"].items():
result.add(db["name"].toString())
Expand Down Expand Up @@ -468,7 +476,6 @@ proc createCollection*(db: Database[AsyncMongo], name: string, capped: bool = fa
proc listCollections*(db: Database[Mongo], filter: Bson = %*{}): seq[string] =
## List collections inside specified database
let response = db["$cmd"].makeQuery(%*{"listCollections": 1'i32}).one()
result = @[]
if response.isReplyOk:
for col in response["cursor"]["firstBatch"]:
result.add(col["name"])
Expand All @@ -478,7 +485,6 @@ proc listCollections*(db: Database[AsyncMongo], filter: Bson = %*{}): Future[seq
let
request = %*{"listCollections": 1'i32}
response = await db["$cmd"].makeQuery(request).one()
result = @[]
if response.isReplyOk:
for col in response["cursor"]["firstBatch"]:
result.add(col["name"])
Expand Down Expand Up @@ -573,7 +579,6 @@ proc unique*(f: Cursor[Mongo], key: string): seq[string] =
}
response = f.collection.db["$cmd"].makeQuery(request).one()

result = @[]
if response.isReplyOk:
for item in response["values"].items():
result.add(item.toString())
Expand All @@ -590,7 +595,6 @@ proc unique*(f: Cursor[AsyncMongo], key: string): Future[seq[string]] {.async.}
}
response = await f.collection.db["$cmd"].makeQuery(request).one()

result = @[]
if response.isReplyOk:
for item in response["values"].items():
result.add(item.toString())
Expand Down
4 changes: 3 additions & 1 deletion nimongo/bsontest.nim → tests/bsontest.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Tests for bson.nim module
import unittest

import bson
import nimongo/bson

suite "BSON serializer/deserializer test suite":

Expand Down Expand Up @@ -29,6 +29,8 @@ suite "BSON serializer/deserializer test suite":
}
check(doc["double"] == 5436.5436)
check(doc["stringkey"] == "stringvalue")
check doc["stringkey"] is Bson
check(doc["stringkey"].string == "stringvalue")
check(doc["document"]["double"] == 5436.5436)
check(doc["document"]["key"] == "value")
check(doc["array"][0] == 1'i64)
Expand Down
6 changes: 4 additions & 2 deletions nimongo/mongotest.nim → tests/mongotest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import strutils
import times
import unittest

import bson
import mongo
import nimongo/bson
import nimongo/mongo

# TODO: unused
import timeit

const
Expand Down
File renamed without changes.
File renamed without changes.