最佳设计模式来更新或插入批量数据

时间:2020-08-05 14:58:13

标签: javascript mysql node.js express mysql2

我想知道哪种是在数据库中更新或插入批量数据的最佳设计模式。

我的应用使用MVVM模式,其中模型执行“ CUD”操作 还有一个用于复杂“ R”查询的QueryObject模式。

例如,我有一些类似的代码:

const book = new BookModel(data);
const id = await book.save();

和其他类似的东西:

const bks = await new BooksQuery().orderByAuthor().whereGenre('engineering').execute();
const books = books.map((book) => new BooksViewModel(req, book));
this.render(req, res, 'books', books);

让我们假设我必须保存许多书籍的排序顺序。 问题是,从逻辑上讲它去了哪里?

  1. 这很丑,我认为该模型被滥用了:
const book = new BookModel();
const result = await book.saveSort(orderedIds);

const book = new BookModel(orderedIds);
const result = await book.saveSort();
  1. 更漂亮,但我必须再次拆分实体管理(也许可以与称为BooksQuery的查询对象合并):
const result = await new BooksCommand().saveSort(orderedIds).execute();
  1. 我不知道的其他选项。

谢谢!

0 个答案:

没有答案