tradle / serverless-iot-local

MIT License
14 stars 7 forks source link

Flatten wildcard results #3

Closed MikeTheReader closed 6 years ago

MikeTheReader commented 6 years ago

Problem Description

When setting up an iot function with a sql statement like:

SELECT *, clientid() AS authorizedUserId FROM 'project/topic'

If you send a message such as:

{
  fieldOne: 'test',
  fieldTwo: 'testTwo'
}

The event that will be sent to the handler is:

{
  * : {
    fieldOne: 'test',
    fieldTwo: 'testTwo'
  },
  authorizationUserId: undefined
}

Using the same sql statement on a live AWS setup generates a message like:

{
  fieldOne: 'test',
  fieldTwo: 'testTwo',
  authorizationUserId: undefined
}

Changes Made

Modified sql.js to treat a field with no alias differently than a with an alias. In the case of no alias, the properties of the JSON object will be assigned directly to the event object. If there is an alias then it will behave as it did previously, and place the JSON object in the event using the alias as the key. This way, a SQL like:

SELECT * as message, clientid() AS authorizedUserId FROM 'project/topic'

Will still generate:

{
  message : {
    fieldOne: 'test',
    fieldTwo: 'testTwo'
  },
  authorizationUserId: undefined
}
mvayngrib commented 6 years ago

@MikeTheReader thanks! will release as major as people might be depending on the broken variant

MikeTheReader commented 6 years ago

@mvayngrib Glad I can help. Thanks for creating this in the first place. It's been really helpful.