@faasjs/knex 1.4.1 → 1.5.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/README.md CHANGED
@@ -1,174 +1,28 @@
1
1
  # @faasjs/knex
2
2
 
3
- [![License: MIT](https://img.shields.io/npm/l/@faasjs/knex.svg)](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/knex/LICENSE)
4
- [![NPM Stable Version](https://img.shields.io/npm/v/@faasjs/knex/stable.svg)](https://www.npmjs.com/package/@faasjs/knex)
5
- [![NPM Beta Version](https://img.shields.io/npm/v/@faasjs/knex/beta.svg)](https://www.npmjs.com/package/@faasjs/knex)
6
-
7
3
  FaasJS's sql plugin, base on [Knex](https://knexjs.org/).
8
4
 
5
+ [![License: MIT](https://img.shields.io/npm/l/@faasjs/knex.svg)](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/knex/LICENSE)
6
+ [![NPM Version](https://img.shields.io/npm/v/@faasjs/knex.svg)](https://www.npmjs.com/package/@faasjs/knex)
7
+
9
8
  ## Install
10
9
 
11
10
  ```sh
12
11
  npm install @faasjs/knex
13
12
  ```
14
13
 
15
- ## Modules
16
-
17
- ### Classes
14
+ ## Classes
18
15
 
19
16
  - [Knex](classes/Knex.md)
20
17
 
21
- ### Type Aliases
22
-
23
- - [KnexConfig](#knexconfig)
24
-
25
- ### Functions
26
-
27
- - [originKnex](#originknex)
28
- - [query](#query)
29
- - [raw](#raw)
30
- - [transaction](#transaction)
31
- - [useKnex](#useknex)
32
-
33
18
  ## Type Aliases
34
19
 
35
- ### KnexConfig
36
-
37
- Ƭ **KnexConfig**: `Object`
38
-
39
- #### Type declaration
40
-
41
- | Name | Type |
42
- | :------ | :------ |
43
- | `config?` | `OriginKnex.Config` |
44
- | `name?` | `string` |
20
+ - [KnexConfig](type-aliases/KnexConfig.md)
45
21
 
46
22
  ## Functions
47
23
 
48
- ### originKnex
49
-
50
- **originKnex**\<`TRecord`, `TResult`\>(`config`): `Knex`\<`TRecord`, `TResult`\>
51
-
52
- Origin [knex](https://knexjs.org/) instance.
53
-
54
- #### Type parameters
55
-
56
- | Name | Type |
57
- | :------ | :------ |
58
- | `TRecord` | extends `Object` = `any` |
59
- | `TResult` | `unknown`[] |
60
-
61
- #### Parameters
62
-
63
- | Name | Type |
64
- | :------ | :------ |
65
- | `config` | `string` \| `Config`\<`any`\> |
66
-
67
- #### Returns
68
-
69
- `Knex`\<`TRecord`, `TResult`\>
70
-
71
- ___
72
-
73
- ### query
74
-
75
- ▸ **query**\<`TName`\>(`table`): `OriginKnex.QueryBuilder`\<`OriginKnex.TableType`\<`TName`\>, \{ `_aliases`: {} ; `_base`: `OriginKnex.ResolveTableType`\<`OriginKnex.TableType`\<`TName`\>, ``"base"``\> ; `_hasSelection`: ``false`` ; `_intersectProps`: {} ; `_keys`: `never` ; `_single`: ``false`` ; `_unionProps`: `never` }[]\>
76
-
77
- #### Type parameters
78
-
79
- | Name | Type |
80
- | :------ | :------ |
81
- | `TName` | extends ``"test"`` |
82
-
83
- #### Parameters
84
-
85
- | Name | Type |
86
- | :------ | :------ |
87
- | `table` | `TName` |
88
-
89
- #### Returns
90
-
91
- `OriginKnex.QueryBuilder`\<`OriginKnex.TableType`\<`TName`\>, \{ `_aliases`: {} ; `_base`: `OriginKnex.ResolveTableType`\<`OriginKnex.TableType`\<`TName`\>, ``"base"``\> ; `_hasSelection`: ``false`` ; `_intersectProps`: {} ; `_keys`: `never` ; `_single`: ``false`` ; `_unionProps`: `never` }[]\>
92
-
93
- ▸ **query**\<`TName`, `TResult`\>(`table`): `OriginKnex.QueryBuilder`\<`TName`, `TResult`\>
94
-
95
- #### Type parameters
96
-
97
- | Name | Type |
98
- | :------ | :------ |
99
- | `TName` | extends `Object` = `any` |
100
- | `TResult` | `any`[] |
101
-
102
- #### Parameters
103
-
104
- | Name | Type |
105
- | :------ | :------ |
106
- | `table` | `string` |
107
-
108
- #### Returns
109
-
110
- `OriginKnex.QueryBuilder`\<`TName`, `TResult`\>
111
-
112
- ___
113
-
114
- ### raw
115
-
116
- ▸ **raw**\<`TResult`\>(`sql`, `bindings?`): `Promise`\<`TResult`\>
117
-
118
- #### Type parameters
119
-
120
- | Name | Type |
121
- | :------ | :------ |
122
- | `TResult` | `any` |
123
-
124
- #### Parameters
125
-
126
- | Name | Type | Default value |
127
- | :------ | :------ | :------ |
128
- | `sql` | `string` | `undefined` |
129
- | `bindings` | `ValueDict` \| `RawBinding`[] | `[]` |
130
-
131
- #### Returns
132
-
133
- `Promise`\<`TResult`\>
134
-
135
- ___
136
-
137
- ### transaction
138
-
139
- ▸ **transaction**\<`TResult`\>(`scope`, `config?`, `options?`): `Promise`\<`TResult` \| `void`\>
140
-
141
- #### Type parameters
142
-
143
- | Name | Type |
144
- | :------ | :------ |
145
- | `TResult` | `any` |
146
-
147
- #### Parameters
148
-
149
- | Name | Type |
150
- | :------ | :------ |
151
- | `scope` | (`trx`: `Transaction`\<`any`, `any`\>) => `void` \| `Promise`\<`TResult`\> |
152
- | `config?` | `TransactionConfig` |
153
- | `options?` | `Object` |
154
- | `options.trx?` | `Transaction`\<`any`, `any`[]\> |
155
-
156
- #### Returns
157
-
158
- `Promise`\<`TResult` \| `void`\>
159
-
160
- ___
161
-
162
- ### useKnex
163
-
164
- ▸ **useKnex**(`config?`): `UseifyPlugin`\<[`Knex`](classes/Knex.md)\>
165
-
166
- #### Parameters
167
-
168
- | Name | Type |
169
- | :------ | :------ |
170
- | `config?` | [`KnexConfig`](#knexconfig) |
171
-
172
- #### Returns
173
-
174
- `UseifyPlugin`\<[`Knex`](classes/Knex.md)\>
24
+ - [originKnex](functions/originKnex.md)
25
+ - [query](functions/query.md)
26
+ - [raw](functions/raw.md)
27
+ - [transaction](functions/transaction.md)
28
+ - [useKnex](functions/useKnex.md)
package/dist/index.d.mts CHANGED
@@ -3,6 +3,20 @@ import { Logger } from '@faasjs/logger';
3
3
  import knex, { Knex as Knex$1 } from 'knex';
4
4
  export { Knex as OriginKnex } from 'knex';
5
5
 
6
+ /**
7
+ * FaasJS's sql plugin, base on [Knex](https://knexjs.org/).
8
+ *
9
+ * [![License: MIT](https://img.shields.io/npm/l/@faasjs/knex.svg)](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/knex/LICENSE)
10
+ * [![NPM Version](https://img.shields.io/npm/v/@faasjs/knex.svg)](https://www.npmjs.com/package/@faasjs/knex)
11
+ *
12
+ * ## Install
13
+ *
14
+ * ```sh
15
+ * npm install @faasjs/knex
16
+ * ```
17
+ * @packageDocumentation
18
+ */
19
+
6
20
  /**
7
21
  * Origin [knex](https://knexjs.org/) instance.
8
22
  */
@@ -29,9 +43,9 @@ declare class Knex implements Plugin {
29
43
  *
30
44
  * - Support 'commit' and 'rollback' event.
31
45
  */
32
- transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult> | void, config?: Knex$1.TransactionConfig, options?: {
46
+ transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult>, config?: Knex$1.TransactionConfig, options?: {
33
47
  trx?: Knex$1.Transaction;
34
- }): Promise<TResult | void>;
48
+ }): Promise<TResult>;
35
49
  schema(): Knex$1.SchemaBuilder;
36
50
  quit(): Promise<void>;
37
51
  }
@@ -46,9 +60,9 @@ declare function query<TName extends Knex$1.TableNames>(table: TName): Knex$1.Qu
46
60
  _unionProps: never;
47
61
  }[]>;
48
62
  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> | void, config?: Knex$1.TransactionConfig, options?: {
63
+ declare function transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult>, config?: Knex$1.TransactionConfig, options?: {
50
64
  trx?: Knex$1.Transaction;
51
- }): Promise<TResult | void>;
52
- declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<TResult>;
65
+ }): Promise<TResult>;
66
+ declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<Knex$1.Raw<TResult>>;
53
67
 
54
68
  export { Knex, type KnexConfig, originKnex, query, raw, transaction, useKnex };
package/dist/index.d.ts CHANGED
@@ -3,6 +3,20 @@ import { Logger } from '@faasjs/logger';
3
3
  import knex, { Knex as Knex$1 } from 'knex';
4
4
  export { Knex as OriginKnex } from 'knex';
5
5
 
6
+ /**
7
+ * FaasJS's sql plugin, base on [Knex](https://knexjs.org/).
8
+ *
9
+ * [![License: MIT](https://img.shields.io/npm/l/@faasjs/knex.svg)](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/knex/LICENSE)
10
+ * [![NPM Version](https://img.shields.io/npm/v/@faasjs/knex.svg)](https://www.npmjs.com/package/@faasjs/knex)
11
+ *
12
+ * ## Install
13
+ *
14
+ * ```sh
15
+ * npm install @faasjs/knex
16
+ * ```
17
+ * @packageDocumentation
18
+ */
19
+
6
20
  /**
7
21
  * Origin [knex](https://knexjs.org/) instance.
8
22
  */
@@ -29,9 +43,9 @@ declare class Knex implements Plugin {
29
43
  *
30
44
  * - Support 'commit' and 'rollback' event.
31
45
  */
32
- transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult> | void, config?: Knex$1.TransactionConfig, options?: {
46
+ transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult>, config?: Knex$1.TransactionConfig, options?: {
33
47
  trx?: Knex$1.Transaction;
34
- }): Promise<TResult | void>;
48
+ }): Promise<TResult>;
35
49
  schema(): Knex$1.SchemaBuilder;
36
50
  quit(): Promise<void>;
37
51
  }
@@ -46,9 +60,9 @@ declare function query<TName extends Knex$1.TableNames>(table: TName): Knex$1.Qu
46
60
  _unionProps: never;
47
61
  }[]>;
48
62
  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> | void, config?: Knex$1.TransactionConfig, options?: {
63
+ declare function transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult>, config?: Knex$1.TransactionConfig, options?: {
50
64
  trx?: Knex$1.Transaction;
51
- }): Promise<TResult | void>;
52
- declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<TResult>;
65
+ }): Promise<TResult>;
66
+ declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<Knex$1.Raw<TResult>>;
53
67
 
54
68
  export { Knex, type KnexConfig, originKnex, query, raw, transaction, useKnex };
package/dist/index.js CHANGED
@@ -166,37 +166,40 @@ var Knex = class {
166
166
  throw Error(`[${this.name}] Client not initialized.`);
167
167
  if (options == null ? void 0 : options.trx)
168
168
  return scope(options.trx);
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();
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()) {
180
175
  this.logger.debug(
181
- "[%s] [%s] query done: %s",
176
+ "[%s] [%s] transaction has been finished in scope",
182
177
  this.name,
183
- trxId,
184
- "COMMIT;"
178
+ trxId
185
179
  );
186
- trx.emit("commit");
187
180
  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
181
  }
199
- }, config);
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
+ }
200
203
  }
201
204
  schema() {
202
205
  if (!this.adapter)
package/dist/index.mjs CHANGED
@@ -160,37 +160,40 @@ var Knex = class {
160
160
  throw Error(`[${this.name}] Client not initialized.`);
161
161
  if (options == null ? void 0 : options.trx)
162
162
  return scope(options.trx);
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();
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()) {
174
169
  this.logger.debug(
175
- "[%s] [%s] query done: %s",
170
+ "[%s] [%s] transaction has been finished in scope",
176
171
  this.name,
177
- trxId,
178
- "COMMIT;"
172
+ trxId
179
173
  );
180
- trx.emit("commit");
181
174
  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
175
  }
193
- }, config);
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
+ }
194
197
  }
195
198
  schema() {
196
199
  if (!this.adapter)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/knex",
3
- "version": "1.4.1",
3
+ "version": "1.5.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.4.1",
25
- "@faasjs/func": "1.4.1",
26
- "@faasjs/logger": "1.4.1"
24
+ "@faasjs/deep_merge": "1.5.0",
25
+ "@faasjs/func": "1.5.0",
26
+ "@faasjs/logger": "1.5.0"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "knex": "*"