loopbackio / loopback-connector-sqlite3

SQLite3 Connector for LoopBack
Other
13 stars 22 forks source link

Buffer property return TEXT instead of buffer #43

Open wuzhuobin opened 5 years ago

wuzhuobin commented 5 years ago

Bug or feature request

Description of feature (or steps to reproduce if bug)

Currently, I am using this with loopback4. But the connector does not provide some type support such as, Buffer or Array. They will all turn into TXET in sqlite3. And more importantly, the 'Buffer' seems to work quite well. When i wrote something to a 'Buffer' property, it will ture in to some TEXT like


{ type: 'Buffer',
  data: 
   [ 120,
     156,
     237,
     205,
     59,
]
```.
And I when i retrieve it is still a JSON object instead of a Buffer object which is different than the TypeScript definition in my model property. While Typescript believe it is a buffer,,,,

### Link to sample repo to reproduce issue (if bug)

### Expected result

### Actual result (if bug)

### Additional information (Node.js version, LoopBack version, etc)
Artoria2e5 commented 10 months ago

I think this is because SQLite3.prototype.fromColumnValue does not do anything beyond a JSON-decode for buffer -- it was never updated for LB4. I believe a change like the following will work:

SQLite3.prototype.fromColumnValue = function(property, value) {
  if (value == null || !property) {
    return value;
  }
  switch (property.type.name) {
    case 'Number':
      return (+value);
    case 'Boolean':
      return (
        value === 'Y' || value === 'y' ||
        value === 'T' || value === 't' ||
        value === '1' || value === 1
      );
    case 'String':
      return String(value);
    case 'Date':
      return new Date(value);
    case 'Buffer':
      return Buffer.from(JSON.parse(value));
    case 'GeoPoint':
    case 'Point':
    case 'List':
    case 'Array':
    case 'Object':
    case 'ModelConstructor':
    case 'JSON':
    default:
      return JSON.parse(value);
  }
};

Well, it might work. I have zero idea how all this works given the upper/lowercase difference from what's on https://loopback.io/doc/en/lb4/LoopBack-types.html.

Storing Buffer as a JSON array in text is also one of the worst ways to do it. This stuff really should've been an sqlite BLOB. Maybe use better-sqlite3 which does that and is faster?