Skip to content

How to insert type 'Date'? #46

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

Open
Sir-Will opened this issue Aug 20, 2019 · 2 comments
Open

How to insert type 'Date'? #46

Sir-Will opened this issue Aug 20, 2019 · 2 comments
Assignees
Labels

Comments

@Sir-Will
Copy link

hey,

I'm trying to insert the clickhouse type Date from js Date().
I tried new Date().toISOString().substr(0,10) which worked fine but reading it causes it to return a String.

What's the proper way of doing this?

@nezed
Copy link
Collaborator

nezed commented Oct 4, 2019

For INSERTs done with JSONEachRow and writing JS objects to stream (like here) you can pass Date instance as a column value.

For now with SELECTs you will always get Date and DateTime values as strings, and forced to parse them by yourself.

@nezed nezed self-assigned this Oct 4, 2019
@nezed nezed added the question label Oct 4, 2019
@suenot
Copy link

suenot commented Jan 10, 2020

You can use moment library:

const ClickHouse = require('@apla/clickhouse')
const moment = require('moment')
const options = <OPTIONS>
const ch = new ClickHouse(options)

const writableStream = ch.query('INSERT INTO testdb.testtable', { format: 'JSONEachRow' }, (err) => {
    console.log(err)
    console.log('Clickhouse insert!')
})
writableStream.write({
    'name': 'Foo',
    'timestamp': new Date(),
    'date': moment.utc(new Date()).format('YYYY-MM-DD'),
    'datetime': moment.utc(new Date()).format('YYYY-MM-DD HH:mm:ss'),
})
writableStream.write({
    'name': 'Bar',
    'timestamp': new Date(),
    'date': moment.utc(new Date()).format('YYYY-MM-DD'),
    'datetime': moment.utc(new Date()).format('YYYY-MM-DD HH:mm:ss'),
})
writableStream.end()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants