Skip to content

Commit

Permalink
Merge pull request OptimalBits#228 from leodutra/master
Browse files Browse the repository at this point in the history
Reworking "Bugfix/mongodb unsupported char" (pull request OptimalBits#113)
  • Loading branch information
manast authored Mar 20, 2017
2 parents a88b07f + 717ba66 commit 38d59fb
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/mongodb-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ var _ = require('lodash');
// If prefix is specified, it will be prepended to this name, like acl_resources
var aclCollectionName = 'resources';

function MongoDBBackend(db, prefix, useSingle){
function MongoDBBackend(db, prefix, useSingle, useRawCollectionNames){
this.db = db;
this.prefix = typeof prefix !== 'undefined' ? prefix : '';
this.useSingle = (typeof useSingle !== 'undefined') ? useSingle : false;
this.useRawCollectionNames = useRawCollectionNames === false; // requires explicit boolean false value
}

MongoDBBackend.prototype = {
Expand Down Expand Up @@ -61,7 +62,7 @@ MongoDBBackend.prototype = {
var searchParams = (this.useSingle? {_bucketname: bucket, key:key} : {key:key});
var collName = (this.useSingle? aclCollectionName : bucket);

this.db.collection(this.prefix + collName,function(err,collection){
this.db.collection(this.prefix + this.removeUnsupportedChar(collName),function(err,collection){
if(err instanceof Error) return cb(err);
// Excluding bucket field from search result
collection.findOne(searchParams, {_bucketname: 0},function(err, doc){
Expand All @@ -84,12 +85,13 @@ MongoDBBackend.prototype = {
var searchParams = (this.useSingle? {_bucketname: bucket, key: { $in: keys }} : {key: { $in: keys }});
var collName = (this.useSingle? aclCollectionName : bucket);

this.db.collection(this.prefix + collName,function(err,collection){
this.db.collection(this.prefix + this.removeUnsupportedChar(collName),function(err,collection){
if(err instanceof Error) return cb(err);
// Excluding bucket field from search result
collection.find(searchParams, {_bucketname: 0}).toArray(function(err,docs){
if(err instanceof Error) return cb(err);
if( ! docs.length ) return cb(undefined, []);

var keyArrays = [];
docs = fixAllKeys(docs);
docs.forEach(function(doc){
Expand All @@ -113,10 +115,9 @@ MongoDBBackend.prototype = {
var self=this;
var updateParams = (self.useSingle? {_bucketname: bucket, key:key} : {key:key});
var collName = (self.useSingle? aclCollectionName : bucket);

transaction.push(function(cb){
values = makeArray(values);
self.db.collection(self.prefix + collName, function(err,collection){
self.db.collection(self.prefix + self.removeUnsupportedChar(collName), function(err,collection){
if(err instanceof Error) return cb(err);

// build doc from array values
Expand All @@ -132,7 +133,7 @@ MongoDBBackend.prototype = {
});

transaction.push(function(cb) {
self.db.collection(self.prefix + collName, function(err,collection){
self.db.collection(self.prefix + self.removeUnsupportedChar(collName), function(err,collection){
// Create index
collection.ensureIndex({_bucketname: 1, key: 1}, function(err){
if (err instanceof Error) {
Expand All @@ -158,7 +159,7 @@ MongoDBBackend.prototype = {
var collName = (self.useSingle? aclCollectionName : bucket);

transaction.push(function(cb){
self.db.collection(self.prefix + collName,function(err,collection){
self.db.collection(self.prefix + self.removeUnsupportedChar(collName),function(err,collection){
if(err instanceof Error) return cb(err);
collection.remove(updateParams,{safe:true},function(err){
if(err instanceof Error) return cb(err);
Expand All @@ -182,7 +183,7 @@ MongoDBBackend.prototype = {

values = makeArray(values);
transaction.push(function(cb){
self.db.collection(self.prefix + collName,function(err,collection){
self.db.collection(self.prefix + self.removeUnsupportedChar(collName),function(err,collection){
if(err instanceof Error) return cb(err);

// build doc from array values
Expand All @@ -196,6 +197,14 @@ MongoDBBackend.prototype = {
});
});
});
},

removeUnsupportedChar: function(text) {
if (!this.useRawCollectionNames && (typeof text === 'string' || text instanceof String)) {
text = decodeURIComponent(text);
text = text.replace(/[/\s]/g, '_'); // replaces slashes and spaces
}
return text;
}
}

Expand Down

0 comments on commit 38d59fb

Please sign in to comment.