Open robertmain opened 2 years ago
Can you share your entity class and its repository class please.
@kirisakiken Sure! Here's my entity class:
import {
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn,
PrimaryColumn,
} from 'typeorm';
import { Exclude } from 'class-transformer';
export abstract class BaseEntity {
@PrimaryColumn({
generated: 'uuid',
type: 'uuid',
})
public id: string;
@CreateDateColumn()
public createdAt: Date;
@UpdateDateColumn()
public updatedAt?: Date;
@Exclude()
@DeleteDateColumn()
public deletedAt?: Date = null;
}
I'm not sure what exactly you mean by "its repository class", but under the assumption you meant the service class - here it is also:
import { Repository, DeepPartial, FindManyOptions, IsNull, In } from 'typeorm';
import { BaseEntity } from './base.entity';
export abstract class BaseService<Entity extends BaseEntity> {
protected readonly repository: Repository<Entity>;
public constructor(repository: Repository<Entity>) {
this.repository = repository;
}
public async findById(
ids: string[],
withDeleted = false,
options: FindManyOptions<Entity> = {}
): Promise<Entity[]> {
const records = await this.repository.find({
...options,
withDeleted,
where: {
id: In(ids),
},
});
return records;
}
public async findAll(
withDeleted = false,
options: FindManyOptions<Entity> = {}
): Promise<Entity[]> {
const records = await this.repository.find({
...options,
withDeleted,
});
return records;
}
public async save(entitites: DeepPartial<Entity>[]): Promise<Entity[]> {
const results = this.repository.save(entitites);
return results;
}
Beware that your base entity class conflicts with typeorm's base entity class BaseEntity
. And, have you tried casting your find options where? e.g. { id: In(ids) } as FindOptionsWhere<Entity>
Why does it conflict? As long as BaseEntity
isn't imported from TypeORM there shouldn't be an issue.
Also yes I have, the issue stems from the fact that entries in where
are nullable whereas fields on BaseEntity
like id
, createdAt
etc. are (by their definition) NOT nullable.
Okay, so it's a temporary workaround, but this works:
const records = await this.repository.find({
...options,
where: {
id: In(ids),
} as FindOptionsWhere<Entity>,
withDeleted,
});
Issue Description
It does not appear to be possible to use the
where
option inRepository<T>.find()
. This is becauseFindOneOptions<Entity>.where?
lists all the columns underwhere
as nullable i.e:But, those types are being typed as
NonNullable
by TypeORM - in this case:FindOptionsWhereProperty<NonNullable<Entity["id"]>>
.Thus, it seems to be complaining because
FindOneOptions<Entity>.where
properties are (rightfully) nullable, when the actual entity properties themselves that those fields refer to are not nullable.Expected Behavior
This code should be valid:
Actual Behavior
I get an error message that says:
My Environment
Additional Context
Relevant Database Driver(s)
aurora-mysql
aurora-postgres
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
spanner
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?