Skip to content

Commit

Permalink
Merge pull request masylum#16 from skimcom/master
Browse files Browse the repository at this point in the history
Authentication for MongoDB Store
  • Loading branch information
masylum committed Jan 29, 2012
2 parents e6b67f1 + 5d9fa18 commit 971c032
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Expand Up @@ -51,6 +51,8 @@ Dialect is a painless nodejs module to manage your translations.
* `host`: _127.0.0.1_
* `port`: _27017_
* `collection`: _translations_
* `username` (optional)
* `password` (optional)
* `sqlite`
* `database`: _dialect.db_
* `table`: _dialect_
Expand Down
37 changes: 28 additions & 9 deletions lib/stores/mongodb.js
Expand Up @@ -55,20 +55,39 @@ module.exports = function (options) {
callback(err, collection);
}

function collectionSetup() {
STORE.db.collection(options.collection || 'translations', function (err, collection) {
if (collection) {
collection.ensureIndex(
{original: 1, locale: 1, translation: 1},
{unique: true},
function (err) {
connect(err, collection);
});
} else {
STORE.db.createCollection(options.collection || 'translations', function (err, collection) {
connect(err, collection);
});
}
});
}

if (!_is_connected) {
STORE.db.open(function (err, db) {
if (err) {
callback(err, null);
} else {
STORE.db.collection(options.collection || 'translations', function (err, collection) {
if (collection) {
connect(err, collection);
} else {
STORE.db.createCollection(options.collection || 'translations', function (err, collection) {
connect(err, collection);
});
}
});
if (options.username && options.password) {
db.authenticate(options.username, options.password, function (err) {
if (err) {
callback(err, null);
} else {
collectionSetup();
}
});
} else {
collectionSetup();
}
}
});
}
Expand Down

0 comments on commit 971c032

Please sign in to comment.