@faasjs/knex 1.4.0 → 1.4.2
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 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +45 -13
- package/dist/index.mjs +45 -13
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -29,9 +29,9 @@ declare class Knex implements Plugin {
|
|
|
29
29
|
*
|
|
30
30
|
* - Support 'commit' and 'rollback' event.
|
|
31
31
|
*/
|
|
32
|
-
transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult
|
|
32
|
+
transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult>, config?: Knex$1.TransactionConfig, options?: {
|
|
33
33
|
trx?: Knex$1.Transaction;
|
|
34
|
-
}): Promise<TResult
|
|
34
|
+
}): Promise<TResult>;
|
|
35
35
|
schema(): Knex$1.SchemaBuilder;
|
|
36
36
|
quit(): Promise<void>;
|
|
37
37
|
}
|
|
@@ -46,9 +46,9 @@ declare function query<TName extends Knex$1.TableNames>(table: TName): Knex$1.Qu
|
|
|
46
46
|
_unionProps: never;
|
|
47
47
|
}[]>;
|
|
48
48
|
declare function query<TName extends {} = any, TResult = any[]>(table: string): Knex$1.QueryBuilder<TName, TResult>;
|
|
49
|
-
declare function transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult
|
|
49
|
+
declare function transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult>, config?: Knex$1.TransactionConfig, options?: {
|
|
50
50
|
trx?: Knex$1.Transaction;
|
|
51
|
-
}): Promise<TResult
|
|
52
|
-
declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<TResult
|
|
51
|
+
}): Promise<TResult>;
|
|
52
|
+
declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<Knex$1.Raw<TResult>>;
|
|
53
53
|
|
|
54
54
|
export { Knex, type KnexConfig, originKnex, query, raw, transaction, useKnex };
|
package/dist/index.d.ts
CHANGED
|
@@ -29,9 +29,9 @@ declare class Knex implements Plugin {
|
|
|
29
29
|
*
|
|
30
30
|
* - Support 'commit' and 'rollback' event.
|
|
31
31
|
*/
|
|
32
|
-
transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult
|
|
32
|
+
transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult>, config?: Knex$1.TransactionConfig, options?: {
|
|
33
33
|
trx?: Knex$1.Transaction;
|
|
34
|
-
}): Promise<TResult
|
|
34
|
+
}): Promise<TResult>;
|
|
35
35
|
schema(): Knex$1.SchemaBuilder;
|
|
36
36
|
quit(): Promise<void>;
|
|
37
37
|
}
|
|
@@ -46,9 +46,9 @@ declare function query<TName extends Knex$1.TableNames>(table: TName): Knex$1.Qu
|
|
|
46
46
|
_unionProps: never;
|
|
47
47
|
}[]>;
|
|
48
48
|
declare function query<TName extends {} = any, TResult = any[]>(table: string): Knex$1.QueryBuilder<TName, TResult>;
|
|
49
|
-
declare function transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult
|
|
49
|
+
declare function transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult>, config?: Knex$1.TransactionConfig, options?: {
|
|
50
50
|
trx?: Knex$1.Transaction;
|
|
51
|
-
}): Promise<TResult
|
|
52
|
-
declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<TResult
|
|
51
|
+
}): Promise<TResult>;
|
|
52
|
+
declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<Knex$1.Raw<TResult>>;
|
|
53
53
|
|
|
54
54
|
export { Knex, type KnexConfig, originKnex, query, raw, transaction, useKnex };
|
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",
|
|
@@ -156,18 +166,40 @@ var Knex = class {
|
|
|
156
166
|
throw Error(`[${this.name}] Client not initialized.`);
|
|
157
167
|
if (options == null ? void 0 : options.trx)
|
|
158
168
|
return scope(options.trx);
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
169
|
+
const trx = await this.adapter.transaction(config);
|
|
170
|
+
const trxId = crypto.randomUUID();
|
|
171
|
+
this.logger.debug("[%s] [%s] transaction begin", this.name, trxId);
|
|
172
|
+
try {
|
|
173
|
+
const result = await scope(trx);
|
|
174
|
+
if (trx.isCompleted()) {
|
|
175
|
+
this.logger.debug(
|
|
176
|
+
"[%s] [%s] transaction has been finished in scope",
|
|
177
|
+
this.name,
|
|
178
|
+
trxId
|
|
179
|
+
);
|
|
164
180
|
return result;
|
|
165
|
-
} catch (error) {
|
|
166
|
-
await trx.rollback(error);
|
|
167
|
-
trx.emit("rollback", error);
|
|
168
|
-
throw error;
|
|
169
181
|
}
|
|
170
|
-
|
|
182
|
+
this.logger.debug("[%s] [%s] transaction begin commit", this.name, trxId);
|
|
183
|
+
await trx.commit();
|
|
184
|
+
this.logger.debug(
|
|
185
|
+
"[%s] [%s] transaction committed: %j",
|
|
186
|
+
this.name,
|
|
187
|
+
trxId,
|
|
188
|
+
result
|
|
189
|
+
);
|
|
190
|
+
trx.emit("commit");
|
|
191
|
+
return result;
|
|
192
|
+
} catch (error) {
|
|
193
|
+
await trx.rollback(error);
|
|
194
|
+
this.logger.error(
|
|
195
|
+
"[%s] [%s] transaction rollback: %s",
|
|
196
|
+
this.name,
|
|
197
|
+
trxId,
|
|
198
|
+
error
|
|
199
|
+
);
|
|
200
|
+
trx.emit("rollback", error);
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
171
203
|
}
|
|
172
204
|
schema() {
|
|
173
205
|
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",
|
|
@@ -150,18 +160,40 @@ var Knex = class {
|
|
|
150
160
|
throw Error(`[${this.name}] Client not initialized.`);
|
|
151
161
|
if (options == null ? void 0 : options.trx)
|
|
152
162
|
return scope(options.trx);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
163
|
+
const trx = await this.adapter.transaction(config);
|
|
164
|
+
const trxId = randomUUID();
|
|
165
|
+
this.logger.debug("[%s] [%s] transaction begin", this.name, trxId);
|
|
166
|
+
try {
|
|
167
|
+
const result = await scope(trx);
|
|
168
|
+
if (trx.isCompleted()) {
|
|
169
|
+
this.logger.debug(
|
|
170
|
+
"[%s] [%s] transaction has been finished in scope",
|
|
171
|
+
this.name,
|
|
172
|
+
trxId
|
|
173
|
+
);
|
|
158
174
|
return result;
|
|
159
|
-
} catch (error) {
|
|
160
|
-
await trx.rollback(error);
|
|
161
|
-
trx.emit("rollback", error);
|
|
162
|
-
throw error;
|
|
163
175
|
}
|
|
164
|
-
|
|
176
|
+
this.logger.debug("[%s] [%s] transaction begin commit", this.name, trxId);
|
|
177
|
+
await trx.commit();
|
|
178
|
+
this.logger.debug(
|
|
179
|
+
"[%s] [%s] transaction committed: %j",
|
|
180
|
+
this.name,
|
|
181
|
+
trxId,
|
|
182
|
+
result
|
|
183
|
+
);
|
|
184
|
+
trx.emit("commit");
|
|
185
|
+
return result;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
await trx.rollback(error);
|
|
188
|
+
this.logger.error(
|
|
189
|
+
"[%s] [%s] transaction rollback: %s",
|
|
190
|
+
this.name,
|
|
191
|
+
trxId,
|
|
192
|
+
error
|
|
193
|
+
);
|
|
194
|
+
trx.emit("rollback", error);
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
165
197
|
}
|
|
166
198
|
schema() {
|
|
167
199
|
if (!this.adapter)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/knex",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
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.2",
|
|
25
|
+
"@faasjs/func": "1.4.2",
|
|
26
|
+
"@faasjs/logger": "1.4.2"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"knex": "*"
|