Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer property return TEXT instead of buffer #43

Open
1 of 2 tasks
wuzhuobin opened this issue Aug 26, 2019 · 1 comment
Open
1 of 2 tasks

Buffer property return TEXT instead of buffer #43

wuzhuobin opened this issue Aug 26, 2019 · 1 comment

Comments

@wuzhuobin
Copy link

Bug or feature request

  • Bug
  • 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
Copy link

Artoria2e5 commented Nov 13, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants