npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@matthieulepers/sequelize-i18n

v3.0.1

Published

🌎 Straightforward internalization using Sequelize

Downloads

7

Readme

Sequelize i18n

🌎 Straightforward internalization using Sequelize

Supports :

  • Node.js >= 10.13.0
  • Sequelize >= 6.17.0

Notes

  • Forked from nicolascava/sequelize-i18n
  • No more vunerabilities
  • No more lodash dependency uses
  • Update to sequelize 6.17.0
  • Improve code security

Usage

Install

yarn add @matthieulepers/sequelize-i18n

Or:

npm install @matthieulepers/sequelize-i18n

Model definition

As usual, define your models using Sequelize. Simply set the i18n property to true to enable internationalized fields:

const product = (sequelize, DataTypes) =>
  sequelize.define('product', {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: DataTypes.STRING,
      i18n: true,
    },
    reference: {
      type: DataTypes.STRING,
    },
  }, {})
;

export default product;

Initialization

Just set the Sequelize i18n module before importing models:

import Sequelize from 'sequelize';
import SequelizeI18N from 'sequelize-i18n"';

const sequelize = new Sequelize('db_name', 'user', 'password');
const i18n = new SequelizeI18N(sequelize, {
  list: ['EN', 'FR', 'ES'],
  default: 'FR',
});

i18n.init();

// Models definition

Options

  • languages: list of allowed languages IDs.
  • defaultLanguage: default language ID.
  • i18nDefaultScope: add i18n to the default model scope.
  • addI18nScope: add i18n scope to model.
  • injectI18nScope: inject i18n to model scopes.

Model options

This options can be set at the model level when defining them. Those are used in the i18n parameter.

Example:

sequelize.define('product', {
  ...rest,
  i18n: {
    underscored: false,
  },
});
  • underscored: set the value of underscored option in Sequelize when generating the table name.

How it works

Sequelize i18n will check for i18n property in your model. If we enable i18n, it will create a new table where to store property's internationalized values.

Starting from the above example Product.

sequelize.define('product', {
  ...rest,
  name: {
    type: DataTypes.STRING,
    i18n: true,
  },
});

It creates a product_i18n model with the following columns:

  • id: the unique row identifier (INTEGER).
  • language_id: identifies the current translation language (INTEGER or STRING).
  • parent_id: the targeted product ID (same as the model primary or unique key).
  • name: the i18n value (same as Product.name.type).

It sets the name property type to VIRTUAL. It sets the language_id property type as VIRTUAL into Product.

Sequelize i18n will set hooks into models on create, find, update, and delete operations.

If language_id is added to the options of a query of type find (findAll, findOne, etc.), language ID will be hard-coded for each instance in the column language_id. This way, the requested language can be used in the results (convenient for use with GraphQL for example). If the where clause includes language_id, the same behavior will happen and so it does not need to be added as an option to the find command.

Sequelize i18n will add the functions below to the model:

  • getI18n(language): get the translation row for a given language.
  • addI18n(values, language): add a new translation using a different language ID. Values represent the fields to add in the form of { field: value, field2: value }.
  • deleteI18n(language): remove the translation row for a given language.

Creation

productModel
  .create({
    id: 1,
    name: 'test',
    reference: 'xxx',
  }).then((result) => {
    // [{ name: 'test', lang: 'FR' }]
    const productI18n = result.product_i18n;
  })
;

Or:

productModel
  .create({
    id: 1,
    name: 'test',
    reference: 'xxx',
  })
  .then((result) => {
    // [{ name: 'test', lang: 'FR' }]
    const productI18n = result.getI18n('FR');
  });

Add new translation

productModel
  .addI18N({ name: 'test EN' }, "EN")
  .then((result) => {})
;

Update

productModel
  .update({ name: 'New name' })
  .then((result) => {})
;

productModel
  .update({ name: 'New name' }, { language_id: 'EN' })
  .then((result) => {})
;

Delete

productModel
  .deleteI18n('EN')
  .then((result) => {})
;

Find requests

productModel
  .findAll({
    where: whereClauseObject,
    language_id: 'EN',
  })
  .then((result) => {
    // 'EN'
    const locale = result.language_id;
  })
;

Or:

productModel
  .findAll({
    where: {
      id: 'XXXX',
      language_id: 'EN',
    },
  })
  .then((result) => {
    // 'EN'
    const locale = result.language_id;
  });

License

The MIT License (MIT)

Copyright (c) 2020 Nicolas Cava

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.