88 lines
3.2 KiB
JavaScript
88 lines
3.2 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.initClickHouse = void 0;
|
|
const client_1 = require("@clickhouse/client");
|
|
const config_1 = __importDefault(require("../config"));
|
|
// Create ClickHouse client with error handling
|
|
const createClickHouseClient = () => {
|
|
try {
|
|
return (0, client_1.createClient)({
|
|
host: `http://${config_1.default.clickhouse.host}:${config_1.default.clickhouse.port}`,
|
|
username: config_1.default.clickhouse.user,
|
|
password: config_1.default.clickhouse.password,
|
|
database: config_1.default.clickhouse.database,
|
|
});
|
|
}
|
|
catch (error) {
|
|
console.error('Error creating ClickHouse client:', error);
|
|
// Return a mock client for development that logs operations instead of executing them
|
|
return {
|
|
query: async ({ query, values }) => {
|
|
console.log('ClickHouse query (mock):', query, values);
|
|
return { rows: [] };
|
|
},
|
|
close: async () => {
|
|
console.log('ClickHouse connection closed (mock)');
|
|
}
|
|
};
|
|
}
|
|
};
|
|
const clickhouse = createClickHouseClient();
|
|
// Initialize ClickHouse database and tables
|
|
const initClickHouse = async () => {
|
|
try {
|
|
// Create database if not exists
|
|
await clickhouse.query({
|
|
query: `CREATE DATABASE IF NOT EXISTS ${config_1.default.clickhouse.database}`,
|
|
});
|
|
// Create tables for tracking events
|
|
await clickhouse.query({
|
|
query: `
|
|
CREATE TABLE IF NOT EXISTS ${config_1.default.clickhouse.database}.view_events (
|
|
user_id String,
|
|
content_id String,
|
|
timestamp DateTime DEFAULT now(),
|
|
ip String,
|
|
user_agent String
|
|
) ENGINE = MergeTree()
|
|
PARTITION BY toYYYYMM(timestamp)
|
|
ORDER BY (user_id, content_id, timestamp)
|
|
`,
|
|
});
|
|
await clickhouse.query({
|
|
query: `
|
|
CREATE TABLE IF NOT EXISTS ${config_1.default.clickhouse.database}.like_events (
|
|
user_id String,
|
|
content_id String,
|
|
timestamp DateTime DEFAULT now(),
|
|
action Enum('like' = 1, 'unlike' = 2)
|
|
) ENGINE = MergeTree()
|
|
PARTITION BY toYYYYMM(timestamp)
|
|
ORDER BY (user_id, content_id, timestamp)
|
|
`,
|
|
});
|
|
await clickhouse.query({
|
|
query: `
|
|
CREATE TABLE IF NOT EXISTS ${config_1.default.clickhouse.database}.follower_events (
|
|
follower_id String,
|
|
followed_id String,
|
|
timestamp DateTime DEFAULT now(),
|
|
action Enum('follow' = 1, 'unfollow' = 2)
|
|
) ENGINE = MergeTree()
|
|
PARTITION BY toYYYYMM(timestamp)
|
|
ORDER BY (follower_id, followed_id, timestamp)
|
|
`,
|
|
});
|
|
console.log('ClickHouse database and tables initialized');
|
|
}
|
|
catch (error) {
|
|
console.error('Error initializing ClickHouse:', error);
|
|
console.log('Continuing with limited functionality...');
|
|
}
|
|
};
|
|
exports.initClickHouse = initClickHouse;
|
|
exports.default = clickhouse;
|