Skip to content

Commit 96f7c6c

Browse files
authored
Merge pull request #13 from aliok/RHMAP-9849-document_custom_id
RHMAP-9849 documents w/ custom id
2 parents e85b307 + 7331613 commit 96f7c6c

File tree

8 files changed

+195
-31
lines changed

8 files changed

+195
-31
lines changed

README.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ Add fh-db as a dependency to your module and require it where required, like any
3030
### Start Mongo Server
3131
Start the docker machine for the version of mongo you wish to test:
3232
```bash
33-
docker run -d -p 27017:27017 mongo:2.4
33+
docker run -d -p 27017:27017 mongo:2.6
3434
```
3535

3636
### Note for docker-machine users
3737
You will need to connect your localhost:27017 to the docker-machine:27017, do this with the following command:
3838
```
39-
VBoxManage controlvm <docker-machine-name> natpf1 "docker-mongo,tcp,127.0.0.1,27017,,27017"
39+
VBoxManage controlvm `docker-machine active` natpf1 "docker-mongo,tcp,127.0.0.1,27017,,27017"
4040
```
4141
In the above `docker-mongo` is the name of the rule, this is important to remember, in order to remove it when not required.
4242

4343
Remove the above rule as follows:
4444
```
45-
VBoxManage controlvm dev natpf1 delete docker-mongo
45+
VBoxManage controlvm `docker-machine active` natpf1 delete docker-mongo
4646
```
4747

4848

@@ -52,8 +52,9 @@ connect to Mongo:
5252
mongo
5353
```
5454

55-
#### For versions > 3.x
56-
You will need to update the authentication to work with fh-db, which is done as follows:
55+
#### For MongoDB versions > 3.x
56+
Well, instead of MongoDB 2.6 stated above if you used MongoDB 3.x, you will need to update the authentication
57+
to work with fh-db, which is done as follows:
5758
```
5859
use admin
5960
db.system.users.remove({})
@@ -73,12 +74,7 @@ mongo
7374
```
7475

7576
#### Add the admin user:
76-
##### 2.4.x
77-
```
78-
use admin
79-
db.addUser('admin', 'admin');
80-
```
81-
##### >= 2.6
77+
8278
```
8379
use admin
8480
db.createUser({user: 'admin', pwd: 'admin', roles: ['root']})
@@ -93,13 +89,7 @@ mongo admin -u admin -p admin
9389
#### Add the ditchuser
9490

9591
Log in as the admin user, if you are not yet, then run:
96-
##### 2.4.x
97-
```
98-
use fh-ditch
99-
db.addUser('ditchuser', 'ditchpassword');
100-
```
10192

102-
##### >= 2.6
10393
```
10494
use fh-ditch
10595
db.createUser({user: 'ditchuser', pwd: 'ditchpassword', roles: ['dbAdmin']})

lib/ditcher.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ Ditcher.prototype.doRead = function (params, callback) {
354354
try {
355355
query = {"_id": self.database.createObjectIdFromHexString(params.guid)};
356356
} catch (err) {
357-
return callback(new Error('Error creating ObjectId from string: ' + JSON.stringify(err)));
357+
// if the guid passed is not a hex ObjectID, use it as is
358+
query = {"_id": params.guid};
358359
}
359360
self.logger.debug("Ditcher.read/query: " + JSON.stringify(query));
360361

@@ -395,7 +396,8 @@ Ditcher.prototype.doUpdate = function (params, callback) {
395396
try {
396397
criteria = {"_id": self.database.createObjectIdFromHexString(params.guid)};
397398
} catch (err) {
398-
return callback(new Error('Error creating ObjectId from string: ' + JSON.stringify(err)));
399+
// if the guid passed is not a hex ObjectID, use it as is
400+
criteria = {"_id": params.guid};
399401
}
400402
self.logger.debug("Ditcher.update/criteria: " + JSON.stringify(criteria));
401403

lib/fhmongodb.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,19 @@ Database.prototype.remove = function (collectionName, id, callback) {
303303
var self = this;
304304

305305
this.db.collection(collectionName, function (err, collection) {
306-
if (null !== err) return callback(err, null);
306+
if (null !== err){
307+
return callback(err, null);
308+
}
309+
310+
// if a GUID id is passed as strings, convert it to an ObjectID
311+
try{
312+
id = self.createObjectIdFromHexString(id);
313+
}catch(err){
314+
// if not, use the id as is
315+
}
307316

308317
collection.remove({
309-
_id: self.createObjectIdFromHexString(id)
318+
_id: id
310319
}, function (err, docs) {
311320
return callback(err, docs);
312321
});
@@ -379,7 +388,9 @@ Database.prototype.countCollection = function (collectionName, callback) {
379388
if (null === this.db) return callback(new Error("no database open"), null);
380389

381390
this.db.collection(collectionName, function (err, collection) {
382-
if (err) return callback(err, null);
391+
if (err){
392+
return callback(err, null);
393+
}
383394
collection.count(callback);
384395
});
385396
};

npm-shrinkwrap.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "fh-db",
33
"description": "FeedHenry Database Library",
4-
"version": "1.2.2",
4+
"version": "1.2.3",
55
"repository": {
66
"type": "git",
77
"url": "git@github.com:feedhenry/fh-db.git"

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sonar.projectKey=fh-db
22
sonar.projectName=fh-db-nightly-master
3-
sonar.projectVersion=1.2.2
3+
sonar.projectVersion=1.2.3
44

55
sonar.sources=./lib
66
sonar.tests=./test

test/test_ditcher.js

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ var testBasicOperationsOwnDatabase = function(cb){
410410
testDelete,
411411
testListCollections,
412412
testDeleteAgain,
413-
testDeleteInvalidGuid,
413+
testDeleteNonHexGuid,
414414
testList1,
415415
testList2,
416416
testList3,
@@ -570,8 +570,8 @@ var testDeleteAgain = function (created, cb) {
570570
});
571571
};
572572

573-
var testDeleteInvalidGuid = function(created, cb) {
574-
logger.info("test testDeleteInvalidGuid()");
573+
var testDeleteNonHexGuid = function(created, cb) {
574+
logger.info("test testDeleteNonHexGuid()");
575575
var deleteReq = {
576576
"__fhdb" : "123456789",
577577
"type" : "fh_test_collection",
@@ -583,7 +583,8 @@ var testDeleteInvalidGuid = function(created, cb) {
583583
}
584584

585585
ditch.doDelete(deleteReq, function(err, deleteRes) {
586-
assert.ok(err, "Should have called back with error");
586+
assert.ok(!err);
587+
assert.equal(JSON.stringify(deleteRes), "{}");
587588
cb();
588589
});
589590
};
@@ -1202,6 +1203,89 @@ var testImport = function(created, cb) {
12021203
});
12031204
};
12041205

1206+
var testNonHexId = function() {
1207+
logger.info("BEGIN testDbActions...");
1208+
1209+
var collectionName = "fh_test_collection_non_hex_id";
1210+
var createData = {
1211+
"__fhdb" : test_fhdb_name,
1212+
"type" : collectionName,
1213+
"fields" : {
1214+
"_id": "foobar",
1215+
"firstName" : "Foo",
1216+
"lastName" : "Bar"
1217+
}
1218+
};
1219+
1220+
async.waterfall([
1221+
function(cb) {
1222+
logger.info("test testCreate()");
1223+
1224+
var testData = JSON.parse(JSON.stringify(createData));
1225+
var expectedResult = JSON.parse(JSON.stringify(testData.fields));
1226+
1227+
ditch.doCreate(testData, function(err, res) {
1228+
assert.equal(res.guid, createData.fields._id);
1229+
assert.equal(res.fields.firstName, createData.fields.firstName);
1230+
assert.equal(res.fields.lastName, createData.fields.lastName);
1231+
cb(undefined, res);
1232+
});
1233+
},
1234+
function(created, cb) {
1235+
logger.info("test testRead()");
1236+
var readReq = {
1237+
"__fhdb" : test_fhdb_name,
1238+
"type" : collectionName,
1239+
"guid" : created.guid
1240+
};
1241+
1242+
logger.debug("readReq", readReq);
1243+
ditch.doRead(readReq, function(err, res) {
1244+
assert.equal(res.guid, createData.fields._id);
1245+
assert.equal(res.fields.firstName, createData.fields.firstName);
1246+
assert.equal(res.fields.lastName, createData.fields.lastName);
1247+
cb(undefined, created);
1248+
});
1249+
},
1250+
function(created, cb) {
1251+
logger.info("test testUpdate()");
1252+
var updateReq = JSON.parse(JSON.stringify(created));
1253+
updateReq.fields.firstName = 'Fizz';
1254+
updateReq["__fhdb"] = test_fhdb_name;
1255+
1256+
ditch.doUpdate(updateReq, function(err, res) {
1257+
logger.debug("updateRes = " + JSON.stringify(res));
1258+
1259+
// firstName should have changed - other fields still the
1260+
// same
1261+
assert.equal(res.guid, createData.fields._id);
1262+
assert.equal(res.fields.firstName, "Fizz");
1263+
assert.equal(res.fields.lastName, createData.fields.lastName);
1264+
cb(undefined, created);
1265+
});
1266+
},
1267+
function(created, cb) {
1268+
logger.info("test testDelete()");
1269+
var deleteReq = {
1270+
"__fhdb" : test_fhdb_name,
1271+
"type" : collectionName,
1272+
"guid" : created.guid
1273+
};
1274+
1275+
ditch.doDelete(deleteReq, function(err, deleteRes) {
1276+
assert.equal(deleteRes.guid, createData.fields._id);
1277+
assert.equal(deleteRes.fields.firstName, "Fizz");
1278+
assert.equal(deleteRes.fields.lastName, createData.fields.lastName);
1279+
cb(undefined, created);
1280+
});
1281+
}
1282+
], function (err, result) {
1283+
assert.ok(!err);
1284+
ditch.tearDown();
1285+
});
1286+
1287+
};
1288+
12051289

12061290
exports.testDbActions = function(beforeExit) {
12071291
logger.info("BEGIN testDbActions...");
@@ -1227,7 +1311,7 @@ exports.testDbActions = function(beforeExit) {
12271311
testListCollections,
12281312
testDelete,
12291313
testDeleteAgain,
1230-
testDeleteInvalidGuid,
1314+
testDeleteNonHexGuid,
12311315
testList1,
12321316
testList2,
12331317
testList3,
@@ -1254,7 +1338,8 @@ exports.testDbActions = function(beforeExit) {
12541338
async.apply(testDeleteAll, 0),
12551339
testCollectionOwnAppDatabase,
12561340
testCollectionDitchAppDatabase,
1257-
testBasicOperationsOwnDatabase
1341+
testBasicOperationsOwnDatabase,
1342+
testNonHexId
12581343
], function (err, result) {
12591344
assert.ok(!err);
12601345
ditch.tearDown();

test/test_fhmongodb.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,79 @@ exports[ 'test basic database ops' ] = function () {
332332
}
333333
self.db.tearUp();
334334
};
335+
336+
exports[ 'test update and delete item with an id that is not hex' ] = function () {
337+
var test_collection_name = "testcollection_forinvaliddocid";
338+
var self = this;
339+
self.db = new Database();
340+
self.db.name = "test-fhmongodb-database";
341+
342+
self.db.on('tearUp', do_ops);
343+
344+
var sampleData = { _id : "foobar", test2 : "test value2"};
345+
346+
function do_ops() {
347+
self.db.db.authenticate(config.database.adminauth.user, config.database.adminauth.user, {authSource:"admin"}, function(err, result){
348+
assert.ok(!err);
349+
self.db.dropDatabase(function(err) {
350+
assert.equal(err, null);
351+
352+
self.db.removeAll(test_collection_name, function(err, items) {
353+
assert.equal(err, null);
354+
355+
self.db.create(test_collection_name, sampleData, function(err, docs) {
356+
assert.ok(!err, JSON.stringify(err));
357+
358+
async.series([
359+
function(cb){
360+
self.db.find(test_collection_name, {"_id": sampleData._id}, function(err, items) {
361+
assert.ok(!err, JSON.stringify(err));
362+
363+
var numItems = items.length;
364+
assert.equal(1, numItems);
365+
return cb();
366+
});
367+
},
368+
function (cb) {
369+
self.db.countCollection(test_collection_name, function(err, count) {
370+
assert.equal(count, 1);
371+
return cb();
372+
});
373+
},
374+
function(cb) {
375+
self.db.update(
376+
test_collection_name,
377+
{"_id": sampleData._id},
378+
{ _id : sampleData._id, test2 : "updated test value2"},
379+
false,
380+
function(err) {
381+
assert.ok(!err, JSON.stringify(err));
382+
return cb();
383+
});
384+
},
385+
function(cb) {
386+
self.db.remove(
387+
test_collection_name,
388+
sampleData._id,
389+
function(err) {
390+
assert.ok(!err, JSON.stringify(err));
391+
return cb();
392+
});
393+
},
394+
function(cb) {
395+
self.db.dropCollection(test_collection_name, function(err, item) {
396+
assert.equal(err, null);
397+
return cb();
398+
});
399+
}
400+
], function(err, results) {
401+
self.db.tearDown();
402+
assert.ok(!err);
403+
});
404+
});
405+
});
406+
});
407+
});
408+
}
409+
self.db.tearUp();
410+
};

0 commit comments

Comments
 (0)