typestack/typeorm-typedi-extensions được TopGit xếp vào nhóm dự án mã nguồn mở, với 261 sao trên GitHub, viết chủ yếu bằng TypeScript. Dependency injection and service container integration with TypeORM using TypeDI library.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Import the reflect-metadata package at the first line of your application:
import 'reflect-metadata';
// Your other imports and initialization code
// comes here after you imported the reflect-metadata package!
You need to enable emitting decorator metadata in your Typescript config. Add these two lines to your tsconfig.json file under the compilerOptions key:
Configure TypeORM in your app to use the TypeDI container before you create a connection:
import { createConnection, useContainer } from 'typeorm';
import { Container } from 'typeorm-typedi-extensions';
// ▲ Notice how we import container from this library, instead of TypeDI.
/** Tell TypeORM to use the container provided by this lib to resolve it's dependencies. */
useContainer(Container);
/** Create a connection and start using TypeORM. */
createConnection({
/* <connection options> */
}).catch(error => {
console.error(`Couldn't connect to the database!`);
console.error(error);
});
Usage
This package exposes three decorators all three decorators can be used on properties or on constructor parameters.
IMPORTANT:
To allow TypeDI to resolve the dependencies on your classes you must mark them with @Service decorator from the TypeDI package.
@InjectConnection decorator
Injects Connection from where you can access anything in your connection.
Optionally, you can specify a connection to inject by name in the decorator parameter.
import { Service } from 'typedi';
import { Connection } from 'typeorm';
import { InjectConnection } from 'typeorm-typedi-extensions';
@Service()
export class MyCustomClass {
@InjectConnection()
private propertyInjectedConnection: Connection;
constructor(@InjectConnection() private constructorInjectedConnection: Connection) {}
}
@InjectManager decorator
Injects EntityManager from where you can access any entity in your connection.
Optionally, you can specify a connection to inject by name in the decorator parameter.
import { Service } from 'typedi';
import { EntityManager } from 'typeorm';
import { InjectManager } from 'typeorm-typedi-extensions';
@Service()
export class MyCustomClass {
@InjectManager()
private propertyInjectedEntityManager: EntityManager;
constructor(@InjectManager() private constructorInjectedEntityManager: EntityManager) {}
}
@InjectRepository decorator
Injects Repository, MongoRepository, TreeRepository or custom repository of some Entity.
Optionally, you can specify a connection to inject by name in the decorator parameter.
import { Service } from 'typedi';
import { Repository } from 'typeorm';
import { InjectRepository } from 'typeorm-typedi-extensions';
// MyDatabaseModel is a TypeORM entity (class marked with `@Entity()` decorator)
import { MyDatabaseModel } from './entities/post.entity.ts';
@Service()
export class MyCustomClass {
@InjectRepository(MyDatabaseModel)
private propertyInjectedRepository: Repository<MyDatabaseModel>;
constructor(@InjectRepository(MyDatabaseModel) private constructorInjectedRepository: Repository<MyDatabaseModel>) {}
}
You can also inject custom Repository of some Entity. To make this work have to create the class which extends the
generic Repository<T> class and decorate it with EntityRepository<T> decorator.
import { Service } from 'typedi';
import { Repository, EntityRepository } from 'typeorm';
import { InjectRepository } from 'typeorm-typedi-extensions';
// UserModel is a TypeORM entity (class marked with `@Entity()` decorator)
import { UserModel } from './entities/user.entity.ts';
@Service()
@EntityRepository(UserModel)
export class UserRepository extends Repository<UserModel> {
public findByEmail(email: string) {
return this.findOne({ email });
}
}
@Service()
export class PostService {
constructor(
@InjectRepository()
private readonly userRepository: UserRepository
) {}
public async userExist(user: User): boolean {
return (await this.userRepository.findByEmail(user.email)) ? true : false;
}
}
typestack/typeorm-typedi-extensions có bao nhiêu sao?
typestack/typeorm-typedi-extensions có 261 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/typestack/typeorm-typedi-extensions. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
typestack/typeorm-typedi-extensions có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho typestack/typeorm-typedi-extensions. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
typestack/typeorm-typedi-extensions còn đang phát triển không?
Commit gần nhất trên typestack/typeorm-typedi-extensions là 3.0 năm trước (theo timestamp GitHub). Repo có 33 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
typestack/typeorm-typedi-extensions là gì?
typestack/typeorm-typedi-extensions (typestack/typeorm-typedi-extensions) là dự án TypeScript trên GitHub. Theo mô tả gốc: Dependency injection and service container integration with TypeORM using TypeDI library.
typestack/typeorm-typedi-extensions viết bằng ngôn ngữ gì?
typestack/typeorm-typedi-extensions chủ yếu viết bằng TypeScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về typeorm-typedi-extensions?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về typeorm-typedi-extensions.