-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typefusion): clickhouse support
closed #29
- Loading branch information
Showing
17 changed files
with
1,112 additions
and
966 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"typefusion": patch | ||
--- | ||
|
||
Clickhouse support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/typefusion/example/clickhouse/typefusion_all_clickhouse_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { clickhouseType, TypefusionDbScript } from "../../src/index.js"; | ||
|
||
const allClickhouseTypes = { | ||
text: clickhouseType.string().notNull(), | ||
int: clickhouseType.int32().notNull(), | ||
boolean: clickhouseType.boolean().notNull(), | ||
date: clickhouseType.date().notNull(), | ||
dateTime: clickhouseType.dateTime64(3).notNull(), | ||
bigint: clickhouseType.int64().notNull(), | ||
smallint: clickhouseType.int16().notNull(), | ||
float: clickhouseType.float32().notNull(), | ||
double: clickhouseType.float64().notNull(), | ||
decimal: clickhouseType.decimal(10, 2).notNull(), | ||
char: clickhouseType.fixedString(10).notNull(), | ||
varchar: clickhouseType.string().notNull(), | ||
time: clickhouseType.string().notNull(), // Clickhouse doesn't have a specific time type | ||
json: clickhouseType.json().notNull(), | ||
binary: clickhouseType.string().notNull(), // Using string as a close approximation | ||
}; | ||
|
||
export default { | ||
name: "typefusion_all_clickhouse_types", | ||
schema: allClickhouseTypes, | ||
resultDatabase: "clickhouse", | ||
run: async () => { | ||
console.log("TYPEFUSION ALL CLICKHOUSE TYPES IS RUN"); | ||
return { | ||
data: [ | ||
{ | ||
text: "Sample text", | ||
int: 42, | ||
boolean: true, | ||
date: "2023-05-17", | ||
dateTime: "2023-05-17T12:34:56", | ||
bigint: BigInt("9007199254740991").toString(), | ||
smallint: 32767, | ||
float: 3.14, | ||
double: 3.141592653589793, | ||
decimal: 123.45, | ||
char: "Fixed ", | ||
varchar: "Variable length text", | ||
time: "12:34:56", | ||
// TODO this needs to be a json string | ||
json: { key: "value" }, | ||
binary: Buffer.from([0x12]).toString("base64"), // Convert to base64 string | ||
}, | ||
], | ||
}; | ||
}, | ||
} satisfies TypefusionDbScript<typeof allClickhouseTypes>; |
27 changes: 27 additions & 0 deletions
27
packages/typefusion/example/clickhouse/typefusion_clickhouse_result.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
typefusionRef, | ||
TypefusionDbScript, | ||
clickhouseType, | ||
} from "../../src/index.js"; | ||
import main from "../main.js"; | ||
|
||
const smallSchema = { | ||
small: clickhouseType.string().notNull(), | ||
}; | ||
|
||
export default { | ||
name: "typefusion_clickhouse_result", | ||
resultDatabase: "clickhouse", | ||
schema: smallSchema, | ||
run: async () => { | ||
const result = await typefusionRef(main); | ||
console.log("TYPEFUSION CLICKHOUSE RESULT IS RUN", result); | ||
return { | ||
data: [ | ||
{ | ||
small: "smallString" as const, | ||
}, | ||
], | ||
}; | ||
}, | ||
} satisfies TypefusionDbScript<typeof smallSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { ClickhouseClient } from "@effect/sql-clickhouse"; | ||
import { Config, Effect, Layer } from "effect"; | ||
import { DatabaseHelper } from "../common/service.js"; | ||
import { | ||
chDropTableIfExists, | ||
chCreateTableIfNotExists, | ||
chInsertIntoTable, | ||
chSelectAllFromTable, | ||
chColumnDDL, | ||
} from "./helpers.js"; | ||
import { valueToClickhouseType, clickhouseIdColumn } from "./types.js"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const ClickhouseDatabaseConfig = Config.all({ | ||
databaseUrl: Config.orElse(Config.string("CLICKHOUSE_DATABASE_URL"), () => | ||
Config.map( | ||
Config.all({ | ||
CLICKHOUSE_HOST: Config.string("CLICKHOUSE_HOST"), | ||
CLICKHOUSE_PORT: Config.integer("CLICKHOUSE_PORT"), | ||
}), | ||
({ CLICKHOUSE_HOST, CLICKHOUSE_PORT }) => | ||
`http://${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT}/`, | ||
), | ||
), | ||
username: Config.string("CLICKHOUSE_USER").pipe( | ||
Config.orElse(() => Config.succeed(undefined)), | ||
), | ||
password: Config.string("CLICKHOUSE_PASSWORD").pipe( | ||
Config.orElse(() => Config.succeed(undefined)), | ||
), | ||
database: Config.string("CLICKHOUSE_DATABASE").pipe( | ||
Config.orElse(() => Config.succeed(undefined)), | ||
), | ||
}); | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const ClickhouseLive = ClickhouseClient.layer({ | ||
url: Config.map(ClickhouseDatabaseConfig, (c) => c.databaseUrl), | ||
username: Config.map(ClickhouseDatabaseConfig, (c) => c.username), | ||
password: Config.map(ClickhouseDatabaseConfig, (c) => c.password), | ||
database: Config.map(ClickhouseDatabaseConfig, (c) => c.database), | ||
clickhouse_settings: { | ||
allow_experimental_json_type: Config.succeed(true), | ||
}, | ||
}); | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export class ClickhouseService extends Effect.Service<ClickhouseService>()( | ||
"@typefusion/clickhouse", | ||
{ | ||
effect: ClickhouseClient.ClickhouseClient, | ||
dependencies: [ClickhouseLive], | ||
}, | ||
) {} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const ClickhouseDatabaseHelperLive = Layer.succeed(DatabaseHelper, { | ||
valueToDbType: valueToClickhouseType, | ||
idColumn: clickhouseIdColumn, | ||
dropTableIfExists: chDropTableIfExists, | ||
createTableIfNotExists: chCreateTableIfNotExists, | ||
insertIntoTable: chInsertIntoTable, | ||
selectAllFromTable: chSelectAllFromTable, | ||
columnDDL: chColumnDDL, | ||
}); | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export class ClickhouseDatabaseHelperService extends Effect.Service<ClickhouseDatabaseHelperService>()( | ||
"@typefusion/clickhouse/databasehelper", | ||
{ | ||
effect: DatabaseHelper, | ||
dependencies: [ClickhouseDatabaseHelperLive], | ||
}, | ||
) {} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const ClickhouseFinalLive = Layer.mergeAll( | ||
ClickhouseService.Default, | ||
ClickhouseDatabaseHelperService.Default, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { ClickhouseClient } from "@effect/sql-clickhouse/ClickhouseClient"; | ||
import { SqlClient } from "@effect/sql/SqlClient"; | ||
import { Effect } from "effect"; | ||
/** | ||
* @internal | ||
*/ | ||
export const chDropTableIfExists = ( | ||
sql: SqlClient | ClickhouseClient, | ||
tableName: string, | ||
) => sql`DROP TABLE IF EXISTS ${sql.unsafe(tableName)}`; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const chCreateTableIfNotExists = ( | ||
sql: SqlClient | ClickhouseClient, | ||
tableName: string, | ||
columnDefinitions: string, | ||
) => { | ||
const firstColumnName = columnDefinitions.split(" ")[0]; | ||
return sql`CREATE TABLE IF NOT EXISTS ${sql.unsafe(tableName)} (${sql.unsafe(columnDefinitions)}) ORDER BY (${sql.unsafe(firstColumnName)})`; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const chInsertIntoTable = ( | ||
sql: SqlClient | ClickhouseClient, | ||
tableName: string, | ||
data: unknown[], | ||
) => | ||
(sql as ClickhouseClient) | ||
.insertQuery({ table: tableName, values: data }) | ||
.pipe(Effect.asVoid); | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const chSelectAllFromTable = ( | ||
sql: SqlClient | ClickhouseClient, | ||
tableName: string, | ||
) => sql`SELECT * FROM ${sql.unsafe(tableName)}`; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const chColumnDDL = (columnName: string, columnType: string) => | ||
`${columnName} ${columnType}`; |
Oops, something went wrong.