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

settings raw as true in sqlite returns JSON as String #16900

Open
1 task
suraj5969 opened this issue Dec 24, 2023 · 2 comments
Open
1 task

settings raw as true in sqlite returns JSON as String #16900

suraj5969 opened this issue Dec 24, 2023 · 2 comments
Labels
dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug

Comments

@suraj5969
Copy link

suraj5969 commented Dec 24, 2023

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 suraj5969 added pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug labels Dec 24, 2023
@suraj5969
Copy link
Author

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.

@ephys
Copy link
Member

ephys commented Jan 30, 2024

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 ephys added the dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). label Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug
Projects
None yet
Development

No branches or pull requests

2 participants