Junior — Middle
Can you explain what the TypeORM library is and what it is used for?
sobes.tech AI
Answer from AI
TypeORM is an ORM (Object-Relational Mapping) library for Node.js and TypeScript that allows working with databases through objects and classes rather than writing SQL queries directly. It supports various DBMSs such as MySQL, PostgreSQL, SQLite, MSSQL, and others.
TypeORM simplifies creating, reading, updating, and deleting data (CRUD) using models that correspond to database tables. It also supports migrations, entity relationships, lazy loading, and other features.
Example of using TypeORM:
import { Entity, PrimaryGeneratedColumn, Column, createConnection } from "typeorm";
@Entity()
class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}
async function main() {
const connection = await createConnection({
type: "sqlite",
database: "test.db",
entities: [User],
synchronize: true,
});
const userRepository = connection.getRepository(User);
const user = new User();
user.name = "Ivan";
await userRepository.save(user);
const users = await userRepository.find();
console.log(users);
}
main();