Ai
8 Star 43 Fork 16

Gitee 极速下载/TypeORM

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/typeorm/typeorm
克隆/下载
container.ts 2.27 KB
一键复制 编辑 原始数据 按行查看 历史
Lucian Mocanu 提交于 2025-12-12 08:29 +08:00 . chore: update prettier to v3 (#11702)
/**
* Container options.
*
* @deprecated
*/
export interface UseContainerOptions {
/**
* If set to true, then default container will be used in the case if given container haven't returned anything.
*/
fallback?: boolean
/**
* If set to true, then default container will be used in the case if given container thrown an exception.
*/
fallbackOnErrors?: boolean
}
/**
* @deprecated
*/
export type ContainedType<T> = { new (...args: any[]): T } | Function
/**
* @deprecated
*/
export interface ContainerInterface {
get<T>(someClass: ContainedType<T>): T
}
/**
* Container to be used by this library for inversion control. If container was not implicitly set then by default
* container simply creates a new instance of the given class.
*
* @deprecated
*/
const defaultContainer: ContainerInterface =
new (class implements ContainerInterface {
private instances: { type: Function; object: any }[] = []
get<T>(someClass: ContainedType<T>): T {
let instance = this.instances.find((i) => i.type === someClass)
if (!instance) {
instance = {
type: someClass,
object: new (someClass as new () => T)(),
}
this.instances.push(instance)
}
return instance.object
}
})()
let userContainer: ContainerInterface
let userContainerOptions: UseContainerOptions | undefined
/**
* Sets container to be used by this library.
*
* @deprecated
*/
export function useContainer(
iocContainer: ContainerInterface,
options?: UseContainerOptions,
) {
userContainer = iocContainer
userContainerOptions = options
}
/**
* Gets the IOC container used by this library.
*
* @deprecated
*/
export function getFromContainer<T>(someClass: ContainedType<T>): T {
if (userContainer) {
try {
const instance = userContainer.get(someClass)
if (instance) return instance
if (!userContainerOptions || !userContainerOptions.fallback)
return instance
} catch (error) {
if (!userContainerOptions || !userContainerOptions.fallbackOnErrors)
throw error
}
}
return defaultContainer.get<T>(someClass)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/mirrors/TypeORM.git
git@gitee.com:mirrors/TypeORM.git
mirrors
TypeORM
TypeORM
master

搜索帮助