DEV Community

子慕大诗人
子慕大诗人

Posted on

Node ClickHouse ORM 3.x published

Clickhouse ORM for Nodejs. Send query over HTTP interface. Using TimonKK/clickhouse.

Install:

npm i clickhouse-orm
Enter fullscreen mode Exit fullscreen mode

Usage

Create instance:

const { ClickhouseOrm } = require("clickhouse-orm");

const chOrm = ClickhouseOrm({
  db: {
    name: "orm_test",
  },
  debug: true,
  client: {
    url: "localhost",
    port: "8123",
    basicAuth: {
      username: "default",
      password: "",
    },
    debug: false,
    isUseGzip: true,
    format: "json", // "json" || "csv" || "tsv"
  },
});
Enter fullscreen mode Exit fullscreen mode

Define Model:

import { DATA_TYPE, ModelSyncTableConfig } from 'clickhouse-orm';
const oldSchema: ModelSyncTableConfig = {
  tableName: "xxx",
  schema: {
    time: { type: DATA_TYPE.DateTime, default: Date },
    will_typeChanged: { type: DATA_TYPE.Int16 },
    will_deleted: { type: DATA_TYPE.String },
  },
  options: `ENGINE = MergeTree
  PARTITION BY toYYYYMM(time)
  ORDER BY time`,
  autoCreate: true,
  autoSync: true,
};
Enter fullscreen mode Exit fullscreen mode

Create data / Find:

// create database 'orm_test'
await chOrm.createDatabase();
// register schema and create [if] table
const Table1Model = await chOrm.model(table1Schema);

// create data
const resCreate = await Table1Model.create({
  status: 1,
  time: new Date(),
  browser: "chrome",
  browser_v: "90.0.1.21",
});
console.log("create:", resCreate);

// find
Table1Model.find({
  select: "*",
  limit: 3,
}).then((res) => {
  // SQL: SELECT * from orm_test.table1 LIMIT 3
  console.log("find:", res);
});
Enter fullscreen mode Exit fullscreen mode

More in Basic Example.

I look forward to your attention and discussion!
node-clickhouse-orm

Top comments (0)