@faasjs/knex 1.3.1 → 1.4.0

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 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
@@ -146,12 +146,28 @@ var Knex = class {
146
146
  throw Error("[Knex] Client not initialized.");
147
147
  return this.adapter.raw(sql, bindings);
148
148
  }
149
+ /**
150
+ * Wraps a transaction, returning a promise that resolves to the return value of the callback.
151
+ *
152
+ * - Support 'commit' and 'rollback' event.
153
+ */
149
154
  async transaction(scope, config, options) {
150
155
  if (!this.adapter)
151
156
  throw Error(`[${this.name}] Client not initialized.`);
152
157
  if (options == null ? void 0 : options.trx)
153
158
  return scope(options.trx);
154
- return this.adapter.transaction(scope, config);
159
+ return this.adapter.transaction(async (trx) => {
160
+ try {
161
+ const result = await scope(trx);
162
+ await trx.commit();
163
+ trx.emit("commit");
164
+ return result;
165
+ } catch (error) {
166
+ await trx.rollback(error);
167
+ trx.emit("rollback", error);
168
+ throw error;
169
+ }
170
+ }, config);
155
171
  }
156
172
  schema() {
157
173
  if (!this.adapter)
package/dist/index.mjs CHANGED
@@ -140,12 +140,28 @@ var Knex = class {
140
140
  throw Error("[Knex] Client not initialized.");
141
141
  return this.adapter.raw(sql, bindings);
142
142
  }
143
+ /**
144
+ * Wraps a transaction, returning a promise that resolves to the return value of the callback.
145
+ *
146
+ * - Support 'commit' and 'rollback' event.
147
+ */
143
148
  async transaction(scope, config, options) {
144
149
  if (!this.adapter)
145
150
  throw Error(`[${this.name}] Client not initialized.`);
146
151
  if (options == null ? void 0 : options.trx)
147
152
  return scope(options.trx);
148
- return this.adapter.transaction(scope, config);
153
+ return this.adapter.transaction(async (trx) => {
154
+ try {
155
+ const result = await scope(trx);
156
+ await trx.commit();
157
+ trx.emit("commit");
158
+ return result;
159
+ } catch (error) {
160
+ await trx.rollback(error);
161
+ trx.emit("rollback", error);
162
+ throw error;
163
+ }
164
+ }, config);
149
165
  }
150
166
  schema() {
151
167
  if (!this.adapter)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/knex",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
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.3.1",
25
- "@faasjs/func": "1.3.1",
26
- "@faasjs/logger": "1.3.1"
24
+ "@faasjs/deep_merge": "1.4.0",
25
+ "@faasjs/func": "1.4.0",
26
+ "@faasjs/logger": "1.4.0"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "knex": "*"