Coder Social home page Coder Social logo

handling Date value about json-sql HOT 7 OPEN

2do2go avatar 2do2go commented on June 15, 2024
handling Date value

from json-sql.

Comments (7)

artzhookov avatar artzhookov commented on June 15, 2024

Yeap, it looks like a bug. But what behaviour will be correct? What should be returned from Date instance: .valueOf() or .toISOString()?

As workaround you could call specific method in your code:

var date = new Date();
var result = jsonSql.build({
  table: 'users',
  condition: {
    insertdate: date.valueOf()
  }
});

from json-sql.

DmitryEfimenko avatar DmitryEfimenko commented on June 15, 2024

I think it should be neither of these. Database clients are written to handle Date instances. Here is an example for Postgres showing a new Date() being used as a value for an insert statement.

Handling javascript Date is hairy and people who wrote database clients spent a lot of time making sure they'll be handled correctly. So json-sql should leave variable as-is and let these clients handle it.

The result of jsonSql.build command discussed above should be:

// result.query: select * from "users" where "insertdate" = $1;
// result.values: [ 2017-01-16T16:48:28.494Z ]

The only situation when json-sql should convert value is when separatedValues is set to false. I would not worry about this use-case too much since it opens SQL injection attacks possibility anyway.

However, in this case, the Date needs to be converted to a string in a format recognizable by the database. Here is a case showing that use of .toISOString() in mysql would result in an error. Below are the formats (used by moment.js) recognizable by databases:

database format
mysql YYYY-MM-DD HH:mm:ss.S
mssql YYYY-MM-DD HH:mm:ss.S
postgresql YYYY-MM-DD HH:mm:ss.SS
sqlite YYYY-MM-DD HH:mm:ss.S

from json-sql.

okv avatar okv commented on June 15, 2024

@DmitryEfimenko solution looks good to me, @artzhookov ?

from json-sql.

DmitryEfimenko avatar DmitryEfimenko commented on June 15, 2024

@artzhookov if you point me in the right direction (where the changes need to happen), I can try submit a PR

from json-sql.

artzhookov avatar artzhookov commented on June 15, 2024

Before I'll able to give you an advice I need to look at the code. I think I'll have time today to find right place at the code and I'll answer after.

from json-sql.

artzhookov avatar artzhookov commented on June 15, 2024

Please, try [email protected], it should process Date values properly now

from json-sql.

DmitryEfimenko avatar DmitryEfimenko commented on June 15, 2024

Awesome work!
MySql works as expected, however, when using PostgreSql dialect the date value inside values object ends up as a string - not an instance of Date:

result.values: [ 2017-01-16T16:48:28.494Z ] // should be
result.values: [ "2017-01-16T16:48:28.494Z" ] // actual

I think it has to do with this line
Because of that, when querying value back, it looses timezone info.

from json-sql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.