NodeJS 使用 Sequelize 和块数据批量插入 MySQL

发布于 2025-01-20 01:04:31 字数 307 浏览 0 评论 0原文

我想用semelize orm和nodejs(Nestjs)插入MySQL。我很好奇是否有一个处理非常大数据的最佳方法。我想到了拉拉维尔(Laravel)的“块”。在Laravel中,我们可以这样做:

$chunks = $insert_data->chunk(500);

foreach ($chunks as $chunk) {
   \DB::table('items_details')->insert($chunk->toArray());
}

是否有人使用semelize和nodejs实现了此操作。谢谢

I want to bulk insert MySQL with Sequelize ORM and NodeJS (NestJS). I'm curious if there is a best way to handle very large data. I thought of "chunk" like Laravel did. In Laravel we can do it like this:

$chunks = $insert_data->chunk(500);

foreach ($chunks as $chunk) {
   \DB::table('items_details')->insert($chunk->toArray());
}

Has anyone implemented this with sequelize and NodeJS. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2025-01-27 01:04:31

我想它几乎是一样的(使用 lodash 来获取块):

const _ = require('lodash');

const chunks = _.chunk(insert_data, 500);

for (const chunk of chunks) {
  await ItemDetails.bulkCreate(chunk);
}

PS 根据您想要实现的目标,您可以使用事务来插入所有块,或者如果出现错误则不插入任何块插入任何块时或使用 try/catch 绕过或在失败后继续插入其他块。

I suppose it would be almost the same (using lodash to get chunks):

const _ = require('lodash');

const chunks = _.chunk(insert_data, 500);

for (const chunk of chunks) {
  await ItemDetails.bulkCreate(chunk);
}

P.S. Depending on what you want to achieve you either use a transaction to insert all chunks or none of them if there will is an error while inserting any of chunks OR use try/catch to bypass or continue inserting other chunks after failed one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文