npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

clickhouse-schema

v2.0.1

Published

Clickhouse-Schema is a TypeScript library that simplifies working with ClickHouse databases by allowing developers to effortlessly define table schemas in TypeScript. This approach not only ensures robust type safety but also significantly reduces develop

Downloads

87

Readme

ClickHouse-Schema Guide

In ClickHouse, defining and managing table schemas and their associated types can be done either manually or through the ClickHouse-Schema library. This guide compares these two approaches to illustrate the simplicity and efficiency ClickHouse-Schema brings to your projects.

Traditional Manual Query Approach

Traditionally, creating a table in clickhouse requires manually writing the SQL query and the interface in your code. This method is straightforward but prone to errors and inconsistencies, especially when schema changes occur.

Create Table Query

CREATE TABLE IF NOT EXISTS students
(
    id UInt32,
    name String,
    height float32,
    age UInt8,
    weight Float64,,
    isStudent Boolean
)
# Manually defined
interface StudentsTableTypeManuallyDefined {
  id: number,
  name: string,
  age: number,
  height: number,
  weight: number,
  isStudent: boolean
}

Did you notice any errors with the code below? These would not get caught till runtime

Using ClickHouse-Schema

ClickHouse-Schema automates schema creation and ensures type safety with minimal code, providing a more robust and maintainable solution.

Defining Schema

const studentsTableSchema = new ClickhouseSchema({
  id: { type: CHUInt32() },
  name: { type: CHString() },
  age: { type: CHUInt8() },
  height: { type: CHFloat32() },
  weight: { type: CHFloat64() },
  isStudent: { type: CHBoolean() }
}, {
  table_name: 'students',
  primary_key: 'id'
})

//Automatic type inference. If schema changes type automatically changes too
type StudentsTableType = InferClickhouseSchemaType<typeof studentsTableSchema>

Getting Started

To start using ClickHouse-Schema in your projects, follow these steps:

  1. Installation To install ClickHouse-Schema, run the following command in your terminal:

    npm install clickhouse-schema
  2. Create a Schema

    Define your table schema and provide options such as the table name and primary key. This will enable automatic type inference, making your code more robust and maintainable.

    
    import {
      ClickhouseSchema, CHUInt32, CHString, CHUInt8, CHFloat32, CHFloat64, CHBoolean, InferClickhouseSchemaType
    } from 'clickhouse-schema'
    
    // Use types directly or import ClickhouseTypes object to get all the types in one place
    const studentsTableSchema = new ClickhouseSchema({
      id: { type: CHUInt32() },
      name: { type: CHString() },
      age: { type: CHUInt8() },
      height: { type: CHFloat32() },
      weight: { type: CHFloat64() },
      isStudent: { type: CHBoolean() }
    }, {
      table_name: 'students',
      primary_key: 'id'
    })
    type MyTableType = InferClickhouseSchemaType<typeof studentsTableSchema>
  3. Utilize Schema Methods ClickHouse-Schema provides several methods to streamline working with your database schema:

    • Use <your_schema>.GetCreateTableQuery() or <your_schema>.toString() to generate the SQL CREATE TABLE query.
    • Use <your_schema>.GetOptions() to access the options passed when creating the table schema.
    • Use <your_schema>.GetCreateTableQueryAsList() to get the CREATE TABLE query as a list of strings, which can be helpful for debugging or logging.

Supported Types

  • Integer (signed and unsigned integers): UInt8, UInt16, UInt32, UInt64, UInt128, UInt256, Int8, Int16, Int32, Int64, Int128, Int256 types
  • Floating-point numbers: Float32 and Float64 types
  • Decimal - Decimal type
  • Boolean: Boolean type
  • Strings: String and FixedString types
  • Dates: Date, Date32, DateTime and DateTime64 types
  • JSON: JSON type
  • UUID: UUID type
  • Arrays: Array type
  • Nullable: Nullable type
  • LowCardinality: LowCardinality and Enum types
  • IP Addresses - IPv4 and IPv6

And support for more types is coming!