Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/mongodb unsupported char #113

Merged
merged 10 commits into from
Mar 20, 2017
Prev Previous commit
Next Next commit
Added a function that will remove slash and spaces from collection.
  • Loading branch information
Jonathan Massot committed May 20, 2015
commit ef1d8d67a7efc9c8269d006d1cc8dc4f630c68ba
26 changes: 18 additions & 8 deletions lib/mongodb-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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 + removeMongoUnsportedChar(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 +84,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 + removeMongoUnsportedChar(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,16 +114,16 @@ 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 + removeMongoUnsportedChar(collName), function(err,collection){
if(err instanceof Error) return cb(err);

// build doc from array values
var doc = {};
values.forEach(function(value){doc[value]=true;});

values.forEach(function(value){
doc[removeMongoUnsportedChar(value)]=true;
});
// update document
collection.update(updateParams,{$set:doc},{safe:true,upsert:true},function(err){
if(err instanceof Error) return cb(err);
Expand All @@ -145,7 +146,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 + removeMongoUnsportedChar(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 @@ -169,7 +170,7 @@ MongoDBBackend.prototype = {

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

// build doc from array values
Expand All @@ -186,6 +187,15 @@ MongoDBBackend.prototype = {
}
}

function removeMongoDBUnsportedChar(text) {
if (typeof text == 'string' || text instanceof String) {
text = decodeURIComponent(text);
text = text.replace(/\//g, '');
text = text.replace(/ /g, '');
}
return text;
}

function encodeText(text) {
if (typeof text == 'string' || text instanceof String) {
text = encodeURIComponent(text);
Expand Down