1.0.0 • Published 9 months ago

@oneralon/nestjs-clickhouse v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

NestJS ClickHouse NPM package

The package based on https://github.com/zimv/node-clickhouse-orm

1. Installation

npm i --save @oneralon/nestjs-clickhouse

2. Add root module

import { ClickHouseModule } from '@oneralon/nestjs-clickhouse';

@Module({
  imports: [
    ClickHouseModule.forRoot({
      url: 'http://clickhouse.host',
      port: 8123,
      username: 'default',
      password: 'password',
      database: 'default',
      debug: true,
      useGzip: true,
      format: 'json', // 'json' | 'csv' | 'tsv'
    }),
    ...
  ]
})
...

3. Decorate model

import { DataType, Model, Prop, Schema } from '@oneralon/nestjs-clickhouse';

@Schema({
  tableName: 'entities',
  options: 'ENGINE = MergeTree ORDER BY field1',
})
export class Entity {
  @Prop({
    type: DataType.String,
  })
  public field1: string;

  @Prop({
    type: DataType.Int64,
  })
  public field2: number;

  @Prop({
    type: DataType.String,
    get: (value: string) => JSON.parse(value),
    set: (value: object) => JSON.stringify(value),
  })
  public field3: { a: number, b: string };
}

export type EntityModel = Model<Entity>;

4. Add feature module

import { ClickHouseModule } from '@oneralon/nestjs-clickhouse';

@Module({
  imports: [
    ClickHouseModule.forFeature([
      Entity,
    ]),
    ...
  ]
})
...

5. Inject and use model

class EntitiesService {
  @Inject(Entity.name)
  private readonly model: EntityModel;

  public async create(value: Entity) {
    await this.model.create(value);
  }

  ...
}