├── .travis.yml ├── .trvis.yml ├── LICENSE ├── README.md ├── lib └── index.js └── package.json /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.11" 4 | - "0.10" 5 | 6 | -------------------------------------------------------------------------------- /.trvis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.11" 4 | - "0.10" 5 | 6 | # .travis.yml 7 | before_script: 8 | - sleep 15 9 | - mongo mydb_test --eval 'db.addUser("travis", "test");' 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Digital Rockers srl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | This is a i18next backend to be used node.js. It will load resources from a [mongoDB](https://www.mongodb.org) database. 4 | 5 | # Getting started 6 | 7 | Source can be loaded via [npm](https://www.npmjs.com/package/i18next-node-mongodb-backend). 8 | 9 | ``` 10 | $ npm install i18next-node-mongodb-backend 11 | ``` 12 | 13 | Wiring up: 14 | 15 | ```js 16 | var i18next = require('i18next'); 17 | var Backend = require('i18next-node-mongodb-backend'); 18 | 19 | i18next 20 | .use(Backend) 21 | .init(i18nextOptions); 22 | ``` 23 | 24 | As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance. 25 | 26 | ## Backend Options 27 | 28 | ```js 29 | { 30 | host: 'localhost', 31 | port: 27017, 32 | db: 'i18next-mongodb-test', 33 | 34 | // or 35 | uri: 'mongodb://localhost:27017/i18next-mongodb-test', 36 | 37 | // collection containing i18next data 38 | collection: 'i18next', 39 | 40 | // optional mongoDB connection options 41 | options: {} 42 | } 43 | ``` 44 | 45 | Options can be passed in: 46 | 47 | **preferred** - by setting options.backend in i18next.init: 48 | 49 | ```js 50 | var i18next = require('i18next'); 51 | var Backend = require('i18next-node-mongodb-backend'); 52 | 53 | i18next 54 | .use(Backend) 55 | .init({ 56 | backend: options 57 | }); 58 | ``` 59 | 60 | on construction: 61 | 62 | ```js 63 | var Backend = require('i18next-node-mongodb-backend'); 64 | var backend = new Backend(null, options); 65 | ``` 66 | 67 | by calling init: 68 | 69 | ```js 70 | var Backend = require('i18next-node-mongodb-backend'); 71 | var backend = new Backend(); 72 | backend.init(options); 73 | ``` 74 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var MongoClient = require('mongodb').MongoClient; 4 | 5 | Object.defineProperty(exports, '__esModule', { 6 | value: true 7 | }); 8 | 9 | var _extends = 10 | Object.assign || 11 | function(target) { 12 | for (var i = 1; i < arguments.length; i++) { 13 | var source = arguments[i]; 14 | for (var key in source) { 15 | if (Object.prototype.hasOwnProperty.call(source, key)) { 16 | target[key] = source[key]; 17 | } 18 | } 19 | } 20 | return target; 21 | }; 22 | 23 | var _createClass = (function() { 24 | function defineProperties(target, props) { 25 | for (var i = 0; i < props.length; i++) { 26 | var descriptor = props[i]; 27 | descriptor.enumerable = descriptor.enumerable || false; 28 | descriptor.configurable = true; 29 | if ('value' in descriptor) descriptor.writable = true; 30 | Object.defineProperty(target, descriptor.key, descriptor); 31 | } 32 | } 33 | return function(Constructor, protoProps, staticProps) { 34 | if (protoProps) defineProperties(Constructor.prototype, protoProps); 35 | if (staticProps) defineProperties(Constructor, staticProps); 36 | return Constructor; 37 | }; 38 | })(); 39 | 40 | function _classCallCheck(instance, Constructor) { 41 | if (!(instance instanceof Constructor)) { 42 | throw new TypeError('Cannot call a class as a function'); 43 | } 44 | } 45 | 46 | function getDefaults() { 47 | return { 48 | host: 'localhost', 49 | db: 'test', 50 | port: 27017, 51 | collection: '18next', 52 | options: { 53 | auto_reconnect: true, 54 | ssl: false, 55 | useUnifiedTopology: true 56 | } 57 | }; 58 | } 59 | 60 | var Backend = (function() { 61 | function Backend(services) { 62 | var options = 63 | arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; 64 | 65 | _classCallCheck(this, Backend); 66 | 67 | this.init(services, options); 68 | 69 | this.type = 'backend'; 70 | } 71 | 72 | _createClass(Backend, [ 73 | { 74 | key: 'init', 75 | value: function init(services, backendOptions) { 76 | var options = 77 | arguments.length <= 1 || arguments[1] === undefined 78 | ? {} 79 | : arguments[1]; 80 | var coreOptions = 81 | arguments.length <= 2 || arguments[2] === undefined 82 | ? {} 83 | : arguments[2]; 84 | 85 | this.services = services; 86 | this.options = this.options || {}; 87 | 88 | const defaults = getDefaults(); 89 | 90 | if (!this.options.uri) { 91 | const { 92 | host = defaults.host, 93 | port = defaults.port, 94 | db = defaults.db 95 | } = this.options; 96 | 97 | this.options.uri = `mongodb://${host}:${port}/${db}`; 98 | } 99 | this.options = _extends({}, defaults, this.options, options); 100 | 101 | this.coreOptions = coreOptions; 102 | } 103 | }, 104 | { 105 | key: 'read', 106 | value: function read(language, namespace, callback) { 107 | var _self = this; 108 | 109 | if (!callback) return; 110 | 111 | MongoClient.connect(_self.options.uri, this.options.options, function( 112 | err, 113 | client 114 | ) { 115 | if (err) return console.error(err); 116 | 117 | const db = client.db(_self.options.db); 118 | 119 | db.createCollection(_self.options.collection, function( 120 | err, 121 | collection 122 | ) { 123 | if (err) return console.error(err); 124 | 125 | collection.findOne( 126 | { language: language, namespace: namespace }, 127 | function(err, lang) { 128 | if (err) return callback(err); 129 | 130 | callback(null, lang ? lang.data : {}); 131 | client.close(); 132 | } 133 | ); 134 | }); 135 | }); 136 | } 137 | }, 138 | { 139 | key: 'readMulti', 140 | value: function read(languages, namespaces, callback) { 141 | var _self = this; 142 | 143 | if (!callback) return; 144 | if (typeof languages === 'string') languages = [languages]; 145 | 146 | MongoClient.connect(_self.options.uri, this.options.options, function( 147 | err, 148 | client 149 | ) { 150 | if (err) return console.error(err); 151 | 152 | const db = client.db(_self.options.db); 153 | 154 | db.createCollection(_self.options.collection, function( 155 | err, 156 | collection 157 | ) { 158 | if (err) return console.error(err); 159 | 160 | collection.update( 161 | { language: { $in: languages }, namespace: { $in: namespaces } }, 162 | function(err, langs) { 163 | if (err) return console.error(err); 164 | 165 | callback(null, _.pluck(langs, 'data')); 166 | client.close(); 167 | } 168 | ); 169 | }); 170 | }); 171 | } 172 | }, 173 | { 174 | key: 'create', 175 | value: function create( 176 | languages, 177 | namespace, 178 | key, 179 | fallbackValue, 180 | callback 181 | ) { 182 | var _self = this; 183 | 184 | if (!callback) callback = function() {}; 185 | if (typeof languages === 'string') languages = [languages]; 186 | 187 | MongoClient.connect(_self.options.uri, this.options.options, function( 188 | err, 189 | client 190 | ) { 191 | if (err) return console.error(err); 192 | 193 | const db = client.db(_self.options.db); 194 | 195 | var set = {}; 196 | set['data.' + key] = fallbackValue; 197 | db.createCollection(_self.options.collection, function( 198 | err, 199 | collection 200 | ) { 201 | if (err) return console.error(err); 202 | 203 | languages.forEach(function(lng) { 204 | collection.update( 205 | { language: lng, namespace: namespace }, 206 | { $set: set }, 207 | { upsert: true }, 208 | function(err) { 209 | if (err) return console.error(err); 210 | client.close(); 211 | } 212 | ); 213 | }); 214 | }); 215 | }); 216 | } 217 | } 218 | ]); 219 | 220 | return Backend; 221 | })(); 222 | 223 | Backend.type = 'backend'; 224 | 225 | module.exports = Backend; 226 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "i18next-node-mongodb-backend", 3 | "version": "0.0.6", 4 | "description": "i18next node.js backend layer for i18next using mongodb", 5 | "keywords": [ 6 | "i18next", 7 | "i18next-backend", 8 | "mongodb" 9 | ], 10 | "bugs": { 11 | "url": "https://github.com/gian788/i18next-node-mongodb-backend/issues" 12 | }, 13 | "dependencies": {}, 14 | "devDependencies": { 15 | "mocha": "*", 16 | "should": "*" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/gian788/i18next-node-mongodb-backend" 21 | }, 22 | "author": { 23 | "name": "Gianluca Pengo", 24 | "url": "https://github.com/gian788" 25 | }, 26 | "license": "MIT", 27 | "main": "./lib/index.js" 28 | } 29 | --------------------------------------------------------------------------------