Skip to content

snakeCaseMappers works differently for objects and FieldExpressions #1089

@Tapppi

Description

@Tapppi

Using snakeCaseMappers, patching an object will not map inner keys, only the first level (columns), but using a FieldExpression will map the json field references.

class TestModel extends Model {
  ...
  static columnNameMappers = snakeCaseMappers();
  ...
}

// Query with array/object
TestModel
  .query()
  .patch({
    jsonColumn: [{
      innerKey: 1
    }]
  });

// Database output
{
  json_column: '[{"innerKey": 1}]' 
}

// Query with FieldExpression
TestModel
  .query()
  .patch({
    'jsonColumn:[0][innerKey]': 1
  });

// Database output
{
  json_column: '[{"inner_key": 1}]' 
}

EDIT: IMHO, this should work consistently across, and there could be an option for snakeCaseMappers | knexSnakeCaseMappers, something like deep|deepKeys|innerKeys. Obviously I would prefer it to default to false, and I'm guessing the usual expectation is around how the normal objects now work, but the default's not a big deal for me eitherway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions