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

typeorm-i18n

v0.2.0-rc.1

Published

Translation tool for your typeorm models.

Downloads

594

Readme

typeorm-i18n 🚧

Translation tool for your typeorm models.

This package does not ready for production, but you can help me to do this! Let`s fork!

Installation

with yarn:

yarn add typeorm-i18n

with npm:

npm install typeorm-i18n

API

I18nColumn

  • I18nColumn(options: I18nColumnOptions)

    Decorator for mark column as translatable.

  • I18nColumnOptions

    Interface of configuration for I18nColumn.

    • languages - list of languages for that column. Example: ['es', 'cn', 'en']
    • default_language - default language ???

Entity configuration example:

import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { I18nColumn } from "typeorm-i18n";

@Entity()
export class Photo {
    @PrimaryGeneratedColumn() id!: number;
    @I18nColumn({
        languages: ["es", "cn", "en"],
        default_language: "en",
    })
    @Column({ length: 500 })
    name: string;

    @Column() filename!: string;
}

I18nConnection

I18nConnection is wrapper around regular typeorm's connection with overwritten methods and special EntityManager(I18nEntityManager).

Overwritten methdos in I18nConnection:

  • createQueryBuilder - methods like original method in typeorm.Conneciton, but this methods returns I18nSelectQueryBuilder<Entity>.
  • getRepository - methods like original method in typeorm.Conneciton, but this methods returns I18nRepository<Entity>.

For getting I18nConnection you can to use this functions:

  • getI18nConnection(connectionName?: string): I18nConnection

    It is function for getting access to wrapper around already exist regular connection (typeorm's Connection). This function returns I18nConnection instance.

  • createI18nConnection(options?: ConnectionOptions): Promise<I18nConnection>

    It is funciton that create regular connection and returns wrapper around regular connection.

I18nRepository

It is like original Repository from typeorm, but this class has additional methods:

  • locale(language: string): I18nRepository<Entity> - configure locale for setting to fields.

    const photo_repository = i18n_connection.getRepository(Photo);
    photo_repository.locale("es");
    const photo_es = photo_repository.findOne();
    console.log(photo_es.name); // 'hom'
    photo_repository.locale("ru"); // change locale
    const photo_ru = photo_repository.findOne();
    console.log(photo_ru.name); // 'дом'

I18nSelectQueryBuilder

I18nSelectQueryBuilder it is special class that overwrite SelectQueryBuilder from typeorm. This class provide additional method locale.

Usage exmaples

Simple

Declare entity:

import { Entity, Column } from "typeorm";
import { I18nColumn } from "typeorm-i18n";

@Entity("articles")
export class Article extends BaseSeoEntity {
    @I18nColumn({
        default_language: "ru",
        languages: ["ru", "en", "kg"],
    })
    @Column()
    title: string;
    @I18nColumn({
        default_language: "ru",
        languages: ["ru", "en", "kg"],
    })
    @Column({
        type: "text",
    })
    body: string;
}

Get data:

import { createConnection, Connection } from "typeorm";
import { getI18nConnection } from "typeorm-i18n";

const connection = await createConnection({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test",
});
const i18n_connection = getI18nConnection();
/* ... */
async function someFunction(): Promise<Post> {
    const repo = i18n_connection.getRepository(Post);
    const post = await repo.locale("fr").findOne();
    return post;
}
/* ... */

With nestjs

For using this package with nestjs you can to use @vlzh/nest-typeorm-i18n

CHANGELOG

  • 0.0.7
    • Upgrade typeorm version to 0.2.25.
    • New prettier version
    • Fix eslint configuratin for using prettier rules
    • Add running of fix script in pre-commit hook
    • Note @vlzh/nest-typeorm-i18n in readme.md

TODO

  • fix registration of another meta information about fields in metadataArgsStorage
  • write tests for getTranslations method and fix creating of a path in raw data(use methods from the transformer)
  • decide how to design creating-api
  • readme/doc📄
  • make more tests!☑️
  • check example👆 and write more examples (by using glitch.com)
  • (maybe🤔) exclude redundant fields from Entity and create api for access to translations
  • (maybe🤔) how to keep locale in a repository? (through creating of clones?)
  • ... a lot of another todos

Development

For the running of test you need the running postgres instance, for this just execute next command:

Note! You should to have installed docker engine.

yarn [run] environment:start-db

after have started postgresql you may to run tests:

yarn test

When you end of testing remove container with db:

yarn [run] environment:stop-db