
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
@cme-pro/mongoose-i18n
Advanced tools
Mongoose schema plugin for multilingual fields, largely inspired by mongoose-intl plugin.
There are several mongoose plugins dealing already with i18n, the most popular being mongoose-intl, but they all changing the translated field into a subdocument containning all translated value.
This plugin only add a new virtual field _i18n
with all translations inside, except the default language, which is stored in the main translated field. It's easier to use on an existing database. No need to migrate all documents, you can use this plugin straighforward.
$ npm install mongoose-i18n-extra --save
Schema definition with i18n
option enabled for some fields:
const mongoose = require('mongoose');
const mongooseI18nExtra = require('mongoose-i18n-extra');
const { Schema } = mongoose;
var BlogPost = new Schema({
title : { type: String, i18n: true },
body : { type: String, i18n: true }
});
Note: i18n
option can be enabled for String type only.
Adding plugin to the schema:
BlogPost.plugin(mongooseI18nExtra, { languages: ['en', 'de', 'fr'], defaultLanguage: 'en' });
languages
array will be used as a default languageBlogPost
schema described above will be translated to the following document in the Mongo collection:
{
"_id": ObjectId(...),
"title": "...",
"body": "...",
"_i18n": {
"en": {"title": "...", "body": "..."},
"de": {"title": "...", "body": "..."},
"fr": {"title": "..., "body": "..."}"
}
}
i18n
-enabled field is converted to a virtual path and continue interacting as a string, not an object.
It means that you can read it and write to it any string, and it will be stored under default language setting.
Other languages values can be set by using model.set()
method. Pass an object with multiple languages instead of the string to set all values together. See examples below.
Multilingual fields can be set with 2 ways:
var BlogPostModel = mongoose.model('Post', BlogPost);
var post = new BlogPostModel();
post.title = 'Title on default language'; // default language definition, will be stored to title
post.set('title_de', 'German title'); // any other language value definition will be stored to _i18n.de.title
// you can also set multiple value at once
post.set(
"_i18n": {
"en": {"title": "...", "body": "..."},
"de": {"title": "...", "body": "..."},
"fr": {"title": "..., "body": "..."}"
});
await post.save();
Values can be read using the same options:
var BlogPostModel = mongoose.model('Post', BlogPost);
const post = await BlogPostModel.findOne({...});
console.log(post.title); // 'Title on default language'
console.log(post.get('title_de')); // 'Another German title'
This plugin is also compliant with the plugin mongoose-lean-virtuals which exposes the virtual fields:
const post = await BlogPostModel.findOne({...}).lean({virtuals: ['_i18n']});
console.log(post.title); // 'Title on default language'
console.log(post._i18n); // { "de": {description: "..."}, "en": {"description": "..."}, "fr": {description: "..."} }
The current language can be set/changed on 1 level for the moment:
Each document will receive the following language methods:
getLanguages()
- returns an array of available languagesgetLanguage()
- returns current document's languagesetLanguage(lang)
- changes document's language to a new one, the value should be equal to the one of available languagesunsetLanguage()
- removes previously set document-specific language, schema's default language will be used for the translationUsage examples:
const posts = await BlogPostModel.find({});
console.log(JSON.stringify(posts)); // [{ _id: '...', title: 'Title 1 on default language' },
// { _id: '...', title: 'Title 2 on default language' }, ...]
posts[0].getLanguages(); // [ 'en', 'de', 'fr' ]
posts[0].getLanguage(); // 'en'
posts[0].setLanguage('de');
console.log(JSON.stringify(posts)); // [{ _id: '...', title: 'Another German title' },
// { _id: '...', title: 'Title 2 on default language' }, ...]
BlogPostModel.setDefaultLanguage('fr'); // schema-level language change (see documentation below)
console.log(JSON.stringify(posts)); // [{ _id: '...', title: 'Another German title' }, // still 'de'
// { _id: '...', title: 'French title 2' }, ...]
posts[0].unsetLanguage();
console.log(JSON.stringify(posts)); // [{ _id: '...', title: 'French title 1' },
// { _id: '...', title: 'French title 2' }, ...]
});
default
and required
options are applied to the default language field only.
2 new options were added for all lang-fields: defaultAll
and requiredAll
.
Example:
var BlogPost = new Schema({
title : { type: String, i18n: true, default: 'Some default title', requiredAll: true },
body : { type: String, i18n: true }
});
All others options and validators (e.g. lowercase
, uppercase
, trim
, minlength
, maxlength
, match
, etc.) will be used for all languages.
But please be careful with some of them like enum
which may not be relevant for multilingual text fields, and indexes which will be added for all fields as well.
FAQs
mongoose plugin to manage i18n fields
The npm package @cme-pro/mongoose-i18n receives a total of 2 weekly downloads. As such, @cme-pro/mongoose-i18n popularity was classified as not popular.
We found that @cme-pro/mongoose-i18n demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.