Skip to content

Some hacks to allow special usage of typeorm, e.g., allow same entities to be stored in different dbs.

License

Notifications You must be signed in to change notification settings

infoloop-opensource/typeorm-hacks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

typeorm-hacks

This package currently is a minimal implementation to meet special usages of typeorm. For now there is no neat hack for Column decorator at runtime.

Examples

  1. Store entities in different tables or different dbs while using the same class type for entity declaration
const config = {
    "host": "localhost",
    "port": 3306,
    "username": "test",
    "password": "",
    "database": "test"
};

class TestClass1 {
    @PrimaryColumn()
    id: number;
}

class TestClass2 {
    @PrimaryColumn()
    id: number;

    @Column()
    name: string;
}

// same table def in different dbs
const test1 = async () => {
    const TC1 = decorateClass(TestClass1, HackedEntity<TestClass1>({
        name: 'test',
        database: 'qa'
    }));
    const TC2 = decorateClass(TestClass1, HackedEntity<TestClass1>({
        name: 'test',
        database: 'stage'
    }));
    const connection = await createConnection({
        ...config,
        type: 'mysql',
        entities: [TC1, TC2],
        logging: false
    });
    const x = new TestClass1();
    await connection.getRepository(TC1).insert(x);
    await connection.getRepository(TC2).insert(x);
    await connection.close();
};

// different table in same db
const test2 = async () => {
    const TC1 = decorateClass(TestClass2, HackedEntity<TestClass2>({
        name: 'test1',
        database: 'qa'
    }));
    const TC2 = decorateClass(TestClass2, HackedEntity<TestClass2>({
        name: 'test2',
        database: 'qa'
    }));
    const connection = await createConnection({
        ...config,
        type: 'mysql',
        entities: [TC1, TC2],
        logging: false
    });
    const x = new TestClass2();
    x.name = 'test';
    await connection.getRepository(TC1).insert(x);
    await connection.getRepository(TC2).insert(x);
    await connection.close();
};

const test = async () => {
    await test1();
    await test2();
};

test();

About

Some hacks to allow special usage of typeorm, e.g., allow same entities to be stored in different dbs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published