Skip to content

Commit 38d59fb

Browse files
authored
Merge pull request #228 from leodutra/master
Reworking "Bugfix/mongodb unsupported char" (pull request #113)
2 parents a88b07f + 717ba66 commit 38d59fb

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/mongodb-backend.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ var _ = require('lodash');
1212
// If prefix is specified, it will be prepended to this name, like acl_resources
1313
var aclCollectionName = 'resources';
1414

15-
function MongoDBBackend(db, prefix, useSingle){
15+
function MongoDBBackend(db, prefix, useSingle, useRawCollectionNames){
1616
this.db = db;
1717
this.prefix = typeof prefix !== 'undefined' ? prefix : '';
1818
this.useSingle = (typeof useSingle !== 'undefined') ? useSingle : false;
19+
this.useRawCollectionNames = useRawCollectionNames === false; // requires explicit boolean false value
1920
}
2021

2122
MongoDBBackend.prototype = {
@@ -61,7 +62,7 @@ MongoDBBackend.prototype = {
6162
var searchParams = (this.useSingle? {_bucketname: bucket, key:key} : {key:key});
6263
var collName = (this.useSingle? aclCollectionName : bucket);
6364

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

87-
this.db.collection(this.prefix + collName,function(err,collection){
88+
this.db.collection(this.prefix + this.removeUnsupportedChar(collName),function(err,collection){
8889
if(err instanceof Error) return cb(err);
8990
// Excluding bucket field from search result
9091
collection.find(searchParams, {_bucketname: 0}).toArray(function(err,docs){
9192
if(err instanceof Error) return cb(err);
9293
if( ! docs.length ) return cb(undefined, []);
94+
9395
var keyArrays = [];
9496
docs = fixAllKeys(docs);
9597
docs.forEach(function(doc){
@@ -113,10 +115,9 @@ MongoDBBackend.prototype = {
113115
var self=this;
114116
var updateParams = (self.useSingle? {_bucketname: bucket, key:key} : {key:key});
115117
var collName = (self.useSingle? aclCollectionName : bucket);
116-
117118
transaction.push(function(cb){
118119
values = makeArray(values);
119-
self.db.collection(self.prefix + collName, function(err,collection){
120+
self.db.collection(self.prefix + self.removeUnsupportedChar(collName), function(err,collection){
120121
if(err instanceof Error) return cb(err);
121122

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

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

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

183184
values = makeArray(values);
184185
transaction.push(function(cb){
185-
self.db.collection(self.prefix + collName,function(err,collection){
186+
self.db.collection(self.prefix + self.removeUnsupportedChar(collName),function(err,collection){
186187
if(err instanceof Error) return cb(err);
187188

188189
// build doc from array values
@@ -196,6 +197,14 @@ MongoDBBackend.prototype = {
196197
});
197198
});
198199
});
200+
},
201+
202+
removeUnsupportedChar: function(text) {
203+
if (!this.useRawCollectionNames && (typeof text === 'string' || text instanceof String)) {
204+
text = decodeURIComponent(text);
205+
text = text.replace(/[/\s]/g, '_'); // replaces slashes and spaces
206+
}
207+
return text;
199208
}
200209
}
201210

0 commit comments

Comments
 (0)