Permalink
Browse files

Merge pull request #16 from skimcom/master

Authentication for MongoDB Store
  • Loading branch information...
2 parents e6b67f1 + 5d9fa18 commit 971c032b292690351a4e2419ff5a475bf07ae941 @masylum committed Jan 29, 2012
Showing with 30 additions and 9 deletions.
  1. +2 −0 Readme.md
  2. +28 −9 lib/stores/mongodb.js
View
@@ -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_
View
@@ -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();
+ }
}
});
}

0 comments on commit 971c032

Please sign in to comment.