jeremydaly / data-api-client

A "DocumentClient" for the Amazon Aurora Serverless Data API
MIT License
439 stars 61 forks source link

Postgres numeric fields returned as strings #53

Closed ffxsam closed 4 years ago

ffxsam commented 4 years ago

In my table, I have a column "duration" of type numeric(7,3). The Data API client is returning this as a string:

  {
    id: 365,
    file_uuid: 'fdc4c6ed-d72e-4621-aaa6-be2efe6ca5a1',
    name: "At World's Edge",
    owner_id: '9dbb70d7-3d17-4215-8966-49815e461dee',
    duration: '285.082',
    // ...
  }
jeremydaly commented 4 years ago

@ffxsam, the data type mappings are for MySQL. This should be fixable by adding the correct data type and mapping it in getType(). Can you send an example of the raw Data API response?

ffxsam commented 4 years ago

@jeremydaly Sorry for the delay, just now circling back on this.

What do you mean adding the correct data type and using getType()?

Here's the raw response from dataApi.query:

{
  records: [
    {
      id: 1,
      file_uuid: 'affaa9ad-a968-452f-9f72-f3914d2c3782',
      name: 'Test File',
      owner_id: 'cbf0cd89-7788-457c-8c32-3e725cd5d26b',
      duration: '233.578',
      metadata: '{"name": "Test 123", "year": 2020}',
      filesize: 827312,
      original_filename: 'Nothing.wav',
      folder_id: null,
      transcode_status: 'PENDING',
      internal_notes: null,
      public_notes: null,
      deleted: false,
      created_at: '2020-08-28 16:08:27.988319',
      updated_at: '2020-08-28 16:08:27.988319',
    }
  ]
}
jeremydaly commented 4 years ago

This looks like the marshalled response from the library. The "raw" response should look something like this:

[
  {  "longValue": 9 },
  { "stringValue": "Cousin Oliver" },
  { "longValue": 10 },
  { "booleanValue": false }
]

There is a getType() function in the source that needs to know the "data type" in order to coerce it to the correct JavaScript data type.

ffxsam commented 4 years ago

Oh, you meant the raw response from RDSDataService.

     {
        "stringValue": "233.578"
      },

Aha! So RDSDataService is lying to you. 😄 I'll report this to AWS, seems like a bug. Now the question is, is it an issue with the core API, or just the JS SDK? (edit: looks like there's an open issue already - https://github.com/aws/aws-sdk-js/issues/3114)

At any rate, feel free to close this issue. Thanks, Jeremy!

ffxsam commented 4 years ago

Expected behavior, evidently: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html#data-api.calling

Very odd to me that they treat DECIMAL as a string. 🤔 But not likely to change, as it would be a breaking change I think.