### ClickHouse Cloud Migration Table Creation Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Example SQL for creating migration tables in ClickHouse Cloud using ReplicatedMergeTree. ```sql CREATE TABLE ON CLUSTER cluster_1S_2R ( ... ) ENGINE = ReplicatedMergeTree ``` -------------------------------- ### ClickHouse Seeder Implementation Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Example of a ClickHouse seeder class implementing the `run` method to insert data into the 'events' table. ```typescript import { BaseSeeder } from 'adonisjs-clickhouse/seeders' export default class extends BaseSeeder { static environment = ['development', 'testing'] async run() { // Write your queries inside the run method await this.client.insert({ table: 'events', values: [{ name: 'click', time: new Date().getTime() }], columns: ['name', 'time'], format: 'JSONEachRow', }) } } ``` -------------------------------- ### Self-Hosted Migration Table Creation with ZooKeeper Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Example SQL for creating migration tables in a self-hosted ClickHouse cluster using ReplicatedMergeTree with explicit ZooKeeper paths. ```sql CREATE TABLE ON CLUSTER cluster_1S_2R ( ... ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/{database}/{table}', '{replica}') ``` -------------------------------- ### Configure adonisjs-clickhouse Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Run this command after installation to configure the package. It automatically creates configuration files, registers necessary components, and sets up environment variables. ```bash node ace configure adonisjs-clickhouse ``` -------------------------------- ### Install adonisjs-clickhouse Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Install the package using npm. This is the first step to integrate ClickHouse into your AdonisJS application. ```bash npm install adonisjs-clickhouse ``` -------------------------------- ### Migrate ClickHouse Database in Test Setup Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Run ClickHouse database migrations before each test run cycle. This hook should be added to your `tests/bootstrap.ts` file. ```typescript import testUtils from '@adonisjs/core/services/test_utils' export const runnerHooks: Required> = { setup: [() => testUtils.clickhouse().migrate()], teardown: [], } ``` -------------------------------- ### Basic Query Execution Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Demonstrates how to execute a raw SQL query using the default ClickHouse connection. ```APIDOC ## query ### Description Executes a raw SQL query against the ClickHouse database. ### Method `clickhouse.query(options)` ### Parameters #### Request Body - **query** (string) - Required - The SQL query to execute. - **format** (string) - Optional - The desired output format (e.g., 'JSONEachRow'). ### Request Example ```json { "query": "SELECT * FROM my_table" } ``` ### Response #### Success Response (200) - **response** (object) - The result of the query. ### Response Example ```json { "response": "[query result]" } ``` ``` -------------------------------- ### Execute Command Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Explains how to execute a command in ClickHouse. ```APIDOC ## command ### Description Executes a command in ClickHouse. ### Method `clickhouse.command(options)` ### Parameters #### Request Body - **command** (string) - Required - The command to execute. ### Request Example ```json { "command": "OPTIMIZE TABLE my_table" } ``` ### Response #### Success Response (200) - **response** (object) - The result of the command execution. ### Response Example ```json { "response": "[command result]" } ``` ``` -------------------------------- ### Run ClickHouse migrations Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Execute all pending ClickHouse migrations using this command. This applies the schema changes defined in the migration files. ```bash node ace clickhouse:migration:run ``` -------------------------------- ### Define migration schema with up and down methods Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Implement the `up` and `down` methods within the generated migration file to define table creation and deletion logic. Uses the `client.command` method for executing ClickHouse queries. ```typescript import { BaseSchema } from 'adonisjs-clickhouse/schema' export default class extends BaseSchema { async up() { await this.client.command({ query: ` CREATE TABLE events (name LowCardinality(String), time DateTime) ENGINE = MergeTree ORDER BY (name, time); `, }) } async down() { await this.client.command({ query: 'DROP TABLE events;' }) } } ``` -------------------------------- ### Manage ClickHouse migrations with additional commands Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Provides commands for dry runs, rolling back migrations (last or specific batch), and specifying a connection for migration operations. ```bash # Dry run mode node ace clickhouse:migration:run --dry-run ``` ```bash # Rollback the last migration node ace clickhouse:migration:rollback ``` ```bash # Rollback using a specific batch node ace clickhouse:migration:rollback --batch 1 ``` ```bash # Specify a connection node ace clickhouse:migration:run --connection secondary ``` -------------------------------- ### Configure Self-Hosted ClickHouse Migrations with Defaults Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Alternatively, set `migrations.replicatedMergeTree` to true to use default replication paths and names from the ClickHouse server configuration. ```typescript { clusterName: 'cluster_1S_2R', migrations: { replicatedMergeTree: true } } ``` -------------------------------- ### Create New ClickHouse Seeder Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Command to generate a new seeder file for the ClickHouse database. ```bash node ace make:clickhouse:seeder events ``` -------------------------------- ### Create a new ClickHouse migration Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Use this command to generate a new migration file for ClickHouse. The file will be placed in the `clickhouse/migrations` directory. ```bash node ace make:clickhouse:migration create_events_table ``` -------------------------------- ### Run ClickHouse Seeders Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Commands to execute ClickHouse seeders, including running all seeders, a specific seeder, in interactive mode, or with a specific connection. ```bash # Run all seeders node ace db:seed # Run a specific seeder node ace db:seed --files "./clickhouse/seeders/events.ts" # Run in interactive mode node ace db:seed -i # Specify a connection node ace db:seed --connection secondary ``` -------------------------------- ### Query Execution with Specific Connection Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Shows how to execute a query using a named ClickHouse connection. ```APIDOC ## query (specific connection) ### Description Executes a raw SQL query against a specific ClickHouse connection. ### Method `clickhouse.connection(connectionName).query(options)` ### Parameters #### Path Parameters - **connectionName** (string) - Required - The name of the ClickHouse connection to use. #### Request Body - **query** (string) - Required - The SQL query to execute. - **format** (string) - Optional - The desired output format (e.g., 'JSONEachRow'). ### Request Example ```json { "query": "SELECT * FROM my_table" } ``` ### Response #### Success Response (200) - **response** (object) - The result of the query. ### Response Example ```json { "response": "[query result]" } ``` ``` -------------------------------- ### Configure Self-Hosted ClickHouse Migrations with ZooKeeper Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md For self-hosted clusters, configure `clusterName` and `migrations.replicatedMergeTree` with `zooKeeperPath` and `replicaName` for explicit replication settings. ```typescript { clusterName: 'cluster_1S_2R', migrations: { replicatedMergeTree: { zooKeeperPath: '/clickhouse/tables/{shard}/{database}/{table}', replicaName: '{replica}', }, }, } ``` -------------------------------- ### Configure ClickHouse Cloud Migrations Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md For ClickHouse Cloud, set `clusterName` and `migrations.replicatedMergeTree` to true. Replication is managed by ClickHouse Cloud. ```typescript { clusterName: 'cluster_1S_2R', migrations: { replicatedMergeTree: true, }, } ``` -------------------------------- ### ClickHouse Seeder Configuration Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Configuration for ClickHouse seeders, including paths to seeder files and natural sorting option. ```typescript { seeders: { /** * Path to the seeders directory * @default ['clickhouse/seeders'] */ paths?: string[] /** * Use natural sort for seeder files */ naturalSort?: boolean } } ``` -------------------------------- ### ClickHouse Configuration Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Define ClickHouse connection settings in `config/clickhouse.ts`. Supports multiple connections and customizes client options, migrations, and seeders. ```typescript import env from '#start/env' import { defineConfig } from 'adonisjs-clickhouse' const clickhouseConfig = defineConfig({ connection: 'primary', connections: { primary: { /** * ClickHouse JS client configuration */ application: 'AdonisJS', url: env.get('CLICKHOUSE_URL'), username: env.get('CLICKHOUSE_USER'), password: env.get('CLICKHOUSE_PASSWORD', ''), database: env.get('CLICKHOUSE_DB'), clickhouse_settings: {}, compression: { request: false, response: true }, request_timeout: 30e3, /** * Cluster name for the package commands when using a ClickHouse cluster. */ clusterName: env.get('CLICKHOUSE_CLUSTER_NAME'), /** * Debug mode for the connection */ debug: false, /** * Migrations configuration */ migrations: { paths: ['clickhouse/migrations'], disableRollbacksInProduction: true, naturalSort: true, /** * ReplicatedMergeTree options for the migration tables when using a ClickHouse cluster. */ replicatedMergeTree: { zooKeeperPath: '/clickhouse/tables/{shard}/{database}/{table}', replicaName: '{replica}', }, }, /** * Seeders configuration */ seeders: { paths: ['clickhouse/seeders'], }, }, }, }) export default clickhouseConfig ``` -------------------------------- ### Query with JSONEachRow Format Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Demonstrates using the `query` method with the `JSONEachRow` format and parsing the result. ```APIDOC ## query with JSONEachRow ### Description Executes a query and specifies the output format as `JSONEachRow`, then parses the response as JSON. ### Method `clickhouse.query({ query: '...', format: 'JSONEachRow' }).json()` ### Parameters #### Request Body - **query** (string) - Required - The SQL query to execute. - **format** (string) - Required - Must be set to 'JSONEachRow'. ### Request Example ```json { "query": "SELECT id, name FROM my_table", "format": "JSONEachRow" } ``` ### Response #### Success Response (200) - **rows** (array) - An array of objects representing the query results, typed by ``. ### Response Example ```json [ { "id": 1, "name": "example1" }, { "id": 2, "name": "example2" } ] ``` ``` -------------------------------- ### ClickHouse Server Default Replication Configuration Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md XML configuration snippet showing default replication path and replica name settings for ClickHouse servers. ```xml /clickhouse/tables/{shard}/{database}/{table} {replica} ``` -------------------------------- ### ClickHouse Migration Configuration Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Configure ClickHouse migrations by defining paths, table names, and other options in the `config/clickhouse.ts` file. Note that migrations are not run within a transaction. ```typescript { migrations: { /** * Path to the migrations directory * @default ['clickhouse/migrations'] */ paths?: string[] /** * Name of the table to store the migrations * @default 'adonis_schema' */ tableName?: string /** * Prevent rollbacks in production * @default false */ disableRollbacksInProduction?: boolean /** * Use natural sort for migration files */ naturalSort?: boolean /** * ReplicatedMergeTree options for the migration tables. */ replicatedMergeTree?: { zooKeeperPath: string; replicaName: string } | true } } ``` -------------------------------- ### Define migration schema for ClickHouse clusters Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md When using a ClickHouse cluster, include the `ON CLUSTER` clause and use a `Replicated` engine for table creation to ensure replication across nodes. The `down` method also specifies the cluster for dropping the table. ```typescript import { BaseSchema } from 'adonisjs-clickhouse/schema' export default class extends BaseSchema { async up() { await this.client.command({ query: ` CREATE TABLE events ON CLUSTER cluster_1S_2R (name LowCardinality(String), time DateTime) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/{database}/{table}', '{replica}') ORDER BY (name, time); `, }) } async down() { await this.client.command({ query: 'DROP TABLE events ON CLUSTER cluster_1S_2R;' }) } } ``` -------------------------------- ### Shorthand toJSONEachRow Method Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Illustrates the shorthand `toJSONEachRow` method for querying with `JSONEachRow` format. ```APIDOC ## toJSONEachRow shorthand ### Description A shorthand method to execute a query with `JSONEachRow` format and parse the response as JSON. ### Method `clickhouse.query({ query: '...' }).toJSONEachRow()` ### Parameters #### Request Body - **query** (string) - Required - The SQL query to execute. ### Request Example ```json { "query": "SELECT id, name FROM my_table" } ``` ### Response #### Success Response (200) - **rows** (array) - An array of objects representing the query results, typed by ``. ### Response Example ```json [ { "id": 1, "name": "example1" }, { "id": 2, "name": "example2" } ] ``` ``` -------------------------------- ### Querying ClickHouse with AdonisJS Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Import and use the ClickHouse service to execute queries. You can specify a connection name to use a specific configuration. ```typescript import clickhouse from 'adonisjs-clickhouse/services/main' const response = await clickhouse.query({ query: 'SELECT * FROM my_table' }) // Using a specific connection: const response = await clickhouse.connection('secondary').query({ query: 'SELECT * FROM my_table' }) ``` -------------------------------- ### Execute Statement Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Describes the method for executing a statement in ClickHouse. ```APIDOC ## exec ### Description Executes a statement in ClickHouse. ### Method `clickhouse.exec(options)` ### Parameters #### Request Body - **query** (string) - Required - The statement to execute. ### Request Example ```json { "query": "CREATE TABLE IF NOT EXISTS my_table (id UInt32, name String)" } ``` ### Response #### Success Response (200) - **response** (object) - The result of the statement execution. ### Response Example ```json { "response": "[exec result]" } ``` ``` -------------------------------- ### Ping Connection Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Details the method for checking the connection status to ClickHouse. ```APIDOC ## ping ### Description Pings the ClickHouse server to check the connection status. ### Method `clickhouse.ping()` ### Response #### Success Response (200) - **response** (object) - Indicates if the ping was successful. ### Response Example ```json { "response": "pong" } ``` ``` -------------------------------- ### Insert Data Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Details the method for inserting data into ClickHouse. ```APIDOC ## insert ### Description Inserts data into a ClickHouse table. ### Method `clickhouse.insert(options)` ### Parameters #### Request Body - **query** (string) - Required - The SQL INSERT statement. - **values** (array) - Required - An array of values to insert. - **format** (string) - Optional - The desired output format. ### Request Example ```json { "query": "INSERT INTO my_table (id, name) VALUES (?, ?)", "values": [1, 'example'] } ``` ### Response #### Success Response (200) - **response** (object) - The result of the insert operation. ### Response Example ```json { "response": "[insert result]" } ``` ``` -------------------------------- ### Using JSONEachRow Format with ClickHouse Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Execute queries with the 'JSONEachRow' format for easier JSON parsing. The `toJSONEachRow` method provides a convenient shorthand. ```typescript // Using the query method with JSONEachRow format const result = await clickhouse.query({ query: 'SELECT * FROM my_table', format: 'JSONEachRow' }) const rows = result.json<{ id: number }>() // Using the shorthand method const rows = await clickhouse .query({ query: 'SELECT * FROM my_table' }) .toJSONEachRow<{ id: number }>() ``` -------------------------------- ### Truncate ClickHouse Database Tables Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Truncates all tables in the ClickHouse database except for migration tables. Ensure the `clusterName` is configured if using a cluster. ```bash node ace clickhouse:db:truncate ``` -------------------------------- ### Wipe ClickHouse Database Tables Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Drops all tables in the ClickHouse database, including migration tables. Ensure the `clusterName` is configured if using a cluster. ```bash node ace clickhouse:db:wipe ``` -------------------------------- ### Truncate ClickHouse Tables After Each Test Source: https://github.com/flozdra/adonisjs-clickhouse/blob/main/README.md Truncate all tables in the ClickHouse database after each test. This is useful for ensuring a clean state for subsequent tests. ```typescript import { test } from '@japa/runner' import testUtils from '@adonisjs/core/services/test_utils' test.group('Events', (group) => { group.each.setup(() => testUtils.clickhouse().truncate()) }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.