An all-TypeScript package which provides safer typing for functions in TypeORM.
- Enhansed type supports for TypeORM data-mapper pattern
- Pick only joined relations with
find***
functions.
- Pick only joined relations with
- Almost zero runtime
In TypeORM repositories, returned types of find***
functions have all relations, regardless of whether they are actually joined.
This package introduces type-safe repository type. Its find***
functions return only the actually joined relations by checking relations
option.
This package is inspired by the article below:
TypeORMのData Mapperパターンにおけるリレーションの型安全性を担保する (Japanese)
- Annotate relations with the
Relation
type in the entity. - Cast repository to the
SafeRepository
. - Just use the repository. The properties of returned types are automatically filtered.
@Entity()
class Sample {
@OneToOne()
rel: Relation<Sample2>; // 1. Annotate relation
}
const sampleRepository = dataSource.getRepository(
Sample
) as unknown as SafeRepository<Sample>; // 2. Cast repository to the SafeRepository
// 3. Just use the repository. The properties of returned types are automatically filtered
const withoutRelations = await sampleRepository.find();
// => {}
const withRelations = await sampleRepository.find({
relations: {
rel: true,
},
});
// => { rel: Relation<Sample2> }
For more information about usage, see the examples
directory.
Available via npm.
npm i --save typeorm-strict-type
MIT