node.js - Node Sequelize Postgres - BulkCreate 忽略自定义字段的重复值

标签 node.js postgresql sequelize.js

目标: 我有一个来自另一个服务的对象列表,我想将这些对象保留在我自己的 Postgres 数据存储中。来自其他服务的数据返回 JSON,但不包含任何 ID。
当前结果:

  • 当我第一次运行 bulkCreate 时,它​​成功地将数据同步到我的数据库(参见下面的示例代码)
  • 当我从其他服务获取新批次(即检查更新)并再次调用 bulkCreate 时,即使标题已存在,也会为每一行将新主题插入到数据库中

  • 预期结果
  • 当我第一次运行 bulkCreate 时,它​​成功地将数据同步到我的数据库(参见下面的示例代码)
  • 当我从其他服务(即检查更新)获取新批次并再次调用 bulkCreate 时,只会插入在数据库中未找到标题的主题,其余主题的“计数”属性已更新。

  • Topic.js
    const database = require('../../shared/database'); // shared db connection. This works
    const {DataTypes, Model} = require('sequelize');
    
    class Topic extends Model { }
    
    Topic.init({
      topicId: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        defaultValue: undefined,
      },
      title: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      count: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
    }, {
      sequelize: database, 
    });
    
    module.exports = Topic;
    
    SyncAPI.js (第一次运行)-按预期工作
    const url = 'https://....'; // the remote service
    const topics = await this.http.get(url); // simple example of getting the new data
    // [{ 'title': 'Leadership', 'count': 214 }, 
    // { 'title': 'Management', 'count': 51 }]
    
    await Topic.bulkCreate(topics);
    // [{ 'topicId': 1, 'title': 'Leadership', 'count': 214 }, 
    // { 'topicId': 2, 'title': 'Management', 'count': 51 }]
    
    SyncAPI.js (第二次运行)- 创建重复项
    const url = 'https://....'; // the remote service
    const topics = await this.http.get(url); // note how the title is the same with updated counts
    // [{ 'title': 'Leadership', 'count': 226 }, 
    // { 'title': 'Management', 'count': 54 }]
    
    await Topic.bulkCreate(topics); // the old, inserted topics remaining, with new entries appended
    // [{ 'topicId': 1, 'title': 'Leadership', 'count': 214 }, 
    // { 'topicId': 2, 'title': 'Management', 'count': 51 },
    // [{ 'topicId': 3, 'title': 'Leadership', 'count': 226 }, 
    // { 'topicId': 4, 'title': 'Management', 'count': 54 }
    
    我在这里看到了 Sequelize 文档 (https://sequelize.org/master/class/lib/model.js~Model.html#static-method-bulkCreate),它说我可以指定一个 'ignoreDuplicates' 选项,但它只能通过比较主键(“忽略主键的重复值)来工作。
    我正在寻找bulkCreate() 方法中的某种方法,用我的自定义键'title' 指定'ignoreDuplicates',然后使用'updateOnDuplicate' 来更新计数。

    最佳答案

    我的最终解决方案是实际获取标题并将其转换为哈希,然后将其作为“topic_uid”作为 UUID 存储在我的数据库中。

    关于node.js - Node Sequelize Postgres - BulkCreate 忽略自定义字段的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63726887/

    相关文章:

    javascript - Node.js xml 到 json 对象

    c++ - [] 不允许重载 C++ 函数数组

    node.js - 从 NPM 安装 PhoneGap 失败

    javascript - yarn 2 : Difference between Zero Installs and normal install?

    postgresql - 如何在 Go 应用程序中处理打开/关闭 Db 连接?

    node.js - 默认时区不适用于 postgres 中的 sequelize 查询

    mysql - SQL:我们需要 ANY/SOME 和 ALL 关键字吗?

    java - pg_hba.conf 中没有条目

    node.js - 如何在 Sequelize ORM 中插入 PostGIS GEOMETRY 点?

    mysql - 序列化创建类型错误: Cannot read property 'apply' of undefined