Translation tool for your typeorm models.
This package does not ready for production, but you can help me to do this! Let`s fork!
with yarn:
yarn add typeorm-i18nwith npm:
npm install typeorm-i18n-
I18nColumn(options: I18nColumnOptions)Decorator for mark column as translatable.
-
I18nColumnOptionsInterface 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 is wrapper around regular typeorm's connection with overwritten methods and special EntityManager(I18nEntityManager).
Overwritten methdos in I18nConnection:
createQueryBuilder- methods like original method intypeorm.Conneciton, but this methods returnsI18nSelectQueryBuilder<Entity>.getRepository- methods like original method intypeorm.Conneciton, but this methods returnsI18nRepository<Entity>.
For getting I18nConnection you can to use this functions:
-
getI18nConnection(connectionName?: string): I18nConnectionIt is function for getting access to wrapper around already exist regular connection (typeorm's
Connection). This function returnsI18nConnectioninstance. -
createI18nConnection(options?: ConnectionOptions): Promise<I18nConnection>It is funciton that create regular connection and returns wrapper around regular connection.
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 it is special class that overwrite SelectQueryBuilder from typeorm. This class provide additional method locale.
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;
}
/* ... */For using this package with nestjs you can to use @vlzh/nest-typeorm-i18n
0.0.7- Upgrade typeorm version to
0.2.25. - New prettier version
- Fix eslint configuratin for using prettier rules
- Add running of
fixscript in pre-commit hook - Note
@vlzh/nest-typeorm-i18ninreadme.md
- Upgrade typeorm version to
- fix registration of another meta information about fields in
metadataArgsStorage - write tests for
getTranslationsmethod 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
Entityand create api for access to translations - (maybe🤔) how to keep locale in a repository? (through creating of clones?)
- ... a lot of another todos
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-dbafter have started postgresql you may to run tests:
yarn testWhen you end of testing remove container with db:
yarn [run] environment:stop-db