序列化时间戳名称 [英] Sequelize timestamp names

查看:53
本文介绍了序列化时间戳名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于续集的小问题,我无法解决,这让我很恼火.

I have a small issue with sequelize that I can't get around and it irritates the hell out of me.

我的所有模型属性都使用驼峰命名法,但我使用蛇形大小写将它们保存到数据库 (postgres) 中.这部分工作正常,除非我想使用 sequelize timestamps 选项.

I'm using camelCase for all my model attributes but I'm using snake case to persist them into the database (postgres). That part works fine except if I want to use sequelize timestamps option.

如果我设置 underscored: false,时间戳会以驼峰命名法保存到数据库中.

If I set underscored: false, timestamps get persisted to the database in camelCase.

如果我设置了underscored: true,它们会被持久化到snake_case 中,但随后它们会在模型上被保存在snake_case 中.

If I set underscored: true, they get persisted in snake_case but then they are in snake_case on the model.

我想要实现的是数据库中的snake_case和模型时间戳的camelCase.

感觉这两个选项是相互排斥的.

It feels like those 2 options are mutually exclusive.

谢谢

推荐答案

是的,你可以.关键是结合模型设置和字段映射.我就是做这个的:定义模型时,添加以下字段映射:

Yes, you can. Key is to combine model setting and field mappings. That's what I do: When you defining your model, add following field mappings:

createdAt: { type: Sequelize.DATE, field: 'created_at' },
updatedAt: { type: Sequelize.DATE, field: 'updated_at' },
deletedAt: { type: Sequelize.DATE, field: 'deleted_at' }

然后在模型选项中:

timestamps: true,
underscored: true

对我来说就像一种魅力.这样你也可以正确使用 paranoid 选项.

Works like a charm for me. In this way you can also use paranoid option correctly.

这篇关于序列化时间戳名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆