loopbackio / loopback-connector-mysql

Loopback Connector for MySQL
Other
125 stars 183 forks source link

char(1) field detected as boolean #312

Closed FredLackeyOfficial closed 7 years ago

FredLackeyOfficial commented 7 years ago

The following JSON is the result of performing...

dataSource.discoverSchema(table.name, { visited: {}, assocations: true }, function(err, schema) {
  var jsonName = path.join(outputPath, table.name + '.json');
  fs.writeFile(jsonName, JSON.stringify(schema, null, 2), function(err) {
    [snip]
  });
});

The following snippet was taken from the resulting JSON file. Notice the type ID is Boolean where the MySQL type remains char(1)...

    "typeId": {
      "type": "Boolean",
      "required": true,
      "length": 1,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "type_id",
        "dataType": "char",
        "dataLength": 1,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      }
    },

darwin x64 6.11.1 loopback-example-database@2.0.0 /Users/flackey/Source/GitHub/StrongLoop/loopback-example-database ├── loopback@3.9.0 ├── loopback-boot@2.25.0 ├── loopback-component-explorer@2.7.0 ├── loopback-connector-mongodb@1.18.1 ├── loopback-connector-mysql@5.0.0

b-admike commented 7 years ago

@FredLackeyOfficial Thank you for bringing this up. What you are seeing is the default behaviour as of v3.0.0 of this connector. If you would like your CHAR(1) column to be treated as a String instead of a Boolean LoopBack type, then you need to set treatCHAR1AsString to true as part of your options to discoverSchema() like so:

module.exports = function(app) {
    var db = app.datasources.mysqlDs;
    var tableName = 'charTable';

    db.discoverSchema(tableName, { visited: {}, associations: true, treatCHAR1AsString: true},
        function(err, schema) {
            if (err) throw err;
            console.log('char(1) col >>', JSON.stringify(schema.properties.charcol));
  });

};

Result:

Web server listening at: http://localhost:3000
Browse your REST API at http://localhost:3000/explorer
char(1) col >> {"type":"String","required":false,"length":1,"precision":null,"scale":null,"mysql":{"columnName":"charcol","dataType":"char","dataLength":1,"dataPrecision":null,"dataScale":null,"nullable":"Y"}}

I have updated our README to reflect the flags introduced then for CHAR(1), BIT(1), and TINYINT(1) columns.

FredLackeyOfficial commented 7 years ago

Interesting. I'd love to understand the logic behind switching the data types from char(1) to boolean as a default.

b-admike commented 7 years ago

You can have a look at #232.