尽管下一个事务步骤失败,TypeORM 事务仍将实体保存到数据库

Sup*_*ero 2 transactions typeorm

我想要进行一个事务来保存新的表行以及该行的多对多辅助表条目。我使用相同的事务管理器保存这两个部分,但是当第二部分失败时,第一个部分不会恢复。为什么会发生这种情况?

await getManager().transaction(async transactionalEntityManager => {
    newFeed = await transactionalEntityManager
        .createQueryBuilder()
            .insert()
            .into(Feed)
            .values({ ...feed })
            .execute();
    console.log('======================================\n', newFeed);
    feed.sources.forEach(async source => {
        console.log('==in forEach===\n', source.id, newFeed.identifiers[0].id);
        await transactionalEntityManager
            .createQueryBuilder()
            .insert()
            .into('feed_source')
            .values({
                sourceId: source.id,
                feedId: newFeed.identifiers[0].id,
                fontColor: feed.fontColor ? feed.fontColor : null,
                postColor: feed.postColor ? feed.postColor : null
            })
            .execute();
    });
});
Run Code Online (Sandbox Code Playgroud)

和语法有关系吗?我以前没有使用过插入查询生成器语法。.execute() 是否会忽略该事务?这样做的正确方法是什么?

Sup*_*ero 6

好的,所以我解决了。这是因为 forEach 的行为似乎是异步的,因此代码会在完成循环之前跳转到事务的末尾。在寻找解决方案时,我更改了我使用的方法和一些功能,因此代码最终完全不同,但最主要的是我使用了 Promise.all() 并将返回的 .map() 的结果传递给它承诺。

await getManager().transaction(async transactionalEntityManager => {
                savedFeed = await transactionalEntityManager.save(newFeed);
                if (feed.feedSources) {
                    newFeed.feedSources = [];
                    await Promise.all(
                        feed.feedSources.map(async feedSource => {
                            return new Promise(async (resolve, reject) => {
                                const referencedSource = await Source.findOne(feedSource.sourceId);
                                console.log('===referencedSource===\n', referencedSource);
                                if (!referencedSource)
                                    throw new Error(
                                        'Could not find referenced source with id ' + feedSource.id
                                    );
                                const currentFeedSource = new FeedSource();
                                currentFeedSource.source = referencedSource;
                                currentFeedSource.feed = savedFeed;
                                feedSource.postColor
                                    ? (currentFeedSource.postColor = feedSource.postColor)
                                    : '';
                                feedSource.fontColor
                                    ? (currentFeedSource.fontColor = feedSource.fontColor)
                                    : '';
                                console.log('===currentFeedSource===\n', currentFeedSource);
                                await transactionalEntityManager.save(currentFeedSource);
                                resolve(true);
                            });
                        })
                    );
                }
Run Code Online (Sandbox Code Playgroud)