sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

settings raw as true in sqlite returns JSON as String

Open suraj5969 opened this issue 2 years ago • 2 comments

Bug Description

when raw is true sequelize returns JSONs as string for sqlite database. for select queries findOne and findAll, when used with Postgres function returns JSON as object in JavaScript but when using with SQLite it return JSON's as strings when raw is true.

Reproducible Example

const user = await userModel.findOne({
    where:{
        id: "12ffe7b6-1234-4dac-5678-e98ab412b968"
    },
    raw: true,
});
console.log(typeof user?.preferences, "user preferences");

here user.preferences is a JSON object but it is returned as string. For postgres it works well and returns a JSON object, but for sqlite it returns string.

I am moving my database from postgres to sqlite as I am moving to a VPS server. I already have a codebase where there are many raw as true are used, and also in select queries most of the time we don't need actual models as we only return data from GET API, So I wanted to keep raw as true.

What do you expect to happen?

JSONs should be returned as a js object for sqlite database

Environment

I am using sequelize version 6.32.1, with node 14 with SQLite database with sqlite3 connector Library.

Would you be willing to resolve this issue by submitting a Pull Request?

  • [ ] Yes, I have the time and I know how to start.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

suraj5969 avatar Dec 24 '23 15:12 suraj5969

As I can see the code that is published to npm is from v6 branch, instead of main. If I take a look at code in v6 at sqlite->query.js at line 119

it checks if query is raw, if yes it returns the results without applying data type parsers, but here we are not using completery raw query with sequelize.query() we are just setting raw as true in findOne, so I think it should still apply the data type parsers.

I don't know if there is some other reason it dosen't apply datatype parsing, but currently ignoring that if condition is fixing my issue.

suraj5969 avatar Dec 24 '23 18:12 suraj5969

The point of raw seems to be to bypass most of the work we do on the database value. It was returned parsed in postgres because the pg library does that, but I'd argue that is wrong and it should have been returned as a string there too

A separate option (or another value for "raw") could be added that makes datatype parsing occurs, but where the data is returned as a plain object instead of a model instance.

ephys avatar Jan 30 '24 21:01 ephys