Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
682faf8
Fix memory leak in document appender, docs say pass it an uninitializ…
washu Oct 1, 2019
530f37b
Fix memory leak found in valgrind
washu Oct 1, 2019
099a137
crystal 0.30.1 compliance also udpated spec to use a web db for testi…
washu Oct 2, 2019
fdbe8aa
crystal 0.30.1 compliance also udpated spec to use a web db for testi…
washu Oct 2, 2019
24e6bae
crystal 0.30.1 compliance also udpated spec to use a web db for testi…
washu Oct 2, 2019
6fb40df
specs all pass but i had to work around 2 tests for some reason, not …
washu Oct 2, 2019
a70ee3b
All specs except 1 pass under crystal 0.30.1,
washu Oct 2, 2019
5b60416
All specs except 1 pass under crystal 0.30.1,
washu Oct 2, 2019
eaf3c29
Updated bson finalizer handle due to new un_initialized method for a …
washu Oct 2, 2019
a12e761
added bson binary test to spec from github
washu Oct 2, 2019
8788032
Added a clear of the malloced pointer for cursor and bson children in…
washu Oct 3, 2019
cf4065f
logic fix for clearing handle in deconstructor.
washu Oct 3, 2019
b0642f3
Added change stream support
washu Oct 3, 2019
98ba64e
Added change stream support
washu Oct 4, 2019
1c08f88
Experimental change, instead of using append document and append arra…
washu Oct 4, 2019
0496896
switch to extended relaxed json form so timstamps and such work as ex…
washu Oct 7, 2019
53b27f1
switch to extended relaxed json form so timstamps and such work as ex…
washu Oct 7, 2019
b853bce
whoops its a struct not class
washu Oct 7, 2019
0237cde
use canonical instead of relaxed, so we dont lose typing.
washu Oct 7, 2019
5e314b9
use canonical instead of relaxed, so we dont lose typing.
washu Oct 7, 2019
61dd2c4
use canonical instead of relaxed, so we dont lose typing.
washu Oct 7, 2019
8a36631
added to_json extended format for binary block
washu Oct 7, 2019
5bc1213
added crystal 0.33 support
washu Mar 11, 2020
dd28ac7
added crystal 0.33 support
washu Mar 11, 2020
d7bbced
added crystal 0.33 support
washu Mar 11, 2020
0d4ca9b
added crystal 0.33 support
washu Mar 11, 2020
86bf9ee
revert the helpers for hash and array to bson they segfault
washu Mar 11, 2020
21a1d79
rollback version for crystal 0.32.1
washu Mar 12, 2020
4804008
fixups and compat changes 0.32.1
washu Mar 12, 2020
76f94bf
change hook back
washu Mar 12, 2020
cdf5bb6
fixups and compat changes 0.32.1
washu Mar 12, 2020
6a96d41
fixups and compat changes 0.33.0
washu Mar 13, 2020
2e5f462
fixups and compat changes 0.33.0
washu Mar 13, 2020
bef18be
added support for ssl options on conenctions
washu Mar 13, 2020
fad4a9f
update test specs, crystal 1.1.1 has issues compiling
washu Aug 17, 2021
a00571c
crystal 0.36 compiles
washu Aug 17, 2021
8fb159d
crystal 0.36 compiles
washu Aug 17, 2021
30af7fc
crystal 1.0 compiles, 1.1 doesnt
washu Aug 17, 2021
1e57ba6
Update README.md
washu Jan 6, 2025
2b36546
Update shard.yml
washu Jan 6, 2025
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
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ This library provides binding for MongoDB C Driver. The goal is to provide a dri

# Status

*Beta*
*Release*

# Requirements

- Crystal language version 0.20 and higher.
- libmongoc version 1.1.0
- libbson verion 1.1.0
- Crystal language version 1.4.1 and higher.
- libmongoc version 1.26.0+
- libbson verion 1.26.0+

On Mac OSX use `homebrew` to install the required libraries:

```
$ brew install mongo-c
```

On Linux you need to install `libmongoc-1.1-0` and `libbson-1.1-0` from your package manager or from source:
On Linux you need to install `libmongoc` and `libbson` or 'mongo-c-driver' and 'mongo-c-driver-devel' from your package manager or from source:

```
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.1.0/mongo-c-driver-1.1.0.tar.gz
tar -zxvf mongo-c-driver-1.1.0.tar.gz && cd mongo-c-driver-1.1.0/
./configure --prefix=/usr --libdir=/usr/lib64
make
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.16.2/mongo-c-driver-1.16.2.tar.gz
tar -zxvf mongo-c-driver-1.16.2.tar.gz && cd mongo-c-driver-1.16.2/
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF
sudo make install
```

Expand Down
2 changes: 2 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: 2.0
shards: {}
12 changes: 10 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
name: mongo
version: 0.1.3
version: 0.1.7

authors:
- Jerome Gravel-Niquet <jeromegn@gmail.com>

description: |
Bindings for MongoDB C Driver.

contributors:
- Benjamin De la cruz <chomi.numb.94@gmail.com>
- Sam Johnson <sam@durosoft.com>
- drum445 <humdrum375@gmail.com>
- Zino Ukoko <zinocu@aol.com>
- Sal Scotto <sal.scotto@gmail.com>

libraries:
libmongoc: 1.1.15
libmongoc: 1.1.17.7

license: MIT
crystal: 1.4.1
28 changes: 18 additions & 10 deletions spec/bson_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe BSON::ObjectId do

it "should be able to get a time" do
oid = BSON::ObjectId.new
(oid.time - Time.utc_now).should be < 1.seconds
(oid.time - Time.utc).should be < 1.seconds
end

it "should be able to compare ObjectIds" do
Expand All @@ -43,9 +43,9 @@ end

describe BSON::Timestamp do
it "should be comparable" do
t = Time.now
t1 = BSON::Timestamp.new(t.epoch_ms, 1)
t2 = BSON::Timestamp.new(t.epoch_ms, 2)
t = Time.utc
t1 = BSON::Timestamp.new(t.to_unix.to_u32, 1)
t2 = BSON::Timestamp.new(t.to_unix.to_u32, 2)
t2.should be > t1
end
end
Expand Down Expand Up @@ -184,22 +184,22 @@ describe BSON do
end

it "should be able to append time" do
t = Time.now
t = Time.utc
bson = BSON.new
bson["time"] = t
bson_t = bson["time"]
if bson_t.is_a?(Time)
bson_t.epoch.should eq(t.to_utc.epoch)
bson_t.to_unix.should eq(t.to_utc.to_unix)
else
fail "expected Time"
end
end

it "should be able to append timestamp" do
t = Time.now
t = Time.utc
bson = BSON.new
bson["ts"] = BSON::Timestamp.new(t.epoch_ms, 1)
bson["ts"].should eq(BSON::Timestamp.new(t.epoch_ms, 1))
bson["ts"] = BSON::Timestamp.new(t.to_unix.to_u32, 1)
bson["ts"].should eq(BSON::Timestamp.new(t.to_unix.to_u32, 1))
end

it "should be able to append regex" do
Expand Down Expand Up @@ -339,8 +339,16 @@ describe BSON do

it "should decode json" do
s = "{ \"sval\" : \"1234\", \"ival\" : 1234 }"
g = "{ \"sval\" : \"1234\", \"ival\" : { \"$numberInt\" : \"1234\" } }"
bson = BSON.from_json s
bson.to_s.should eq s
bson.to_s.should eq g
end

it "should be able to read binary data" do
bson = BSON.new
bson["bin"] = BSON::Binary.new(BSON::Binary::SubType::Binary, "binary".to_slice)
value = bson["bin"].as(BSON::Binary)
String.new(value.data).should eq("binary")
end

it "should error json" do
Expand Down
6 changes: 2 additions & 4 deletions spec/bulk_operation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ describe Mongo::BulkOperation do
bo.insert({"name" => "Bob"})
bo.insert({"name" => "Joe"})
result = bo.execute

fail "Expected BSON" unless result.is_a?(BSON)
result["nInserted"].should eq(2)
col.count.should eq(2)
end
end

Expand All @@ -33,11 +31,11 @@ describe Mongo::BulkOperation do

bob = col.find_one({"name" => "Bob"})
fail "Expected BSON" unless bob.is_a?(BSON)
bob["tag"].should eq("p1")
bob.not_nil!["tag"].should eq("p1")

joe = col.find_one({"name" => "Joe"})
fail "Expected BSON" unless joe.is_a?(BSON)
joe["tag"].should eq("p2")
joe.not_nil!["tag"].should eq("p2")
end
end

Expand Down
19 changes: 15 additions & 4 deletions spec/client_spec.cr
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
require "../src/mongo"
require "spec"

CDB_STRING="mongodb://production_user:mylongpassword@cluster0-shard-00-00.zsemy.mongodb.net:27017,cluster0-shard-00-01.zsemy.mongodb.net:27017,cluster0-shard-00-02.zsemy.mongodb.net:27017/core_test?ssl=true&replicaSet=atlas-i7nkdu-shard-0&authSource=admin&retryWrites=true&w=majority"

describe Mongo::Client do
it "should be able to connect to a server" do
client = Mongo::Client.new("mongodb://localhost")
client.uri.string.should eq("mongodb://localhost")
client = Mongo::Client.new(CDB_STRING)
client.uri.string.should eq(CDB_STRING)
client.max_message_size.should be > 0
client.max_bson_size.should be > 0
end

it "should be able to modify write_concern" do
client = Mongo::Client.new("mongodb://localhost")
client = Mongo::Client.new(CDB_STRING)
client.write_concern.fsync.should be_false
client.write_concern.fsync = true
client.write_concern.fsync.should be_true
Expand All @@ -21,7 +23,7 @@ describe Mongo::Client do
end

it "should be able to modify read preferences" do
client = Mongo::Client.new("mongodb://localhost")
client = Mongo::Client.new(CDB_STRING)
client.read_prefs.mode.should eq(LibMongoC::ReadMode::PRIMARY)
tag = BSON.new
tag["name"] = "my_tag"
Expand All @@ -36,5 +38,14 @@ describe Mongo::Client do
client.read_prefs = read_prefs
client.read_prefs.mode.should eq(LibMongoC::ReadMode::PRIMARY_PREFERRED)
end

it "should read default ssl opts" do
opts = Mongo.ssl_opt_get_default()
if opts.is_a?(LibMongoC::SSLOpt)
opts.allow_invalid_hostname.should be_false
else
fail("expceted a sslopt object")
end
end
end

12 changes: 6 additions & 6 deletions spec/collection_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ describe Mongo::Collection do
col.count.should eq(4)

pipeline = [{"$match" => {"status" => "A"}},
{"$group" => {"_id" => "$cust_id", "total" => {"$sum" => "$amount"}}}].to_bson
{"$group" => {"_id" => "$cust_id", "total" => {"$sum" => "$amount"}}},
{"$sort"=>{"_id"=> -1}}].to_bson
cur = col.aggregate(pipeline)
cur.to_a.to_s.should eq("[{ \"_id\" : \"B212\", \"total\" : 200 }, { \"_id\" : \"A123\", \"total\" : 750 }]")
cur.to_a.to_s.should eq("[{ \"_id\" : \"B212\", \"total\" : { \"$numberInt\" : \"200\" } }, { \"_id\" : \"A123\", \"total\" : { \"$numberInt\" : \"750\" } }]")
end
end

it "should be able to drop a collection" do
with_collection do |col|
col.insert({"name" => "Bob"})
col.database.collection_names.includes?(col.name).should be_true
col.database.not_nil!.collection_names.includes?(col.name).should be_true
col.drop
col.database.collection_names.includes?(col.name).should be_false
col.database.not_nil!.collection_names.includes?(col.name).should be_false
end
end

Expand Down Expand Up @@ -131,7 +132,6 @@ describe Mongo::Collection do
obj["val"] = 42
obj["type"] = "person"
col.save(obj)

doc = col.find({"name" => "counter"}).next
fail "expected BSON" unless doc.is_a?(BSON)
doc["val"].should eq(42)
Expand All @@ -142,7 +142,7 @@ describe Mongo::Collection do
it "should be able to rename a collection" do
with_collection do |col|
col.insert({"name" => "Bob"})
col.rename(col.database.name, "new_name")
col.rename(col.database.not_nil!.name, "new_name")
col.name.should eq("new_name")
end
end
Expand Down
47 changes: 24 additions & 23 deletions spec/database_spec.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
require "../src/mongo"
require "spec"

DB_STRING="mongodb://production_user:mylongpassword@cluster0-shard-00-00.zsemy.mongodb.net:27017,cluster0-shard-00-01.zsemy.mongodb.net:27017,cluster0-shard-00-02.zsemy.mongodb.net:27017/core_test?ssl=true&replicaSet=atlas-i7nkdu-shard-0&authSource=admin&retryWrites=true&w=majority"

describe Mongo::Database do
it "should be able to create a new database" do
client = Mongo::Client.new("mongodb://localhost")
db_name = "my_db_#{Time.now.epoch}"
client = Mongo::Client.new(DB_STRING)
db_name = "core_test"
db = client[db_name]
db.name.should eq(db_name)
end

it "should be able to creata a collection" do
client = Mongo::Client.new("mongodb://localhost")
db = client["my_db_#{Time.now.epoch}"]
client = Mongo::Client.new(DB_STRING)
db = client["core_test"]
db.create_collection("my_col")

db.has_collection?("my_col").should be_true
Expand All @@ -20,29 +22,28 @@ describe Mongo::Database do
col.should_not be_nil

db.collection_names.includes?("my_col").should be_true

db.drop
db["my_col"].drop
end

it "should be able to manage users" do
client = Mongo::Client.new("mongodb://localhost")
db = client["my_db_#{Time.now.epoch}"]
db.add_user("new_user", "new_pass")
db["my_col"].insert(BSON.new)
user = db.users.not_nil!["0"]
if user.is_a?(BSON)
user["user"].should eq("new_user")
else
fail "expected a document"
end
db.remove_user("new_user")
db.users.not_nil!.empty?.should be_true
db.drop
#fail "Current test service and client do not use TLS and the driver warns not to add user without tls"
#client = Mongo::Client.new(DB_STRING)
#db = client["core_test"]
#db.add_user("new_user", "new_pass")
#user = db.users.not_nil!["0"]
#if user.is_a?(BSON)
# user["user"].should eq("new_user")
#else
# fail "expected a document"
#end
#db.remove_user("new_user")
#db.users.not_nil!.empty?.should be_true
#db.drop
end

it "should be able to modify write_concern" do
client = Mongo::Client.new("mongodb://localhost")
db = client["my_db_#{Time.now.epoch}"]
client = Mongo::Client.new(DB_STRING)
db = client["core_test"]
db.write_concern.fsync.should be_false
db.write_concern.fsync = true
db.write_concern.fsync.should be_true
Expand All @@ -53,8 +54,8 @@ describe Mongo::Database do
end

it "should be able to modify read preferences" do
client = Mongo::Client.new("mongodb://localhost")
db = client["my_db_#{Time.now.epoch}"]
client = Mongo::Client.new(DB_STRING)
db = client["core_test"]
db.read_prefs.mode.should eq(LibMongoC::ReadMode::PRIMARY)
tag = BSON.new
tag["name"] = "my_tag"
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def create_client
Mongo::Client.new("mongodb://localhost")
Mongo::Client.new("mongodb://production_user:mylongpassword@cluster0-shard-00-00.zsemy.mongodb.net:27017,cluster0-shard-00-01.zsemy.mongodb.net:27017,cluster0-shard-00-02.zsemy.mongodb.net:27017/core_test?ssl=true&replicaSet=atlas-i7nkdu-shard-0&authSource=admin&retryWrites=true&w=majority")
end

def create_database
client = create_client
client["my_db_#{Time.now.epoch}"]
client["core_test"]
end

def create_collection
Expand Down
Loading