YtramX
YtramX

Reputation: 180

Boolean attribute for Waterline model

I have a MySQL database and a Waterline model for a fairly simple record: Todo. It's straight-forward as we're using it for proof-of-concept for a Sails.js API.

CREATE TABLE `Todo` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `TodoItem` varchar(200) NOT NULL,
  `Completed` bit(1) NOT NULL DEFAULT b'0',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COMMENT='   ';

I have the model set up in this way:

module.exports = {
  connection: 'companyMySql',
  tableName: 'Todo',
  attributes: {
    ID: {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true
    },
    TodoItem: {
      type: 'string',
      required: true
    },
    Completed: {
      type: 'boolean',
      defaultsTo: 'false',
      size: 1,
      boolean: true
    }
  },
  autoCreatedAt: false,
  autoUpdatedAt: false,
  autoPK: false
};

The problem I'm having is that the auto-created API for Todo creates a bunch of extra cruft in the 'Completed' field, and I'm trying to find out how I remove it. I'm expecting a boolean value, but I could definitely work with 0/1, or string literals, but that's not what I'm getting. I've tried various validation on the field, like 'min: 0, max: 1', but that has not changed the result.

Here is the results of a retrieval of a single record:

{
  "ID": 1,
  "TodoItem": "Create Todo API",
  "Completed": {
    "0": 1,
    "length": 1,
    "parent": [
      100,
      0,
      0,
      0,
      3,
      83,
      69,
      76,
      69,
      67,
      96,
      46,
      96,
      73,
      68,
      96,
      44,
      32,
      96,
      116,
      111,
      100,
      111,
      96,
      // This continues on for quite some time
    ],
    "offset": 2128 // This value increases each time I call a GET until it gets to ~8000, then it resets to ~500
  }
}

So does anyone have any idea on what I can do to get a straight-forward boolean value out of this?

Upvotes: 0

Views: 1948

Answers (1)

YtramX
YtramX

Reputation: 180

After some further testing, I was able to get a boolean value to work as expected by using TINYINT(1) for my column datatype. Once I did that, this model worked as I expected it to:

Completed: {
  type: 'boolean',
  defaultsTo: false,
  boolean: true
}

This still doesn't seem ideal, but it was a solution for my purposes.

Upvotes: 1

Related Questions