【发布时间】:2021-01-01 00:44:34
【问题描述】:
我正在尝试在 Mikro-orm 和我的服务之间设置一个 base repository 类,以便拥有一个简单的接口,并在技术堆栈需要时将实现交换为另一个框架。
但是,FilterQuery 似乎不接受 {_id: ObjectId} 作为参数:
abstract class MyBaseEntity {
@PrimaryKey()
_id: ObjectId;
@SerializedPrimaryKey()
id!: string; // won't be saved in the database
[PrimaryKeyType]: [ObjectId, string]; // i've set this up in the hope it would pick it up in the type check
}
class BaseRepositoryImpl<Entity extends MyBaseEntity> {
private collection: EntityRepository<Entity>;
constructor(
private repository: EntityRepository<Entity>
) {
this.collection = repository;
}
async findOne(id: ObjectId): Promise<Entity | null> {
const result = this.collection.findOne({_id: id}); //'{ _id: ObjectId; }' is not assignable to parameter of type 'FilterQuery<Entity>
// and '_id' does not exist in type '{ [PrimaryKeyType]?: any; }
return result;
}
}
【问题讨论】:
标签: typescript mikro-orm