mikro-orm 使用 type-graphql 缓存数据
cached data by mikro-orm using type-graphql
我正在尝试 mikro-orm,所以我做了一个简单的 CRUD 解析器,但我从 graphql 调试控制台看到删除后像:
mutation{
deleteItem(id:9)
}
有如下代码
@Mutation(() => Boolean)
async deleteItem(@Arg('id') id: number, @Ctx() { em }: MyContext): Promise<boolean> {
try {
await em.nativeDelete(Item, { id });
return true;
} catch (error) {
console.log(error);
return false;
}
}
响应正常。
但是,如果我尝试编辑已删除的项目,则会返回已编辑的项目...即使禁用了 mikro-orm 缓存 cache: { enabled: false },
错误在哪里?我需要强制冲水吗?
这是 editItem 解析器:
@Mutation(() => Item, { nullable: true })
async editItem(
@Arg('id') id: number,
@Arg('data', () => String, { nullable: true }) data: string,
@Ctx() { em }: MyContext
): Promise<Item | null> {
const item = await em.findOne(Item, { id });
if (!item) {
return null;
}
console.log(item);
if (typeof data !== 'undefined') {
post.data = data;
await em.persistAndFlush(item);
}
return item;
}
谢谢
even with mikro-orm cache disabled cache: { enabled: false }
那是元数据缓存,除非你使用 ts-morph.
,否则默认情况下它是禁用的
em.nativeDelete()
不会从身份映射中删除任何内容,因此如果您在给定的上下文中加载了该实体,通过其 PK 查询将始终 return 身份映射中的实体而不查询数据库
一般来说这应该没问题,因为删除请求有自己的上下文 - 您是否正确处理了请求上下文?通过 em.fork()
或通过 RequestContext
助手。这是必需的。
https://mikro-orm.io/docs/identity-map
如果这是有意在单一上下文中发生的,您可以使用 em.removeAndFlush(em.getReference())
将从身份映射中删除该项目。
我正在尝试 mikro-orm,所以我做了一个简单的 CRUD 解析器,但我从 graphql 调试控制台看到删除后像:
mutation{
deleteItem(id:9)
}
有如下代码
@Mutation(() => Boolean)
async deleteItem(@Arg('id') id: number, @Ctx() { em }: MyContext): Promise<boolean> {
try {
await em.nativeDelete(Item, { id });
return true;
} catch (error) {
console.log(error);
return false;
}
}
响应正常。
但是,如果我尝试编辑已删除的项目,则会返回已编辑的项目...即使禁用了 mikro-orm 缓存 cache: { enabled: false },
错误在哪里?我需要强制冲水吗?
这是 editItem 解析器:
@Mutation(() => Item, { nullable: true })
async editItem(
@Arg('id') id: number,
@Arg('data', () => String, { nullable: true }) data: string,
@Ctx() { em }: MyContext
): Promise<Item | null> {
const item = await em.findOne(Item, { id });
if (!item) {
return null;
}
console.log(item);
if (typeof data !== 'undefined') {
post.data = data;
await em.persistAndFlush(item);
}
return item;
}
谢谢
even with mikro-orm cache disabled cache: { enabled: false }
那是元数据缓存,除非你使用 ts-morph.
,否则默认情况下它是禁用的em.nativeDelete()
不会从身份映射中删除任何内容,因此如果您在给定的上下文中加载了该实体,通过其 PK 查询将始终 return 身份映射中的实体而不查询数据库
一般来说这应该没问题,因为删除请求有自己的上下文 - 您是否正确处理了请求上下文?通过 em.fork()
或通过 RequestContext
助手。这是必需的。
https://mikro-orm.io/docs/identity-map
如果这是有意在单一上下文中发生的,您可以使用 em.removeAndFlush(em.getReference())
将从身份映射中删除该项目。