serverless-simple-middleware 0.0.52 → 0.0.53
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/.prettierignore +2 -2
- package/README.md +4 -4
- package/dist/aws/config.d.ts +15 -15
- package/dist/aws/config.js +29 -29
- package/dist/aws/define.d.ts +21 -21
- package/dist/aws/define.js +8 -8
- package/dist/aws/index.d.ts +3 -3
- package/dist/aws/index.js +8 -8
- package/dist/aws/simple.d.ts +44 -42
- package/dist/aws/simple.js +656 -590
- package/dist/index.d.ts +3 -3
- package/dist/index.js +8 -8
- package/dist/middleware/aws.d.ts +25 -25
- package/dist/middleware/aws.js +128 -128
- package/dist/middleware/base.d.ts +54 -54
- package/dist/middleware/base.js +140 -140
- package/dist/middleware/build.d.ts +3 -3
- package/dist/middleware/build.js +234 -234
- package/dist/middleware/index.d.ts +12 -12
- package/dist/middleware/index.js +22 -22
- package/dist/middleware/logger.d.ts +18 -18
- package/dist/middleware/logger.js +71 -71
- package/dist/middleware/mysql.d.ts +44 -44
- package/dist/middleware/mysql.js +289 -289
- package/dist/middleware/trace.d.ts +86 -86
- package/dist/middleware/trace.js +255 -255
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +7 -7
- package/dist/utils/logger.d.ts +26 -26
- package/dist/utils/logger.js +73 -73
- package/dist/utils/misc.d.ts +1 -1
- package/dist/utils/misc.js +9 -9
- package/jest.config.js +7 -7
- package/package.json +61 -60
- package/src/aws/config.ts +46 -46
- package/src/aws/define.ts +29 -29
- package/src/aws/index.ts +3 -3
- package/src/aws/simple.ts +531 -482
- package/src/index.ts +3 -3
- package/src/middleware/aws.ts +78 -78
- package/src/middleware/base.ts +164 -164
- package/src/middleware/build.ts +173 -173
- package/src/middleware/index.ts +20 -20
- package/src/middleware/logger.ts +28 -28
- package/src/middleware/mysql.ts +210 -210
- package/src/middleware/trace.ts +269 -269
- package/src/utils/index.ts +2 -2
- package/src/utils/logger.ts +94 -94
- package/src/utils/misc.ts +11 -11
- package/tsconfig.json +15 -15
- package/tslint.json +12 -12
package/src/middleware/mysql.ts
CHANGED
|
@@ -1,210 +1,210 @@
|
|
|
1
|
-
import * as mysql from 'mysql';
|
|
2
|
-
|
|
3
|
-
import { getLogger } from '../utils';
|
|
4
|
-
import { HandlerAuxBase, HandlerPluginBase } from './base';
|
|
5
|
-
|
|
6
|
-
const logger = getLogger(__filename);
|
|
7
|
-
|
|
8
|
-
export interface MySQLPluginOptions {
|
|
9
|
-
config: mysql.ConnectionConfig;
|
|
10
|
-
schema?: {
|
|
11
|
-
eager?: boolean;
|
|
12
|
-
ignoreError?: boolean;
|
|
13
|
-
database?: string;
|
|
14
|
-
tables?: {
|
|
15
|
-
[tableName: string]: string;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class ConnectionProxy {
|
|
21
|
-
private pluginConfig: MySQLPluginOptions;
|
|
22
|
-
private connection?: mysql.Connection;
|
|
23
|
-
|
|
24
|
-
private initialized: boolean;
|
|
25
|
-
private dbName?: string;
|
|
26
|
-
|
|
27
|
-
public constructor(config: MySQLPluginOptions) {
|
|
28
|
-
this.pluginConfig = config;
|
|
29
|
-
if (config.schema && config.schema.database) {
|
|
30
|
-
this.dbName = config.config.database;
|
|
31
|
-
config.config.database = undefined;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public query = <T>(sql: string, params?: any[]) =>
|
|
36
|
-
new Promise<T | undefined>(async (resolve, reject) => {
|
|
37
|
-
const connection = this.prepareConnection();
|
|
38
|
-
await this.tryToInitializeSchema(false);
|
|
39
|
-
|
|
40
|
-
if (process.env.NODE_ENV !== 'test') {
|
|
41
|
-
logger.silly(`Execute query[${sql}] with params[${params}]`);
|
|
42
|
-
}
|
|
43
|
-
connection.query(sql, params, (err: mysql.MysqlError, result?: T) => {
|
|
44
|
-
if (err) {
|
|
45
|
-
logger.error(`error occurred in database query=${sql}, error=${err}`);
|
|
46
|
-
reject(err);
|
|
47
|
-
} else {
|
|
48
|
-
resolve(result);
|
|
49
|
-
if (process.env.NODE_ENV !== 'test') {
|
|
50
|
-
logger.silly(`DB result is ${JSON.stringify(result)}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
public fetch = <T>(sql: string, params?: any[]) =>
|
|
57
|
-
this.query<T[]>(sql, params).then(res => res || []);
|
|
58
|
-
|
|
59
|
-
public fetchOne = <T>(sql: string, params?: any[], defaultValue?: T) =>
|
|
60
|
-
this.fetch<T>(sql, params).then(res => {
|
|
61
|
-
if (res === undefined || res[0] === undefined) {
|
|
62
|
-
// Makes it as non-null result.
|
|
63
|
-
return defaultValue || (({} as any) as T);
|
|
64
|
-
}
|
|
65
|
-
return res[0];
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
public beginTransaction = () =>
|
|
69
|
-
new Promise<void>(async (resolve, reject) => {
|
|
70
|
-
const connection = this.prepareConnection();
|
|
71
|
-
await this.tryToInitializeSchema(false);
|
|
72
|
-
|
|
73
|
-
connection.beginTransaction((err: mysql.MysqlError) => {
|
|
74
|
-
if (err) {
|
|
75
|
-
reject(err);
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
resolve();
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
public commit = () =>
|
|
83
|
-
new Promise<void>(async (resolve, reject) => {
|
|
84
|
-
const connection = this.prepareConnection();
|
|
85
|
-
await this.tryToInitializeSchema(false);
|
|
86
|
-
|
|
87
|
-
connection.commit((err: mysql.MysqlError) => {
|
|
88
|
-
if (err) {
|
|
89
|
-
reject(err);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
resolve();
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
public rollback = () =>
|
|
97
|
-
new Promise<void>(async (resolve, reject) => {
|
|
98
|
-
const connection = this.prepareConnection();
|
|
99
|
-
await this.tryToInitializeSchema(false);
|
|
100
|
-
|
|
101
|
-
connection.rollback((err: mysql.MysqlError) => {
|
|
102
|
-
if (err) {
|
|
103
|
-
reject(err);
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
resolve();
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
public clearConnection = () => {
|
|
111
|
-
if (this.connection) {
|
|
112
|
-
this.connection.end();
|
|
113
|
-
this.connection = undefined;
|
|
114
|
-
logger.verbose('Connection is end');
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
public onPluginCreated = async () => this.tryToInitializeSchema(true);
|
|
119
|
-
|
|
120
|
-
private prepareConnection = () => {
|
|
121
|
-
if (this.connection) {
|
|
122
|
-
return this.connection;
|
|
123
|
-
}
|
|
124
|
-
this.connection = mysql.createConnection(this.pluginConfig.config);
|
|
125
|
-
this.connection.connect();
|
|
126
|
-
return this.connection;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
private changeDatabase = (dbName: string) =>
|
|
130
|
-
new Promise((resolve, reject) =>
|
|
131
|
-
this.prepareConnection().changeUser(
|
|
132
|
-
{
|
|
133
|
-
database: dbName,
|
|
134
|
-
},
|
|
135
|
-
err => (err ? reject(err) : resolve()),
|
|
136
|
-
),
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
private tryToInitializeSchema = async (initial: boolean) => {
|
|
140
|
-
const { eager = false, ignoreError = false, database = '', tables = {} } =
|
|
141
|
-
this.pluginConfig.schema || {};
|
|
142
|
-
if (initial && !eager) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// This method can be called twice when eager option is on,
|
|
147
|
-
// so this flag should be set and checked at first.
|
|
148
|
-
if (this.initialized) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
this.initialized = true;
|
|
152
|
-
|
|
153
|
-
try {
|
|
154
|
-
if (database) {
|
|
155
|
-
logger.debug(`Prepare a database[${this.dbName}]`);
|
|
156
|
-
logger.stupid(this.dbName!, database);
|
|
157
|
-
const result = await this.query(database);
|
|
158
|
-
logger.debug(
|
|
159
|
-
`Database[${this.dbName}] is initialized: ${JSON.stringify(result)}`,
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
if (this.dbName) {
|
|
163
|
-
await this.changeDatabase(this.dbName);
|
|
164
|
-
logger.verbose(`Database[${this.dbName}] is connected.`);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
for (const [name, query] of Object.entries(tables)) {
|
|
168
|
-
logger.debug(`Prepare a table[${name}]`);
|
|
169
|
-
logger.stupid(name, query);
|
|
170
|
-
const result = await this.query(query);
|
|
171
|
-
logger.debug(
|
|
172
|
-
`Table[${name}] is initialized: ${JSON.stringify(result)}`,
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
logger.verbose(`Database schema is initialized.`);
|
|
176
|
-
} catch (error) {
|
|
177
|
-
logger.warn(error);
|
|
178
|
-
if (!ignoreError) {
|
|
179
|
-
throw error;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export interface MySQLPluginAux extends HandlerAuxBase {
|
|
186
|
-
db: ConnectionProxy;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
export class MySQLPlugin extends HandlerPluginBase<MySQLPluginAux> {
|
|
190
|
-
private proxy: ConnectionProxy;
|
|
191
|
-
|
|
192
|
-
constructor(options: MySQLPluginOptions) {
|
|
193
|
-
super();
|
|
194
|
-
this.proxy = new ConnectionProxy(options);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
public create = async () => {
|
|
198
|
-
await this.proxy.onPluginCreated();
|
|
199
|
-
return { db: this.proxy };
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
public end = () => {
|
|
203
|
-
if (this.proxy) {
|
|
204
|
-
this.proxy.clearConnection();
|
|
205
|
-
}
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const build = (options: MySQLPluginOptions) => new MySQLPlugin(options);
|
|
210
|
-
export default build;
|
|
1
|
+
import * as mysql from 'mysql';
|
|
2
|
+
|
|
3
|
+
import { getLogger } from '../utils';
|
|
4
|
+
import { HandlerAuxBase, HandlerPluginBase } from './base';
|
|
5
|
+
|
|
6
|
+
const logger = getLogger(__filename);
|
|
7
|
+
|
|
8
|
+
export interface MySQLPluginOptions {
|
|
9
|
+
config: mysql.ConnectionConfig;
|
|
10
|
+
schema?: {
|
|
11
|
+
eager?: boolean;
|
|
12
|
+
ignoreError?: boolean;
|
|
13
|
+
database?: string;
|
|
14
|
+
tables?: {
|
|
15
|
+
[tableName: string]: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class ConnectionProxy {
|
|
21
|
+
private pluginConfig: MySQLPluginOptions;
|
|
22
|
+
private connection?: mysql.Connection;
|
|
23
|
+
|
|
24
|
+
private initialized: boolean;
|
|
25
|
+
private dbName?: string;
|
|
26
|
+
|
|
27
|
+
public constructor(config: MySQLPluginOptions) {
|
|
28
|
+
this.pluginConfig = config;
|
|
29
|
+
if (config.schema && config.schema.database) {
|
|
30
|
+
this.dbName = config.config.database;
|
|
31
|
+
config.config.database = undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public query = <T>(sql: string, params?: any[]) =>
|
|
36
|
+
new Promise<T | undefined>(async (resolve, reject) => {
|
|
37
|
+
const connection = this.prepareConnection();
|
|
38
|
+
await this.tryToInitializeSchema(false);
|
|
39
|
+
|
|
40
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
41
|
+
logger.silly(`Execute query[${sql}] with params[${params}]`);
|
|
42
|
+
}
|
|
43
|
+
connection.query(sql, params, (err: mysql.MysqlError, result?: T) => {
|
|
44
|
+
if (err) {
|
|
45
|
+
logger.error(`error occurred in database query=${sql}, error=${err}`);
|
|
46
|
+
reject(err);
|
|
47
|
+
} else {
|
|
48
|
+
resolve(result);
|
|
49
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
50
|
+
logger.silly(`DB result is ${JSON.stringify(result)}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
public fetch = <T>(sql: string, params?: any[]) =>
|
|
57
|
+
this.query<T[]>(sql, params).then(res => res || []);
|
|
58
|
+
|
|
59
|
+
public fetchOne = <T>(sql: string, params?: any[], defaultValue?: T) =>
|
|
60
|
+
this.fetch<T>(sql, params).then(res => {
|
|
61
|
+
if (res === undefined || res[0] === undefined) {
|
|
62
|
+
// Makes it as non-null result.
|
|
63
|
+
return defaultValue || (({} as any) as T);
|
|
64
|
+
}
|
|
65
|
+
return res[0];
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
public beginTransaction = () =>
|
|
69
|
+
new Promise<void>(async (resolve, reject) => {
|
|
70
|
+
const connection = this.prepareConnection();
|
|
71
|
+
await this.tryToInitializeSchema(false);
|
|
72
|
+
|
|
73
|
+
connection.beginTransaction((err: mysql.MysqlError) => {
|
|
74
|
+
if (err) {
|
|
75
|
+
reject(err);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
resolve();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
public commit = () =>
|
|
83
|
+
new Promise<void>(async (resolve, reject) => {
|
|
84
|
+
const connection = this.prepareConnection();
|
|
85
|
+
await this.tryToInitializeSchema(false);
|
|
86
|
+
|
|
87
|
+
connection.commit((err: mysql.MysqlError) => {
|
|
88
|
+
if (err) {
|
|
89
|
+
reject(err);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
resolve();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
public rollback = () =>
|
|
97
|
+
new Promise<void>(async (resolve, reject) => {
|
|
98
|
+
const connection = this.prepareConnection();
|
|
99
|
+
await this.tryToInitializeSchema(false);
|
|
100
|
+
|
|
101
|
+
connection.rollback((err: mysql.MysqlError) => {
|
|
102
|
+
if (err) {
|
|
103
|
+
reject(err);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
resolve();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
public clearConnection = () => {
|
|
111
|
+
if (this.connection) {
|
|
112
|
+
this.connection.end();
|
|
113
|
+
this.connection = undefined;
|
|
114
|
+
logger.verbose('Connection is end');
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
public onPluginCreated = async () => this.tryToInitializeSchema(true);
|
|
119
|
+
|
|
120
|
+
private prepareConnection = () => {
|
|
121
|
+
if (this.connection) {
|
|
122
|
+
return this.connection;
|
|
123
|
+
}
|
|
124
|
+
this.connection = mysql.createConnection(this.pluginConfig.config);
|
|
125
|
+
this.connection.connect();
|
|
126
|
+
return this.connection;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
private changeDatabase = (dbName: string) =>
|
|
130
|
+
new Promise((resolve, reject) =>
|
|
131
|
+
this.prepareConnection().changeUser(
|
|
132
|
+
{
|
|
133
|
+
database: dbName,
|
|
134
|
+
},
|
|
135
|
+
err => (err ? reject(err) : resolve()),
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
private tryToInitializeSchema = async (initial: boolean) => {
|
|
140
|
+
const { eager = false, ignoreError = false, database = '', tables = {} } =
|
|
141
|
+
this.pluginConfig.schema || {};
|
|
142
|
+
if (initial && !eager) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// This method can be called twice when eager option is on,
|
|
147
|
+
// so this flag should be set and checked at first.
|
|
148
|
+
if (this.initialized) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.initialized = true;
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
if (database) {
|
|
155
|
+
logger.debug(`Prepare a database[${this.dbName}]`);
|
|
156
|
+
logger.stupid(this.dbName!, database);
|
|
157
|
+
const result = await this.query(database);
|
|
158
|
+
logger.debug(
|
|
159
|
+
`Database[${this.dbName}] is initialized: ${JSON.stringify(result)}`,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
if (this.dbName) {
|
|
163
|
+
await this.changeDatabase(this.dbName);
|
|
164
|
+
logger.verbose(`Database[${this.dbName}] is connected.`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
for (const [name, query] of Object.entries(tables)) {
|
|
168
|
+
logger.debug(`Prepare a table[${name}]`);
|
|
169
|
+
logger.stupid(name, query);
|
|
170
|
+
const result = await this.query(query);
|
|
171
|
+
logger.debug(
|
|
172
|
+
`Table[${name}] is initialized: ${JSON.stringify(result)}`,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
logger.verbose(`Database schema is initialized.`);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
logger.warn(error);
|
|
178
|
+
if (!ignoreError) {
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface MySQLPluginAux extends HandlerAuxBase {
|
|
186
|
+
db: ConnectionProxy;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export class MySQLPlugin extends HandlerPluginBase<MySQLPluginAux> {
|
|
190
|
+
private proxy: ConnectionProxy;
|
|
191
|
+
|
|
192
|
+
constructor(options: MySQLPluginOptions) {
|
|
193
|
+
super();
|
|
194
|
+
this.proxy = new ConnectionProxy(options);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
public create = async () => {
|
|
198
|
+
await this.proxy.onPluginCreated();
|
|
199
|
+
return { db: this.proxy };
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
public end = () => {
|
|
203
|
+
if (this.proxy) {
|
|
204
|
+
this.proxy.clearConnection();
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const build = (options: MySQLPluginOptions) => new MySQLPlugin(options);
|
|
210
|
+
export default build;
|