指定小数点(数字)数据类型的长度和比例参数(sails js)

发布于 2025-02-06 20:38:19 字数 1136 浏览 2 评论 0原文

如何将十进制(长度,比例)作为货币列中的水线ORM中的列型?我可以找到 this 已经提出了问题。但是答案与问题无关。

这就是列属性的样子。我正在使用Sails-Postgres。

attributes: { 
    price: {
        type: 'number',
        columnType: 'decimal(9,2)',
        columnName: 'PRICE'
       }
     }

sails文档中

列类型完全取决于数据库。确保您选择的columnType对应于对您的数据库有效的数据类型!如果您未指定columnType,则适配器将根据属性的type

为您选择一个。

但是,当我提到columnType时:'DECIMAL(9,2)'(尽管它在Postgres中支持),它无法正常工作。当我使用蓝图检索数据时,它将价格作为字符串。

{
    "price": "10.00",
}

我已经读到这些数据类型字符串,文本,整数,浮点,日期,日期,日期,布尔值,二进制,数组,JSON,电子邮件,电子邮件由Waterline支持。在这种情况下,存储货币价值的最佳数据类型是什么?如果我们选择float数据类型,则如何将价格存储为十进制(9,2)

任何帮助都将被大大实现。谢谢。

How to specify decimal(length, scale) as a columnType in waterline orm for a currency column? I could find this question has been already asked. But the answer is not relevant to the question.

This is what the column attribute looks like. I'm using sails-Postgres.

attributes: { 
    price: {
        type: 'number',
        columnType: 'decimal(9,2)',
        columnName: 'PRICE'
       }
     }

In sails documents they have mentioned that

Column types are entirely database-dependent. Be sure that the columnType you select corresponds to a data type that is valid for your database! If you don’t specify a columnType, the adapter will choose one for you based on the attribute’s type.

But when I mentioned columnType: 'decimal(9,2)' (although it supports in Postgres) it does not properly work. When I retrieving data using blueprints it gives the price as a string.

{
    "price": "10.00",
}

I have read that these data types ­string, text, ­integer, float, date, ­dat­etime, ­boolean, ­binary, array, json, ­email are supported by waterline. In that case what would be the best data type to store a currency value? If we choose the float data type, how to store the price as decimal(9,2)?

Any help would be much appriciated. Thank you.

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

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

发布评论

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

评论(1

独夜无伴 2025-02-13 20:38:19

在这种情况下,我要做的是指定这样的定义:

 price: {
      type: 'number',
      defaultsTo: 34.99,
  },

由于我在此处指定默认价格,因此Waterline将为该值创建float4列类型。

还值得注意的是,我将将该金额转换为中心(通过乘以100),因此数据库将存储的内容将为3499我认为可以这样存储这种方式,因为付款提供商将是需要一分钱的任何方法。

在转换中,我发现在beforecreate Waterline Lifecle lifecycle回调中进行此操作。这样的东西。

beforeCreate: async function (valuesToSet, proceed) {
    // Compute amounts
    valuesToSet.price = sails.helpers.convertAmountToCent(valuesToSet.price)

    return proceed()
  },

另外,水线将列类型设置为float4,因为它是从defaultsto 34.99

的值推理。

What I do in this case is to specify the definition like so:

 price: {
      type: 'number',
      defaultsTo: 34.99,
  },

Since I'm specifying a default price in here, Waterline will create a float4 column type for that value.

It is also worthy of note that I will be converting that amount to center(by multiplying by 100) so What the database will store will be 3499 I find it okay to store this way because payment providers will be needing a cent amount any ways.

On the conversion, I find it suitable to do in a beforeCreate Waterline lifecycle callback. So something like this.

beforeCreate: async function (valuesToSet, proceed) {
    // Compute amounts
    valuesToSet.price = sails.helpers.convertAmountToCent(valuesToSet.price)

    return proceed()
  },

Also, Waterline set the column type to float4 because it is inferring that from the defaultsTo value of 34.99

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