镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

sqlite: statement.setReadNullAsUndefined() #59457

Description

@LiviaMedeiros

What is the problem this feature will solve?

In JS, we have two different oddballs null and undefined. In SQL, they are represented as NULL.
Inserting them to database makes them coerced to NULL; however selecting to JS realm always produces nulls, which is not always desirable.

What is the feature you are proposing to solve the problem?

Similarly to statement.setReadBigInts(enabled), we can have statement.setReadNullAsUndefined() to make reads return NULLs as undefined instead of null.

Also we can have database-wide readNullAsUndefined option in new DatabaseSync(path[, options])

What alternatives have you considered?

Drowning in boilerplate code.

Activity

  1. added
    feature requestIssues requesting new Node.js features.
    sqliteIssues and PRs related to the SQLite subsystem.
    on Aug 12, 2025
  2. bakkot commented on Aug 13, 2025

    @bakkot
    Contributor

    When does getting null instead of undefined matter?

  3. jac0bandres commented on Aug 13, 2025

    @jac0bandres

    Honestly, this would be a good feature and I've run into a couple of cases where I have to convert SQL null types by hand. I've opened a pull request with the change: #59462.

    If it gets approved there could be a database-wide option after. But this one only implements setReadNullAsUndefined() on statements.

  4. LiviaMedeiros commented on Aug 14, 2025

    @LiviaMedeiros
    MemberAuthor

    When does getting null instead of undefined matter?

    Most notably, whenever we serialize the data to store or send. In JSON, every null consumes space, increases parsing time, reduces readability.

    Web API, especially having databases with sparse columns, usually should prefer not sending nulls to avoid wasting traffic.

    Also this is semantically more correct behaviour when we expect business logic to add new columns that are sparse by design and should not affect previously inserted rows. hasDiscountOnApril2010Promo, bonusAttackDuring1252Event, isAiGenerated, showSpecialWarningPopup, extraParams, and other stuff that inevitably happens someday.

  5. geeksilva97 commented on Aug 14, 2025

    @geeksilva97
    Contributor

    Is there any use case for this? In which situation does an undefined behave in a way that, with null, it would be a problem?

    I was looking into other players, and it's just a null.

  6. jac0bandres commented on Aug 14, 2025

    @jac0bandres

    Is there any use case for this? In which situation does an undefined behave in a way that, with null, it would be a problem?

    Besides the use cases we've laid out in this issue and the PR discussion, here are some more sources:

    Drizzle ORM request for distinct null and undefined handling: drizzle-team/drizzle-orm#2745
    Another for Supabase: supabase/supabase-js#244
    MongoDB distinction between the types and their use cases: https://www.mongodb.com/docs/manual/reference/bson-types/migrate-undefined/

  7. geeksilva97 commented on Aug 18, 2025

    @geeksilva97
    Contributor

    Is there any use case for this? In which situation does an undefined behave in a way that, with null, it would be a problem?

    Besides the use cases we've laid out in this issue and the PR discussion, here are some more sources:

    Drizzle ORM request for distinct null and undefined handling: drizzle-team/drizzle-orm#2745 Another for Supabase: supabase/supabase-js#244 MongoDB distinction between the types and their use cases: https://www.mongodb.com/docs/manual/reference/bson-types/migrate-undefined/

    Thank you for the references.

    What I saw from these links is people complaining about typing (where they'd like to use something like myprop?: string). The MongoDB link is about a migration from undefined to null.

    The players I brought are more similar to what node:sqlite is.

  8. geeksilva97 commented on Aug 18, 2025

    @geeksilva97
    Contributor

    Would you have an opinion here @cjihrig @nodejs/sqlite ?

  9. cjihrig commented on Aug 18, 2025

    @cjihrig
    Contributor

    Would you have an opinion here

    Are any other libraries actually implementing something like this? I think it's good to differentiate between feature requests and actual implementations.

    That said, I'm not opposed to this as long as it doesn't break anything. I also think it's worth benchmarking since SQLITE_VALUE_TO_JS() is likely a pretty hot path, and taking a performance hit there wouldn't be a good idea for a feature that no other libraries ship.

  10. louwers commented on Aug 18, 2025

    @louwers
    Contributor

    People really seem passionate about wanting undefined to be returned instead of null. I can see the appeal if you want to serialize to JSON.

    Interesting idea. API seems consistent. Not very complex to implement. Why not!

  11. tpoisseau commented on Aug 25, 2025

    @tpoisseau
    Contributor

    I consider applications should use pattern like DTO to return data to external services / users.
    It's a good practice to avoid returning unwanted or sensible data. Also decoupling raw data from data-source to data-view allow easy switch from one data-source to another one, explicit interface for aggregation and so on.

    I think a low level API like node:sqlite should not have the responsibility to transform data. This kind of transformation should be done at higher level like ORM layer or Data view layer.

  12. mattskel commented on Jan 22, 2026

    @mattskel

    @LiviaMedeiros @cjihrig I’ve put together a draft PR that adds statement-level support for reading SQL NULL values as undefined.

    If you’re happy with the approach, I’m happy to continue and add the relevant tests.

  13. geeksilva97 commented on Jan 23, 2026

    @geeksilva97
    Contributor

    Is there any use case for this? In which situation does an undefined behave in a way that, with null, it would be a problem?

    I was looking into other players, and it's just a null.

    Bun has the same behavior: https://bun.com/docs/runtime/sqlite

  14. mike-git374 commented on Feb 14, 2026

    @mike-git374
    Contributor

    Note that after db.prepare(sql, options) was added these options like readNullAsUndefined should be added to db.prepare(sql, options) and new DatabaseSync(path, options) as the statement.set* functions may be deprecated.

    I see 2 PR's open for this issue but neither have correctly added the options: #61472 #59462

  15. github-actions commented on Jul 20, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  16. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.sqliteIssues and PRs related to the SQLite subsystem.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions