📅  最后修改于: 2023-12-03 14:48:05.894000             🧑  作者: Mango
TypeORM是一个基于Typescript的ORM(Object Relationship Mapping),支持多个数据库,包括MySQL、PostgreSQL、MariaDB、Oracle、SQLite和Microsoft SQL Server等。它提供了可扩展的对象关系映射器,并包含了Active Record和Data Mapper模式,使得在应用开发过程中,开发者可以更加方便地处理数据库操作。
npm install typeorm --save
yarn add typeorm
import { createConnection } from "typeorm";
createConnection({
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "your_database_password",
database: "test",
entities: [
// ...
],
synchronize: true,
}).then(connection => {
// ...
}).catch(error => console.log(error));
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
age: number;
}
import { getRepository } from "typeorm";
import { User } from "./entity/User";
const userRepository = getRepository(User);
const user = new User();
user.firstName = "John";
user.lastName = "Doe";
user.age = 30;
userRepository.save(user);
userRepository.delete(1);
const user = await userRepository.findOne(1);
user.firstName = "Jane";
await userRepository.save(user);
userRepository.find();
userRepository.findOne(1);
userRepository.find({ firstName: "John" });
userRepository.createQueryBuilder("user").where("user.firstName = :firstName", { firstName: "John" }).getMany();
TypeORM 的维护者和贡献者们在 GitHub 上分享您的问题、帮助以及贡献。