Skip to content

Commit fc821d8

Browse files
committed
Initial commit
1 parent 6b2120d commit fc821d8

File tree

9 files changed

+401
-0
lines changed

9 files changed

+401
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
*.log
3+
node_modules
4+
_build
5+
_test

osx/mongod

54.6 MB
Binary file not shown.

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"test": "node start_db.js& node test.js",
5+
"test:win32": "start /b node start_db.js && node test.js"
6+
},
7+
"dependencies": {
8+
"mkdirp": "^0.5.1",
9+
"mongodb": "^2.2.31",
10+
"tape": "^4.8.0"
11+
}
12+
}

start_db.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
3+
const npath = require("path");
4+
const spawn = require("child_process").spawn;
5+
const mkdirp = require("mkdirp");
6+
7+
const MONGO_DBPATH = npath.join(__dirname, "_test/mongo/db");
8+
const MONGO_LOGPATH = npath.join(__dirname, "_test/mongo/log/mongod.log");
9+
const MONGO_PORT = 27777;
10+
11+
function startMongoDB({dbpath, logpath, port=27777}) {
12+
let mongod;
13+
14+
if (process.platform === "win32")
15+
mongod = npath.join(__dirname, "./win32/mongod.exe");
16+
else if (process.platform === "darwin")
17+
mongod = npath.join(__dirname, "./osx/mongod");
18+
else
19+
throw Error("Unsupported platform: " + process.platform);
20+
21+
mkdirp.sync(dbpath);
22+
23+
let args = [
24+
"--dbpath", dbpath,
25+
"--port", port
26+
];
27+
28+
if (logpath) {
29+
mkdirp.sync(npath.dirname(logpath));
30+
args = args.concat(["--logpath", logpath]);
31+
}
32+
33+
const child = spawn(mongod, args, {
34+
stdio: "inherit"
35+
});
36+
37+
child.on("exit", code => {
38+
console.log("MongoDB terminated with exit code: %d", code);
39+
});
40+
41+
return child;
42+
}
43+
44+
const mongodb = startMongoDB({
45+
dbpath: MONGO_DBPATH,
46+
logpath: MONGO_LOGPATH,
47+
port: MONGO_PORT
48+
});
49+
50+
console.log("Running MongoDB as a child process, pid = %d", mongodb.pid);

test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
3+
const MongoClient = require("mongodb").MongoClient;
4+
const test = require("tape");
5+
6+
const MONGO_PORT = 27777;
7+
8+
let db, docs;
9+
10+
test("connecting to mongod", t => {
11+
MongoClient.connect(`mongodb://localhost:${MONGO_PORT}/hello`).then(_db => {
12+
t.pass("MongoDB connection");
13+
db = _db;
14+
}).then(t.end, t.end);
15+
});
16+
17+
test("prepare", t => {
18+
docs = db.collection("documents");
19+
docs.remove({}).then(res => {
20+
t.ok(res.result.ok);
21+
}).then(t.end, t.end);
22+
});
23+
24+
test("create", t => {
25+
docs.insertMany([
26+
{_id: 1, a: 111},
27+
{_id: 2, a: 222},
28+
{_id: 3, a: 333}
29+
]).then(res => {
30+
t.equal(res.insertedCount, 3);
31+
}).then(t.end, t.end);
32+
});
33+
34+
test("read", t => {
35+
docs.find({_id: {$in: [1, 2, 3]}}).toArray().then(res => {
36+
t.deepEqual(res, [
37+
{_id: 1, a: 111},
38+
{_id: 2, a: 222},
39+
{_id: 3, a: 333}
40+
]);
41+
}).then(t.end, t.end);
42+
});
43+
44+
test("shutting down mongod", t => {
45+
db.close();
46+
t.end();
47+
});

win32/libeay32.dll

1.91 MB
Binary file not shown.

win32/mongod.exe

25.9 MB
Binary file not shown.

win32/ssleay32.dll

318 KB
Binary file not shown.

0 commit comments

Comments
 (0)