sql-js / sql-js/sql.js

Feature Request: Handling JS Date variables

Open
#334 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
13.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

First of all, thanks for creating/maintaining this amazing library. I'll cut into my request.

Background

I am using sql.js with typeorm to do integration testing rather than spinning up a mysql service for that.

Issue

I noted that if I use Javascript Date objects in the where conditions, it works in production, but fails in tests, because sql.js doesn't convert the dates similar to how node-mysql driver does that.

Expectation

I would love it if sql.js would convert the dates correctly.
For example, take the following example:

const updatedAt = new Date(/*Some date string goes here*/);

// Assume db is setup correctly as explained in ReadMe
db.run("SELECT * FROM test_table WHERE example_column = ?", [updatedAt]);

Now, this fails because sql.js would call updatedAt.toString(). But AFAIK, in node-mysql they call updatedAt.toISOString() (or some valid date string) internally.

Notes
  1. Code works with sql.js when we do updatedAt.toISOString() explicitly
  2. The error that's being thrown at the moment is:
QueryFailedError: Wrong API use : tried to bind a value of an unknown type (Fri Jan 11 2019 01:01:01 GMT+1100 (Australian Eastern Daylight Time)).

    at new QueryFailedError (/Users/pubudu/Projects/typeorm-test/src/error/QueryFailedError.ts:9:9)
    at SqljsQueryRunner.<anonymous> (/Users/pubudu/Projects/typeorm-test/src/driver/sqljs/SqljsQueryRunner.ts:79:22)

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 by reproducing the db.run example with a JavaScript Date value and inspect the parameter-binding path that produces the reported unknown-type error. The issue mentions SqljsQueryRunner.ts:79:22 in the stack trace; done means Date parameters are accepted with the expected SQL date representation and the existing binding behavior remains covered by a test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.