DataSource API
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
options- 创建此 dataSource 所使用的配置选项。 了解更多关于 DataSource 配置选项。
const dataSourceOptions: DataSourceOptions = dataSource.options
isInitialized- 标识 DataSource 是否已完成初始化,以及是否已建立与数据库的初始连接/连接池。
const isInitialized: boolean = dataSource.isInitialized
driver- 此 dataSource 使用的基础数据库驱动。
const driver: Driver = dataSource.driver
manager- 用于操作实体的EntityManager。 了解更多关于 实体管理器 和 Repository。
const manager: EntityManager = dataSource.manager
// you can call manager methods, for example find:
const users = await manager.find()
mongoManager- 用于操作 MongoDB dataSource 中实体的MongoEntityManager。 有关 MongoEntityManager 的详细信息,请参阅 MongoDB 文档。
const manager: MongoEntityManager = dataSource.mongoManager
// you can call manager or mongodb-manager specific methods, for example find:
const users = await manager.find()
initialize- 初始化 dataSource 并打开与数据库的连接池。
await dataSource.initialize()
destroy- 销毁 DataSource 并关闭所有数据库连接。 通常在应用程序关闭时调用此方法。
await dataSource.destroy()
synchronize- 同步数据库模式。当在 dataSource 配置中设置synchronize: true时将调用此方法。 通常在应用程序启动时调用此方法。
await dataSource.synchronize()
dropDatabase- 删除数据库及其所有数据。 生产环境请谨慎使用此方法,它会清除所有数据库表及其数据。 仅在建立数据库连接后可用。
await dataSource.dropDatabase()
runMigrations- 运行所有待处理的迁移。
await dataSource.runMigrations()
undoLastMigration- 撤销最近一次执行的迁移。
await dataSource.undoLastMigration()
hasMetadata- 检查给定实体的元数据是否已注册。
if (dataSource.hasMetadata(User))
const userMetadata = dataSource.getMetadata(User)
getMetadata- 获取给定实体的EntityMetadata。 也可指定表名,若找到匹配表名的实体元数据则返回。
const userMetadata = dataSource.getMetadata(User)
// now you can get any information about User entity
getRepository- 获取给定实体的Repository。 也可指定表名,若找到对应表的 repository 则返回。 了解更多关于 Repository。
const repository = dataSource.getRepository(User)
// now you can call repository methods, for example find:
const users = await repository.find()
getTreeRepository- 获取给定实体的TreeRepository。 也可指定表名,若找到对应表的 repository 则返回。 了解更多关于 Repository。
const repository = dataSource.getTreeRepository(Category)
// now you can call tree repository methods, for example findTrees:
const categories = await repository.findTrees()
getMongoRepository- 获取给定实体的MongoRepository。 该 repository 用于 MongoDB dataSource 中的实体。 了解更多关于 MongoDB 支持。
const repository = dataSource.getMongoRepository(User)
// now you can call mongodb-specific repository methods, for example createEntityCursor:
const categoryCursor = repository.createEntityCursor()
const category1 = await categoryCursor.next()
const category2 = await categoryCursor.next()
transaction- 提供单一事务环境,使多个数据库请求在单个数据库事务中执行。 了解更多关于 事务。
await dataSource.transaction(async (manager) => {
// NOTE: you must perform all database operations using given manager instance
// its a special instance of EntityManager working with this transaction
// and don't forget to await things here
})
query- 执行原始 SQL 查询。
const rawData = await dataSource.query(`SELECT * FROM USERS`)
// You can also use parameters to avoid SQL injection
// The syntax differs between the drivers
// aurora-mysql, better-sqlite3, capacitor, cordova,
// expo, mariadb, mysql, nativescript, react-native,
// sap, sqlite, sqljs
const rawData = await dataSource.query(
"SELECT * FROM USERS WHERE name = ? and age = ?",
["John", 24],
)
// aurora-postgres, cockroachdb, postgres
const rawData = await dataSource.query(
"SELECT * FROM USERS WHERE name = $1 and age = $2",
["John", 24],
)
// oracle
const rawData = await dataSource.query(
"SELECT * FROM USERS WHERE name = :1 and age = :2",
["John", 24],
)
// spanner
const rawData = await dataSource.query(
"SELECT * FROM USERS WHERE name = @param0 and age = @param1",
["John", 24],
)
// mssql
const rawData = await dataSource.query(
"SELECT * FROM USERS WHERE name = @0 and age = @1",
["John", 24],
)
sql- 使用模板字面量执行原始 SQL 查询。
const rawData =
await dataSource.sql`SELECT * FROM USERS WHERE name = ${"John"} and age = ${24}`
了解更多关于SQL 标签语法的用法。
createQueryBuilder- 创建用于构建查询的查询构建器。 了解更多关于 查询构建器。
const users = await dataSource
.createQueryBuilder()
.select()
.from(User, "user")
.where("user.name = :name", { name: "John" })
.getMany()
createQueryRunner- 创建用于管理单个真实数据库 dataSource 的查询运行器。 了解更多关于 查询运行器。
const queryRunner = dataSource.createQueryRunner()
// you can use its methods only after you call connect
// which performs real database connection
await queryRunner.connect()
// .. now you can work with query runner and call its methods
// very important - don't forget to release query runner once you finished working with it
await queryRunner.release()