|
| 1 | +import { ClickhouseClient } from "@effect/sql-clickhouse"; |
| 2 | +import { Config, Effect, Layer } from "effect"; |
| 3 | +import { DatabaseHelper } from "../common/service.js"; |
| 4 | +import { |
| 5 | + chDropTableIfExists, |
| 6 | + chCreateTableIfNotExists, |
| 7 | + chInsertIntoTable, |
| 8 | + chSelectAllFromTable, |
| 9 | + chColumnDDL, |
| 10 | +} from "./helpers.js"; |
| 11 | +import { valueToClickhouseType, clickhouseIdColumn } from "./types.js"; |
| 12 | + |
| 13 | +/** |
| 14 | + * @internal |
| 15 | + */ |
| 16 | +export const ClickhouseDatabaseConfig = Config.all({ |
| 17 | + databaseUrl: Config.orElse(Config.string("CLICKHOUSE_DATABASE_URL"), () => |
| 18 | + Config.map( |
| 19 | + Config.all({ |
| 20 | + CLICKHOUSE_HOST: Config.string("CLICKHOUSE_HOST"), |
| 21 | + CLICKHOUSE_PORT: Config.integer("CLICKHOUSE_PORT"), |
| 22 | + }), |
| 23 | + ({ CLICKHOUSE_HOST, CLICKHOUSE_PORT }) => |
| 24 | + `http://${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT}/`, |
| 25 | + ), |
| 26 | + ), |
| 27 | + username: Config.string("CLICKHOUSE_USER").pipe( |
| 28 | + Config.orElse(() => Config.succeed(undefined)), |
| 29 | + ), |
| 30 | + password: Config.string("CLICKHOUSE_PASSWORD").pipe( |
| 31 | + Config.orElse(() => Config.succeed(undefined)), |
| 32 | + ), |
| 33 | + database: Config.string("CLICKHOUSE_DATABASE").pipe( |
| 34 | + Config.orElse(() => Config.succeed(undefined)), |
| 35 | + ), |
| 36 | +}); |
| 37 | + |
| 38 | +/** |
| 39 | + * @internal |
| 40 | + */ |
| 41 | +const ClickhouseLive = ClickhouseClient.layer({ |
| 42 | + url: Config.map(ClickhouseDatabaseConfig, (c) => c.databaseUrl), |
| 43 | + username: Config.map(ClickhouseDatabaseConfig, (c) => c.username), |
| 44 | + password: Config.map(ClickhouseDatabaseConfig, (c) => c.password), |
| 45 | + database: Config.map(ClickhouseDatabaseConfig, (c) => c.database), |
| 46 | + clickhouse_settings: { |
| 47 | + allow_experimental_json_type: Config.succeed(true), |
| 48 | + }, |
| 49 | +}); |
| 50 | + |
| 51 | +/** |
| 52 | + * @internal |
| 53 | + */ |
| 54 | +export class ClickhouseService extends Effect.Service<ClickhouseService>()( |
| 55 | + "@typefusion/clickhouse", |
| 56 | + { |
| 57 | + effect: ClickhouseClient.ClickhouseClient, |
| 58 | + dependencies: [ClickhouseLive], |
| 59 | + }, |
| 60 | +) {} |
| 61 | + |
| 62 | +/** |
| 63 | + * @internal |
| 64 | + */ |
| 65 | +export const ClickhouseDatabaseHelperLive = Layer.succeed(DatabaseHelper, { |
| 66 | + valueToDbType: valueToClickhouseType, |
| 67 | + idColumn: clickhouseIdColumn, |
| 68 | + dropTableIfExists: chDropTableIfExists, |
| 69 | + createTableIfNotExists: chCreateTableIfNotExists, |
| 70 | + insertIntoTable: chInsertIntoTable, |
| 71 | + selectAllFromTable: chSelectAllFromTable, |
| 72 | + columnDDL: chColumnDDL, |
| 73 | +}); |
| 74 | + |
| 75 | +/** |
| 76 | + * @internal |
| 77 | + */ |
| 78 | +export class ClickhouseDatabaseHelperService extends Effect.Service<ClickhouseDatabaseHelperService>()( |
| 79 | + "@typefusion/clickhouse/databasehelper", |
| 80 | + { |
| 81 | + effect: DatabaseHelper, |
| 82 | + dependencies: [ClickhouseDatabaseHelperLive], |
| 83 | + }, |
| 84 | +) {} |
| 85 | + |
| 86 | +/** |
| 87 | + * @internal |
| 88 | + */ |
| 89 | +export const ClickhouseFinalLive = Layer.mergeAll( |
| 90 | + ClickhouseService.Default, |
| 91 | + ClickhouseDatabaseHelperService.Default, |
| 92 | +); |
0 commit comments