-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
227 lines (194 loc) · 6.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// eslint-disable-next-line import/no-extraneous-dependencies
const { MongoClient } = require('mongodb');
const sanitizeFieldNameCharacter = require('./libs/sanitizeFieldNameCharacter');
const defaultOpts = {
collectionName: 'i18n',
languageFieldName: 'lang',
namespaceFieldName: 'ns',
dataFieldName: 'data',
sanitizeFieldNameCharacter: true,
// eslint-disable-next-line no-console
readOnError: console.error,
// eslint-disable-next-line no-console
readMultiOnError: console.error,
// eslint-disable-next-line no-console
createOnError: console.error,
mongodb: {
useUnifiedTopology: true,
},
};
// https://www.i18next.com/misc/creating-own-plugins#backend
class Backend {
/**
* @param {*} services `i18next.services`
* @param {object} opts Backend Options
* @param {string} opts.uri MongoDB Uri
* @param {string} opts.host MongoDB Host
* @param {number} opts.port MongoDB Port
* @param {MongoClient} opts.client Use your custom `MongoClient` instance. Example: `new MongoClient()`
* @param {string} [opts.username] MongoDB Username
* @param {string} [opts.user] MongoDB Username
* @param {string} [opts.password] MongoDB Password
* @param {string} opts.dbName Database name for storing i18next data
* @param {string} [opts.collectionName="i18n"] Collection name for storing i18next data
* @param {string} [opts.languageFieldName="lang"] Field name for language attribute
* @param {string} [opts.namespaceFieldName="ns"] Field name for namespace attribute
* @param {string} [opts.dataFieldName="data"] Field name for data attribute
* @param {boolean} [opts.sanitizeFieldNameCharacter=true] Remove MongoDB special character (contains ".", or starts with "$"). See https://jira.mongodb.org/browse/SERVER-3229
* @param {function} [opts.readOnError] Error handler for `read` process
* @param {function} [opts.readMultiOnError] Error handler for `readMulti` process
* @param {function} [opts.createOnError] Error handler for `create` process
* @param {object} [opts.mongodb] `MongoClient` Options. See https://mongodb.github.io/node-mongodb-native/3.5/api/MongoClient.html
* @param {boolean} [opts.mongodb.useUnifiedTopology=true]
*/
constructor(services, opts = {}) {
this.init(services, opts);
}
// Private methods
async getClient() {
const client = this.client || new MongoClient(this.uri, this.opts.mongodb);
if (!client.isConnected || !client.isConnected()) {
await client.connect();
}
return client;
}
async closeClientIfNeeded(client) {
if (this.client) return;
await client.close();
}
async getCollection(client) {
const collections = await client
.db(this.opts.dbName)
.listCollections()
.toArray();
const isCollectionExists = collections.some(
(collection) => collection.name === this.opts.collectionName,
);
if (isCollectionExists) {
return client.db(this.opts.dbName).collection(this.opts.collectionName);
}
return client
.db(this.opts.dbName)
.createCollection(this.opts.collectionName);
}
sanitizeOpts(opts) {
this.opts = { ...defaultOpts, ...this.options, ...opts };
if (this.opts.sanitizeFieldNameCharacter) {
this.opts.languageFieldName = sanitizeFieldNameCharacter(
this.opts.languageFieldName,
);
this.opts.namespaceFieldName = sanitizeFieldNameCharacter(
this.opts.namespaceFieldName,
);
this.opts.dataFieldName = sanitizeFieldNameCharacter(
this.opts.dataFieldName,
);
}
}
// i18next required methods
init(services, opts, i18nOpts) {
this.services = services;
this.i18nOpts = i18nOpts;
this.sanitizeOpts(opts);
if (this.opts.client) {
this.client = this.opts.client;
return;
}
this.uri =
this.opts.uri ||
`mongodb://${this.opts.host}:${this.opts.port}/${this.opts.dbName}`;
const username = this.opts.user || this.opts.username;
if (username && this.opts.password)
this.opts.mongodb.auth = {
user: username,
username,
password: this.opts.password,
};
}
async read(lang, ns, cb) {
if (!cb) return;
const client = await this.getClient();
try {
const col = await this.getCollection(client);
const doc = await col.findOne(
{
[this.opts.languageFieldName]: lang,
[this.opts.namespaceFieldName]: ns,
},
{
[this.opts.dataFieldName]: true,
},
);
cb(null, (doc && doc[this.opts.dataFieldName]) || {});
} catch (error) {
this.opts.readOnError(error);
cb(error);
} finally {
await this.closeClientIfNeeded(client);
}
}
async readMulti(langs, nss, cb) {
if (!cb) return;
const client = await this.getClient();
try {
const col = await this.getCollection(client);
const docs = await col
.find({
[this.opts.languageFieldName]: { $in: langs },
[this.opts.namespaceFieldName]: { $in: nss },
})
.toArray();
const parsed = {};
for (let i = 0; i < docs.length; i += 1) {
const doc = docs[i];
const lang = doc[this.opts.languageFieldName];
const ns = doc[this.opts.namespaceFieldName];
const data = doc[this.opts.dataFieldName];
if (!parsed[lang]) {
parsed[lang] = {};
}
parsed[lang][ns] = data;
}
cb(null, parsed);
} catch (error) {
this.opts.readMultiOnError(error);
cb(error);
} finally {
await this.closeClientIfNeeded(client);
}
}
async create(langs, ns, key, fallbackVal, cb) {
const client = await this.getClient();
try {
const col = await this.getCollection(client);
// Make `updateOne` process run concurrently
await Promise.all(
(typeof langs === 'string' ? [langs] : langs).map((lang) =>
col.updateOne(
{
[this.opts.languageFieldName]: lang,
[this.opts.namespaceFieldName]: ns,
},
{
$set: {
[`${this.opts.dataFieldName}.${key}`]: fallbackVal,
},
},
{
upsert: true,
},
),
),
);
if (cb && typeof cb === 'function') cb();
} catch (error) {
this.opts.createOnError(error);
if (cb) cb(error);
} finally {
await this.closeClientIfNeeded(client);
}
}
}
// https://www.i18next.com/misc/creating-own-plugins#make-sure-to-set-the-plugin-type
Backend.type = 'backend';
module.exports = Backend;