@deterministic-code/datasources-mysql 0.1.0 → 0.1.2
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/MysqlAlterTableStatementRenderer.d.ts +6 -0
- package/dist/MysqlAlterTableStatementRenderer.js +13 -0
- package/dist/MysqlBulkUpdateStatementTextRenderer.d.ts +4 -0
- package/dist/MysqlBulkUpdateStatementTextRenderer.js +9 -0
- package/dist/MysqlCreateRoutineStatementRenderer.d.ts +4 -0
- package/dist/MysqlCreateTableStatementRenderer.d.ts +5 -0
- package/dist/MysqlCreateTableStatementRenderer.js +10 -0
- package/dist/MysqlCreateTriggerStatementRenderer.d.ts +4 -0
- package/dist/MysqlCreateTriggerStatementRenderer.js +7 -0
- package/dist/MysqlDriver.d.ts +16 -0
- package/dist/MysqlDriver.js +76 -0
- package/dist/MysqlDropIndexStatementRenderer.d.ts +4 -0
- package/dist/MysqlDropIndexStatementRenderer.js +6 -0
- package/dist/MysqlDropRoutineStatementRenderer.d.ts +4 -0
- package/dist/MysqlDropRoutineStatementRenderer.js +6 -0
- package/dist/MysqlStatementRenderer.d.ts +18 -0
- package/dist/MysqlStatementRenderer.js +60 -0
- package/dist/MysqlStoredProcedureStatementRenderer.d.ts +10 -0
- package/dist/MysqlStoredProcedureStatementRenderer.js +101 -0
- package/dist/bind.d.ts +5 -0
- package/dist/bind.js +20 -0
- package/dist/columnTypes.d.ts +1 -0
- package/dist/columnTypes.js +10 -0
- package/dist/command.d.ts +16 -0
- package/dist/command.js +143 -0
- package/dist/connection-options.d.ts +5 -0
- package/dist/connection-options.js +1 -0
- package/dist/connection.d.ts +19 -0
- package/dist/connection.js +64 -0
- package/dist/converters/BinaryConverter.d.ts +5 -0
- package/dist/converters/BinaryConverter.js +6 -0
- package/dist/converters/BoolConverter.d.ts +5 -0
- package/dist/converters/BoolConverter.js +5 -0
- package/dist/converters/DateTimeConverter.d.ts +5 -0
- package/dist/converters/DateTimeConverter.js +10 -0
- package/dist/converters/DecimalConverter.d.ts +5 -0
- package/dist/converters/DecimalConverter.js +5 -0
- package/dist/converters/MysqlRowTypeConverters.d.ts +3 -0
- package/dist/converters/MysqlRowTypeConverters.js +20 -0
- package/dist/converters/UuidConverter.d.ts +5 -0
- package/dist/converters/UuidConverter.js +5 -0
- package/dist/i-mysql-engine.d.ts +27 -0
- package/dist/i-mysql-engine.js +14 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/scalar.js +1 -0
- package/dist/transaction.d.ts +9 -0
- package/dist/transaction.js +15 -0
- package/package.json +10 -7
- package/src/MysqlBulkUpdateStatementTextRenderer.ts +0 -10
- package/src/MysqlCreateTableStatementRenderer.ts +0 -12
- package/src/MysqlDriver.ts +0 -54
- package/src/MysqlStatementRenderer.ts +0 -34
- package/src/bind.ts +0 -31
- package/src/columnTypes.ts +0 -10
- package/src/command.ts +0 -197
- package/src/connection-options.ts +0 -6
- package/src/connection.ts +0 -91
- package/src/converters/BinaryConverter.ts +0 -9
- package/src/converters/BoolConverter.ts +0 -6
- package/src/converters/DateTimeConverter.ts +0 -15
- package/src/converters/DecimalConverter.ts +0 -7
- package/src/converters/MysqlRowTypeConverters.ts +0 -25
- package/src/converters/UuidConverter.ts +0 -6
- package/src/i-mysql-engine.ts +0 -58
- package/src/index.ts +0 -1
- package/src/transaction.ts +0 -21
- /package/{src/scalar.ts → dist/scalar.d.ts} +0 -0
package/src/connection.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
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 "@deterministic-code/datasources";
|
|
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
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { TypeConverter, type Cell } from "@deterministic-code/datasources";
|
|
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
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { TypeConverter, type Cell } from "@deterministic-code/datasources";
|
|
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
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { TypeConverter, type Cell } from "@deterministic-code/datasources";
|
|
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
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { TypeConverter, type Cell } from "@deterministic-code/datasources";
|
|
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
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { type Cell, type TypeConverter } from "@deterministic-code/datasources";
|
|
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
|
-
|
package/src/i-mysql-engine.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type { Parameter } from "@deterministic-code/datasources";
|
|
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
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { MysqlDriver } from "./MysqlDriver.ts";
|
package/src/transaction.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IsolationLevel,
|
|
3
|
-
type ITransaction,
|
|
4
|
-
type IsolationLevelName,
|
|
5
|
-
} from "@deterministic-code/datasources";
|
|
6
|
-
import type { MysqlConnection } from "./connection.ts";
|
|
7
|
-
|
|
8
|
-
export class MysqlTransaction implements ITransaction {
|
|
9
|
-
constructor(
|
|
10
|
-
readonly connection: MysqlConnection,
|
|
11
|
-
readonly isolation: IsolationLevelName = IsolationLevel.ReadCommitted,
|
|
12
|
-
) {}
|
|
13
|
-
|
|
14
|
-
async commit(): Promise<void> {
|
|
15
|
-
await this.connection.require().commit();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async abort(): Promise<void> {
|
|
19
|
-
await this.connection.require().rollback();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
File without changes
|