@faasjs/knex 3.0.0 → 3.1.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 CHANGED
@@ -27,7 +27,7 @@ type KnexConfig = {
27
27
  config?: Knex$1.Config;
28
28
  };
29
29
  declare class Knex implements Plugin {
30
- readonly type: string;
30
+ readonly type = "knex";
31
31
  readonly name: string;
32
32
  config: Knex$1.Config;
33
33
  adapter: Knex$1;
package/dist/index.d.ts CHANGED
@@ -27,7 +27,7 @@ type KnexConfig = {
27
27
  config?: Knex$1.Config;
28
28
  };
29
29
  declare class Knex implements Plugin {
30
- readonly type: string;
30
+ readonly type = "knex";
31
31
  readonly name: string;
32
32
  config: Knex$1.Config;
33
33
  adapter: Knex$1;
package/dist/index.js CHANGED
@@ -16,12 +16,12 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
16
16
  throw Error('Dynamic require of "' + x + '" is not supported');
17
17
  });
18
18
  var originKnex = knex__default.default;
19
- var Name = "knex";
19
+ var Name = "Knex";
20
20
  if (!global.FaasJS_Knex) {
21
21
  global.FaasJS_Knex = {};
22
22
  }
23
23
  var Knex = class {
24
- type = Name;
24
+ type = "knex";
25
25
  name = Name;
26
26
  config;
27
27
  adapter;
@@ -29,10 +29,9 @@ var Knex = class {
29
29
  logger;
30
30
  constructor(config) {
31
31
  if (config) {
32
- this.name = config.name || this.type;
32
+ this.name = config.name || this.name;
33
33
  this.config = config.config || /* @__PURE__ */ Object.create(null);
34
34
  } else {
35
- this.name = this.type;
36
35
  this.config = /* @__PURE__ */ Object.create(null);
37
36
  }
38
37
  }
@@ -42,7 +41,7 @@ var Knex = class {
42
41
  this.config = global.FaasJS_Knex[this.name].config;
43
42
  this.adapter = global.FaasJS_Knex[this.name].adapter;
44
43
  this.query = this.adapter;
45
- this.logger.debug("[%s] use exists adapter", this.name);
44
+ this.logger.debug("use exists adapter");
46
45
  await next();
47
46
  return;
48
47
  }
@@ -64,25 +63,46 @@ var Knex = class {
64
63
  data.config.plugins[this.name].config,
65
64
  this.config
66
65
  );
67
- if (this.config.client === "sqlite3") {
68
- this.config.client = "better-sqlite3";
69
- this.config.useNullAsDefault = true;
70
- }
71
- if (this.config.client === "pg") {
72
- if (!this.config.pool) this.config.pool = /* @__PURE__ */ Object.create(null);
73
- this.config.pool = Object.assign(
74
- {
75
- propagateCreateError: false,
76
- min: 0,
77
- max: 10,
78
- acquireTimeoutMillis: 5e3,
79
- idleTimeoutMillis: 3e4
80
- },
81
- this.config.pool
82
- );
83
- if (typeof this.config.connection === "string" && !this.config.connection.includes("json=true"))
84
- this.config.connection = `${this.config.connection}?json=true`;
66
+ switch (this.config.client) {
67
+ case "sqlite3":
68
+ this.config.client = "better-sqlite3";
69
+ this.config.useNullAsDefault = true;
70
+ break;
71
+ case "pg":
72
+ if (!this.config.pool) this.config.pool = /* @__PURE__ */ Object.create(null);
73
+ this.config.pool = Object.assign(
74
+ {
75
+ propagateCreateError: false,
76
+ min: 0,
77
+ max: 10,
78
+ acquireTimeoutMillis: 5e3,
79
+ idleTimeoutMillis: 3e4
80
+ },
81
+ this.config.pool
82
+ );
83
+ if (typeof this.config.connection === "string" && !this.config.connection.includes("json=true"))
84
+ this.config.connection = `${this.config.connection}?json=true`;
85
+ break;
86
+ default:
87
+ if (typeof this.config.client === "string") {
88
+ if (this.config.client.startsWith("npm:")) {
89
+ const client = __require(this.config.client.replace("npm:", ""));
90
+ if (!client) throw Error(`Invalid client: ${this.config.client}`);
91
+ if (typeof client === "function") {
92
+ this.config.client = client;
93
+ break;
94
+ }
95
+ if (client.default && typeof client.default === "function") {
96
+ this.config.client = client.default;
97
+ break;
98
+ }
99
+ console.log(client);
100
+ throw Error(`Invalid client: ${this.config.client}`);
101
+ }
102
+ }
103
+ break;
85
104
  }
105
+ console.log(this.config);
86
106
  this.adapter = knex__default.default(this.config);
87
107
  if (this.config.client === "pg") {
88
108
  const pg = __require("pg");
@@ -102,8 +122,7 @@ var Knex = class {
102
122
  if (!__knexQueryUid) return;
103
123
  this.logger.time(`Knex${this.name}${__knexQueryUid}`);
104
124
  this.logger.debug(
105
- "[%s] [%s] query begin: %s %j",
106
- this.name,
125
+ "[%s] query begin: %s %j",
107
126
  __knexQueryUid,
108
127
  sql,
109
128
  bindings
@@ -112,8 +131,7 @@ var Knex = class {
112
131
  if (!__knexQueryUid) return;
113
132
  this.logger.timeEnd(
114
133
  `Knex${this.name}${__knexQueryUid}`,
115
- "[%s] [%s] query done: %s %j %j",
116
- this.name,
134
+ "[%s] query done: %s %j %j",
117
135
  __knexQueryUid,
118
136
  sql,
119
137
  bindings,
@@ -123,14 +141,13 @@ var Knex = class {
123
141
  if (!__knexQueryUid) return;
124
142
  this.logger.timeEnd(
125
143
  `Knex${this.name}${__knexQueryUid}`,
126
- "[%s] [%s] query failed: %s %j",
127
- this.name,
144
+ "[%s] query failed: %s %j",
128
145
  __knexQueryUid,
129
146
  sql,
130
147
  bindings
131
148
  );
132
149
  });
133
- data.logger.debug("[%s] connected", this.name);
150
+ data.logger.debug("connected");
134
151
  global.FaasJS_Knex[this.name] = this;
135
152
  await next();
136
153
  }
@@ -152,35 +169,21 @@ var Knex = class {
152
169
  if (options?.trx) return scope(options.trx);
153
170
  const trx = await this.adapter.transaction(config);
154
171
  const trxId = crypto.randomUUID();
155
- this.logger.debug("[%s] [%s] transaction begin", this.name, trxId);
172
+ this.logger.debug("[%s] transaction begin", trxId);
156
173
  try {
157
174
  const result = await scope(trx);
158
175
  if (trx.isCompleted()) {
159
- this.logger.debug(
160
- "[%s] [%s] transaction has been finished in scope",
161
- this.name,
162
- trxId
163
- );
176
+ this.logger.debug("[%s] transaction has been finished in scope", trxId);
164
177
  return result;
165
178
  }
166
- this.logger.debug("[%s] [%s] transaction begin commit", this.name, trxId);
179
+ this.logger.debug("[%s] transaction begin commit", trxId);
167
180
  await trx.commit();
168
- this.logger.debug(
169
- "[%s] [%s] transaction committed: %j",
170
- this.name,
171
- trxId,
172
- result
173
- );
181
+ this.logger.debug("[%s] transaction committed: %j", trxId, result);
174
182
  trx.emit("commit");
175
183
  return result;
176
184
  } catch (error) {
177
185
  await trx.rollback(error);
178
- this.logger.error(
179
- "[%s] [%s] transaction rollback: %s",
180
- this.name,
181
- trxId,
182
- error
183
- );
186
+ this.logger.error("[%s] transaction rollback: %s", trxId, error);
184
187
  trx.emit("rollback", error);
185
188
  throw error;
186
189
  }
package/dist/index.mjs CHANGED
@@ -10,12 +10,12 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
10
10
  throw Error('Dynamic require of "' + x + '" is not supported');
11
11
  });
12
12
  var originKnex = knex;
13
- var Name = "knex";
13
+ var Name = "Knex";
14
14
  if (!global.FaasJS_Knex) {
15
15
  global.FaasJS_Knex = {};
16
16
  }
17
17
  var Knex = class {
18
- type = Name;
18
+ type = "knex";
19
19
  name = Name;
20
20
  config;
21
21
  adapter;
@@ -23,10 +23,9 @@ var Knex = class {
23
23
  logger;
24
24
  constructor(config) {
25
25
  if (config) {
26
- this.name = config.name || this.type;
26
+ this.name = config.name || this.name;
27
27
  this.config = config.config || /* @__PURE__ */ Object.create(null);
28
28
  } else {
29
- this.name = this.type;
30
29
  this.config = /* @__PURE__ */ Object.create(null);
31
30
  }
32
31
  }
@@ -36,7 +35,7 @@ var Knex = class {
36
35
  this.config = global.FaasJS_Knex[this.name].config;
37
36
  this.adapter = global.FaasJS_Knex[this.name].adapter;
38
37
  this.query = this.adapter;
39
- this.logger.debug("[%s] use exists adapter", this.name);
38
+ this.logger.debug("use exists adapter");
40
39
  await next();
41
40
  return;
42
41
  }
@@ -58,25 +57,46 @@ var Knex = class {
58
57
  data.config.plugins[this.name].config,
59
58
  this.config
60
59
  );
61
- if (this.config.client === "sqlite3") {
62
- this.config.client = "better-sqlite3";
63
- this.config.useNullAsDefault = true;
64
- }
65
- if (this.config.client === "pg") {
66
- if (!this.config.pool) this.config.pool = /* @__PURE__ */ Object.create(null);
67
- this.config.pool = Object.assign(
68
- {
69
- propagateCreateError: false,
70
- min: 0,
71
- max: 10,
72
- acquireTimeoutMillis: 5e3,
73
- idleTimeoutMillis: 3e4
74
- },
75
- this.config.pool
76
- );
77
- if (typeof this.config.connection === "string" && !this.config.connection.includes("json=true"))
78
- this.config.connection = `${this.config.connection}?json=true`;
60
+ switch (this.config.client) {
61
+ case "sqlite3":
62
+ this.config.client = "better-sqlite3";
63
+ this.config.useNullAsDefault = true;
64
+ break;
65
+ case "pg":
66
+ if (!this.config.pool) this.config.pool = /* @__PURE__ */ Object.create(null);
67
+ this.config.pool = Object.assign(
68
+ {
69
+ propagateCreateError: false,
70
+ min: 0,
71
+ max: 10,
72
+ acquireTimeoutMillis: 5e3,
73
+ idleTimeoutMillis: 3e4
74
+ },
75
+ this.config.pool
76
+ );
77
+ if (typeof this.config.connection === "string" && !this.config.connection.includes("json=true"))
78
+ this.config.connection = `${this.config.connection}?json=true`;
79
+ break;
80
+ default:
81
+ if (typeof this.config.client === "string") {
82
+ if (this.config.client.startsWith("npm:")) {
83
+ const client = __require(this.config.client.replace("npm:", ""));
84
+ if (!client) throw Error(`Invalid client: ${this.config.client}`);
85
+ if (typeof client === "function") {
86
+ this.config.client = client;
87
+ break;
88
+ }
89
+ if (client.default && typeof client.default === "function") {
90
+ this.config.client = client.default;
91
+ break;
92
+ }
93
+ console.log(client);
94
+ throw Error(`Invalid client: ${this.config.client}`);
95
+ }
96
+ }
97
+ break;
79
98
  }
99
+ console.log(this.config);
80
100
  this.adapter = knex(this.config);
81
101
  if (this.config.client === "pg") {
82
102
  const pg = __require("pg");
@@ -96,8 +116,7 @@ var Knex = class {
96
116
  if (!__knexQueryUid) return;
97
117
  this.logger.time(`Knex${this.name}${__knexQueryUid}`);
98
118
  this.logger.debug(
99
- "[%s] [%s] query begin: %s %j",
100
- this.name,
119
+ "[%s] query begin: %s %j",
101
120
  __knexQueryUid,
102
121
  sql,
103
122
  bindings
@@ -106,8 +125,7 @@ var Knex = class {
106
125
  if (!__knexQueryUid) return;
107
126
  this.logger.timeEnd(
108
127
  `Knex${this.name}${__knexQueryUid}`,
109
- "[%s] [%s] query done: %s %j %j",
110
- this.name,
128
+ "[%s] query done: %s %j %j",
111
129
  __knexQueryUid,
112
130
  sql,
113
131
  bindings,
@@ -117,14 +135,13 @@ var Knex = class {
117
135
  if (!__knexQueryUid) return;
118
136
  this.logger.timeEnd(
119
137
  `Knex${this.name}${__knexQueryUid}`,
120
- "[%s] [%s] query failed: %s %j",
121
- this.name,
138
+ "[%s] query failed: %s %j",
122
139
  __knexQueryUid,
123
140
  sql,
124
141
  bindings
125
142
  );
126
143
  });
127
- data.logger.debug("[%s] connected", this.name);
144
+ data.logger.debug("connected");
128
145
  global.FaasJS_Knex[this.name] = this;
129
146
  await next();
130
147
  }
@@ -146,35 +163,21 @@ var Knex = class {
146
163
  if (options?.trx) return scope(options.trx);
147
164
  const trx = await this.adapter.transaction(config);
148
165
  const trxId = randomUUID();
149
- this.logger.debug("[%s] [%s] transaction begin", this.name, trxId);
166
+ this.logger.debug("[%s] transaction begin", trxId);
150
167
  try {
151
168
  const result = await scope(trx);
152
169
  if (trx.isCompleted()) {
153
- this.logger.debug(
154
- "[%s] [%s] transaction has been finished in scope",
155
- this.name,
156
- trxId
157
- );
170
+ this.logger.debug("[%s] transaction has been finished in scope", trxId);
158
171
  return result;
159
172
  }
160
- this.logger.debug("[%s] [%s] transaction begin commit", this.name, trxId);
173
+ this.logger.debug("[%s] transaction begin commit", trxId);
161
174
  await trx.commit();
162
- this.logger.debug(
163
- "[%s] [%s] transaction committed: %j",
164
- this.name,
165
- trxId,
166
- result
167
- );
175
+ this.logger.debug("[%s] transaction committed: %j", trxId, result);
168
176
  trx.emit("commit");
169
177
  return result;
170
178
  } catch (error) {
171
179
  await trx.rollback(error);
172
- this.logger.error(
173
- "[%s] [%s] transaction rollback: %s",
174
- this.name,
175
- trxId,
176
- error
177
- );
180
+ this.logger.error("[%s] transaction rollback: %s", trxId, error);
178
181
  trx.emit("rollback", error);
179
182
  throw error;
180
183
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/knex",
3
- "version": "3.0.0",
3
+ "version": "3.1.1",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -34,9 +34,9 @@
34
34
  "dist"
35
35
  ],
36
36
  "dependencies": {
37
- "@faasjs/deep_merge": "3.0.0",
38
- "@faasjs/func": "3.0.0",
39
- "@faasjs/logger": "3.0.0"
37
+ "@faasjs/deep_merge": "3.1.1",
38
+ "@faasjs/func": "3.1.1",
39
+ "@faasjs/logger": "3.1.1"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "knex": "*"
@@ -49,7 +49,8 @@
49
49
  "mysql": "*",
50
50
  "pg": "*",
51
51
  "better-sqlite3": "*",
52
- "tsd": "*"
52
+ "tsd": "*",
53
+ "cloudflare-d1-http-knex": "*"
53
54
  },
54
55
  "engines": {
55
56
  "node": ">=22.0.0",