@deterministic-code/datasources-mysql 0.0.0-stage → 0.1.1

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.
Files changed (48) hide show
  1. package/dist/MysqlBulkUpdateStatementTextRenderer.d.ts +4 -0
  2. package/dist/MysqlCreateTableStatementRenderer.d.ts +5 -0
  3. package/dist/MysqlDriver.d.ts +12 -0
  4. package/dist/MysqlStatementRenderer.d.ts +7 -0
  5. package/dist/bind.d.ts +5 -0
  6. package/dist/columnTypes.d.ts +1 -0
  7. package/dist/command.d.ts +16 -0
  8. package/dist/connection-options.d.ts +5 -0
  9. package/dist/connection.d.ts +19 -0
  10. package/dist/converters/BinaryConverter.d.ts +5 -0
  11. package/dist/converters/BoolConverter.d.ts +5 -0
  12. package/dist/converters/DateTimeConverter.d.ts +5 -0
  13. package/dist/converters/DecimalConverter.d.ts +5 -0
  14. package/dist/converters/MysqlRowTypeConverters.d.ts +3 -0
  15. package/dist/converters/UuidConverter.d.ts +5 -0
  16. package/dist/i-mysql-engine.d.ts +27 -0
  17. package/dist/index.d.ts +1 -0
  18. package/dist/scalar.d.ts +2 -0
  19. package/dist/transaction.d.ts +9 -0
  20. package/package.json +23 -4
  21. package/src/MysqlBulkUpdateStatementTextRenderer.ts +10 -0
  22. package/src/MysqlCreateTableStatementRenderer.ts +12 -0
  23. package/src/MysqlDriver.ts +54 -0
  24. package/src/MysqlStatementRenderer.test.ts +64 -0
  25. package/src/MysqlStatementRenderer.ts +34 -0
  26. package/src/bind.ts +31 -0
  27. package/src/columnTypes.ts +10 -0
  28. package/src/command.ts +197 -0
  29. package/src/connection-options.ts +6 -0
  30. package/src/connection.ts +91 -0
  31. package/src/converters/BinaryConverter.test.ts +12 -0
  32. package/src/converters/BinaryConverter.ts +9 -0
  33. package/src/converters/BoolConverter.test.ts +14 -0
  34. package/src/converters/BoolConverter.ts +6 -0
  35. package/src/converters/DateTimeConverter.test.ts +14 -0
  36. package/src/converters/DateTimeConverter.ts +15 -0
  37. package/src/converters/DecimalConverter.test.ts +11 -0
  38. package/src/converters/DecimalConverter.ts +7 -0
  39. package/src/converters/MysqlRowTypeConverters.test.ts +19 -0
  40. package/src/converters/MysqlRowTypeConverters.ts +25 -0
  41. package/src/converters/UuidConverter.test.ts +11 -0
  42. package/src/converters/UuidConverter.ts +6 -0
  43. package/src/i-mysql-engine.ts +58 -0
  44. package/src/index.ts +1 -0
  45. package/src/mysql.test.ts +342 -0
  46. package/src/scalar.ts +2 -0
  47. package/src/transaction.ts +21 -0
  48. package/README.md +0 -3
@@ -0,0 +1,91 @@
1
+ import {
2
+ DatasourceError,
3
+ IsolationLevel,
4
+ StatementTextRenderer,
5
+ type DatasourceOptions,
6
+ type ICommand,
7
+ type IConnection,
8
+ type IsolationLevelName,
9
+ type ITransaction,
10
+ } from "@datasources/core";
11
+ import { createRequire } from "node:module";
12
+ import { MysqlCommand } from "./command.ts";
13
+ import type { MysqlConnectionOptions } from "./connection-options.ts";
14
+ import {
15
+ mysql2Connect,
16
+ type IMysqlClient,
17
+ type Mysql2Module,
18
+ type MysqlCreateConnection,
19
+ } from "./i-mysql-engine.ts";
20
+ import { MysqlStatementRenderer } from "./MysqlStatementRenderer.ts";
21
+ import { MysqlTransaction } from "./transaction.ts";
22
+
23
+ const require = createRequire(import.meta.url);
24
+
25
+ const isoSql = (isolation: IsolationLevelName): string =>
26
+ isolation === IsolationLevel.ReadUncommitted
27
+ ? "READ UNCOMMITTED"
28
+ : isolation === IsolationLevel.ReadCommitted
29
+ ? "READ COMMITTED"
30
+ : "REPEATABLE READ";
31
+
32
+ export class MysqlConnection implements IConnection {
33
+ readonly provider = "mysql";
34
+ url: string;
35
+ isOpen = false;
36
+ private client?: IMysqlClient;
37
+ private readonly connect?: MysqlCreateConnection;
38
+
39
+ static from = (options?: DatasourceOptions): MysqlConnection => {
40
+ const url = options?.DATASOURCE_URL ?? "";
41
+ return new MysqlConnection({
42
+ url,
43
+ connect: mysql2Connect(require("mysql2/promise") as Mysql2Module),
44
+ });
45
+ };
46
+
47
+ constructor(options: MysqlConnectionOptions = {}) {
48
+ this.url = options.url ?? "";
49
+ this.connect = options.connect;
50
+ }
51
+
52
+ createCommand = (): ICommand =>
53
+ new MysqlCommand(this, new StatementTextRenderer(t => new MysqlStatementRenderer(t)));
54
+
55
+ async open(): Promise<void> {
56
+ if (this.isOpen) throw new DatasourceError("Connection is already open");
57
+ if (!this.client) {
58
+ if (!this.connect) throw new DatasourceError("MySQL engine is required");
59
+ this.client = await this.connect(this.url);
60
+ }
61
+ await this.client.query("SET SESSION sql_mode = CONCAT(@@sql_mode, ',ANSI_QUOTES')");
62
+ this.isOpen = true;
63
+ }
64
+
65
+ async close(): Promise<void> {
66
+ await this.client?.end();
67
+ this.client = undefined;
68
+ this.isOpen = false;
69
+ }
70
+
71
+ [Symbol.asyncDispose] = (): Promise<void> => this.close();
72
+
73
+ async startTransaction(
74
+ isolation: IsolationLevelName = IsolationLevel.ReadCommitted,
75
+ ): Promise<ITransaction> {
76
+ this.ensureOpen();
77
+ const client = this.require();
78
+ await client.query(`SET TRANSACTION ISOLATION LEVEL ${isoSql(isolation)}`);
79
+ await client.beginTransaction();
80
+ return new MysqlTransaction(this, isolation);
81
+ }
82
+
83
+ ensureOpen(): void {
84
+ if (!this.isOpen) throw new DatasourceError("Connection is not open");
85
+ }
86
+
87
+ require(): IMysqlClient {
88
+ if (!this.client) throw new DatasourceError("Connection is not open");
89
+ return this.client;
90
+ }
91
+ }
@@ -0,0 +1,12 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { BinaryConverter } from "./BinaryConverter.ts";
3
+
4
+ describe("BinaryConverter", () => {
5
+ it("maps binary", () => {
6
+ const converter = new BinaryConverter();
7
+ const raw = new Uint8Array([1, 2, 3, 4]);
8
+ expect(converter.convert(raw)).toBe(raw);
9
+ expect(converter.reverseConvert(raw)).toBe(raw);
10
+ expect(converter.reverseConvert("AQIDBA==")).toEqual(raw);
11
+ });
12
+ });
@@ -0,0 +1,9 @@
1
+ import { TypeConverter, type Cell } from "@datasources/core";
2
+
3
+ const bytes = (value: Cell): Cell =>
4
+ typeof value === "string" ? Uint8Array.from(Buffer.from(value, "base64")) : value;
5
+
6
+ export class BinaryConverter extends TypeConverter<Cell, Cell> {
7
+ convert = (from: Cell): Cell => bytes(from);
8
+ reverseConvert = (to: Cell): Cell => bytes(to);
9
+ }
@@ -0,0 +1,14 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { BoolConverter } from "./BoolConverter.ts";
3
+
4
+ describe("BoolConverter", () => {
5
+ it("maps bool", () => {
6
+ const converter = new BoolConverter();
7
+ expect(converter.convert(1)).toBe(true);
8
+ expect(converter.convert(0)).toBe(false);
9
+ expect(converter.convert("x")).toBe("x");
10
+ expect(converter.reverseConvert(true)).toBe(1);
11
+ expect(converter.reverseConvert(false)).toBe(0);
12
+ expect(converter.reverseConvert("x")).toBe("x");
13
+ });
14
+ });
@@ -0,0 +1,6 @@
1
+ import { TypeConverter, type Cell } from "@datasources/core";
2
+
3
+ export class BoolConverter extends TypeConverter<Cell, Cell> {
4
+ convert = (from: Cell): Cell => (typeof from === "number" ? from !== 0 : from);
5
+ reverseConvert = (to: Cell): Cell => (typeof to === "boolean" ? (to ? 1 : 0) : to);
6
+ }
@@ -0,0 +1,14 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { DateTimeConverter } from "./DateTimeConverter.ts";
3
+
4
+ describe("DateTimeConverter", () => {
5
+ it("maps datetime", () => {
6
+ const converter = new DateTimeConverter();
7
+ const date = new Date("2020-01-02T03:04:05.000Z");
8
+ expect(converter.convert("2020-01-02T03:04:05.000Z")).toEqual(date);
9
+ expect(converter.convert(date)).toBe(date);
10
+ expect(converter.reverseConvert(date)).toBe("2020-01-02 03:04:05.000");
11
+ expect(converter.reverseConvert("2020-01-02T03:04:05.000Z")).toBe("2020-01-02 03:04:05.000");
12
+ expect(converter.reverseConvert("x")).toBe("x");
13
+ });
14
+ });
@@ -0,0 +1,15 @@
1
+ import { TypeConverter, type Cell } from "@datasources/core";
2
+
3
+ const stamp = (date: Date): string =>
4
+ date.toISOString().replace("T", " ").replace("Z", "");
5
+
6
+ export class DateTimeConverter extends TypeConverter<Cell, Cell> {
7
+ convert = (from: Cell): Cell =>
8
+ typeof from === "string" ? new Date(from) : from;
9
+ reverseConvert = (to: Cell): Cell =>
10
+ to instanceof Date
11
+ ? stamp(to)
12
+ : typeof to === "string" && /^\d{4}-\d{2}-\d{2}T/.test(to)
13
+ ? stamp(new Date(to))
14
+ : to;
15
+ }
@@ -0,0 +1,11 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { DecimalConverter } from "./DecimalConverter.ts";
3
+
4
+ describe("DecimalConverter", () => {
5
+ it("maps decimal", () => {
6
+ const converter = new DecimalConverter();
7
+ expect(converter.convert("10.5")).toBe("10.5");
8
+ expect(converter.reverseConvert(10.5)).toBe("10.5");
9
+ expect(converter.reverseConvert("10.5")).toBe("10.5");
10
+ });
11
+ });
@@ -0,0 +1,7 @@
1
+ import { TypeConverter, type Cell } from "@datasources/core";
2
+
3
+ export class DecimalConverter extends TypeConverter<Cell, Cell> {
4
+ convert = (from: Cell): Cell => from;
5
+ reverseConvert = (to: Cell): Cell =>
6
+ typeof to === "number" ? String(to) : to;
7
+ }
@@ -0,0 +1,19 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { MysqlDriver } from "../MysqlDriver.ts";
3
+ import { reverse, MysqlRowTypeConverters } from "./MysqlRowTypeConverters.ts";
4
+
5
+ describe("MysqlRowTypeConverters", () => {
6
+ it("registers default types", () => {
7
+ expect(Object.keys(new MysqlDriver().rowTypeConverters)).toEqual(Object.keys(MysqlRowTypeConverters));
8
+ });
9
+
10
+ it("reverses booleans, dates, and bytes", () => {
11
+ const when = new Date("2024-01-02T00:00:00.000Z");
12
+ expect(reverse(true)).toBe(1);
13
+ expect(reverse(false)).toBe(0);
14
+ expect(reverse(when)).toBe(when.toISOString().replace("T", " ").replace("Z", ""));
15
+ expect(reverse(new Uint8Array([1, 2]))).toEqual(new Uint8Array([1, 2]));
16
+ expect(reverse("a")).toBe("a");
17
+ expect(reverse("2024-01-02T00:00:00Z")).toBe("2024-01-02 00:00:00.000");
18
+ });
19
+ });
@@ -0,0 +1,25 @@
1
+ import { type Cell, type TypeConverter } from "@datasources/core";
2
+ import { BinaryConverter } from "./BinaryConverter.ts";
3
+ import { BoolConverter } from "./BoolConverter.ts";
4
+ import { DateTimeConverter } from "./DateTimeConverter.ts";
5
+ import { DecimalConverter } from "./DecimalConverter.ts";
6
+ import { UuidConverter } from "./UuidConverter.ts";
7
+
8
+ export const MysqlRowTypeConverters: Record<string, TypeConverter<Cell, Cell>> = {
9
+ bool: new BoolConverter(),
10
+ datetime: new DateTimeConverter(),
11
+ uuid: new UuidConverter(),
12
+ decimal: new DecimalConverter(),
13
+ binary: new BinaryConverter(),
14
+ };
15
+
16
+ export const reverse = (value: Cell): Cell =>
17
+ typeof value === "boolean"
18
+ ? MysqlRowTypeConverters.bool.reverseConvert(value)
19
+ : value instanceof Date
20
+ || (typeof value === "string" && /^\d{4}-\d{2}-\d{2}T/.test(value))
21
+ ? MysqlRowTypeConverters.datetime.reverseConvert(value)
22
+ : value instanceof Uint8Array
23
+ ? MysqlRowTypeConverters.binary.reverseConvert(value)
24
+ : value;
25
+
@@ -0,0 +1,11 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { UuidConverter } from "./UuidConverter.ts";
3
+
4
+ describe("UuidConverter", () => {
5
+ it("maps uuid", () => {
6
+ const converter = new UuidConverter();
7
+ const id = "550e8400-e29b-41d4-a716-446655440000";
8
+ expect(converter.convert(id)).toBe(id);
9
+ expect(converter.reverseConvert(id)).toBe(id);
10
+ });
11
+ });
@@ -0,0 +1,6 @@
1
+ import { TypeConverter, type Cell } from "@datasources/core";
2
+
3
+ export class UuidConverter extends TypeConverter<Cell, Cell> {
4
+ convert = (from: Cell): Cell => from;
5
+ reverseConvert = (to: Cell): Cell => to;
6
+ }
@@ -0,0 +1,58 @@
1
+ import type { Parameter } from "@datasources/core";
2
+ import type { Cells, Scalar } from "./scalar.ts";
3
+
4
+ export interface MysqlExecuteResult {
5
+ rowsAffected: number;
6
+ result?: Scalar;
7
+ parameters?: Parameter[];
8
+ }
9
+
10
+ export interface MysqlQueryHeader {
11
+ affectedRows?: number;
12
+ insertId?: Scalar;
13
+ }
14
+
15
+ export interface IMysqlClient {
16
+ query(
17
+ sql: string,
18
+ params?: Record<string, Scalar>,
19
+ ): Promise<[MysqlQueryHeader | Cells[]]>;
20
+ queryRows(sql: string, params?: Record<string, Scalar>): AsyncIterable<Cells>;
21
+ beginTransaction(): Promise<void>;
22
+ commit(): Promise<void>;
23
+ rollback(): Promise<void>;
24
+ end(): Promise<void>;
25
+ }
26
+
27
+ interface MysqlCoreQuery {
28
+ stream(): AsyncIterable<Cells>;
29
+ }
30
+
31
+ interface Mysql2PromiseConnection extends IMysqlClient {
32
+ connection: {
33
+ query(sql: string, params?: Record<string, Scalar>): MysqlCoreQuery;
34
+ };
35
+ }
36
+
37
+ export type MysqlCreateConnection = (url: string) => Promise<IMysqlClient>;
38
+
39
+ export type Mysql2Module = {
40
+ createConnection: (
41
+ options: { uri: string; namedPlaceholders: boolean },
42
+ ) => Promise<IMysqlClient>;
43
+ };
44
+
45
+ export const mysql2Connect = (mysql: Mysql2Module): MysqlCreateConnection => async (uri: string) => {
46
+ const client = (await mysql.createConnection({
47
+ uri,
48
+ namedPlaceholders: true,
49
+ })) as Mysql2PromiseConnection;
50
+ return {
51
+ query: (sql, params) => client.query(sql, params),
52
+ queryRows: (sql, params) => client.connection.query(sql, params).stream(),
53
+ beginTransaction: () => client.beginTransaction(),
54
+ commit: () => client.commit(),
55
+ rollback: () => client.rollback(),
56
+ end: () => client.end(),
57
+ };
58
+ };
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { MysqlDriver } from "./MysqlDriver.ts";
@@ -0,0 +1,342 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+
3
+ vi.mock("mysql2/promise", () => ({
4
+ createConnection: async () => ({
5
+ query: async () => [{}],
6
+ beginTransaction: async () => {},
7
+ commit: async () => {},
8
+ rollback: async () => {},
9
+ end: async () => {},
10
+ }),
11
+ }));
12
+
13
+ import { Datasource, DatasourceFactory, DatasourceError, IsolationLevel, CRUDRepository, ReadonlyRepository, untyped, Native, ParameterDirection, columnType, RowTypeConverter, CreateTableStatement, ExecuteStoredProcedureStatement, ReturnStatement, SelectStatement, type IDriver } from "@datasources/core";
14
+ import { bindParameters, toMysql } from "./bind.ts";
15
+ import { MysqlCommand } from "./command.ts";
16
+ import { MysqlConnection } from "./connection.ts";
17
+ import { MysqlRowTypeConverters } from "./converters/MysqlRowTypeConverters.ts";
18
+ import { mysql2Connect, type IMysqlClient, type MysqlQueryHeader } from "./i-mysql-engine.ts";
19
+ import { MysqlDriver } from "./MysqlDriver.ts";
20
+ import { MysqlTransaction } from "./transaction.ts";
21
+ import type { Cells, Scalar } from "./scalar.ts";
22
+
23
+ class FakeClient implements IMysqlClient {
24
+ queries: Array<{ sql: string; params?: Record<string, Scalar> }> = [];
25
+ begins = 0;
26
+ commits = 0;
27
+ rollbacks = 0;
28
+ ended = false;
29
+ header: MysqlQueryHeader = { affectedRows: 2, insertId: 5 };
30
+ rows: Cells[] = [{ id: 1 }];
31
+ arrayResult = false;
32
+ /** Multi-result-set payload for CALL (array of row arrays). */
33
+ callSets?: Cells[][];
34
+
35
+ async query(sql: string, params?: Record<string, Scalar>) {
36
+ this.queries.push({ sql, params });
37
+ if (this.callSets !== undefined)
38
+ return [this.callSets] as [MysqlQueryHeader | Cells[]];
39
+ return [this.arrayResult ? this.rows : this.header] as [
40
+ MysqlQueryHeader | Cells[],
41
+ ];
42
+ }
43
+ async *queryRows(sql: string, params?: Record<string, Scalar>): AsyncGenerator<Cells> {
44
+ this.queries.push({ sql, params });
45
+ if (!this.arrayResult) return;
46
+ for (const row of this.rows) yield row;
47
+ }
48
+ async beginTransaction(): Promise<void> {
49
+ this.begins += 1;
50
+ }
51
+ async commit(): Promise<void> {
52
+ this.commits += 1;
53
+ }
54
+ async rollback(): Promise<void> {
55
+ this.rollbacks += 1;
56
+ }
57
+ async end(): Promise<void> {
58
+ this.ended = true;
59
+ }
60
+ }
61
+
62
+ const opened = async (url = "mysql://localhost/app", client = new FakeClient()) => {
63
+ const connection = new MysqlConnection({ url, connect: async () => client });
64
+ await connection.open();
65
+ return { connection, client, command: new MysqlCommand(connection) };
66
+ };
67
+
68
+ afterEach(() => {
69
+ DatasourceFactory.reset();
70
+ DatasourceFactory.register("mysql", new MysqlDriver());
71
+ });
72
+
73
+ describe("bindParameters", () => {
74
+ it("binds and rewrites parameters", () => {
75
+ expect(bindParameters()).toEqual({});
76
+ expect(bindParameters(null)).toEqual({});
77
+ expect(bindParameters([{ name: "id", value: 1 }])).toEqual({ id: 1 });
78
+ expect(toMysql("SELECT * FROM t WHERE id = $id")).toBe(
79
+ "SELECT * FROM t WHERE id = :id",
80
+ );
81
+ expect(toMysql("INSERT INTO t (id) VALUES ($id) RETURNING id")).toBe(
82
+ "INSERT INTO t (id) VALUES (:id)",
83
+ );
84
+ expect(toMysql("INSERT INTO contact_settings (contact_id, key, value) VALUES ($contact_id, $key, $value)"))
85
+ .toBe("INSERT INTO contact_settings (contact_id, `key`, value) VALUES (:contact_id, :key, :value)");
86
+ expect(toMysql("CREATE TABLE t (id INT PRIMARY KEY)")).toBe("CREATE TABLE t (id INT PRIMARY KEY)");
87
+ expect(toMysql("CREATE TABLE t (id INT AUTO_INCREMENT, CONSTRAINT `pk` PRIMARY KEY (`id`))"))
88
+ .toBe("CREATE TABLE t (id INT AUTO_INCREMENT, CONSTRAINT `pk` PRIMARY KEY (`id`))");
89
+ expect(toMysql("INSERT INTO OldContactsTbl (CntID) VALUES ($CntID)"))
90
+ .toBe("INSERT INTO `OldContactsTbl` (`CntID`) VALUES (:CntID)");
91
+ });
92
+ });
93
+
94
+ describe("MysqlConnection", () => {
95
+ it("opens, runs SQL, transacts, and closes", async () => {
96
+ const { connection, client, command } = await opened("mysql://db/app");
97
+ expect(await command.execute("INSERT INTO items (id) VALUES ($id)", [{ name: "id", value: 1 }]))
98
+ .toMatchObject({ rowsAffected: 2, result: null });
99
+ expect(await command.executeStatement(new CreateTableStatement("users", [{ name: "id", type: "INTEGER" }])))
100
+ .toMatchObject({ rowsAffected: 2 });
101
+ expect(client.queries.at(-1)?.sql).toBe("CREATE TABLE users (id INTEGER)");
102
+ client.arrayResult = true;
103
+ expect(await (await command.executeReaderNative(new Native("SELECT 1"))).next()).toBe(true);
104
+ expect(await command.executeNative(new Native(" "))).toEqual({ result: undefined, rowsAffected: 0, parameters: undefined });
105
+ client.arrayResult = true;
106
+ const reader = await command.executeReader("SELECT * FROM items");
107
+ expect(await reader.next()).toBe(true);
108
+ expect(reader.current).toEqual({ id: 1 });
109
+ expect(await (await command.executeReaderNative(new Native(""))).next()).toBe(false);
110
+ client.arrayResult = false;
111
+ await connection.startTransaction();
112
+ await connection.startTransaction(IsolationLevel.ReadUncommitted);
113
+ await connection.startTransaction(IsolationLevel.ReadCommitted);
114
+ await connection.startTransaction(IsolationLevel.RepeatableRead);
115
+ await new MysqlTransaction(connection).commit();
116
+ await new MysqlTransaction(connection).abort();
117
+ expect(client.begins).toBe(4);
118
+ expect(client.commits).toBe(1);
119
+ expect(client.rollbacks).toBe(1);
120
+ expect(client.queries.map((q) => q.sql)).toEqual([
121
+ "SET SESSION sql_mode = CONCAT(@@sql_mode, ',ANSI_QUOTES')",
122
+ "INSERT INTO items (id) VALUES (:id)",
123
+ "CREATE TABLE users (id INTEGER)",
124
+ "SELECT 1",
125
+ "SELECT * FROM items",
126
+ "SET TRANSACTION ISOLATION LEVEL READ COMMITTED",
127
+ "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED",
128
+ "SET TRANSACTION ISOLATION LEVEL READ COMMITTED",
129
+ "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ",
130
+ ]);
131
+ await connection.close();
132
+ expect(client.ended).toBe(true);
133
+ expect(() => connection.require()).toThrow("not open");
134
+ await expect(command.executeNative(new Native("x"))).rejects.toThrow("not open");
135
+ });
136
+
137
+ it("connects through mysql2 and treats array results as row counts", async () => {
138
+ const connect = mysql2Connect({
139
+ createConnection: async () => new FakeClient(),
140
+ });
141
+ const named = new MysqlConnection({ url: "mysql://db/app", connect });
142
+ await named.open();
143
+ await named.close();
144
+
145
+ const { command, client, connection } = await opened("x");
146
+ client.arrayResult = true;
147
+ expect(await command.executeNative(new Native("SELECT 1"))).toMatchObject({ rowsAffected: 1, result: undefined });
148
+ client.arrayResult = false;
149
+ client.header = {};
150
+ expect(await command.executeNative(new Native("UPDATE items"))).toMatchObject({ rowsAffected: 0 });
151
+ await connection.close();
152
+ await connection.close();
153
+ });
154
+
155
+ it("opens, reopens, transacts, and closes", async () => {
156
+ const { connection, client } = await opened("mysql://db/app");
157
+ await expect(connection.open()).rejects.toThrow("Connection is already open");
158
+ expect(connection.createCommand()).toBeInstanceOf(MysqlCommand);
159
+ expect(connection.isOpen).toBe(true);
160
+ const txn = await connection.startTransaction();
161
+ await txn.commit();
162
+ const txn2 = await connection.startTransaction(IsolationLevel.ReadUncommitted);
163
+ expect(txn2.isolation).toBe(IsolationLevel.ReadUncommitted);
164
+ await txn2.abort();
165
+ expect(txn.isolation).toBe("readCommitted");
166
+ await connection.close();
167
+ expect(client.ended).toBe(true);
168
+ expect(() => connection.ensureOpen()).toThrow(DatasourceError);
169
+ await expect(connection.startTransaction()).rejects.toThrow(DatasourceError);
170
+ });
171
+
172
+ it("defaults url and requires a client to open", async () => {
173
+ const connection = new MysqlConnection();
174
+ expect(connection.url).toBe("");
175
+ await expect(connection.open()).rejects.toThrow(DatasourceError);
176
+ });
177
+ });
178
+
179
+ describe("MysqlCommand", () => {
180
+ it("executes SQL through the client", async () => {
181
+ const { command, client } = await opened();
182
+ client.arrayResult = true;
183
+ expect(await command.execute("DELETE FROM items", [{ name: "id", value: 1 }]))
184
+ .toEqual({ result: undefined, rowsAffected: 1, parameters: [{ name: "id", value: 1 }] });
185
+ expect(await command.executeNative(new Native(""))).toEqual({ result: undefined, rowsAffected: 0, parameters: undefined });
186
+ const reader = await command.executeReader("SELECT * FROM items");
187
+ expect(await reader.next()).toBe(true);
188
+ expect(reader.current).toEqual({ id: 1 });
189
+ const empty = await command.executeReaderNative(new Native(""));
190
+ expect(await empty.next()).toBe(false);
191
+ expect(await (await command.executeReaderNative(new Native(";"))).next()).toBe(false);
192
+ client.rows = [{ add_one: 5 }];
193
+ const n = { name: "n", value: 4 };
194
+ expect(await command.executeStatement(new ReturnStatement("add_one", [n]), [n]))
195
+ .toEqual({ result: 5, rowsAffected: 1, parameters: [n] });
196
+ client.rows = [];
197
+ expect(await command.executeNative(new Native("SELECT 1"))).toEqual({
198
+ result: undefined,
199
+ rowsAffected: 0,
200
+ parameters: undefined,
201
+ });
202
+ client.rows = [{ result: 5 }];
203
+ const start = { name: "start_n", value: 4 };
204
+ const out = { name: "result", direction: ParameterDirection.Output };
205
+ expect(
206
+ await command.executeStatement(new ExecuteStoredProcedureStatement("bump", [start, out]), [start, out]),
207
+ ).toMatchObject({
208
+ parameters: [start, { name: "result", direction: ParameterDirection.Output, value: 5 }],
209
+ });
210
+ const inout = { name: "n", value: 4, direction: ParameterDirection.InputOutput };
211
+ client.rows = [{ n: 5 }];
212
+ expect(await command.executeStatement(new ExecuteStoredProcedureStatement("bump", [inout]), [inout])).toMatchObject({
213
+ parameters: [{ name: "n", value: 5, direction: ParameterDirection.InputOutput }],
214
+ });
215
+ expect(
216
+ await command.executeStatement(new ExecuteStoredProcedureStatement("bump", [
217
+ { name: "start_n" },
218
+ { name: "result", direction: ParameterDirection.Output },
219
+ ]), [
220
+ { name: "start_n" },
221
+ { name: "result", direction: ParameterDirection.Output },
222
+ ]),
223
+ ).toMatchObject({ rowsAffected: 0 });
224
+ client.arrayResult = true;
225
+ client.rows = [];
226
+ expect(await command.executeStatement(new SelectStatement({ table: "dual", fields: ["1"] }))).toMatchObject({
227
+ result: null,
228
+ rowsAffected: 0,
229
+ });
230
+ client.rows = [{}];
231
+ expect(await command.executeStatement(new SelectStatement({ table: "dual", fields: ["1"] }))).toMatchObject({ result: null });
232
+ expect(await command.executeStatement(new ExecuteStoredProcedureStatement("bump", []))).toMatchObject({
233
+ result: undefined,
234
+ });
235
+ client.arrayResult = false;
236
+ expect(await (await command.executeReaderNative(new Native("SELECT 1"))).next()).toBe(false);
237
+ expect(
238
+ await command.executeStatement(new ExecuteStoredProcedureStatement("bump", [
239
+ { name: "result", direction: ParameterDirection.Output },
240
+ ]), [
241
+ { name: "result", direction: ParameterDirection.Output },
242
+ ]),
243
+ ).toMatchObject({ rowsAffected: 0 });
244
+ });
245
+
246
+ it("reads CALL result sets through the reader", async () => {
247
+ const { command, client } = await opened();
248
+ client.callSets = [[{ id: 1 }, { id: 2 }], [{ id: 3 }]];
249
+ const reader = await command.executeReaderNative(new Native("CALL two_ids()"));
250
+ expect(await reader.next()).toBe(true);
251
+ expect(reader.current).toEqual({ id: 1 });
252
+ expect(await reader.next()).toBe(true);
253
+ expect(reader.current).toEqual({ id: 2 });
254
+ expect(await reader.next()).toBe(false);
255
+ expect(await reader.nextResult()).toBe(true);
256
+ expect(await reader.next()).toBe(true);
257
+ expect(reader.current).toEqual({ id: 3 });
258
+ expect(await reader.next()).toBe(false);
259
+ expect(await reader.nextResult()).toBe(false);
260
+
261
+ client.callSets = undefined;
262
+ client.arrayResult = false;
263
+ const empty = await command.executeReaderNative(new Native("CALL noop()"));
264
+ expect(await empty.next()).toBe(false);
265
+ expect(await empty.nextResult()).toBe(false);
266
+ });
267
+ });
268
+
269
+ describe("Mysql datasource", () => {
270
+ it("registers and constructs children", async () => {
271
+ expect(
272
+ await new Datasource("mysql", new MysqlDriver()).createConnection(),
273
+ ).toBeInstanceOf(MysqlConnection);
274
+ const of = (): IDriver => ({
275
+ connection: () => new MysqlConnection({ connect: async () => new FakeClient() }),
276
+ command: (c) => c.createCommand(),
277
+ transaction: (c, isolation = "readCommitted") =>
278
+ new MysqlTransaction(c as MysqlConnection, isolation),
279
+ rowTypeConverters: MysqlRowTypeConverters,
280
+ columnType,
281
+ });
282
+ const ds = new Datasource("mysql", of());
283
+ expect(ds.provider).toBe("mysql");
284
+ const connection = await ds.createConnection();
285
+ await (connection as MysqlConnection).open();
286
+ const command = await ds.createCommand(connection);
287
+ expect(command).toBeInstanceOf(MysqlCommand);
288
+ expect(command.connection).toBe(connection);
289
+ await command.execute("DELETE FROM items");
290
+ await command.executeReaderNative(new Native("SELECT 1"));
291
+ await expect(ds.createCommand()).rejects.toThrow("Connection is already open");
292
+ const repo = ds.createRepository(untyped(), connection);
293
+ expect(ds.createReadonlyRepository(untyped(), connection).constructor).toBe(ReadonlyRepository);
294
+ expect(repo).toBeInstanceOf(CRUDRepository);
295
+ expect(new Datasource("mysql", of()).createRepository(untyped())).toBeInstanceOf(CRUDRepository);
296
+ expect(Datasource.create({ PROVIDER: "mysql" }).provider).toBe("mysql");
297
+ expect(MysqlConnection.from({ DATASOURCE_URL: "mysql://db/app" }).url).toBe("mysql://db/app");
298
+ expect(MysqlConnection.from({ DATASOURCE_URL: "mysql://db/app" }).url).toBe("mysql://db/app");
299
+ expect(MysqlConnection.from().url).toBe("");
300
+ const provider = new MysqlDriver();
301
+ expect(provider.columnType("int")).toBe("INT");
302
+ expect(provider.columnType("uuid")).toBe("CHAR(36)");
303
+ expect(provider.columnType("string")).toBe("TEXT");
304
+ expect(provider.columnType("datetime")).toBe("TEXT");
305
+ expect(provider.columnType("bool")).toBe("TINYINT");
306
+ expect(provider.columnType("decimal")).toBe("DECIMAL(18,4)");
307
+ expect(provider.columnType("float")).toBe("DOUBLE");
308
+ expect(provider.columnType("binary")).toBe("BLOB");
309
+ expect(() => provider.columnType("xml")).toThrow('Unknown mysql type "xml"');
310
+ expect(provider.command(connection)).toBeInstanceOf(MysqlCommand);
311
+ expect(provider.transaction(connection as MysqlConnection).isolation).toBe("readCommitted");
312
+ });
313
+
314
+ it("maps property targets", () => {
315
+ const provider = new MysqlDriver();
316
+ const created = "2020-01-02T03:04:05.000Z";
317
+ const converter = RowTypeConverter.from(provider.rowTypeConverters, {
318
+ Key: "uuid",
319
+ Active: "bool",
320
+ Login: "string",
321
+ Created: "datetime",
322
+ });
323
+ const row = converter.convert({
324
+ Key: "11111111-1111-1111-1111-111111111111",
325
+ Active: 1,
326
+ Login: "ada",
327
+ Created: created,
328
+ });
329
+ expect(row).toEqual({
330
+ Key: "11111111-1111-1111-1111-111111111111",
331
+ Active: true,
332
+ Login: "ada",
333
+ Created: new Date(created),
334
+ });
335
+ expect(converter.reverseConvert(row)).toEqual({
336
+ Key: "11111111-1111-1111-1111-111111111111",
337
+ Active: 1,
338
+ Login: "ada",
339
+ Created: "2020-01-02 03:04:05.000",
340
+ });
341
+ });
342
+ });
package/src/scalar.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type Scalar = string | number | boolean | bigint | null;
2
+ export type Cells = Record<string, Scalar>;