TypeORM is a complete ORM library for Node.js and other Javascript-based platforms which has support for a variety of databases such as PostgreSQL, MySQL, MongoDB, and so on. It can be utilized using Typescript or Javascript. There are several methods that can be used for creating connections in TypeORM.
Create single connection
await createConnection({ name, type, host, port, username, password, database });
Then, we can take the connection everywhere using getConnection()
.
const conn: Conection = getConnection();
Not only the connection, but we can also directly utilize EntityManager
and Repository
of the connection everywhere.
const users = getManager().find(User);
const user = getRepository(User).findOne(id);
Another method is using getConnectionManager()
.
const connManager = getConnectionManager();
const conn = connManager.create({ ...params });
await conn.connect();
Create multiple connections
await createConnections([{ ...params1 }, { ...params2 }]);
Then, we can retrieve the connection individually by its name
property.
const conn1 = getConnectionManager().get('conn1');
const conn2 = getConnection('conn2');
Comments
Post a Comment