@faasjs/knex 1.4.0 → 1.4.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.
- package/dist/index.js +32 -3
- package/dist/index.mjs +32 -3
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var func = require('@faasjs/func');
|
|
|
4
4
|
var logger = require('@faasjs/logger');
|
|
5
5
|
var deep_merge = require('@faasjs/deep_merge');
|
|
6
6
|
var knex = require('knex');
|
|
7
|
+
var crypto = require('crypto');
|
|
7
8
|
|
|
8
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
10
|
|
|
@@ -46,12 +47,13 @@ var Knex = class {
|
|
|
46
47
|
await next();
|
|
47
48
|
}
|
|
48
49
|
async onMount(data, next) {
|
|
49
|
-
var _a, _b
|
|
50
|
+
var _a, _b;
|
|
50
51
|
this.logger = data.logger;
|
|
51
52
|
if (global.FaasJS_Knex[this.name]) {
|
|
52
53
|
this.config = global.FaasJS_Knex[this.name].config;
|
|
53
54
|
this.adapter = global.FaasJS_Knex[this.name].adapter;
|
|
54
55
|
this.query = this.adapter;
|
|
56
|
+
this.logger.debug("[%s] use exists adapter", this.name);
|
|
55
57
|
await next();
|
|
56
58
|
return;
|
|
57
59
|
}
|
|
@@ -74,9 +76,11 @@ var Knex = class {
|
|
|
74
76
|
data.config.plugins[this.name].config,
|
|
75
77
|
this.config
|
|
76
78
|
);
|
|
77
|
-
if (this.config.client === "sqlite3")
|
|
79
|
+
if (this.config.client === "sqlite3") {
|
|
78
80
|
this.config.client = "better-sqlite3";
|
|
79
|
-
|
|
81
|
+
this.config.useNullAsDefault = true;
|
|
82
|
+
}
|
|
83
|
+
if (this.config.client === "pg") {
|
|
80
84
|
if (!this.config.pool)
|
|
81
85
|
this.config.pool = /* @__PURE__ */ Object.create(null);
|
|
82
86
|
this.config.pool = Object.assign(
|
|
@@ -105,6 +109,8 @@ var Knex = class {
|
|
|
105
109
|
}
|
|
106
110
|
this.query = this.adapter;
|
|
107
111
|
this.query.on("query", ({ sql, __knexQueryUid, bindings }) => {
|
|
112
|
+
if (!__knexQueryUid)
|
|
113
|
+
return;
|
|
108
114
|
this.logger.time(`Knex${this.name}${__knexQueryUid}`);
|
|
109
115
|
this.logger.debug(
|
|
110
116
|
"[%s] [%s] query begin: %s %j",
|
|
@@ -114,6 +120,8 @@ var Knex = class {
|
|
|
114
120
|
bindings
|
|
115
121
|
);
|
|
116
122
|
}).on("query-response", (response, { sql, __knexQueryUid, bindings }) => {
|
|
123
|
+
if (!__knexQueryUid)
|
|
124
|
+
return;
|
|
117
125
|
this.logger.timeEnd(
|
|
118
126
|
`Knex${this.name}${__knexQueryUid}`,
|
|
119
127
|
"[%s] [%s] query done: %s %j %j",
|
|
@@ -124,6 +132,8 @@ var Knex = class {
|
|
|
124
132
|
response
|
|
125
133
|
);
|
|
126
134
|
}).on("query-error", (_, { __knexQueryUid, sql, bindings }) => {
|
|
135
|
+
if (!__knexQueryUid)
|
|
136
|
+
return;
|
|
127
137
|
this.logger.timeEnd(
|
|
128
138
|
`Knex${this.name}${__knexQueryUid}`,
|
|
129
139
|
"[%s] [%s] query failed: %s %j",
|
|
@@ -157,13 +167,32 @@ var Knex = class {
|
|
|
157
167
|
if (options == null ? void 0 : options.trx)
|
|
158
168
|
return scope(options.trx);
|
|
159
169
|
return this.adapter.transaction(async (trx) => {
|
|
170
|
+
const trxId = crypto.randomUUID();
|
|
160
171
|
try {
|
|
161
172
|
const result = await scope(trx);
|
|
173
|
+
this.logger.debug(
|
|
174
|
+
"[%s] [%s] query begin: %s",
|
|
175
|
+
this.name,
|
|
176
|
+
trxId,
|
|
177
|
+
"COMMIT;"
|
|
178
|
+
);
|
|
162
179
|
await trx.commit();
|
|
180
|
+
this.logger.debug(
|
|
181
|
+
"[%s] [%s] query done: %s",
|
|
182
|
+
this.name,
|
|
183
|
+
trxId,
|
|
184
|
+
"COMMIT;"
|
|
185
|
+
);
|
|
163
186
|
trx.emit("commit");
|
|
164
187
|
return result;
|
|
165
188
|
} catch (error) {
|
|
166
189
|
await trx.rollback(error);
|
|
190
|
+
this.logger.debug(
|
|
191
|
+
"[%s] [%s] query failed: %s",
|
|
192
|
+
this.name,
|
|
193
|
+
trxId,
|
|
194
|
+
"ROLLBACK;"
|
|
195
|
+
);
|
|
167
196
|
trx.emit("rollback", error);
|
|
168
197
|
throw error;
|
|
169
198
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { usePlugin } from '@faasjs/func';
|
|
|
2
2
|
import { Logger } from '@faasjs/logger';
|
|
3
3
|
import { deepMerge } from '@faasjs/deep_merge';
|
|
4
4
|
import knex from 'knex';
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
5
6
|
|
|
6
7
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
7
8
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
@@ -40,12 +41,13 @@ var Knex = class {
|
|
|
40
41
|
await next();
|
|
41
42
|
}
|
|
42
43
|
async onMount(data, next) {
|
|
43
|
-
var _a, _b
|
|
44
|
+
var _a, _b;
|
|
44
45
|
this.logger = data.logger;
|
|
45
46
|
if (global.FaasJS_Knex[this.name]) {
|
|
46
47
|
this.config = global.FaasJS_Knex[this.name].config;
|
|
47
48
|
this.adapter = global.FaasJS_Knex[this.name].adapter;
|
|
48
49
|
this.query = this.adapter;
|
|
50
|
+
this.logger.debug("[%s] use exists adapter", this.name);
|
|
49
51
|
await next();
|
|
50
52
|
return;
|
|
51
53
|
}
|
|
@@ -68,9 +70,11 @@ var Knex = class {
|
|
|
68
70
|
data.config.plugins[this.name].config,
|
|
69
71
|
this.config
|
|
70
72
|
);
|
|
71
|
-
if (this.config.client === "sqlite3")
|
|
73
|
+
if (this.config.client === "sqlite3") {
|
|
72
74
|
this.config.client = "better-sqlite3";
|
|
73
|
-
|
|
75
|
+
this.config.useNullAsDefault = true;
|
|
76
|
+
}
|
|
77
|
+
if (this.config.client === "pg") {
|
|
74
78
|
if (!this.config.pool)
|
|
75
79
|
this.config.pool = /* @__PURE__ */ Object.create(null);
|
|
76
80
|
this.config.pool = Object.assign(
|
|
@@ -99,6 +103,8 @@ var Knex = class {
|
|
|
99
103
|
}
|
|
100
104
|
this.query = this.adapter;
|
|
101
105
|
this.query.on("query", ({ sql, __knexQueryUid, bindings }) => {
|
|
106
|
+
if (!__knexQueryUid)
|
|
107
|
+
return;
|
|
102
108
|
this.logger.time(`Knex${this.name}${__knexQueryUid}`);
|
|
103
109
|
this.logger.debug(
|
|
104
110
|
"[%s] [%s] query begin: %s %j",
|
|
@@ -108,6 +114,8 @@ var Knex = class {
|
|
|
108
114
|
bindings
|
|
109
115
|
);
|
|
110
116
|
}).on("query-response", (response, { sql, __knexQueryUid, bindings }) => {
|
|
117
|
+
if (!__knexQueryUid)
|
|
118
|
+
return;
|
|
111
119
|
this.logger.timeEnd(
|
|
112
120
|
`Knex${this.name}${__knexQueryUid}`,
|
|
113
121
|
"[%s] [%s] query done: %s %j %j",
|
|
@@ -118,6 +126,8 @@ var Knex = class {
|
|
|
118
126
|
response
|
|
119
127
|
);
|
|
120
128
|
}).on("query-error", (_, { __knexQueryUid, sql, bindings }) => {
|
|
129
|
+
if (!__knexQueryUid)
|
|
130
|
+
return;
|
|
121
131
|
this.logger.timeEnd(
|
|
122
132
|
`Knex${this.name}${__knexQueryUid}`,
|
|
123
133
|
"[%s] [%s] query failed: %s %j",
|
|
@@ -151,13 +161,32 @@ var Knex = class {
|
|
|
151
161
|
if (options == null ? void 0 : options.trx)
|
|
152
162
|
return scope(options.trx);
|
|
153
163
|
return this.adapter.transaction(async (trx) => {
|
|
164
|
+
const trxId = randomUUID();
|
|
154
165
|
try {
|
|
155
166
|
const result = await scope(trx);
|
|
167
|
+
this.logger.debug(
|
|
168
|
+
"[%s] [%s] query begin: %s",
|
|
169
|
+
this.name,
|
|
170
|
+
trxId,
|
|
171
|
+
"COMMIT;"
|
|
172
|
+
);
|
|
156
173
|
await trx.commit();
|
|
174
|
+
this.logger.debug(
|
|
175
|
+
"[%s] [%s] query done: %s",
|
|
176
|
+
this.name,
|
|
177
|
+
trxId,
|
|
178
|
+
"COMMIT;"
|
|
179
|
+
);
|
|
157
180
|
trx.emit("commit");
|
|
158
181
|
return result;
|
|
159
182
|
} catch (error) {
|
|
160
183
|
await trx.rollback(error);
|
|
184
|
+
this.logger.debug(
|
|
185
|
+
"[%s] [%s] query failed: %s",
|
|
186
|
+
this.name,
|
|
187
|
+
trxId,
|
|
188
|
+
"ROLLBACK;"
|
|
189
|
+
);
|
|
161
190
|
trx.emit("rollback", error);
|
|
162
191
|
throw error;
|
|
163
192
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/knex",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@faasjs/deep_merge": "1.4.
|
|
25
|
-
"@faasjs/func": "1.4.
|
|
26
|
-
"@faasjs/logger": "1.4.
|
|
24
|
+
"@faasjs/deep_merge": "1.4.1",
|
|
25
|
+
"@faasjs/func": "1.4.1",
|
|
26
|
+
"@faasjs/logger": "1.4.1"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"knex": "*"
|