forked from lamualfa/i18next-node-mongodb-backend-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (123 loc) · 3.46 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
const deepExtend = require('deep-extend');
const { MongoClient } = require('mongodb');
const defaultOpts = {
host: '127.0.0.1',
port: 27017,
collectionName: 'i18n',
languageFieldName: 'lang',
namespaceFieldName: 'ns',
dataFieldName: 'data',
readErrorHandler: console.error,
readMultiErrorHandler: console.error,
createErrorHandler: console.error,
mongodb: {
auto_reconnect: true,
useUnifiedTopology: true
}
};
class Backend {
constructor(services, opts = {}) {
this.init(services, opts);
}
// Private methods
async getCollection() {
return await (this.client
? this.client.isConnected()
? this.client
: await this.client.connect()
: (this.client = await MongoClient.connect(this.uri, this.opts.mongodb))
)
.db(this.opts.dbName)
.createCollection(this.opts.collectionName);
}
// i18next methods
init(services, opts, i18nOpts) {
this.services = services;
this.opts = defaultOpts;
deepExtend(this.opts, this.options, opts);
if (this.opts.user && this.opts.password)
this.opts.mongodb.auth = {
user: this.opts.user,
password: this.opts.password
};
// Cache Mongodb Connection
this.client = this.opts.client || null;
this.i18nOpts = i18nOpts;
this.uri = `mongodb://${this.opts.host}:${this.opts.port}/${this.opts.dbName}`;
}
read(lang, ns, cb) {
if (!cb) return;
this.getCollection()
.then(col =>
col.findOne(
{
[this.opts.languageFieldName]: lang,
[this.opts.namespaceFieldName]: ns
},
{
[this.opts.dataFieldName]: true
}
)
)
.then(doc => cb(null, (doc && doc[this.opts.dataFieldName]) || {}))
.catch(this.opts.readErrorHandler);
}
readMulti(langs, nss, cb) {
if (!cb) return;
this.getCollection()
.then(col =>
col
.find({
[this.opts.languageFieldName]: { $in: langs },
[this.opts.namespaceFieldName]: { $in: nss }
})
.toArray()
)
.then(docs => {
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(this.opts.readMultiErrorHandler);
}
create(langs, ns, key, fallbackVal, cb) {
if (typeof langs === 'string') langs = [langs];
this.getCollection()
.then(col =>
(async () => {
for (let i = 0; i < langs.length; i += 1) {
const lang = langs[i];
await col.updateOne(
{
[this.opts.languageFieldName]: lang,
[this.opts.namespaceFieldName]: ns
},
{
$set: {
[`${this.opts.dataFieldName}.${key}`]: fallbackVal
}
},
{
upsert: true
}
);
}
})()
)
// Call cb if exists
.then(() => cb && cb())
.catch(this.opts.createErrorHandler);
}
}
//https://www.i18next.com/misc/creating-own-plugins#make-sure-to-set-the-plugin-type
Backend.type = 'backend';
module.exports = Backend;