node-clickhouse icon indicating copy to clipboard operation
node-clickhouse copied to clipboard

How to insert type 'Date'?

Open Sir-Will opened this issue 6 years ago • 2 comments

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?

Sir-Will avatar Aug 20 '19 21:08 Sir-Will

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 avatar Oct 04 '19 20:10 nezed

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()

suenot avatar Jan 10 '20 21:01 suenot