@sqldoc/db-mysql 0.0.10
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/package.json +25 -0
- package/src/index.ts +34 -0
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "@sqldoc/db-mysql",
|
|
4
|
+
"version": "0.0.10",
|
|
5
|
+
"description": "MySQL adapter for sqldoc (Node.js, uses mysql2)",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"import": "./src/index.ts",
|
|
10
|
+
"default": "./src/index.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "./src/index.ts",
|
|
14
|
+
"types": "./src/index.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"package.json"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"mysql2": "^3.20.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@sqldoc/db": "0.0.10"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AdapterPluginContext, DatabaseAdapter, DatabaseAdapterPlugin, ExecResult, QueryResult } from '@sqldoc/db'
|
|
2
|
+
import { normalizeValue } from '@sqldoc/db'
|
|
3
|
+
|
|
4
|
+
const plugin: DatabaseAdapterPlugin = {
|
|
5
|
+
apiVersion: 1,
|
|
6
|
+
name: 'mysql',
|
|
7
|
+
schemes: ['mysql'],
|
|
8
|
+
dialects: ['mysql'],
|
|
9
|
+
runtime: 'node',
|
|
10
|
+
|
|
11
|
+
async createAdapter(connectionString: string, _context: AdapterPluginContext): Promise<DatabaseAdapter> {
|
|
12
|
+
const mysql = await import('mysql2/promise')
|
|
13
|
+
const connection = await mysql.default.createConnection(connectionString)
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
async query(sql: string, args?: unknown[]): Promise<QueryResult> {
|
|
17
|
+
const [rows, fields] = await connection.query({ sql, values: args, rowsAsArray: true })
|
|
18
|
+
return {
|
|
19
|
+
columns: (fields as Array<{ name: string }>).map((f) => f.name),
|
|
20
|
+
rows: (rows as unknown[][]).map((row) => (row as unknown[]).map((v) => normalizeValue(v))),
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
async exec(sql: string, args?: unknown[]): Promise<ExecResult> {
|
|
24
|
+
const [result] = await connection.query(sql, args as any)
|
|
25
|
+
return { rowsAffected: (result as any).affectedRows ?? 0 }
|
|
26
|
+
},
|
|
27
|
+
async close(): Promise<void> {
|
|
28
|
+
await connection.end()
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default plugin
|