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.
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.
In TypeORM there are several ways to create and save a new model:
const employee = new Employee(); // you can use constructor parameters as well
employee.name = "John Doe";
employee.title = "senior engineer";
await getRepository(Employee).save(employee)