Skip to content

Commit

Permalink
add sql-clickhouse & clickhouse dialect (#3760)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Oct 20, 2024
1 parent b836fc4 commit dacbf7d
Show file tree
Hide file tree
Showing 29 changed files with 765 additions and 96 deletions.
10 changes: 10 additions & 0 deletions .changeset/happy-dingos-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@effect/sql-clickhouse": minor
"@effect/sql-mssql": minor
"@effect/sql-pg": minor
"@effect/sql": minor
"@effect/cluster-workflow": patch
"@effect/cluster": patch
---

add sql-clickhouse & clickhouse dialect
4 changes: 2 additions & 2 deletions packages/cluster-workflow/src/DurableExecutionJournal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const make = ({ table }: DurableExecutionJournal.MakeOptions) =>
Effect.gen(function*() {
const sql = yield* SqlClient.SqlClient

yield* sql.onDialect({
yield* sql.onDialectOrElse({
mssql: () =>
sql`
IF OBJECT_ID(N'${sql.literal(table)}', N'U') IS NULL
Expand Down Expand Up @@ -98,7 +98,7 @@ export const make = ({ table }: DurableExecutionJournal.MakeOptions) =>
CONSTRAINT ${sql(table)}_pkey PRIMARY KEY (execution_id, sequence)
)
`,
sqlite: () =>
orElse: () =>
sql`
CREATE TABLE IF NOT EXISTS ${sql(table)} (
execution_id VARCHAR(255) NOT NULL,
Expand Down
10 changes: 5 additions & 5 deletions packages/cluster/src/internal/atLeastOnceStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const make = ({ table }: AtLeastOnceStorage.AtLeastOnceStorage.MakeOptions): Eff
const sql = yield* SqlClient.SqlClient
const serialization = yield* InternalSerialization.serializationTag

yield* sql.onDialect({
yield* sql.onDialectOrElse({
mssql: () =>
sql`
IF OBJECT_ID(N'${sql.literal(table)}', N'U') IS NULL
Expand Down Expand Up @@ -67,7 +67,7 @@ const make = ({ table }: AtLeastOnceStorage.AtLeastOnceStorage.MakeOptions): Eff
pg: () =>
Effect.catchAll(sql`SELECT ${table}::regclass`, () =>
sql`
CREATE TABLE ${sql(table)} (
CREATE TABLE IF NOT EXISTS ${sql(table)} (
recipient_name VARCHAR(255) NOT NULL,
shard_id INT NOT NULL DEFAULT 0,
entity_id VARCHAR(255) NOT NULL,
Expand All @@ -77,7 +77,7 @@ const make = ({ table }: AtLeastOnceStorage.AtLeastOnceStorage.MakeOptions): Eff
CONSTRAINT ${sql(table)}_pkey PRIMARY KEY (recipient_name, entity_id, message_id)
)
`),
sqlite: () =>
orElse: () =>
sql`
CREATE TABLE IF NOT EXISTS ${sql.literal(table)} (
recipient_name VARCHAR(255) NOT NULL,
Expand All @@ -100,7 +100,7 @@ const make = ({ table }: AtLeastOnceStorage.AtLeastOnceStorage.MakeOptions): Eff
message_body: Schema.String
}),
execute: (requests) =>
sql.onDialect({
sql.onDialectOrElse({
mssql: () =>
sql`
INSERT INTO ${sql(table)}
Expand All @@ -121,7 +121,7 @@ const make = ({ table }: AtLeastOnceStorage.AtLeastOnceStorage.MakeOptions): Eff
${sql.insert(requests)}
ON CONFLICT ON CONSTRAINT ${sql(table)}_pkey DO NOTHING
`,
sqlite: () =>
orElse: () =>
sql`
INSERT INTO ${sql(table)}
${sql.insert(requests)}
Expand Down
21 changes: 21 additions & 0 deletions packages/sql-clickhouse/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-present The Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions packages/sql-clickhouse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Effect SQL - Postgresql

An @effect/sql implementation using the `postgres.js` library.

See here for more information: https://github.com/Effect-TS/effect/tree/main/packages/sql
6 changes: 6 additions & 0 deletions packages/sql-clickhouse/docgen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../../node_modules/@effect/docgen/schema.json",
"exclude": [
"src/internal/**/*.ts"
]
}
41 changes: 41 additions & 0 deletions packages/sql-clickhouse/examples/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NodeRuntime } from "@effect/platform-node"
import { ClickhouseClient } from "@effect/sql-clickhouse"
import { Config, Effect, Stream } from "effect"

const ClickhouseLive = ClickhouseClient.layer({
url: Config.succeed("https://r8raccaqh3.ap-southeast-2.aws.clickhouse.cloud:8443"),
username: Config.succeed("default"),
password: Config.string("CLICKHOUSE_PASSWORD")
})

Effect.gen(function*() {
const sql = yield* ClickhouseClient.ClickhouseClient
yield* sql`CREATE TABLE IF NOT EXISTS clickhouse_js_example_cloud_table
(id UInt64, name String)
ORDER BY (id)`
yield* sql`TRUNCATE TABLE clickhouse_js_example_cloud_table`

yield* sql.insertQuery({
table: "clickhouse_js_example_cloud_table",
values: [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" }
]
})
yield* sql.asCommand(
sql`INSERT INTO clickhouse_js_example_cloud_table ${
sql.insert({
id: 3,
name: "Charlie"
})
}`
)

yield* sql`SELECT * FROM clickhouse_js_example_cloud_table ORDER BY id`.stream.pipe(
Stream.runForEach(Effect.log),
sql.withQueryId("select")
)
}).pipe(
Effect.provide(ClickhouseLive),
NodeRuntime.runMain
)
58 changes: 58 additions & 0 deletions packages/sql-clickhouse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@effect/sql-clickhouse",
"version": "0.0.0",
"type": "module",
"license": "MIT",
"description": "A Clickhouse toolkit for Effect",
"homepage": "https://effect.website",
"repository": {
"type": "git",
"url": "https://github.com/Effect-TS/effect.git",
"directory": "packages/sql-clickhouse"
},
"bugs": {
"url": "https://github.com/Effect-TS/effect/issues"
},
"tags": [
"typescript",
"sql",
"database"
],
"keywords": [
"typescript",
"sql",
"database"
],
"publishConfig": {
"access": "public",
"directory": "dist",
"provenance": true
},
"scripts": {
"codegen": "build-utils prepare-v2",
"build": "pnpm build-esm && pnpm build-annotate && pnpm build-cjs && build-utils pack-v2",
"build-esm": "tsc -b tsconfig.build.json",
"build-cjs": "babel build/esm --plugins @babel/transform-export-namespace-from --plugins @babel/transform-modules-commonjs --out-dir build/cjs --source-maps",
"build-annotate": "babel build/esm --plugins annotate-pure-calls --out-dir build/esm --source-maps",
"check": "tsc -b tsconfig.json",
"test": "vitest",
"coverage": "vitest --coverage"
},
"devDependencies": {
"@effect/platform-node": "workspace:^",
"@effect/platform": "workspace:^",
"@effect/sql": "workspace:^",
"@types/node": "^20.14.10",
"effect": "workspace:^"
},
"peerDependencies": {
"@effect/platform-node": "workspace:^",
"@effect/platform": "workspace:^",
"@effect/sql": "workspace:^",
"effect": "workspace:^"
},
"dependencies": {
"@clickhouse/client": "^1.6.0",
"@opentelemetry/semantic-conventions": "^1.25.1"
}
}
Loading

0 comments on commit dacbf7d

Please sign in to comment.