VLZH / typeorm-i18n

🚧Translation tool for your typeorm models.
MIT License
13 stars 4 forks source link

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

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:

For getting I18nConnection you can to use this functions:

I18nRepository

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

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

TODO

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