gpt4 book ai didi

javascript - 如何从 Sequelize 的所有查询中排除 createdAT 和 updatedAt?

转载 作者:行者123 更新时间:2023-12-03 22:30:59 24 4
gpt4 key购买 nike

我正在使用 Node 14 和 Sequelize 对现有的 Postgres 数据库进行建模。
已经创建的表格没有 createdAt neighter updatedAt Colums 所以我想指示我的 Sequelize 永远不要尝试选择、插入或更新这些属性。
所以我尝试了

const sequelize = new Sequelize (
config.database,
config.username,
config.password,
config.params,
{ define: {
defaultScope: {
attributes: { exclude: ['createdAt', 'updatedAt'] }
},
timestamps: false
}
})

但即使这样......我的 findAll()坚持选择 createdAtupdatedAt我错过了什么?

最佳答案

根据我的研究,你所拥有的应该是可行的,但正如我们所知,编程有时会很有趣。 :(
考虑尝试以下选项:

  • 使用 beforeFind 钩法
  • 添加范围以排除 createdAt & updatedAt this guide 中所教的属性

  • 选项一示例
    const sequelize = new Sequelize (
    config.database,
    config.username,
    config.password,
    config.params,
    {
    define: {
    hooks: {
    beforeFind: (model) => {
    model.attributes = {}
    models.attributes.exclude: ['createdAt', 'updatedAt']
    }
    },
    timestamps: false
    }
    })
    选项二示例
    const sequelize = new Sequelize (
    config.database,
    config.username,
    config.password,
    config.params,
    { define: {
    scopes: {
    excludeCreatedAtUpdateAt: {
    attributes: { exclude: ['createdAt', 'updatedAt'] }
    }
    },
    timestamps: false
    }
    })

    // Then use it when making your queries like this
    const data = await Model.scope('excludeCreatedAtUpdateAt').findAll(...)

    关于javascript - 如何从 Sequelize 的所有查询中排除 createdAT 和 updatedAt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66978480/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com