constsequelize=newSequelize("database","username","password",{host:"localhost",dialect:"mysql"});sequelize.authenticate().then(()=>{console.log("Connection has been established successfully.");}).catch(err=>{console.error("Unable to connect to the database:",err);});
In TypeORM you create a connection like this:
import{createConnection}from"typeorm";createConnection({type:"mysql",host:"localhost",username:"username",password:"password"}).then(connection=>{console.log("Connection has been established successfully.");}).catch(err=>{console.error("Unable to connect to the database:",err);});
Then you can get your connection instance from anywhere in your app using getConnection.
In sequelize you do schema synchronization this way:
In TypeORM you just add synchronize: true in the connection options:
Creating a models
This is how models are defined in sequelize:
In TypeORM these models are called entities and you can define them like this:
It's highly recommended to define one entity class per file. TypeORM allows you to use your classes as database models and provides a declarative way to define what part of your model will become part of your database table. The power of TypeScript gives you type hinting and other useful features that you can use in classes.
const employee = new Employee(); // you can use constructor parameters as well
employee.name = "John Doe";
employee.title = "senior engineer";
await getRepository(Employee).save(employee)