Skip to content

Commit 3fabcf3

Browse files
Michael KornelakisCodeFoodPixels
authored andcommitted
Create a project in the examples folder and move all the needed files in there
Export the FieldInfo and the Query from the index.d.ts Update the example's Readme
1 parent 2032a62 commit 3fabcf3

File tree

10 files changed

+266
-15
lines changed

10 files changed

+266
-15
lines changed

examples/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ To run a docker container with this data and bound to the correct port, you can
88
docker run -p 3306:3306 --name mysql_container -e MYSQL_ROOT_PASSWORD=password -d genschsa/mysql-employees
99
```
1010

11-
### Typecript
11+
### Typescript
1212

13-
In the `typescript` folder there are the equivalent examples. In order to run them you can use `npm run ts-node` followed by the path of the file. So for example you can run `npm run ts-node ./examples/connection/typescript/query`
13+
In the `typescript` folder there is a sample project with the examples that demostrate how this library would be used in a typescript project. In order to run them you need to navigate in the project's folder and then install the npm packages with `npm install` and then you can use `npm run ts-node` followed by the name of the file. So for example you can run `npm run ts-node .src/query.ts`
1414

1515
### Note
1616

17-
These examples are using `ts-node` in order to avoid including a bundler. If you try to compile the typescript files to javascript using ithe `outDir` option in `tsconfig` then you will get import errors beacuse we are pulling the `index` file form the root of the project.
17+
These examples are using `ts-node` in order to avoid including a bundler. If you try to compile the typescript files to javascript using the `outDir` option in `tsconfig` then you will get import errors because we are pulling the `index` file form the root.

examples/connection/typescript/package-lock.json

Lines changed: 233 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "typescript-promisify-mysql",
3+
"version": "1.0.0",
4+
"description": "A demo to show how one would use promisify-mysql withing a typescript project",
5+
"scripts": {
6+
"ts-node": "ts-node"
7+
},
8+
"author": "Michael Kornelakis",
9+
"license": "MIT",
10+
"devDependencies": {
11+
"mysql": "^2.18.1",
12+
"@types/mysql": "^2.15.2",
13+
"@types/bluebird": "^3.5.26",
14+
"bluebird": "^3.5.1",
15+
"ts-node": "^10.7.0",
16+
"typescript": "^4.6.3"
17+
}
18+
}
File renamed without changes.

examples/connection/typescript/mysqlWrapper.ts renamed to examples/connection/typescript/src/mysqlWrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Bluebird from 'bluebird';
2-
import * as mysql from '../../../index';
2+
import * as mysql from '../../../../index';
33

44
async function runReturn() {
55
const connection = await mysql.createConnection({

examples/connection/typescript/query.ts renamed to examples/connection/typescript/src/query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as mysql from '../../../index';
1+
import * as mysql from '../../../../index';
22
import { Employee } from './employee';
33

44
async function run() {
@@ -16,7 +16,7 @@ async function run() {
1616
employees.forEach(employee => {
1717
console.log(`The employee with the employee number ${employee.emp_no} is ${employee.first_name} ${employee.last_name}`);
1818
});
19-
19+
2020
connection.end();
2121
})
2222
}

examples/connection/typescript/querystream.ts renamed to examples/connection/typescript/src/querystream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as mysql from '../../../index';
1+
import * as mysql from '../../../../index';
22
import { Employee } from './employee';
33

44
function run() {

examples/connection/typescript/returnArgumentsArray.ts renamed to examples/connection/typescript/src/returnArgumentsArray.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mysql from '../../../index';
1+
import mysql from '../../../../index';
2+
import type { FieldInfo, Query } from '../../../../index';
23
import { Employee } from './employee';
34

45
function run() {
@@ -12,7 +13,7 @@ function run() {
1213
}).then((conn) => {
1314
connection = conn;
1415

15-
return connection.query<[data: Employee[], fields: mysql.FieldInfo[], query: mysql.Query]>('select * from employees limit 0, 10');
16+
return connection.query<[data: Employee[], fields: FieldInfo[], query: Query<Employee>]>('select * from employees limit 0, 10');
1617
}).then(([data, fields, query]) => {
1718
console.log(`The SQL for the query was: ${query.sql}\n`);
1819

@@ -39,7 +40,7 @@ async function runAwait() {
3940
returnArgumentsArray: true
4041
});
4142

42-
const [data, fields, query] = await connection.query<[data: Employee[], fields: mysql.FieldInfo[], query: mysql.Query]>('select * from employees limit 0, 10');
43+
const [data, fields, query] = await connection.query<[data: Employee[], fields: mysql.FieldInfo[], query: Query<Employee>]>('select * from employees limit 0, 10');
4344

4445
console.log(`The SQL for the query was: ${query.sql}\n`);
4546

tsconfig.json renamed to examples/connection/typescript/tsconfig.json

File renamed without changes.

index.d.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as mysql from 'mysql';
22
import * as Bluebird from 'bluebird';
33

4+
export type { FieldInfo } from 'mysql';
5+
46
export function createConnection(connectionUri: string | ConnectionConfig): Bluebird<Connection>;
57

68
export function createPool(config: PoolConfig | string): Bluebird<Pool>;
@@ -10,9 +12,6 @@ export function createPoolCluster(config: mysql.PoolClusterConfig): Bluebird<Poo
1012
export { Types, escape, escapeId, format, raw, ConnectionOptions, PoolClusterConfig, MysqlError } from 'mysql';
1113

1214
export type mysqlModule = typeof mysql;
13-
export type FieldInfo = typeof mysql.FieldInfo;
14-
export type Query = typeof mysql.Query;
15-
1615
export interface ConnectionConfig extends mysql.ConnectionConfig {
1716
mysqlWrapper?: (mysql: mysqlModule, callback?: (err: Error | null, success?: mysqlModule) => void) => mysqlModule | Promise<mysqlModule> | void;
1817
returnArgumentsArray?: boolean;
@@ -26,9 +25,9 @@ export interface PoolConfig extends mysql.PoolConfig {
2625
}
2726

2827
export interface QueryFunction {
29-
<T extends unknown>(query: mysql.Query | string | mysql.QueryOptions): T;
28+
<T extends unknown>(query: mysql.Query | string | mysql.QueryOptions): Bluebird<T>;
3029

31-
<T extends unknown>(options: string, values?: any): T;
30+
<T extends unknown>(options: string, values?: any): Bluebird<T>;
3231
}
3332

3433
export interface Query<T> extends mysql.Query {

0 commit comments

Comments
 (0)