diff --git a/.changeset/four-lions-heal.md b/.changeset/four-lions-heal.md new file mode 100644 index 00000000000..f94b07c95f9 --- /dev/null +++ b/.changeset/four-lions-heal.md @@ -0,0 +1,5 @@ +--- +"@effect/sql-clickhouse": minor +--- + +Exposes clickhouse_settings parameter diff --git a/packages/sql-clickhouse/examples/basic.ts b/packages/sql-clickhouse/examples/basic.ts index 5bcf6e96257..74de2e17b37 100644 --- a/packages/sql-clickhouse/examples/basic.ts +++ b/packages/sql-clickhouse/examples/basic.ts @@ -33,7 +33,10 @@ Effect.gen(function*() { yield* sql`SELECT * FROM clickhouse_js_example_cloud_table ORDER BY id`.stream.pipe( Stream.runForEach(Effect.log), - sql.withQueryId("select") + sql.withQueryId("select"), + sql.withClickhouseSettings({ + log_comment: "Some comment to be stored in the query log" + }) ) }).pipe( Effect.provide(ClickhouseLive), diff --git a/packages/sql-clickhouse/src/ClickhouseClient.ts b/packages/sql-clickhouse/src/ClickhouseClient.ts index 33fca29f8eb..4effe74cbf7 100644 --- a/packages/sql-clickhouse/src/ClickhouseClient.ts +++ b/packages/sql-clickhouse/src/ClickhouseClient.ts @@ -53,6 +53,15 @@ export interface ClickhouseClient extends Client.SqlClient { (queryId: string): (effect: Effect.Effect) => Effect.Effect (effect: Effect.Effect, queryId: string): Effect.Effect } + readonly withClickhouseSettings: { + ( + settings: NonNullable + ): (effect: Effect.Effect) => Effect.Effect + ( + effect: Effect.Effect, + settings: NonNullable + ): Effect.Effect + } } /** @@ -106,13 +115,15 @@ export const make = ( const method = fiber.getFiberRef(currentClientMethod) return Effect.async | Clickhouse.CommandResult, SqlError>((resume) => { const queryId = fiber.getFiberRef(currentQueryId) ?? Crypto.randomUUID() + const settings = fiber.getFiberRef(currentClickhouseSettings) ?? {} const controller = new AbortController() if (method === "command") { this.conn.command({ query: sql, query_params: paramsObj, abort_signal: controller.signal, - query_id: queryId + query_id: queryId, + clickhouse_settings: settings }).then( (result) => resume(Effect.succeed(result)), (cause) => resume(Effect.fail(new SqlError({ cause, message: "Failed to execute statement" }))) @@ -123,6 +134,7 @@ export const make = ( query_params: paramsObj, abort_signal: controller.signal, query_id: queryId, + clickhouse_settings: settings, format }).then( (result) => resume(Effect.succeed(result)), @@ -231,28 +243,38 @@ export const make = ( readonly values: Clickhouse.InsertValues readonly format?: Clickhouse.DataFormat }) { - return FiberRef.getWith(currentQueryId, (queryId_) => - Effect.async((resume) => { - const queryId = queryId_ ?? Crypto.randomUUID() + return Effect.withFiberRuntime((fiber) => + Effect.async((resume) => { + const queryId = fiber.getFiberRef(currentQueryId) ?? Crypto.randomUUID() + const settings = fiber.getFiberRef(currentClickhouseSettings) const controller = new AbortController() client.insert({ format: "JSONEachRow", ...options, abort_signal: controller.signal, - query_id: queryId + query_id: queryId, + clickhouse_settings: settings }).then( - (result) => - resume(Effect.succeed(result)), + (result) => resume(Effect.succeed(result)), (cause) => resume(Effect.fail(new SqlError({ cause, message: "Failed to insert data" }))) ) return Effect.suspend(() => { controller.abort() return Effect.promise(() => client.command({ query: `KILL QUERY WHERE query_id = '${queryId}'` })) }) - })) + }) + ) }, withQueryId: dual(2, (effect: Effect.Effect, queryId: string) => - Effect.locally(effect, currentQueryId, queryId)) + Effect.locally(effect, currentQueryId, queryId)), + withClickhouseSettings: dual( + 2, + ( + effect: Effect.Effect, + settings: NonNullable + ) => + Effect.locally(effect, currentClickhouseSettings, settings) + ) } ) }) @@ -275,6 +297,17 @@ export const currentQueryId: FiberRef.FiberRef = globalValue () => FiberRef.unsafeMake(undefined) ) +/** + * @category fiber refs + * @since 1.0.0 + */ +export const currentClickhouseSettings: FiberRef.FiberRef< + NonNullable +> = globalValue( + "@effect/sql-clickhouse/ClickhouseClient/currentClickhouseSettings", + () => FiberRef.unsafeMake({}) +) + /** * @category layers * @since 1.0.0