sequelize / sequelize/sequelize

settings raw as true in sqlite returns JSON as String

Open
#16,900 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

dialect: sqlite pending-approval
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SQLite handling used by Model.findOne and findAll when raw: true, then trace how JSON values are converted before being returned. Reproduce the behavior with the provided model query and verify that SQLite returns preferences as a JavaScript object, matching the PostgreSQL result.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, sqlite
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.