@effect/sql-mysql2 0.49.1 → 4.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{dts/MysqlClient.d.ts → MysqlClient.d.ts} +10 -11
- package/dist/MysqlClient.d.ts.map +1 -0
- package/dist/{esm/MysqlClient.js → MysqlClient.js} +39 -31
- package/dist/MysqlClient.js.map +1 -0
- package/dist/MysqlMigrator.d.ts +23 -0
- package/dist/MysqlMigrator.d.ts.map +1 -0
- package/dist/MysqlMigrator.js +70 -0
- package/dist/MysqlMigrator.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/{esm/index.js → index.js} +4 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -50
- package/src/MysqlClient.ts +58 -47
- package/src/MysqlMigrator.ts +60 -80
- package/src/index.ts +8 -2
- package/MysqlClient/package.json +0 -6
- package/MysqlMigrator/package.json +0 -6
- package/dist/cjs/MysqlClient.js +0 -223
- package/dist/cjs/MysqlClient.js.map +0 -1
- package/dist/cjs/MysqlMigrator.js +0 -104
- package/dist/cjs/MysqlMigrator.js.map +0 -1
- package/dist/cjs/index.js +0 -12
- package/dist/cjs/index.js.map +0 -1
- package/dist/dts/MysqlClient.d.ts.map +0 -1
- package/dist/dts/MysqlMigrator.d.ts +0 -28
- package/dist/dts/MysqlMigrator.d.ts.map +0 -1
- package/dist/dts/index.d.ts +0 -9
- package/dist/dts/index.d.ts.map +0 -1
- package/dist/esm/MysqlClient.js.map +0 -1
- package/dist/esm/MysqlMigrator.js +0 -68
- package/dist/esm/MysqlMigrator.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/package.json +0 -4
- package/index/package.json +0 -6
package/src/MysqlClient.ts
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import * as Reactivity from "@effect/experimental/Reactivity"
|
|
5
|
-
import * as Client from "@effect/sql/SqlClient"
|
|
6
|
-
import type { Connection } from "@effect/sql/SqlConnection"
|
|
7
|
-
import { SqlError } from "@effect/sql/SqlError"
|
|
8
|
-
import { asyncPauseResume } from "@effect/sql/SqlStream"
|
|
9
|
-
import * as Statement from "@effect/sql/Statement"
|
|
10
|
-
import * as Chunk from "effect/Chunk"
|
|
11
4
|
import * as Config from "effect/Config"
|
|
12
|
-
import type { ConfigError } from "effect/ConfigError"
|
|
13
|
-
import * as Context from "effect/Context"
|
|
14
5
|
import * as Duration from "effect/Duration"
|
|
15
6
|
import * as Effect from "effect/Effect"
|
|
16
7
|
import * as Layer from "effect/Layer"
|
|
17
8
|
import * as Redacted from "effect/Redacted"
|
|
18
9
|
import type { Scope } from "effect/Scope"
|
|
10
|
+
import * as ServiceMap from "effect/ServiceMap"
|
|
19
11
|
import * as Stream from "effect/Stream"
|
|
12
|
+
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
13
|
+
import * as Client from "effect/unstable/sql/SqlClient"
|
|
14
|
+
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
15
|
+
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
16
|
+
import { asyncPauseResume } from "effect/unstable/sql/SqlStream"
|
|
17
|
+
import * as Statement from "effect/unstable/sql/Statement"
|
|
20
18
|
import * as Mysql from "mysql2"
|
|
21
19
|
|
|
22
20
|
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
|
@@ -28,13 +26,13 @@ const ATTR_SERVER_PORT = "server.port"
|
|
|
28
26
|
* @category type ids
|
|
29
27
|
* @since 1.0.0
|
|
30
28
|
*/
|
|
31
|
-
export const TypeId:
|
|
29
|
+
export const TypeId: TypeId = "~@effect/sql-mysql2/MysqlClient"
|
|
32
30
|
|
|
33
31
|
/**
|
|
34
32
|
* @category type ids
|
|
35
33
|
* @since 1.0.0
|
|
36
34
|
*/
|
|
37
|
-
export type TypeId =
|
|
35
|
+
export type TypeId = "~@effect/sql-mysql2/MysqlClient"
|
|
38
36
|
|
|
39
37
|
/**
|
|
40
38
|
* @category models
|
|
@@ -49,7 +47,7 @@ export interface MysqlClient extends Client.SqlClient {
|
|
|
49
47
|
* @category tags
|
|
50
48
|
* @since 1.0.0
|
|
51
49
|
*/
|
|
52
|
-
export const MysqlClient =
|
|
50
|
+
export const MysqlClient = ServiceMap.Service<MysqlClient>("@effect/sql-mysql2/MysqlClient")
|
|
53
51
|
|
|
54
52
|
/**
|
|
55
53
|
* @category models
|
|
@@ -94,7 +92,10 @@ export const make = (
|
|
|
94
92
|
undefined
|
|
95
93
|
|
|
96
94
|
class ConnectionImpl implements Connection {
|
|
97
|
-
|
|
95
|
+
readonly conn: Mysql.PoolConnection | Mysql.Pool
|
|
96
|
+
constructor(conn: Mysql.PoolConnection | Mysql.Pool) {
|
|
97
|
+
this.conn = conn
|
|
98
|
+
}
|
|
98
99
|
|
|
99
100
|
private runRaw(
|
|
100
101
|
sql: string,
|
|
@@ -102,7 +103,7 @@ export const make = (
|
|
|
102
103
|
rowsAsArray = false,
|
|
103
104
|
method: "execute" | "query" = "execute"
|
|
104
105
|
) {
|
|
105
|
-
return Effect.
|
|
106
|
+
return Effect.callback<unknown, SqlError>((resume) => {
|
|
106
107
|
;(this.conn as any)[method]({
|
|
107
108
|
sql,
|
|
108
109
|
values,
|
|
@@ -159,10 +160,7 @@ export const make = (
|
|
|
159
160
|
) {
|
|
160
161
|
const stream = queryStream(this.conn as any, sql, params)
|
|
161
162
|
return transformRows
|
|
162
|
-
? Stream.
|
|
163
|
-
Chunk.unsafeFromArray(
|
|
164
|
-
transformRows(Chunk.toReadonlyArray(_) as Array<object>)
|
|
165
|
-
))
|
|
163
|
+
? Stream.mapArray(stream, (_) => transformRows(_) as any)
|
|
166
164
|
: stream
|
|
167
165
|
}
|
|
168
166
|
}
|
|
@@ -174,11 +172,11 @@ export const make = (
|
|
|
174
172
|
supportBigNumbers: true,
|
|
175
173
|
connectionLimit: options.maxConnections!,
|
|
176
174
|
idleTimeout: options.connectionTTL
|
|
177
|
-
? Duration.toMillis(options.connectionTTL)
|
|
175
|
+
? Duration.toMillis(Duration.fromDurationInputUnsafe(options.connectionTTL))
|
|
178
176
|
: undefined as any
|
|
179
177
|
})
|
|
180
178
|
: Mysql.createPool({
|
|
181
|
-
...
|
|
179
|
+
...options.poolConfig,
|
|
182
180
|
host: options.host,
|
|
183
181
|
port: options.port,
|
|
184
182
|
database: options.database,
|
|
@@ -189,14 +187,13 @@ export const make = (
|
|
|
189
187
|
multipleStatements: true,
|
|
190
188
|
supportBigNumbers: true,
|
|
191
189
|
connectionLimit: options.maxConnections,
|
|
192
|
-
maxIdle: options.poolConfig?.maxIdle ?? 0,
|
|
193
190
|
idleTimeout: options.connectionTTL
|
|
194
|
-
? Duration.toMillis(options.connectionTTL)
|
|
191
|
+
? Duration.toMillis(Duration.fromDurationInputUnsafe(options.connectionTTL))
|
|
195
192
|
: undefined
|
|
196
193
|
} as Mysql.PoolOptions)
|
|
197
194
|
|
|
198
195
|
yield* Effect.acquireRelease(
|
|
199
|
-
Effect.
|
|
196
|
+
Effect.callback<void, SqlError>((resume) => {
|
|
200
197
|
;(pool as any).query("SELECT 1", (cause: Error) => {
|
|
201
198
|
if (cause) {
|
|
202
199
|
resume(Effect.fail(
|
|
@@ -211,27 +208,29 @@ export const make = (
|
|
|
211
208
|
})
|
|
212
209
|
}),
|
|
213
210
|
() =>
|
|
214
|
-
Effect.
|
|
211
|
+
Effect.callback<void>((resume) => {
|
|
215
212
|
pool.end(() => resume(Effect.void))
|
|
216
213
|
})
|
|
217
214
|
).pipe(
|
|
218
|
-
Effect.
|
|
215
|
+
Effect.timeoutOrElse({
|
|
219
216
|
duration: Duration.seconds(5),
|
|
220
217
|
onTimeout: () =>
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
218
|
+
Effect.fail(
|
|
219
|
+
new SqlError({
|
|
220
|
+
message: "MysqlClient: Connection timeout",
|
|
221
|
+
cause: new Error("connection timeout")
|
|
222
|
+
})
|
|
223
|
+
)
|
|
225
224
|
})
|
|
226
225
|
)
|
|
227
226
|
|
|
228
227
|
const poolConnection = new ConnectionImpl(pool)
|
|
229
228
|
|
|
230
229
|
const acquireConn = Effect.acquireRelease(
|
|
231
|
-
Effect.
|
|
230
|
+
Effect.callback<Mysql.PoolConnection, SqlError>((resume) => {
|
|
232
231
|
pool.getConnection((cause, conn) => {
|
|
233
232
|
if (cause) {
|
|
234
|
-
resume(new SqlError({ cause, message: "Failed to acquire connection" }))
|
|
233
|
+
resume(Effect.fail(new SqlError({ cause, message: "Failed to acquire connection" })))
|
|
235
234
|
} else {
|
|
236
235
|
resume(Effect.succeed(conn))
|
|
237
236
|
}
|
|
@@ -273,14 +272,14 @@ export const make = (
|
|
|
273
272
|
* @since 1.0.0
|
|
274
273
|
*/
|
|
275
274
|
export const layerConfig = (
|
|
276
|
-
config: Config.
|
|
277
|
-
): Layer.Layer<MysqlClient | Client.SqlClient, ConfigError | SqlError> =>
|
|
278
|
-
Layer.
|
|
279
|
-
Config.unwrap(config).pipe(
|
|
275
|
+
config: Config.Wrap<MysqlClientConfig>
|
|
276
|
+
): Layer.Layer<MysqlClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
277
|
+
Layer.effectServices(
|
|
278
|
+
Config.unwrap(config).asEffect().pipe(
|
|
280
279
|
Effect.flatMap(make),
|
|
281
280
|
Effect.map((client) =>
|
|
282
|
-
|
|
283
|
-
|
|
281
|
+
ServiceMap.make(MysqlClient, client).pipe(
|
|
282
|
+
ServiceMap.add(Client.SqlClient, client)
|
|
284
283
|
)
|
|
285
284
|
)
|
|
286
285
|
)
|
|
@@ -292,11 +291,11 @@ export const layerConfig = (
|
|
|
292
291
|
*/
|
|
293
292
|
export const layer = (
|
|
294
293
|
config: MysqlClientConfig
|
|
295
|
-
): Layer.Layer<MysqlClient | Client.SqlClient, ConfigError | SqlError> =>
|
|
296
|
-
Layer.
|
|
294
|
+
): Layer.Layer<MysqlClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
295
|
+
Layer.effectServices(
|
|
297
296
|
Effect.map(make(config), (client) =>
|
|
298
|
-
|
|
299
|
-
|
|
297
|
+
ServiceMap.make(MysqlClient, client).pipe(
|
|
298
|
+
ServiceMap.add(Client.SqlClient, client)
|
|
300
299
|
))
|
|
301
300
|
).pipe(Layer.provide(Reactivity.layer))
|
|
302
301
|
|
|
@@ -330,8 +329,10 @@ function queryStream(
|
|
|
330
329
|
sql: string,
|
|
331
330
|
params?: ReadonlyArray<any>
|
|
332
331
|
) {
|
|
333
|
-
return asyncPauseResume<any, SqlError>((emit)
|
|
332
|
+
return asyncPauseResume<any, SqlError>(Effect.fnUntraced(function*(emit) {
|
|
334
333
|
const query = (conn as any).query(sql, params).stream()
|
|
334
|
+
yield* Effect.addFinalizer(() => Effect.sync(() => query.destroy()))
|
|
335
|
+
|
|
335
336
|
let buffer: Array<any> = []
|
|
336
337
|
let taskPending = false
|
|
337
338
|
query.on("error", (cause: unknown) => emit.fail(new SqlError({ cause, message: "Failed to stream statement" })))
|
|
@@ -347,11 +348,21 @@ function queryStream(
|
|
|
347
348
|
})
|
|
348
349
|
}
|
|
349
350
|
})
|
|
350
|
-
query.on("end", () =>
|
|
351
|
+
query.on("end", () => {
|
|
352
|
+
if (buffer.length > 0) {
|
|
353
|
+
emit.array(buffer)
|
|
354
|
+
buffer = []
|
|
355
|
+
}
|
|
356
|
+
emit.end()
|
|
357
|
+
})
|
|
358
|
+
|
|
351
359
|
return {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
360
|
+
onPause() {
|
|
361
|
+
query.pause()
|
|
362
|
+
},
|
|
363
|
+
onResume() {
|
|
364
|
+
query.resume()
|
|
365
|
+
}
|
|
355
366
|
}
|
|
356
|
-
})
|
|
367
|
+
}))
|
|
357
368
|
}
|
package/src/MysqlMigrator.ts
CHANGED
|
@@ -1,100 +1,80 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import * as
|
|
5
|
-
import type { CommandExecutor } from "@effect/platform/CommandExecutor"
|
|
6
|
-
import { FileSystem } from "@effect/platform/FileSystem"
|
|
7
|
-
import { Path } from "@effect/platform/Path"
|
|
8
|
-
import * as Migrator from "@effect/sql/Migrator"
|
|
9
|
-
import type * as Client from "@effect/sql/SqlClient"
|
|
10
|
-
import type { SqlError } from "@effect/sql/SqlError"
|
|
11
|
-
import * as Effect from "effect/Effect"
|
|
12
|
-
import { pipe } from "effect/Function"
|
|
4
|
+
import type * as Effect from "effect/Effect"
|
|
13
5
|
import * as Layer from "effect/Layer"
|
|
14
|
-
import * as
|
|
15
|
-
import
|
|
6
|
+
import * as Migrator from "effect/unstable/sql/Migrator"
|
|
7
|
+
import type * as Client from "effect/unstable/sql/SqlClient"
|
|
8
|
+
import type { SqlError } from "effect/unstable/sql/SqlError"
|
|
16
9
|
|
|
17
10
|
/**
|
|
18
11
|
* @since 1.0.0
|
|
19
12
|
*/
|
|
20
|
-
export * from "
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @since 1.0.0
|
|
24
|
-
*/
|
|
25
|
-
export * from "@effect/sql/Migrator/FileSystem"
|
|
13
|
+
export * from "effect/unstable/sql/Migrator"
|
|
26
14
|
|
|
27
15
|
/**
|
|
28
16
|
* @category constructor
|
|
29
17
|
* @since 1.0.0
|
|
30
18
|
*/
|
|
31
19
|
export const run: <R2 = never>(
|
|
32
|
-
|
|
20
|
+
{ loader, schemaDirectory, table }: Migrator.MigratorOptions<R2>
|
|
33
21
|
) => Effect.Effect<
|
|
34
22
|
ReadonlyArray<readonly [id: number, name: string]>,
|
|
35
23
|
Migrator.MigrationError | SqlError,
|
|
36
|
-
|
|
24
|
+
Client.SqlClient | R2
|
|
37
25
|
> = Migrator.make({
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
yield* fs.makeDirectory(path.dirname(file), { recursive: true })
|
|
91
|
-
yield* fs.writeFileString(file, dump)
|
|
92
|
-
}).pipe(
|
|
93
|
-
Effect.mapError((error) => new Migrator.MigrationError({ reason: "failed", message: error.message }))
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
return dumpFile(path)
|
|
97
|
-
}
|
|
26
|
+
// TODO: re-add when Command module is available
|
|
27
|
+
// dumpSchema(path, table) {
|
|
28
|
+
// const mysqlDump = (args: Array<string>) =>
|
|
29
|
+
// Effect.gen(function*() {
|
|
30
|
+
// const sql = yield* MysqlClient
|
|
31
|
+
// const dump = yield* pipe(
|
|
32
|
+
// Command.make(
|
|
33
|
+
// "mysqldump",
|
|
34
|
+
// ...(sql.config.host ? ["-h", sql.config.host] : []),
|
|
35
|
+
// ...(sql.config.port ? ["-P", sql.config.port.toString()] : []),
|
|
36
|
+
// ...(sql.config.username ? ["-u", sql.config.username] : []),
|
|
37
|
+
// ...(sql.config.password ? [`-p${Redacted.value(sql.config.password)}`] : []),
|
|
38
|
+
// ...(sql.config.database ? [sql.config.database] : []),
|
|
39
|
+
// "--skip-comments",
|
|
40
|
+
// "--compact",
|
|
41
|
+
// ...args
|
|
42
|
+
// ),
|
|
43
|
+
// Command.env({
|
|
44
|
+
// PATH: (globalThis as any).process?.env.PATH
|
|
45
|
+
// }),
|
|
46
|
+
// Command.string
|
|
47
|
+
// )
|
|
48
|
+
//
|
|
49
|
+
// return dump.replace(/^\/\*.*$/gm, "")
|
|
50
|
+
// .replace(/\n{2,}/gm, "\n\n")
|
|
51
|
+
// .trim()
|
|
52
|
+
// }).pipe(
|
|
53
|
+
// Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
|
|
54
|
+
// )
|
|
55
|
+
//
|
|
56
|
+
// const dumpSchema = mysqlDump(["--no-data"])
|
|
57
|
+
//
|
|
58
|
+
// const dumpMigrations = mysqlDump(["--no-create-info", "--tables", table])
|
|
59
|
+
//
|
|
60
|
+
// const dumpAll = Effect.map(
|
|
61
|
+
// Effect.all([dumpSchema, dumpMigrations], { concurrency: 2 }),
|
|
62
|
+
// ([schema, migrations]) => schema + "\n\n" + migrations
|
|
63
|
+
// )
|
|
64
|
+
//
|
|
65
|
+
// const dumpFile = (file: string) =>
|
|
66
|
+
// Effect.gen(function*() {
|
|
67
|
+
// const fs = yield* FileSystem
|
|
68
|
+
// const path = yield* Path
|
|
69
|
+
// const dump = yield* dumpAll
|
|
70
|
+
// yield* fs.makeDirectory(path.dirname(file), { recursive: true })
|
|
71
|
+
// yield* fs.writeFileString(file, dump)
|
|
72
|
+
// }).pipe(
|
|
73
|
+
// Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
|
|
74
|
+
// )
|
|
75
|
+
//
|
|
76
|
+
// return dumpFile(path)
|
|
77
|
+
// }
|
|
98
78
|
})
|
|
99
79
|
|
|
100
80
|
/**
|
|
@@ -106,5 +86,5 @@ export const layer = <R>(
|
|
|
106
86
|
): Layer.Layer<
|
|
107
87
|
never,
|
|
108
88
|
Migrator.MigrationError | SqlError,
|
|
109
|
-
|
|
89
|
+
Client.SqlClient | R
|
|
110
90
|
> => Layer.effectDiscard(run(options))
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
// @barrel: Auto-generated exports. Do not edit manually.
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @since 1.0.0
|
|
9
|
+
*/
|
|
10
|
+
export * as MysqlClient from "./MysqlClient.ts"
|
|
5
11
|
|
|
6
12
|
/**
|
|
7
13
|
* @since 1.0.0
|
|
8
14
|
*/
|
|
9
|
-
export * as MysqlMigrator from "./MysqlMigrator.
|
|
15
|
+
export * as MysqlMigrator from "./MysqlMigrator.ts"
|
package/MysqlClient/package.json
DELETED
package/dist/cjs/MysqlClient.js
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.makeCompiler = exports.make = exports.layerConfig = exports.layer = exports.TypeId = exports.MysqlClient = void 0;
|
|
7
|
-
var Reactivity = _interopRequireWildcard(require("@effect/experimental/Reactivity"));
|
|
8
|
-
var Client = _interopRequireWildcard(require("@effect/sql/SqlClient"));
|
|
9
|
-
var _SqlError = require("@effect/sql/SqlError");
|
|
10
|
-
var _SqlStream = require("@effect/sql/SqlStream");
|
|
11
|
-
var Statement = _interopRequireWildcard(require("@effect/sql/Statement"));
|
|
12
|
-
var Chunk = _interopRequireWildcard(require("effect/Chunk"));
|
|
13
|
-
var Config = _interopRequireWildcard(require("effect/Config"));
|
|
14
|
-
var Context = _interopRequireWildcard(require("effect/Context"));
|
|
15
|
-
var Duration = _interopRequireWildcard(require("effect/Duration"));
|
|
16
|
-
var Effect = _interopRequireWildcard(require("effect/Effect"));
|
|
17
|
-
var Layer = _interopRequireWildcard(require("effect/Layer"));
|
|
18
|
-
var Redacted = _interopRequireWildcard(require("effect/Redacted"));
|
|
19
|
-
var Stream = _interopRequireWildcard(require("effect/Stream"));
|
|
20
|
-
var Mysql = _interopRequireWildcard(require("mysql2"));
|
|
21
|
-
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
22
|
-
/**
|
|
23
|
-
* @since 1.0.0
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
const ATTR_DB_SYSTEM_NAME = "db.system.name";
|
|
27
|
-
const ATTR_DB_NAMESPACE = "db.namespace";
|
|
28
|
-
const ATTR_SERVER_ADDRESS = "server.address";
|
|
29
|
-
const ATTR_SERVER_PORT = "server.port";
|
|
30
|
-
/**
|
|
31
|
-
* @category type ids
|
|
32
|
-
* @since 1.0.0
|
|
33
|
-
*/
|
|
34
|
-
const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("@effect/sql-mysql2/MysqlClient");
|
|
35
|
-
/**
|
|
36
|
-
* @category tags
|
|
37
|
-
* @since 1.0.0
|
|
38
|
-
*/
|
|
39
|
-
const MysqlClient = exports.MysqlClient = /*#__PURE__*/Context.GenericTag("@effect/sql-mysql2/MysqlClient");
|
|
40
|
-
/**
|
|
41
|
-
* @category constructors
|
|
42
|
-
* @since 1.0.0
|
|
43
|
-
*/
|
|
44
|
-
const make = options => Effect.gen(function* () {
|
|
45
|
-
const compiler = makeCompiler(options.transformQueryNames);
|
|
46
|
-
const transformRows = options.transformResultNames ? Statement.defaultTransforms(options.transformResultNames).array : undefined;
|
|
47
|
-
class ConnectionImpl {
|
|
48
|
-
conn;
|
|
49
|
-
constructor(conn) {
|
|
50
|
-
this.conn = conn;
|
|
51
|
-
}
|
|
52
|
-
runRaw(sql, values, rowsAsArray = false, method = "execute") {
|
|
53
|
-
return Effect.async(resume => {
|
|
54
|
-
;
|
|
55
|
-
this.conn[method]({
|
|
56
|
-
sql,
|
|
57
|
-
values,
|
|
58
|
-
rowsAsArray
|
|
59
|
-
}, (cause, results, _fields) => {
|
|
60
|
-
if (cause) {
|
|
61
|
-
resume(Effect.fail(new _SqlError.SqlError({
|
|
62
|
-
cause,
|
|
63
|
-
message: "Failed to execute statement"
|
|
64
|
-
})));
|
|
65
|
-
} else {
|
|
66
|
-
resume(Effect.succeed(results));
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
run(sql, values, rowsAsArray = false, method = "execute") {
|
|
72
|
-
return this.runRaw(sql, values, rowsAsArray, method).pipe(Effect.map(results => Array.isArray(results) ? results : []));
|
|
73
|
-
}
|
|
74
|
-
execute(sql, params, transformRows) {
|
|
75
|
-
return transformRows ? Effect.map(this.run(sql, params), transformRows) : this.run(sql, params);
|
|
76
|
-
}
|
|
77
|
-
executeRaw(sql, params) {
|
|
78
|
-
return this.runRaw(sql, params);
|
|
79
|
-
}
|
|
80
|
-
executeValues(sql, params) {
|
|
81
|
-
return this.run(sql, params, true);
|
|
82
|
-
}
|
|
83
|
-
executeUnprepared(sql, params, transformRows) {
|
|
84
|
-
return transformRows ? Effect.map(this.run(sql, params, false, "query"), transformRows) : this.run(sql, params, false, "query");
|
|
85
|
-
}
|
|
86
|
-
executeStream(sql, params, transformRows) {
|
|
87
|
-
const stream = queryStream(this.conn, sql, params);
|
|
88
|
-
return transformRows ? Stream.mapChunks(stream, _ => Chunk.unsafeFromArray(transformRows(Chunk.toReadonlyArray(_)))) : stream;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
const pool = options.url ? Mysql.createPool({
|
|
92
|
-
uri: Redacted.value(options.url),
|
|
93
|
-
multipleStatements: true,
|
|
94
|
-
supportBigNumbers: true,
|
|
95
|
-
connectionLimit: options.maxConnections,
|
|
96
|
-
idleTimeout: options.connectionTTL ? Duration.toMillis(options.connectionTTL) : undefined
|
|
97
|
-
}) : Mysql.createPool({
|
|
98
|
-
...(options.poolConfig ?? {}),
|
|
99
|
-
host: options.host,
|
|
100
|
-
port: options.port,
|
|
101
|
-
database: options.database,
|
|
102
|
-
user: options.username,
|
|
103
|
-
password: options.password ? Redacted.value(options.password) : undefined,
|
|
104
|
-
multipleStatements: true,
|
|
105
|
-
supportBigNumbers: true,
|
|
106
|
-
connectionLimit: options.maxConnections,
|
|
107
|
-
maxIdle: options.poolConfig?.maxIdle ?? 0,
|
|
108
|
-
idleTimeout: options.connectionTTL ? Duration.toMillis(options.connectionTTL) : undefined
|
|
109
|
-
});
|
|
110
|
-
yield* Effect.acquireRelease(Effect.async(resume => {
|
|
111
|
-
;
|
|
112
|
-
pool.query("SELECT 1", cause => {
|
|
113
|
-
if (cause) {
|
|
114
|
-
resume(Effect.fail(new _SqlError.SqlError({
|
|
115
|
-
cause,
|
|
116
|
-
message: "MysqlClient: Failed to connect"
|
|
117
|
-
})));
|
|
118
|
-
} else {
|
|
119
|
-
resume(Effect.void);
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
}), () => Effect.async(resume => {
|
|
123
|
-
pool.end(() => resume(Effect.void));
|
|
124
|
-
})).pipe(Effect.timeoutFail({
|
|
125
|
-
duration: Duration.seconds(5),
|
|
126
|
-
onTimeout: () => new _SqlError.SqlError({
|
|
127
|
-
message: "MysqlClient: Connection timeout",
|
|
128
|
-
cause: new Error("connection timeout")
|
|
129
|
-
})
|
|
130
|
-
}));
|
|
131
|
-
const poolConnection = new ConnectionImpl(pool);
|
|
132
|
-
const acquireConn = Effect.acquireRelease(Effect.async(resume => {
|
|
133
|
-
pool.getConnection((cause, conn) => {
|
|
134
|
-
if (cause) {
|
|
135
|
-
resume(new _SqlError.SqlError({
|
|
136
|
-
cause,
|
|
137
|
-
message: "Failed to acquire connection"
|
|
138
|
-
}));
|
|
139
|
-
} else {
|
|
140
|
-
resume(Effect.succeed(conn));
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}), conn => Effect.sync(() => conn.release()));
|
|
144
|
-
const transactionAcquirer = Effect.map(acquireConn, conn => new ConnectionImpl(conn));
|
|
145
|
-
const spanAttributes = [...(options.spanAttributes ? Object.entries(options.spanAttributes) : []), [ATTR_DB_SYSTEM_NAME, "mysql"], [ATTR_SERVER_ADDRESS, options.host ?? "localhost"], [ATTR_SERVER_PORT, options.port ?? 3306]];
|
|
146
|
-
if (options.database) {
|
|
147
|
-
spanAttributes.push([ATTR_DB_NAMESPACE, options.database]);
|
|
148
|
-
}
|
|
149
|
-
return Object.assign(yield* Client.make({
|
|
150
|
-
acquirer: Effect.succeed(poolConnection),
|
|
151
|
-
transactionAcquirer,
|
|
152
|
-
compiler,
|
|
153
|
-
spanAttributes,
|
|
154
|
-
transformRows
|
|
155
|
-
}), {
|
|
156
|
-
[TypeId]: TypeId,
|
|
157
|
-
config: options
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
/**
|
|
161
|
-
* @category layers
|
|
162
|
-
* @since 1.0.0
|
|
163
|
-
*/
|
|
164
|
-
exports.make = make;
|
|
165
|
-
const layerConfig = config => Layer.scopedContext(Config.unwrap(config).pipe(Effect.flatMap(make), Effect.map(client => Context.make(MysqlClient, client).pipe(Context.add(Client.SqlClient, client))))).pipe(Layer.provide(Reactivity.layer));
|
|
166
|
-
/**
|
|
167
|
-
* @category layers
|
|
168
|
-
* @since 1.0.0
|
|
169
|
-
*/
|
|
170
|
-
exports.layerConfig = layerConfig;
|
|
171
|
-
const layer = config => Layer.scopedContext(Effect.map(make(config), client => Context.make(MysqlClient, client).pipe(Context.add(Client.SqlClient, client)))).pipe(Layer.provide(Reactivity.layer));
|
|
172
|
-
/**
|
|
173
|
-
* @category compiler
|
|
174
|
-
* @since 1.0.0
|
|
175
|
-
*/
|
|
176
|
-
exports.layer = layer;
|
|
177
|
-
const makeCompiler = transform => Statement.makeCompiler({
|
|
178
|
-
dialect: "mysql",
|
|
179
|
-
placeholder(_) {
|
|
180
|
-
return `?`;
|
|
181
|
-
},
|
|
182
|
-
onIdentifier: transform ? function (value, withoutTransform) {
|
|
183
|
-
return withoutTransform ? escape(value) : escape(transform(value));
|
|
184
|
-
} : escape,
|
|
185
|
-
onCustom() {
|
|
186
|
-
return ["", []];
|
|
187
|
-
},
|
|
188
|
-
onRecordUpdate() {
|
|
189
|
-
return ["", []];
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
exports.makeCompiler = makeCompiler;
|
|
193
|
-
const escape = /*#__PURE__*/Statement.defaultEscape("`");
|
|
194
|
-
function queryStream(conn, sql, params) {
|
|
195
|
-
return (0, _SqlStream.asyncPauseResume)(emit => {
|
|
196
|
-
const query = conn.query(sql, params).stream();
|
|
197
|
-
let buffer = [];
|
|
198
|
-
let taskPending = false;
|
|
199
|
-
query.on("error", cause => emit.fail(new _SqlError.SqlError({
|
|
200
|
-
cause,
|
|
201
|
-
message: "Failed to stream statement"
|
|
202
|
-
})));
|
|
203
|
-
query.on("data", row => {
|
|
204
|
-
buffer.push(row);
|
|
205
|
-
if (!taskPending) {
|
|
206
|
-
taskPending = true;
|
|
207
|
-
queueMicrotask(() => {
|
|
208
|
-
const items = buffer;
|
|
209
|
-
buffer = [];
|
|
210
|
-
emit.array(items);
|
|
211
|
-
taskPending = false;
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
query.on("end", () => emit.end());
|
|
216
|
-
return {
|
|
217
|
-
onInterrupt: Effect.sync(() => query.destroy()),
|
|
218
|
-
onPause: Effect.sync(() => query.pause()),
|
|
219
|
-
onResume: Effect.sync(() => query.resume())
|
|
220
|
-
};
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
//# sourceMappingURL=MysqlClient.js.map
|