@effect/sql-mysql2 4.0.0-beta.7 → 4.0.0-beta.71
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/MysqlClient.d.ts +63 -14
- package/dist/MysqlClient.d.ts.map +1 -1
- package/dist/MysqlClient.js +151 -25
- package/dist/MysqlClient.js.map +1 -1
- package/dist/MysqlMigrator.d.ts +39 -5
- package/dist/MysqlMigrator.d.ts.map +1 -1
- package/dist/MysqlMigrator.js +8 -4
- package/dist/MysqlMigrator.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/package.json +5 -5
- package/src/MysqlClient.ts +198 -31
- package/src/MysqlMigrator.ts +39 -5
- package/src/index.ts +3 -3
package/dist/MysqlClient.d.ts
CHANGED
|
@@ -1,44 +1,85 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* MySQL adapter for Effect SQL, backed by the `mysql2` driver.
|
|
3
|
+
*
|
|
4
|
+
* This module provides constructors and layers for a {@link MysqlClient} and
|
|
5
|
+
* the generic Effect SQL client service. Use it in server applications,
|
|
6
|
+
* background workers, migrations, and tests that need MySQL query compilation,
|
|
7
|
+
* scoped pool management, streaming queries, and consistent `SqlError`
|
|
8
|
+
* classification for mysql2 driver failures.
|
|
9
|
+
*
|
|
10
|
+
* **Mental model**
|
|
11
|
+
*
|
|
12
|
+
* {@link make} allocates a mysql2 pool inside the current scope, verifies it
|
|
13
|
+
* with `SELECT 1`, and closes it when the scope is released. Regular queries
|
|
14
|
+
* use the shared pool, transactions acquire a dedicated pooled connection for
|
|
15
|
+
* their lifetime, and streams stay tied to mysql2 stream resources until they
|
|
16
|
+
* are consumed or closed.
|
|
17
|
+
*
|
|
18
|
+
* **Common tasks**
|
|
19
|
+
*
|
|
20
|
+
* Use {@link layer} when configuration is already available, or
|
|
21
|
+
* {@link layerConfig} when it should be read through `Config`. Pass `url` for a
|
|
22
|
+
* connection URI, or pass `host`, `port`, `database`, `username`, and
|
|
23
|
+
* `password` for discrete connection settings. Use `poolConfig` for additional
|
|
24
|
+
* mysql2 pool options when configuring discrete fields.
|
|
25
|
+
*
|
|
26
|
+
* **Gotchas**
|
|
27
|
+
*
|
|
28
|
+
* When `url` is supplied it takes precedence over the discrete connection
|
|
29
|
+
* fields and does not merge in `poolConfig`. Long-running transactions and
|
|
30
|
+
* streams occupy pool capacity, so tune `maxConnections` and `connectionTTL`
|
|
31
|
+
* for those workloads.
|
|
32
|
+
*
|
|
33
|
+
* @since 4.0.0
|
|
3
34
|
*/
|
|
4
35
|
import * as Config from "effect/Config";
|
|
36
|
+
import * as Context from "effect/Context";
|
|
5
37
|
import * as Duration from "effect/Duration";
|
|
6
38
|
import * as Effect from "effect/Effect";
|
|
7
39
|
import * as Layer from "effect/Layer";
|
|
8
40
|
import * as Redacted from "effect/Redacted";
|
|
9
41
|
import type { Scope } from "effect/Scope";
|
|
10
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
11
42
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
12
43
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
13
44
|
import { SqlError } from "effect/unstable/sql/SqlError";
|
|
14
45
|
import * as Statement from "effect/unstable/sql/Statement";
|
|
15
46
|
import * as Mysql from "mysql2";
|
|
16
47
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
48
|
+
* Runtime type identifier used to mark `MysqlClient` values.
|
|
49
|
+
*
|
|
50
|
+
* @category type IDs
|
|
51
|
+
* @since 4.0.0
|
|
19
52
|
*/
|
|
20
53
|
export declare const TypeId: TypeId;
|
|
21
54
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
55
|
+
* Type-level identifier used to mark `MysqlClient` values.
|
|
56
|
+
*
|
|
57
|
+
* @category type IDs
|
|
58
|
+
* @since 4.0.0
|
|
24
59
|
*/
|
|
25
60
|
export type TypeId = "~@effect/sql-mysql2/MysqlClient";
|
|
26
61
|
/**
|
|
62
|
+
* mysql2-backed SQL client service, extending `SqlClient` with its runtime type marker and client configuration.
|
|
63
|
+
*
|
|
27
64
|
* @category models
|
|
28
|
-
* @since
|
|
65
|
+
* @since 4.0.0
|
|
29
66
|
*/
|
|
30
67
|
export interface MysqlClient extends Client.SqlClient {
|
|
31
68
|
readonly [TypeId]: TypeId;
|
|
32
69
|
readonly config: MysqlClientConfig;
|
|
33
70
|
}
|
|
34
71
|
/**
|
|
72
|
+
* Context tag used to access the `MysqlClient` service.
|
|
73
|
+
*
|
|
35
74
|
* @category tags
|
|
36
|
-
* @since
|
|
75
|
+
* @since 4.0.0
|
|
37
76
|
*/
|
|
38
|
-
export declare const MysqlClient:
|
|
77
|
+
export declare const MysqlClient: Context.Service<MysqlClient, MysqlClient>;
|
|
39
78
|
/**
|
|
79
|
+
* Configuration for a mysql2 client, including connection URI or connection fields, pool options, span attributes, and query/result name transforms.
|
|
80
|
+
*
|
|
40
81
|
* @category models
|
|
41
|
-
* @since
|
|
82
|
+
* @since 4.0.0
|
|
42
83
|
*/
|
|
43
84
|
export interface MysqlClientConfig {
|
|
44
85
|
/**
|
|
@@ -58,23 +99,31 @@ export interface MysqlClientConfig {
|
|
|
58
99
|
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
59
100
|
}
|
|
60
101
|
/**
|
|
102
|
+
* Creates a scoped MySQL client backed by a managed mysql2 pool, verifying connectivity and supporting streaming queries through mysql2 query streams.
|
|
103
|
+
*
|
|
61
104
|
* @category constructors
|
|
62
|
-
* @since
|
|
105
|
+
* @since 4.0.0
|
|
63
106
|
*/
|
|
64
107
|
export declare const make: (options: MysqlClientConfig) => Effect.Effect<MysqlClient, SqlError, Scope | Reactivity.Reactivity>;
|
|
65
108
|
/**
|
|
109
|
+
* Creates a layer from a `Config`-wrapped MySQL client configuration, providing both `MysqlClient` and `SqlClient`.
|
|
110
|
+
*
|
|
66
111
|
* @category layers
|
|
67
|
-
* @since
|
|
112
|
+
* @since 4.0.0
|
|
68
113
|
*/
|
|
69
114
|
export declare const layerConfig: (config: Config.Wrap<MysqlClientConfig>) => Layer.Layer<MysqlClient | Client.SqlClient, Config.ConfigError | SqlError>;
|
|
70
115
|
/**
|
|
116
|
+
* Creates a layer from a concrete MySQL client configuration, providing both `MysqlClient` and `SqlClient`.
|
|
117
|
+
*
|
|
71
118
|
* @category layers
|
|
72
|
-
* @since
|
|
119
|
+
* @since 4.0.0
|
|
73
120
|
*/
|
|
74
121
|
export declare const layer: (config: MysqlClientConfig) => Layer.Layer<MysqlClient | Client.SqlClient, Config.ConfigError | SqlError>;
|
|
75
122
|
/**
|
|
123
|
+
* Creates the MySQL statement compiler, using `?` placeholders and backtick-escaped identifiers.
|
|
124
|
+
*
|
|
76
125
|
* @category compiler
|
|
77
|
-
* @since
|
|
126
|
+
* @since 4.0.0
|
|
78
127
|
*/
|
|
79
128
|
export declare const makeCompiler: (transform?: (_: string) => string) => Statement.Compiler;
|
|
80
129
|
//# sourceMappingURL=MysqlClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MysqlClient.d.ts","sourceRoot":"","sources":["../src/MysqlClient.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"MysqlClient.d.ts","sourceRoot":"","sources":["../src/MysqlClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AAEzC,OAAO,KAAK,UAAU,MAAM,uCAAuC,CAAA;AACnE,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EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;IAE7D,QAAQ,CAAC,oBAAoB,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,SAAS,CAAA;IACrE,QAAQ,CAAC,mBAAmB,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,SAAS,CAAA;CACrE;AAED;;;;;GAKG;AACH,eAAO,MAAM,IAAI,GACf,SAAS,iBAAiB,KACzB,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,QAAQ,EAAE,KAAK,GAAG,UAAU,CAAC,UAAU,CAkMjE,CAAA;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GACtB,QAAQ,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,KACrC,KAAK,CAAC,KAAK,CAAC,WAAW,GAAG,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,WAAW,GAAG,QAAQ,CAUnC,CAAA;AAEzC;;;;;GAKG;AACH,eAAO,MAAM,KAAK,GAChB,QAAQ,iBAAiB,KACxB,KAAK,CAAC,KAAK,CAAC,WAAW,GAAG,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,WAAW,GAAG,QAAQ,CAMnC,CAAA;AAEzC;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,YAAY,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,uBAiB1D,CAAA"}
|
package/dist/MysqlClient.js
CHANGED
|
@@ -1,16 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* MySQL adapter for Effect SQL, backed by the `mysql2` driver.
|
|
3
|
+
*
|
|
4
|
+
* This module provides constructors and layers for a {@link MysqlClient} and
|
|
5
|
+
* the generic Effect SQL client service. Use it in server applications,
|
|
6
|
+
* background workers, migrations, and tests that need MySQL query compilation,
|
|
7
|
+
* scoped pool management, streaming queries, and consistent `SqlError`
|
|
8
|
+
* classification for mysql2 driver failures.
|
|
9
|
+
*
|
|
10
|
+
* **Mental model**
|
|
11
|
+
*
|
|
12
|
+
* {@link make} allocates a mysql2 pool inside the current scope, verifies it
|
|
13
|
+
* with `SELECT 1`, and closes it when the scope is released. Regular queries
|
|
14
|
+
* use the shared pool, transactions acquire a dedicated pooled connection for
|
|
15
|
+
* their lifetime, and streams stay tied to mysql2 stream resources until they
|
|
16
|
+
* are consumed or closed.
|
|
17
|
+
*
|
|
18
|
+
* **Common tasks**
|
|
19
|
+
*
|
|
20
|
+
* Use {@link layer} when configuration is already available, or
|
|
21
|
+
* {@link layerConfig} when it should be read through `Config`. Pass `url` for a
|
|
22
|
+
* connection URI, or pass `host`, `port`, `database`, `username`, and
|
|
23
|
+
* `password` for discrete connection settings. Use `poolConfig` for additional
|
|
24
|
+
* mysql2 pool options when configuring discrete fields.
|
|
25
|
+
*
|
|
26
|
+
* **Gotchas**
|
|
27
|
+
*
|
|
28
|
+
* When `url` is supplied it takes precedence over the discrete connection
|
|
29
|
+
* fields and does not merge in `poolConfig`. Long-running transactions and
|
|
30
|
+
* streams occupy pool capacity, so tune `maxConnections` and `connectionTTL`
|
|
31
|
+
* for those workloads.
|
|
32
|
+
*
|
|
33
|
+
* @since 4.0.0
|
|
3
34
|
*/
|
|
4
35
|
import * as Config from "effect/Config";
|
|
36
|
+
import * as Context from "effect/Context";
|
|
5
37
|
import * as Duration from "effect/Duration";
|
|
6
38
|
import * as Effect from "effect/Effect";
|
|
7
39
|
import * as Layer from "effect/Layer";
|
|
8
40
|
import * as Redacted from "effect/Redacted";
|
|
9
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
10
41
|
import * as Stream from "effect/Stream";
|
|
11
42
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
12
43
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
13
|
-
import { SqlError } from "effect/unstable/sql/SqlError";
|
|
44
|
+
import { AuthenticationError, AuthorizationError, ConnectionError, ConstraintError, DeadlockError, LockTimeoutError, SqlError, SqlSyntaxError, StatementTimeoutError, UniqueViolation, UnknownError } from "effect/unstable/sql/SqlError";
|
|
14
45
|
import { asyncPauseResume } from "effect/unstable/sql/SqlStream";
|
|
15
46
|
import * as Statement from "effect/unstable/sql/Statement";
|
|
16
47
|
import * as Mysql from "mysql2";
|
|
@@ -18,19 +49,109 @@ const ATTR_DB_SYSTEM_NAME = "db.system.name";
|
|
|
18
49
|
const ATTR_DB_NAMESPACE = "db.namespace";
|
|
19
50
|
const ATTR_SERVER_ADDRESS = "server.address";
|
|
20
51
|
const ATTR_SERVER_PORT = "server.port";
|
|
52
|
+
const mysqlErrnoFromCause = cause => {
|
|
53
|
+
if (typeof cause !== "object" || cause === null || !("errno" in cause)) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const errno = cause.errno;
|
|
57
|
+
return typeof errno === "number" ? errno : undefined;
|
|
58
|
+
};
|
|
59
|
+
const mysqlConnectionErrorCodes = /*#__PURE__*/new Set([1040, 1042, 1043, 1129, 1130, 1203]);
|
|
60
|
+
const mysqlAuthorizationErrorCodes = /*#__PURE__*/new Set([1044, 1142, 1143, 1227]);
|
|
61
|
+
const mysqlSyntaxErrorCodes = /*#__PURE__*/new Set([1054, 1064, 1146]);
|
|
62
|
+
const mysqlConstraintErrorCodes = /*#__PURE__*/new Set([1022, 1048, 1169, 1216, 1217, 1451, 1452, 1557]);
|
|
63
|
+
const UNKNOWN_CONSTRAINT = "unknown";
|
|
64
|
+
const normalizeConstraintIdentifier = identifier => {
|
|
65
|
+
if (typeof identifier !== "string") {
|
|
66
|
+
return UNKNOWN_CONSTRAINT;
|
|
67
|
+
}
|
|
68
|
+
const trimmed = identifier.trim();
|
|
69
|
+
return trimmed.length === 0 ? UNKNOWN_CONSTRAINT : trimmed;
|
|
70
|
+
};
|
|
71
|
+
const mysqlCauseProperty = (cause, property) => {
|
|
72
|
+
if (typeof cause !== "object" || cause === null || !(property in cause)) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
return cause[property];
|
|
76
|
+
};
|
|
77
|
+
const mysqlDuplicateEntryConstraintFromMessage = message => {
|
|
78
|
+
if (typeof message !== "string") {
|
|
79
|
+
return UNKNOWN_CONSTRAINT;
|
|
80
|
+
}
|
|
81
|
+
const match = /\bfor key\s+(?:'([^']*)'|\x60([^\x60]*)\x60|([^\s'\x60]+))/i.exec(message);
|
|
82
|
+
return match === null ? UNKNOWN_CONSTRAINT : normalizeConstraintIdentifier(match[1] ?? match[2] ?? match[3]);
|
|
83
|
+
};
|
|
84
|
+
const mysqlDuplicateEntryConstraintFromCause = cause => {
|
|
85
|
+
const constraint = normalizeConstraintIdentifier(mysqlCauseProperty(cause, "constraint"));
|
|
86
|
+
if (constraint !== UNKNOWN_CONSTRAINT) {
|
|
87
|
+
return constraint;
|
|
88
|
+
}
|
|
89
|
+
const sqlMessageConstraint = mysqlDuplicateEntryConstraintFromMessage(mysqlCauseProperty(cause, "sqlMessage"));
|
|
90
|
+
if (sqlMessageConstraint !== UNKNOWN_CONSTRAINT) {
|
|
91
|
+
return sqlMessageConstraint;
|
|
92
|
+
}
|
|
93
|
+
return mysqlDuplicateEntryConstraintFromMessage(mysqlCauseProperty(cause, "message"));
|
|
94
|
+
};
|
|
95
|
+
const classifyError = (cause, message, operation) => {
|
|
96
|
+
const props = {
|
|
97
|
+
cause,
|
|
98
|
+
message,
|
|
99
|
+
operation
|
|
100
|
+
};
|
|
101
|
+
const errno = mysqlErrnoFromCause(cause);
|
|
102
|
+
if (errno !== undefined) {
|
|
103
|
+
if (mysqlConnectionErrorCodes.has(errno)) {
|
|
104
|
+
return new ConnectionError(props);
|
|
105
|
+
}
|
|
106
|
+
if (errno === 1045) {
|
|
107
|
+
return new AuthenticationError(props);
|
|
108
|
+
}
|
|
109
|
+
if (mysqlAuthorizationErrorCodes.has(errno)) {
|
|
110
|
+
return new AuthorizationError(props);
|
|
111
|
+
}
|
|
112
|
+
if (mysqlSyntaxErrorCodes.has(errno)) {
|
|
113
|
+
return new SqlSyntaxError(props);
|
|
114
|
+
}
|
|
115
|
+
if (errno === 1062) {
|
|
116
|
+
return new UniqueViolation({
|
|
117
|
+
...props,
|
|
118
|
+
constraint: mysqlDuplicateEntryConstraintFromCause(cause)
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (mysqlConstraintErrorCodes.has(errno)) {
|
|
122
|
+
return new ConstraintError(props);
|
|
123
|
+
}
|
|
124
|
+
if (errno === 1213) {
|
|
125
|
+
return new DeadlockError(props);
|
|
126
|
+
}
|
|
127
|
+
if (errno === 1205) {
|
|
128
|
+
return new LockTimeoutError(props);
|
|
129
|
+
}
|
|
130
|
+
if (errno === 3024) {
|
|
131
|
+
return new StatementTimeoutError(props);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return new UnknownError(props);
|
|
135
|
+
};
|
|
21
136
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
137
|
+
* Runtime type identifier used to mark `MysqlClient` values.
|
|
138
|
+
*
|
|
139
|
+
* @category type IDs
|
|
140
|
+
* @since 4.0.0
|
|
24
141
|
*/
|
|
25
142
|
export const TypeId = "~@effect/sql-mysql2/MysqlClient";
|
|
26
143
|
/**
|
|
144
|
+
* Context tag used to access the `MysqlClient` service.
|
|
145
|
+
*
|
|
27
146
|
* @category tags
|
|
28
|
-
* @since
|
|
147
|
+
* @since 4.0.0
|
|
29
148
|
*/
|
|
30
|
-
export const MysqlClient = /*#__PURE__*/
|
|
149
|
+
export const MysqlClient = /*#__PURE__*/Context.Service("@effect/sql-mysql2/MysqlClient");
|
|
31
150
|
/**
|
|
151
|
+
* Creates a scoped MySQL client backed by a managed mysql2 pool, verifying connectivity and supporting streaming queries through mysql2 query streams.
|
|
152
|
+
*
|
|
32
153
|
* @category constructors
|
|
33
|
-
* @since
|
|
154
|
+
* @since 4.0.0
|
|
34
155
|
*/
|
|
35
156
|
export const make = options => Effect.gen(function* () {
|
|
36
157
|
const compiler = makeCompiler(options.transformQueryNames);
|
|
@@ -42,7 +163,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
42
163
|
}
|
|
43
164
|
runRaw(sql, values, rowsAsArray = false, method = "execute") {
|
|
44
165
|
return Effect.callback(resume => {
|
|
45
|
-
;
|
|
166
|
+
const operation = method === "query" ? "executeUnprepared" : "execute";
|
|
46
167
|
this.conn[method]({
|
|
47
168
|
sql,
|
|
48
169
|
values,
|
|
@@ -50,8 +171,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
50
171
|
}, (cause, results, _fields) => {
|
|
51
172
|
if (cause) {
|
|
52
173
|
resume(Effect.fail(new SqlError({
|
|
53
|
-
cause,
|
|
54
|
-
message: "Failed to execute statement"
|
|
174
|
+
reason: classifyError(cause, "Failed to execute statement", operation)
|
|
55
175
|
})));
|
|
56
176
|
} else {
|
|
57
177
|
resume(Effect.succeed(results));
|
|
@@ -102,8 +222,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
102
222
|
pool.query("SELECT 1", cause => {
|
|
103
223
|
if (cause) {
|
|
104
224
|
resume(Effect.fail(new SqlError({
|
|
105
|
-
cause,
|
|
106
|
-
message: "MysqlClient: Failed to connect"
|
|
225
|
+
reason: classifyError(cause, "MysqlClient: Failed to connect", "connect")
|
|
107
226
|
})));
|
|
108
227
|
} else {
|
|
109
228
|
resume(Effect.void);
|
|
@@ -113,9 +232,12 @@ export const make = options => Effect.gen(function* () {
|
|
|
113
232
|
pool.end(() => resume(Effect.void));
|
|
114
233
|
})).pipe(Effect.timeoutOrElse({
|
|
115
234
|
duration: Duration.seconds(5),
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
235
|
+
orElse: () => Effect.fail(new SqlError({
|
|
236
|
+
reason: new ConnectionError({
|
|
237
|
+
message: "MysqlClient: Connection timeout",
|
|
238
|
+
cause: new Error("connection timeout"),
|
|
239
|
+
operation: "connect"
|
|
240
|
+
})
|
|
119
241
|
}))
|
|
120
242
|
}));
|
|
121
243
|
const poolConnection = new ConnectionImpl(pool);
|
|
@@ -123,8 +245,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
123
245
|
pool.getConnection((cause, conn) => {
|
|
124
246
|
if (cause) {
|
|
125
247
|
resume(Effect.fail(new SqlError({
|
|
126
|
-
cause,
|
|
127
|
-
message: "Failed to acquire connection"
|
|
248
|
+
reason: classifyError(cause, "Failed to acquire connection", "acquireConnection")
|
|
128
249
|
})));
|
|
129
250
|
} else {
|
|
130
251
|
resume(Effect.succeed(conn));
|
|
@@ -148,18 +269,24 @@ export const make = options => Effect.gen(function* () {
|
|
|
148
269
|
});
|
|
149
270
|
});
|
|
150
271
|
/**
|
|
272
|
+
* Creates a layer from a `Config`-wrapped MySQL client configuration, providing both `MysqlClient` and `SqlClient`.
|
|
273
|
+
*
|
|
151
274
|
* @category layers
|
|
152
|
-
* @since
|
|
275
|
+
* @since 4.0.0
|
|
153
276
|
*/
|
|
154
|
-
export const layerConfig = config => Layer.
|
|
277
|
+
export const layerConfig = config => Layer.effectContext(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));
|
|
155
278
|
/**
|
|
279
|
+
* Creates a layer from a concrete MySQL client configuration, providing both `MysqlClient` and `SqlClient`.
|
|
280
|
+
*
|
|
156
281
|
* @category layers
|
|
157
|
-
* @since
|
|
282
|
+
* @since 4.0.0
|
|
158
283
|
*/
|
|
159
|
-
export const layer = config => Layer.
|
|
284
|
+
export const layer = config => Layer.effectContext(Effect.map(make(config), client => Context.make(MysqlClient, client).pipe(Context.add(Client.SqlClient, client)))).pipe(Layer.provide(Reactivity.layer));
|
|
160
285
|
/**
|
|
286
|
+
* Creates the MySQL statement compiler, using `?` placeholders and backtick-escaped identifiers.
|
|
287
|
+
*
|
|
161
288
|
* @category compiler
|
|
162
|
-
* @since
|
|
289
|
+
* @since 4.0.0
|
|
163
290
|
*/
|
|
164
291
|
export const makeCompiler = transform => Statement.makeCompiler({
|
|
165
292
|
dialect: "mysql",
|
|
@@ -184,8 +311,7 @@ function queryStream(conn, sql, params) {
|
|
|
184
311
|
let buffer = [];
|
|
185
312
|
let taskPending = false;
|
|
186
313
|
query.on("error", cause => emit.fail(new SqlError({
|
|
187
|
-
cause,
|
|
188
|
-
message: "Failed to stream statement"
|
|
314
|
+
reason: classifyError(cause, "Failed to stream statement", "stream")
|
|
189
315
|
})));
|
|
190
316
|
query.on("data", row => {
|
|
191
317
|
buffer.push(row);
|
package/dist/MysqlClient.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MysqlClient.js","names":["Config","Duration","Effect","Layer","Redacted","ServiceMap","Stream","Reactivity","Client","SqlError","asyncPauseResume","Statement","Mysql","ATTR_DB_SYSTEM_NAME","ATTR_DB_NAMESPACE","ATTR_SERVER_ADDRESS","ATTR_SERVER_PORT","TypeId","MysqlClient","Service","make","options","gen","compiler","makeCompiler","transformQueryNames","transformRows","transformResultNames","defaultTransforms","array","undefined","ConnectionImpl","conn","constructor","runRaw","sql","values","rowsAsArray","method","callback","resume","cause","results","_fields","fail","message","succeed","run","pipe","map","Array","isArray","execute","params","executeRaw","executeValues","executeUnprepared","executeStream","stream","queryStream","mapArray","_","pool","url","createPool","uri","value","multipleStatements","supportBigNumbers","connectionLimit","maxConnections","idleTimeout","connectionTTL","toMillis","fromInputUnsafe","poolConfig","host","port","database","user","username","password","acquireRelease","query","void","end","timeoutOrElse","duration","seconds","onTimeout","Error","poolConnection","acquireConn","getConnection","sync","release","transactionAcquirer","spanAttributes","Object","entries","push","assign","acquirer","config","layerConfig","effectServices","unwrap","asEffect","flatMap","client","add","SqlClient","provide","layer","transform","dialect","placeholder","onIdentifier","withoutTransform","escape","onCustom","onRecordUpdate","defaultEscape","fnUntraced","emit","addFinalizer","destroy","buffer","taskPending","on","row","queueMicrotask","items","length","onPause","pause","onResume"],"sources":["../src/MysqlClient.ts"],"sourcesContent":[null],"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AAC8C,EAAE,CAAC,MAAM,EAAGC,GAAQ,IAAI;MAC5BH,MAAM,CAAC3B,IAAI,CAAC8B,GAAG,CAAC;MAChB,IAAI,CAACF,WAAW,EAAE;QAChBA,WAAW,GAAG,IAAI;QAClBG,cAAc,CAAC,MAAK;UAClB,MAAMC,KAAK,GAAGL,MAAM;UACpBA,MAAM,GAAG,EAAE;UACXH,IAAI,CAAC/F,KAAK,CAACuG,KAAK,CAAC;UACjBJ,WAAW,GAAG,KAAK;QACrB,CAAC,CAAC;MACJ;IACF,CAAC,CAAC;IACF7C,KAAK,CAAC8C,EAAE,CAAC,KAAK,EAAE,MAAK;MACnB,IAAIF,MAAM,CAACM,MAAM,GAAG,CAAC,EAAE;QACrBT,IAAI,CAAC/F,KAAK,CAACkG,MAAM,CAAC;QAClBA,MAAM,GAAG,EAAE;MACb;MACAH,IAAI,CAACvC,GAAG,EAAE;IACZ,CAAC,CAAC;IAEF,OAAO;MACLiD,OAAOA,CAAA;QACLnD,KAAK,CAACoD,KAAK,EAAE;MACf,CAAC;MACDC,QAAQA,CAAA;QACNrD,KAAK,CAAC3C,MAAM,EAAE;MAChB;KACD;EACH,CAAC,CAAC,CAAC;AACL","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"MysqlClient.js","names":["Config","Context","Duration","Effect","Layer","Redacted","Stream","Reactivity","Client","AuthenticationError","AuthorizationError","ConnectionError","ConstraintError","DeadlockError","LockTimeoutError","SqlError","SqlSyntaxError","StatementTimeoutError","UniqueViolation","UnknownError","asyncPauseResume","Statement","Mysql","ATTR_DB_SYSTEM_NAME","ATTR_DB_NAMESPACE","ATTR_SERVER_ADDRESS","ATTR_SERVER_PORT","mysqlErrnoFromCause","cause","undefined","errno","mysqlConnectionErrorCodes","Set","mysqlAuthorizationErrorCodes","mysqlSyntaxErrorCodes","mysqlConstraintErrorCodes","UNKNOWN_CONSTRAINT","normalizeConstraintIdentifier","identifier","trimmed","trim","length","mysqlCauseProperty","property","mysqlDuplicateEntryConstraintFromMessage","message","match","exec","mysqlDuplicateEntryConstraintFromCause","constraint","sqlMessageConstraint","classifyError","operation","props","has","TypeId","MysqlClient","Service","make","options","gen","compiler","makeCompiler","transformQueryNames","transformRows","transformResultNames","defaultTransforms","array","ConnectionImpl","conn","constructor","runRaw","sql","values","rowsAsArray","method","callback","resume","results","_fields","fail","reason","succeed","run","pipe","map","Array","isArray","execute","params","executeRaw","executeValues","executeUnprepared","executeStream","stream","queryStream","mapArray","_","pool","url","createPool","uri","value","multipleStatements","supportBigNumbers","connectionLimit","maxConnections","idleTimeout","connectionTTL","toMillis","fromInputUnsafe","poolConfig","host","port","database","user","username","password","acquireRelease","query","void","end","timeoutOrElse","duration","seconds","orElse","Error","poolConnection","acquireConn","getConnection","sync","release","transactionAcquirer","spanAttributes","Object","entries","push","assign","acquirer","config","layerConfig","effectContext","unwrap","flatMap","client","add","SqlClient","provide","layer","transform","dialect","placeholder","onIdentifier","withoutTransform","escape","onCustom","onRecordUpdate","defaultEscape","fnUntraced","emit","addFinalizer","destroy","buffer","taskPending","on","row","queueMicrotask","items","onPause","pause","onResume"],"sources":["../src/MysqlClient.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,OAAO,KAAKA,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,QAAQ,MAAM,iBAAiB;AAC3C,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,QAAQ,MAAM,iBAAiB;AAE3C,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,UAAU,MAAM,uCAAuC;AACnE,OAAO,KAAKC,MAAM,MAAM,+BAA+B;AAEvD,SACEC,mBAAmB,EACnBC,kBAAkB,EAClBC,eAAe,EACfC,eAAe,EACfC,aAAa,EACbC,gBAAgB,EAChBC,QAAQ,EAC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AAK,CAAC6C,EAAE,CAAC,KAAK,EAAE,MAAK;MACnB,IAAIF,MAAM,CAACzH,MAAM,GAAG,CAAC,EAAE;QACrBsH,IAAI,CAAC5F,KAAK,CAAC+F,MAAM,CAAC;QAClBA,MAAM,GAAG,EAAE;MACb;MACAH,IAAI,CAACtC,GAAG,EAAE;IACZ,CAAC,CAAC;IAEF,OAAO;MACL+C,OAAOA,CAAA;QACLjD,KAAK,CAACkD,KAAK,EAAE;MACf,CAAC;MACDC,QAAQA,CAAA;QACNnD,KAAK,CAAC1C,MAAM,EAAE;MAChB;KACD;EACH,CAAC,CAAC,CAAC;AACL","ignoreList":[]}
|
package/dist/MysqlMigrator.d.ts
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* MySQL adapter for the shared Effect SQL migration runner.
|
|
3
|
+
*
|
|
4
|
+
* This module re-exports the shared `Migrator` loaders and error types, then
|
|
5
|
+
* provides `run` and `layer` helpers that apply ordered migrations through the
|
|
6
|
+
* current mysql2-backed `SqlClient`. Use it during application startup,
|
|
7
|
+
* deployment, or integration tests when MySQL schema changes must be applied
|
|
8
|
+
* before dependent services start reading or writing data.
|
|
9
|
+
*
|
|
10
|
+
* **Mental model**
|
|
11
|
+
*
|
|
12
|
+
* Migrations are loaded with the shared `<id>_<name>` convention and recorded in
|
|
13
|
+
* `effect_sql_migrations` by default. The runner reads the latest recorded id
|
|
14
|
+
* and applies only migrations with larger ids, returning the ids and names that
|
|
15
|
+
* were applied in the current run.
|
|
16
|
+
*
|
|
17
|
+
* **Common tasks**
|
|
18
|
+
*
|
|
19
|
+
* - Call `run` when an effect should explicitly migrate a MySQL database.
|
|
20
|
+
* - Use `layer` when schema setup should happen while building a layer graph.
|
|
21
|
+
* - Reuse the shared `Migrator` loaders from this module for file-based or
|
|
22
|
+
* in-memory migration definitions.
|
|
23
|
+
*
|
|
24
|
+
* **Gotchas**
|
|
25
|
+
*
|
|
26
|
+
* MySQL DDL can cause implicit commits, and this adapter relies on migration
|
|
27
|
+
* table constraints to detect concurrent runners. Coordinate startup runners and
|
|
28
|
+
* write migrations to tolerate MySQL's transactional semantics. Schema dump
|
|
29
|
+
* support is not enabled in this adapter, so `schemaDirectory` does not emit a
|
|
30
|
+
* MySQL dump.
|
|
31
|
+
*
|
|
32
|
+
* @since 4.0.0
|
|
3
33
|
*/
|
|
4
34
|
import type * as Effect from "effect/Effect";
|
|
5
35
|
import * as Layer from "effect/Layer";
|
|
@@ -7,17 +37,21 @@ import * as Migrator from "effect/unstable/sql/Migrator";
|
|
|
7
37
|
import type * as Client from "effect/unstable/sql/SqlClient";
|
|
8
38
|
import type { SqlError } from "effect/unstable/sql/SqlError";
|
|
9
39
|
/**
|
|
10
|
-
* @since
|
|
40
|
+
* @since 4.0.0
|
|
11
41
|
*/
|
|
12
42
|
export * from "effect/unstable/sql/Migrator";
|
|
13
43
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
44
|
+
* Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
|
|
45
|
+
*
|
|
46
|
+
* @category constructors
|
|
47
|
+
* @since 4.0.0
|
|
16
48
|
*/
|
|
17
49
|
export declare const run: <R2 = never>({ loader, schemaDirectory, table }: Migrator.MigratorOptions<R2>) => Effect.Effect<ReadonlyArray<readonly [id: number, name: string]>, Migrator.MigrationError | SqlError, Client.SqlClient | R2>;
|
|
18
50
|
/**
|
|
51
|
+
* Creates a layer that runs the configured SQL migrations during layer construction.
|
|
52
|
+
*
|
|
19
53
|
* @category layers
|
|
20
|
-
* @since
|
|
54
|
+
* @since 4.0.0
|
|
21
55
|
*/
|
|
22
56
|
export declare const layer: <R>(options: Migrator.MigratorOptions<R>) => Layer.Layer<never, Migrator.MigrationError | SqlError, Client.SqlClient | R>;
|
|
23
57
|
//# sourceMappingURL=MysqlMigrator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MysqlMigrator.d.ts","sourceRoot":"","sources":["../src/MysqlMigrator.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"MysqlMigrator.d.ts","sourceRoot":"","sources":["../src/MysqlMigrator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAA;AAC5C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,8BAA8B,CAAA;AACxD,OAAO,KAAK,KAAK,MAAM,MAAM,+BAA+B,CAAA;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAE5D;;GAEG;AACH,cAAc,8BAA8B,CAAA;AAE5C;;;;;GAKG;AACH,eAAO,MAAM,GAAG,EAAE,CAAC,EAAE,GAAG,KAAK,EAC3B,EAAE,MAAM,EAAE,eAAe,EAAE,KAAK,EAAE,EAAE,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,KAC7D,MAAM,CAAC,MAAM,CAChB,aAAa,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,EAClD,QAAQ,CAAC,cAAc,GAAG,QAAQ,EAClC,MAAM,CAAC,SAAS,GAAG,EAAE,CAsDrB,CAAA;AAEF;;;;;GAKG;AACH,eAAO,MAAM,KAAK,GAAI,CAAC,EACrB,SAAS,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,KACnC,KAAK,CAAC,KAAK,CACZ,KAAK,EACL,QAAQ,CAAC,cAAc,GAAG,QAAQ,EAClC,MAAM,CAAC,SAAS,GAAG,CAAC,CACgB,CAAA"}
|
package/dist/MysqlMigrator.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import * as Layer from "effect/Layer";
|
|
2
2
|
import * as Migrator from "effect/unstable/sql/Migrator";
|
|
3
3
|
/**
|
|
4
|
-
* @since
|
|
4
|
+
* @since 4.0.0
|
|
5
5
|
*/
|
|
6
6
|
export * from "effect/unstable/sql/Migrator";
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
|
|
9
|
+
*
|
|
10
|
+
* @category constructors
|
|
11
|
+
* @since 4.0.0
|
|
10
12
|
*/
|
|
11
13
|
export const run = /*#__PURE__*/Migrator.make({
|
|
12
14
|
// TODO: re-add when Command module is available
|
|
@@ -63,8 +65,10 @@ export const run = /*#__PURE__*/Migrator.make({
|
|
|
63
65
|
// }
|
|
64
66
|
});
|
|
65
67
|
/**
|
|
68
|
+
* Creates a layer that runs the configured SQL migrations during layer construction.
|
|
69
|
+
*
|
|
66
70
|
* @category layers
|
|
67
|
-
* @since
|
|
71
|
+
* @since 4.0.0
|
|
68
72
|
*/
|
|
69
73
|
export const layer = options => Layer.effectDiscard(run(options));
|
|
70
74
|
//# sourceMappingURL=MysqlMigrator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MysqlMigrator.js","names":["Layer","Migrator","run","make","layer","options","effectDiscard"],"sources":["../src/MysqlMigrator.ts"],"sourcesContent":[null],"mappings":"
|
|
1
|
+
{"version":3,"file":"MysqlMigrator.js","names":["Layer","Migrator","run","make","layer","options","effectDiscard"],"sources":["../src/MysqlMigrator.ts"],"sourcesContent":[null],"mappings":"AAkCA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,QAAQ,MAAM,8BAA8B;AAIxD;;;AAGA,cAAc,8BAA8B;AAE5C;;;;;;AAMA,OAAO,MAAMC,GAAG,gBAMZD,QAAQ,CAACE,IAAI,CAAC;EAChB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;AAAA,CACD,CAAC;AAEF;;;;;;AAMA,OAAO,MAAMC,KAAK,GAChBC,OAAoC,IAKjCL,KAAK,CAACM,aAAa,CAACJ,GAAG,CAACG,OAAO,CAAC,CAAC","ignoreList":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
* @since
|
|
5
|
+
* @since 4.0.0
|
|
6
6
|
*/
|
|
7
7
|
export * as MysqlClient from "./MysqlClient.ts";
|
|
8
8
|
/**
|
|
9
|
-
* @since
|
|
9
|
+
* @since 4.0.0
|
|
10
10
|
*/
|
|
11
11
|
export * as MysqlMigrator from "./MysqlMigrator.ts";
|
|
12
12
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
// @barrel: Auto-generated exports. Do not edit manually.
|
|
5
5
|
/**
|
|
6
|
-
* @since
|
|
6
|
+
* @since 4.0.0
|
|
7
7
|
*/
|
|
8
8
|
export * as MysqlClient from "./MysqlClient.js";
|
|
9
9
|
/**
|
|
10
|
-
* @since
|
|
10
|
+
* @since 4.0.0
|
|
11
11
|
*/
|
|
12
12
|
export * as MysqlMigrator from "./MysqlMigrator.js";
|
|
13
13
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/sql-mysql2",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.71",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A MySQL toolkit for Effect",
|
|
@@ -43,14 +43,14 @@
|
|
|
43
43
|
"provenance": true
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@testcontainers/mysql": "^11.
|
|
47
|
-
"effect": "^4.0.0-beta.
|
|
46
|
+
"@testcontainers/mysql": "^11.14.0",
|
|
47
|
+
"effect": "^4.0.0-beta.71"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"effect": "^4.0.0-beta.
|
|
50
|
+
"effect": "^4.0.0-beta.71"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"mysql2": "^3.
|
|
53
|
+
"mysql2": "^3.22.3"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"codegen": "effect-utils codegen",
|
package/src/MysqlClient.ts
CHANGED
|
@@ -1,18 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* MySQL adapter for Effect SQL, backed by the `mysql2` driver.
|
|
3
|
+
*
|
|
4
|
+
* This module provides constructors and layers for a {@link MysqlClient} and
|
|
5
|
+
* the generic Effect SQL client service. Use it in server applications,
|
|
6
|
+
* background workers, migrations, and tests that need MySQL query compilation,
|
|
7
|
+
* scoped pool management, streaming queries, and consistent `SqlError`
|
|
8
|
+
* classification for mysql2 driver failures.
|
|
9
|
+
*
|
|
10
|
+
* **Mental model**
|
|
11
|
+
*
|
|
12
|
+
* {@link make} allocates a mysql2 pool inside the current scope, verifies it
|
|
13
|
+
* with `SELECT 1`, and closes it when the scope is released. Regular queries
|
|
14
|
+
* use the shared pool, transactions acquire a dedicated pooled connection for
|
|
15
|
+
* their lifetime, and streams stay tied to mysql2 stream resources until they
|
|
16
|
+
* are consumed or closed.
|
|
17
|
+
*
|
|
18
|
+
* **Common tasks**
|
|
19
|
+
*
|
|
20
|
+
* Use {@link layer} when configuration is already available, or
|
|
21
|
+
* {@link layerConfig} when it should be read through `Config`. Pass `url` for a
|
|
22
|
+
* connection URI, or pass `host`, `port`, `database`, `username`, and
|
|
23
|
+
* `password` for discrete connection settings. Use `poolConfig` for additional
|
|
24
|
+
* mysql2 pool options when configuring discrete fields.
|
|
25
|
+
*
|
|
26
|
+
* **Gotchas**
|
|
27
|
+
*
|
|
28
|
+
* When `url` is supplied it takes precedence over the discrete connection
|
|
29
|
+
* fields and does not merge in `poolConfig`. Long-running transactions and
|
|
30
|
+
* streams occupy pool capacity, so tune `maxConnections` and `connectionTTL`
|
|
31
|
+
* for those workloads.
|
|
32
|
+
*
|
|
33
|
+
* @since 4.0.0
|
|
3
34
|
*/
|
|
4
35
|
import * as Config from "effect/Config"
|
|
36
|
+
import * as Context from "effect/Context"
|
|
5
37
|
import * as Duration from "effect/Duration"
|
|
6
38
|
import * as Effect from "effect/Effect"
|
|
7
39
|
import * as Layer from "effect/Layer"
|
|
8
40
|
import * as Redacted from "effect/Redacted"
|
|
9
41
|
import type { Scope } from "effect/Scope"
|
|
10
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
11
42
|
import * as Stream from "effect/Stream"
|
|
12
43
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
13
44
|
import * as Client from "effect/unstable/sql/SqlClient"
|
|
14
45
|
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
15
|
-
import {
|
|
46
|
+
import {
|
|
47
|
+
AuthenticationError,
|
|
48
|
+
AuthorizationError,
|
|
49
|
+
ConnectionError,
|
|
50
|
+
ConstraintError,
|
|
51
|
+
DeadlockError,
|
|
52
|
+
LockTimeoutError,
|
|
53
|
+
SqlError,
|
|
54
|
+
SqlSyntaxError,
|
|
55
|
+
StatementTimeoutError,
|
|
56
|
+
UniqueViolation,
|
|
57
|
+
UnknownError
|
|
58
|
+
} from "effect/unstable/sql/SqlError"
|
|
16
59
|
import { asyncPauseResume } from "effect/unstable/sql/SqlStream"
|
|
17
60
|
import * as Statement from "effect/unstable/sql/Statement"
|
|
18
61
|
import * as Mysql from "mysql2"
|
|
@@ -22,21 +65,118 @@ const ATTR_DB_NAMESPACE = "db.namespace"
|
|
|
22
65
|
const ATTR_SERVER_ADDRESS = "server.address"
|
|
23
66
|
const ATTR_SERVER_PORT = "server.port"
|
|
24
67
|
|
|
68
|
+
const mysqlErrnoFromCause = (cause: unknown): number | undefined => {
|
|
69
|
+
if (typeof cause !== "object" || cause === null || !("errno" in cause)) {
|
|
70
|
+
return undefined
|
|
71
|
+
}
|
|
72
|
+
const errno = cause.errno
|
|
73
|
+
return typeof errno === "number" ? errno : undefined
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const mysqlConnectionErrorCodes = new Set([1040, 1042, 1043, 1129, 1130, 1203])
|
|
77
|
+
const mysqlAuthorizationErrorCodes = new Set([1044, 1142, 1143, 1227])
|
|
78
|
+
const mysqlSyntaxErrorCodes = new Set([1054, 1064, 1146])
|
|
79
|
+
const mysqlConstraintErrorCodes = new Set([1022, 1048, 1169, 1216, 1217, 1451, 1452, 1557])
|
|
80
|
+
|
|
81
|
+
const UNKNOWN_CONSTRAINT = "unknown"
|
|
82
|
+
|
|
83
|
+
const normalizeConstraintIdentifier = (identifier: unknown): string => {
|
|
84
|
+
if (typeof identifier !== "string") {
|
|
85
|
+
return UNKNOWN_CONSTRAINT
|
|
86
|
+
}
|
|
87
|
+
const trimmed = identifier.trim()
|
|
88
|
+
return trimmed.length === 0 ? UNKNOWN_CONSTRAINT : trimmed
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const mysqlCauseProperty = (cause: unknown, property: "constraint" | "message" | "sqlMessage"): unknown => {
|
|
92
|
+
if (typeof cause !== "object" || cause === null || !(property in cause)) {
|
|
93
|
+
return undefined
|
|
94
|
+
}
|
|
95
|
+
return (cause as Record<string, unknown>)[property]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const mysqlDuplicateEntryConstraintFromMessage = (message: unknown): string => {
|
|
99
|
+
if (typeof message !== "string") {
|
|
100
|
+
return UNKNOWN_CONSTRAINT
|
|
101
|
+
}
|
|
102
|
+
const match = /\bfor key\s+(?:'([^']*)'|\x60([^\x60]*)\x60|([^\s'\x60]+))/i.exec(message)
|
|
103
|
+
return match === null ?
|
|
104
|
+
UNKNOWN_CONSTRAINT :
|
|
105
|
+
normalizeConstraintIdentifier(match[1] ?? match[2] ?? match[3])
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const mysqlDuplicateEntryConstraintFromCause = (cause: unknown): string => {
|
|
109
|
+
const constraint = normalizeConstraintIdentifier(mysqlCauseProperty(cause, "constraint"))
|
|
110
|
+
if (constraint !== UNKNOWN_CONSTRAINT) {
|
|
111
|
+
return constraint
|
|
112
|
+
}
|
|
113
|
+
const sqlMessageConstraint = mysqlDuplicateEntryConstraintFromMessage(mysqlCauseProperty(cause, "sqlMessage"))
|
|
114
|
+
if (sqlMessageConstraint !== UNKNOWN_CONSTRAINT) {
|
|
115
|
+
return sqlMessageConstraint
|
|
116
|
+
}
|
|
117
|
+
return mysqlDuplicateEntryConstraintFromMessage(mysqlCauseProperty(cause, "message"))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const classifyError = (
|
|
121
|
+
cause: unknown,
|
|
122
|
+
message: string,
|
|
123
|
+
operation: string
|
|
124
|
+
) => {
|
|
125
|
+
const props = { cause, message, operation }
|
|
126
|
+
const errno = mysqlErrnoFromCause(cause)
|
|
127
|
+
if (errno !== undefined) {
|
|
128
|
+
if (mysqlConnectionErrorCodes.has(errno)) {
|
|
129
|
+
return new ConnectionError(props)
|
|
130
|
+
}
|
|
131
|
+
if (errno === 1045) {
|
|
132
|
+
return new AuthenticationError(props)
|
|
133
|
+
}
|
|
134
|
+
if (mysqlAuthorizationErrorCodes.has(errno)) {
|
|
135
|
+
return new AuthorizationError(props)
|
|
136
|
+
}
|
|
137
|
+
if (mysqlSyntaxErrorCodes.has(errno)) {
|
|
138
|
+
return new SqlSyntaxError(props)
|
|
139
|
+
}
|
|
140
|
+
if (errno === 1062) {
|
|
141
|
+
return new UniqueViolation({ ...props, constraint: mysqlDuplicateEntryConstraintFromCause(cause) })
|
|
142
|
+
}
|
|
143
|
+
if (mysqlConstraintErrorCodes.has(errno)) {
|
|
144
|
+
return new ConstraintError(props)
|
|
145
|
+
}
|
|
146
|
+
if (errno === 1213) {
|
|
147
|
+
return new DeadlockError(props)
|
|
148
|
+
}
|
|
149
|
+
if (errno === 1205) {
|
|
150
|
+
return new LockTimeoutError(props)
|
|
151
|
+
}
|
|
152
|
+
if (errno === 3024) {
|
|
153
|
+
return new StatementTimeoutError(props)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return new UnknownError(props)
|
|
157
|
+
}
|
|
158
|
+
|
|
25
159
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
160
|
+
* Runtime type identifier used to mark `MysqlClient` values.
|
|
161
|
+
*
|
|
162
|
+
* @category type IDs
|
|
163
|
+
* @since 4.0.0
|
|
28
164
|
*/
|
|
29
165
|
export const TypeId: TypeId = "~@effect/sql-mysql2/MysqlClient"
|
|
30
166
|
|
|
31
167
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
168
|
+
* Type-level identifier used to mark `MysqlClient` values.
|
|
169
|
+
*
|
|
170
|
+
* @category type IDs
|
|
171
|
+
* @since 4.0.0
|
|
34
172
|
*/
|
|
35
173
|
export type TypeId = "~@effect/sql-mysql2/MysqlClient"
|
|
36
174
|
|
|
37
175
|
/**
|
|
176
|
+
* mysql2-backed SQL client service, extending `SqlClient` with its runtime type marker and client configuration.
|
|
177
|
+
*
|
|
38
178
|
* @category models
|
|
39
|
-
* @since
|
|
179
|
+
* @since 4.0.0
|
|
40
180
|
*/
|
|
41
181
|
export interface MysqlClient extends Client.SqlClient {
|
|
42
182
|
readonly [TypeId]: TypeId
|
|
@@ -44,14 +184,18 @@ export interface MysqlClient extends Client.SqlClient {
|
|
|
44
184
|
}
|
|
45
185
|
|
|
46
186
|
/**
|
|
187
|
+
* Context tag used to access the `MysqlClient` service.
|
|
188
|
+
*
|
|
47
189
|
* @category tags
|
|
48
|
-
* @since
|
|
190
|
+
* @since 4.0.0
|
|
49
191
|
*/
|
|
50
|
-
export const MysqlClient =
|
|
192
|
+
export const MysqlClient = Context.Service<MysqlClient>("@effect/sql-mysql2/MysqlClient")
|
|
51
193
|
|
|
52
194
|
/**
|
|
195
|
+
* Configuration for a mysql2 client, including connection URI or connection fields, pool options, span attributes, and query/result name transforms.
|
|
196
|
+
*
|
|
53
197
|
* @category models
|
|
54
|
-
* @since
|
|
198
|
+
* @since 4.0.0
|
|
55
199
|
*/
|
|
56
200
|
export interface MysqlClientConfig {
|
|
57
201
|
/**
|
|
@@ -77,8 +221,10 @@ export interface MysqlClientConfig {
|
|
|
77
221
|
}
|
|
78
222
|
|
|
79
223
|
/**
|
|
224
|
+
* Creates a scoped MySQL client backed by a managed mysql2 pool, verifying connectivity and supporting streaming queries through mysql2 query streams.
|
|
225
|
+
*
|
|
80
226
|
* @category constructors
|
|
81
|
-
* @since
|
|
227
|
+
* @since 4.0.0
|
|
82
228
|
*/
|
|
83
229
|
export const make = (
|
|
84
230
|
options: MysqlClientConfig
|
|
@@ -104,13 +250,16 @@ export const make = (
|
|
|
104
250
|
method: "execute" | "query" = "execute"
|
|
105
251
|
) {
|
|
106
252
|
return Effect.callback<unknown, SqlError>((resume) => {
|
|
253
|
+
const operation = method === "query" ? "executeUnprepared" : "execute"
|
|
107
254
|
;(this.conn as any)[method]({
|
|
108
255
|
sql,
|
|
109
256
|
values,
|
|
110
257
|
rowsAsArray
|
|
111
258
|
}, (cause: unknown | null, results: unknown, _fields: any) => {
|
|
112
259
|
if (cause) {
|
|
113
|
-
resume(
|
|
260
|
+
resume(
|
|
261
|
+
Effect.fail(new SqlError({ reason: classifyError(cause, "Failed to execute statement", operation) }))
|
|
262
|
+
)
|
|
114
263
|
} else {
|
|
115
264
|
resume(Effect.succeed(results))
|
|
116
265
|
}
|
|
@@ -198,8 +347,7 @@ export const make = (
|
|
|
198
347
|
if (cause) {
|
|
199
348
|
resume(Effect.fail(
|
|
200
349
|
new SqlError({
|
|
201
|
-
cause,
|
|
202
|
-
message: "MysqlClient: Failed to connect"
|
|
350
|
+
reason: classifyError(cause, "MysqlClient: Failed to connect", "connect")
|
|
203
351
|
})
|
|
204
352
|
))
|
|
205
353
|
} else {
|
|
@@ -214,11 +362,14 @@ export const make = (
|
|
|
214
362
|
).pipe(
|
|
215
363
|
Effect.timeoutOrElse({
|
|
216
364
|
duration: Duration.seconds(5),
|
|
217
|
-
|
|
365
|
+
orElse: () =>
|
|
218
366
|
Effect.fail(
|
|
219
367
|
new SqlError({
|
|
220
|
-
|
|
221
|
-
|
|
368
|
+
reason: new ConnectionError({
|
|
369
|
+
message: "MysqlClient: Connection timeout",
|
|
370
|
+
cause: new Error("connection timeout"),
|
|
371
|
+
operation: "connect"
|
|
372
|
+
})
|
|
222
373
|
})
|
|
223
374
|
)
|
|
224
375
|
})
|
|
@@ -230,7 +381,13 @@ export const make = (
|
|
|
230
381
|
Effect.callback<Mysql.PoolConnection, SqlError>((resume) => {
|
|
231
382
|
pool.getConnection((cause, conn) => {
|
|
232
383
|
if (cause) {
|
|
233
|
-
resume(
|
|
384
|
+
resume(
|
|
385
|
+
Effect.fail(
|
|
386
|
+
new SqlError({
|
|
387
|
+
reason: classifyError(cause, "Failed to acquire connection", "acquireConnection")
|
|
388
|
+
})
|
|
389
|
+
)
|
|
390
|
+
)
|
|
234
391
|
} else {
|
|
235
392
|
resume(Effect.succeed(conn))
|
|
236
393
|
}
|
|
@@ -268,40 +425,46 @@ export const make = (
|
|
|
268
425
|
})
|
|
269
426
|
|
|
270
427
|
/**
|
|
428
|
+
* Creates a layer from a `Config`-wrapped MySQL client configuration, providing both `MysqlClient` and `SqlClient`.
|
|
429
|
+
*
|
|
271
430
|
* @category layers
|
|
272
|
-
* @since
|
|
431
|
+
* @since 4.0.0
|
|
273
432
|
*/
|
|
274
433
|
export const layerConfig = (
|
|
275
434
|
config: Config.Wrap<MysqlClientConfig>
|
|
276
435
|
): Layer.Layer<MysqlClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
277
|
-
Layer.
|
|
278
|
-
Config.unwrap(config).
|
|
436
|
+
Layer.effectContext(
|
|
437
|
+
Config.unwrap(config).pipe(
|
|
279
438
|
Effect.flatMap(make),
|
|
280
439
|
Effect.map((client) =>
|
|
281
|
-
|
|
282
|
-
|
|
440
|
+
Context.make(MysqlClient, client).pipe(
|
|
441
|
+
Context.add(Client.SqlClient, client)
|
|
283
442
|
)
|
|
284
443
|
)
|
|
285
444
|
)
|
|
286
445
|
).pipe(Layer.provide(Reactivity.layer))
|
|
287
446
|
|
|
288
447
|
/**
|
|
448
|
+
* Creates a layer from a concrete MySQL client configuration, providing both `MysqlClient` and `SqlClient`.
|
|
449
|
+
*
|
|
289
450
|
* @category layers
|
|
290
|
-
* @since
|
|
451
|
+
* @since 4.0.0
|
|
291
452
|
*/
|
|
292
453
|
export const layer = (
|
|
293
454
|
config: MysqlClientConfig
|
|
294
455
|
): Layer.Layer<MysqlClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
295
|
-
Layer.
|
|
456
|
+
Layer.effectContext(
|
|
296
457
|
Effect.map(make(config), (client) =>
|
|
297
|
-
|
|
298
|
-
|
|
458
|
+
Context.make(MysqlClient, client).pipe(
|
|
459
|
+
Context.add(Client.SqlClient, client)
|
|
299
460
|
))
|
|
300
461
|
).pipe(Layer.provide(Reactivity.layer))
|
|
301
462
|
|
|
302
463
|
/**
|
|
464
|
+
* Creates the MySQL statement compiler, using `?` placeholders and backtick-escaped identifiers.
|
|
465
|
+
*
|
|
303
466
|
* @category compiler
|
|
304
|
-
* @since
|
|
467
|
+
* @since 4.0.0
|
|
305
468
|
*/
|
|
306
469
|
export const makeCompiler = (transform?: (_: string) => string) =>
|
|
307
470
|
Statement.makeCompiler({
|
|
@@ -331,11 +494,15 @@ function queryStream(
|
|
|
331
494
|
) {
|
|
332
495
|
return asyncPauseResume<any, SqlError>(Effect.fnUntraced(function*(emit) {
|
|
333
496
|
const query = (conn as any).query(sql, params).stream()
|
|
334
|
-
yield* Effect.addFinalizer(() => Effect.sync(() => query.destroy()))
|
|
497
|
+
yield* Effect.addFinalizer(() => Effect.sync(() => query.destroy() as void))
|
|
335
498
|
|
|
336
499
|
let buffer: Array<any> = []
|
|
337
500
|
let taskPending = false
|
|
338
|
-
query.on(
|
|
501
|
+
query.on(
|
|
502
|
+
"error",
|
|
503
|
+
(cause: unknown) =>
|
|
504
|
+
emit.fail(new SqlError({ reason: classifyError(cause, "Failed to stream statement", "stream") }))
|
|
505
|
+
)
|
|
339
506
|
query.on("data", (row: any) => {
|
|
340
507
|
buffer.push(row)
|
|
341
508
|
if (!taskPending) {
|
package/src/MysqlMigrator.ts
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* MySQL adapter for the shared Effect SQL migration runner.
|
|
3
|
+
*
|
|
4
|
+
* This module re-exports the shared `Migrator` loaders and error types, then
|
|
5
|
+
* provides `run` and `layer` helpers that apply ordered migrations through the
|
|
6
|
+
* current mysql2-backed `SqlClient`. Use it during application startup,
|
|
7
|
+
* deployment, or integration tests when MySQL schema changes must be applied
|
|
8
|
+
* before dependent services start reading or writing data.
|
|
9
|
+
*
|
|
10
|
+
* **Mental model**
|
|
11
|
+
*
|
|
12
|
+
* Migrations are loaded with the shared `<id>_<name>` convention and recorded in
|
|
13
|
+
* `effect_sql_migrations` by default. The runner reads the latest recorded id
|
|
14
|
+
* and applies only migrations with larger ids, returning the ids and names that
|
|
15
|
+
* were applied in the current run.
|
|
16
|
+
*
|
|
17
|
+
* **Common tasks**
|
|
18
|
+
*
|
|
19
|
+
* - Call `run` when an effect should explicitly migrate a MySQL database.
|
|
20
|
+
* - Use `layer` when schema setup should happen while building a layer graph.
|
|
21
|
+
* - Reuse the shared `Migrator` loaders from this module for file-based or
|
|
22
|
+
* in-memory migration definitions.
|
|
23
|
+
*
|
|
24
|
+
* **Gotchas**
|
|
25
|
+
*
|
|
26
|
+
* MySQL DDL can cause implicit commits, and this adapter relies on migration
|
|
27
|
+
* table constraints to detect concurrent runners. Coordinate startup runners and
|
|
28
|
+
* write migrations to tolerate MySQL's transactional semantics. Schema dump
|
|
29
|
+
* support is not enabled in this adapter, so `schemaDirectory` does not emit a
|
|
30
|
+
* MySQL dump.
|
|
31
|
+
*
|
|
32
|
+
* @since 4.0.0
|
|
3
33
|
*/
|
|
4
34
|
import type * as Effect from "effect/Effect"
|
|
5
35
|
import * as Layer from "effect/Layer"
|
|
@@ -8,13 +38,15 @@ import type * as Client from "effect/unstable/sql/SqlClient"
|
|
|
8
38
|
import type { SqlError } from "effect/unstable/sql/SqlError"
|
|
9
39
|
|
|
10
40
|
/**
|
|
11
|
-
* @since
|
|
41
|
+
* @since 4.0.0
|
|
12
42
|
*/
|
|
13
43
|
export * from "effect/unstable/sql/Migrator"
|
|
14
44
|
|
|
15
45
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
46
|
+
* Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
|
|
47
|
+
*
|
|
48
|
+
* @category constructors
|
|
49
|
+
* @since 4.0.0
|
|
18
50
|
*/
|
|
19
51
|
export const run: <R2 = never>(
|
|
20
52
|
{ loader, schemaDirectory, table }: Migrator.MigratorOptions<R2>
|
|
@@ -78,8 +110,10 @@ export const run: <R2 = never>(
|
|
|
78
110
|
})
|
|
79
111
|
|
|
80
112
|
/**
|
|
113
|
+
* Creates a layer that runs the configured SQL migrations during layer construction.
|
|
114
|
+
*
|
|
81
115
|
* @category layers
|
|
82
|
-
* @since
|
|
116
|
+
* @since 4.0.0
|
|
83
117
|
*/
|
|
84
118
|
export const layer = <R>(
|
|
85
119
|
options: Migrator.MigratorOptions<R>
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
// @barrel: Auto-generated exports. Do not edit manually.
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* @since
|
|
8
|
+
* @since 4.0.0
|
|
9
9
|
*/
|
|
10
10
|
export * as MysqlClient from "./MysqlClient.ts"
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @since
|
|
13
|
+
* @since 4.0.0
|
|
14
14
|
*/
|
|
15
15
|
export * as MysqlMigrator from "./MysqlMigrator.ts"
|