@faasjs/knex 1.3.2 → 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.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +49 -4
- package/dist/index.mjs +49 -4
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -24,6 +24,11 @@ declare class Knex implements Plugin {
|
|
|
24
24
|
onMount(data: MountData, next: Next): Promise<void>;
|
|
25
25
|
onInvoke(data: InvokeData<any, any, any>, next: Next): Promise<void>;
|
|
26
26
|
raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<Knex$1.Raw<TResult>>;
|
|
27
|
+
/**
|
|
28
|
+
* Wraps a transaction, returning a promise that resolves to the return value of the callback.
|
|
29
|
+
*
|
|
30
|
+
* - Support 'commit' and 'rollback' event.
|
|
31
|
+
*/
|
|
27
32
|
transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult> | void, config?: Knex$1.TransactionConfig, options?: {
|
|
28
33
|
trx?: Knex$1.Transaction;
|
|
29
34
|
}): Promise<TResult | void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,11 @@ declare class Knex implements Plugin {
|
|
|
24
24
|
onMount(data: MountData, next: Next): Promise<void>;
|
|
25
25
|
onInvoke(data: InvokeData<any, any, any>, next: Next): Promise<void>;
|
|
26
26
|
raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<Knex$1.Raw<TResult>>;
|
|
27
|
+
/**
|
|
28
|
+
* Wraps a transaction, returning a promise that resolves to the return value of the callback.
|
|
29
|
+
*
|
|
30
|
+
* - Support 'commit' and 'rollback' event.
|
|
31
|
+
*/
|
|
27
32
|
transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult> | void, config?: Knex$1.TransactionConfig, options?: {
|
|
28
33
|
trx?: Knex$1.Transaction;
|
|
29
34
|
}): Promise<TResult | void>;
|
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",
|
|
@@ -146,12 +156,47 @@ var Knex = class {
|
|
|
146
156
|
throw Error("[Knex] Client not initialized.");
|
|
147
157
|
return this.adapter.raw(sql, bindings);
|
|
148
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Wraps a transaction, returning a promise that resolves to the return value of the callback.
|
|
161
|
+
*
|
|
162
|
+
* - Support 'commit' and 'rollback' event.
|
|
163
|
+
*/
|
|
149
164
|
async transaction(scope, config, options) {
|
|
150
165
|
if (!this.adapter)
|
|
151
166
|
throw Error(`[${this.name}] Client not initialized.`);
|
|
152
167
|
if (options == null ? void 0 : options.trx)
|
|
153
168
|
return scope(options.trx);
|
|
154
|
-
return this.adapter.transaction(
|
|
169
|
+
return this.adapter.transaction(async (trx) => {
|
|
170
|
+
const trxId = crypto.randomUUID();
|
|
171
|
+
try {
|
|
172
|
+
const result = await scope(trx);
|
|
173
|
+
this.logger.debug(
|
|
174
|
+
"[%s] [%s] query begin: %s",
|
|
175
|
+
this.name,
|
|
176
|
+
trxId,
|
|
177
|
+
"COMMIT;"
|
|
178
|
+
);
|
|
179
|
+
await trx.commit();
|
|
180
|
+
this.logger.debug(
|
|
181
|
+
"[%s] [%s] query done: %s",
|
|
182
|
+
this.name,
|
|
183
|
+
trxId,
|
|
184
|
+
"COMMIT;"
|
|
185
|
+
);
|
|
186
|
+
trx.emit("commit");
|
|
187
|
+
return result;
|
|
188
|
+
} catch (error) {
|
|
189
|
+
await trx.rollback(error);
|
|
190
|
+
this.logger.debug(
|
|
191
|
+
"[%s] [%s] query failed: %s",
|
|
192
|
+
this.name,
|
|
193
|
+
trxId,
|
|
194
|
+
"ROLLBACK;"
|
|
195
|
+
);
|
|
196
|
+
trx.emit("rollback", error);
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}, config);
|
|
155
200
|
}
|
|
156
201
|
schema() {
|
|
157
202
|
if (!this.adapter)
|
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",
|
|
@@ -140,12 +150,47 @@ var Knex = class {
|
|
|
140
150
|
throw Error("[Knex] Client not initialized.");
|
|
141
151
|
return this.adapter.raw(sql, bindings);
|
|
142
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Wraps a transaction, returning a promise that resolves to the return value of the callback.
|
|
155
|
+
*
|
|
156
|
+
* - Support 'commit' and 'rollback' event.
|
|
157
|
+
*/
|
|
143
158
|
async transaction(scope, config, options) {
|
|
144
159
|
if (!this.adapter)
|
|
145
160
|
throw Error(`[${this.name}] Client not initialized.`);
|
|
146
161
|
if (options == null ? void 0 : options.trx)
|
|
147
162
|
return scope(options.trx);
|
|
148
|
-
return this.adapter.transaction(
|
|
163
|
+
return this.adapter.transaction(async (trx) => {
|
|
164
|
+
const trxId = randomUUID();
|
|
165
|
+
try {
|
|
166
|
+
const result = await scope(trx);
|
|
167
|
+
this.logger.debug(
|
|
168
|
+
"[%s] [%s] query begin: %s",
|
|
169
|
+
this.name,
|
|
170
|
+
trxId,
|
|
171
|
+
"COMMIT;"
|
|
172
|
+
);
|
|
173
|
+
await trx.commit();
|
|
174
|
+
this.logger.debug(
|
|
175
|
+
"[%s] [%s] query done: %s",
|
|
176
|
+
this.name,
|
|
177
|
+
trxId,
|
|
178
|
+
"COMMIT;"
|
|
179
|
+
);
|
|
180
|
+
trx.emit("commit");
|
|
181
|
+
return result;
|
|
182
|
+
} catch (error) {
|
|
183
|
+
await trx.rollback(error);
|
|
184
|
+
this.logger.debug(
|
|
185
|
+
"[%s] [%s] query failed: %s",
|
|
186
|
+
this.name,
|
|
187
|
+
trxId,
|
|
188
|
+
"ROLLBACK;"
|
|
189
|
+
);
|
|
190
|
+
trx.emit("rollback", error);
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}, config);
|
|
149
194
|
}
|
|
150
195
|
schema() {
|
|
151
196
|
if (!this.adapter)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/knex",
|
|
3
|
-
"version": "1.
|
|
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.
|
|
25
|
-
"@faasjs/func": "1.
|
|
26
|
-
"@faasjs/logger": "1.
|
|
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": "*"
|