@faasjs/knex 0.0.2-beta.99 → 0.0.3-beta.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/README.md +139 -3
- package/dist/index.d.ts +40 -0
- package/dist/index.js +193 -0
- package/dist/index.mjs +168 -0
- package/package.json +25 -17
- package/lib/index.d.ts +0 -32
- package/lib/index.es.js +0 -98
- package/lib/index.js +0 -105
package/README.md
CHANGED
|
@@ -1,7 +1,143 @@
|
|
|
1
1
|
# @faasjs/knex
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](https://github.com/faasjs/faasjs/blob/master/packages/faasjs/knex/LICENSE)
|
|
3
|
+
[](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/knex/LICENSE)
|
|
6
4
|
[](https://www.npmjs.com/package/@faasjs/knex)
|
|
7
5
|
[](https://www.npmjs.com/package/@faasjs/knex)
|
|
6
|
+
|
|
7
|
+
FaasJS's sql plugin, base on [Knex](https://knexjs.org/).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
npm install @faasjs/knex
|
|
12
|
+
## Modules
|
|
13
|
+
|
|
14
|
+
### Classes
|
|
15
|
+
|
|
16
|
+
- [Knex](classes/Knex.md)
|
|
17
|
+
|
|
18
|
+
### Type Aliases
|
|
19
|
+
|
|
20
|
+
- [KnexConfig](#knexconfig)
|
|
21
|
+
|
|
22
|
+
### Functions
|
|
23
|
+
|
|
24
|
+
- [query](#query)
|
|
25
|
+
- [raw](#raw)
|
|
26
|
+
- [transaction](#transaction)
|
|
27
|
+
- [useKnex](#useknex)
|
|
28
|
+
|
|
29
|
+
## Type Aliases
|
|
30
|
+
|
|
31
|
+
### KnexConfig
|
|
32
|
+
|
|
33
|
+
Ƭ **KnexConfig**: `Object`
|
|
34
|
+
|
|
35
|
+
#### Type declaration
|
|
36
|
+
|
|
37
|
+
| Name | Type |
|
|
38
|
+
| :------ | :------ |
|
|
39
|
+
| `config?` | `K.Config` |
|
|
40
|
+
| `name?` | `string` |
|
|
41
|
+
|
|
42
|
+
## Functions
|
|
43
|
+
|
|
44
|
+
### query
|
|
45
|
+
|
|
46
|
+
▸ **query**<`TName`\>(`table`): `K.QueryBuilder`<`K.TableType`<`TName`\>, { `_aliases`: {} ; `_base`: `K.ResolveTableType`<`K.TableType`<`TName`\>, ``"base"``\> ; `_hasSelection`: ``false`` ; `_intersectProps`: {} ; `_keys`: `never` ; `_single`: ``false`` ; `_unionProps`: `never` }[]\>
|
|
47
|
+
|
|
48
|
+
#### Type parameters
|
|
49
|
+
|
|
50
|
+
| Name | Type |
|
|
51
|
+
| :------ | :------ |
|
|
52
|
+
| `TName` | extends ``"test"`` |
|
|
53
|
+
|
|
54
|
+
#### Parameters
|
|
55
|
+
|
|
56
|
+
| Name | Type |
|
|
57
|
+
| :------ | :------ |
|
|
58
|
+
| `table` | `TName` |
|
|
59
|
+
|
|
60
|
+
#### Returns
|
|
61
|
+
|
|
62
|
+
`K.QueryBuilder`<`K.TableType`<`TName`\>, { `_aliases`: {} ; `_base`: `K.ResolveTableType`<`K.TableType`<`TName`\>, ``"base"``\> ; `_hasSelection`: ``false`` ; `_intersectProps`: {} ; `_keys`: `never` ; `_single`: ``false`` ; `_unionProps`: `never` }[]\>
|
|
63
|
+
|
|
64
|
+
▸ **query**<`TName`, `TResult`\>(`table`): `K.QueryBuilder`<`TName`, `TResult`\>
|
|
65
|
+
|
|
66
|
+
#### Type parameters
|
|
67
|
+
|
|
68
|
+
| Name | Type |
|
|
69
|
+
| :------ | :------ |
|
|
70
|
+
| `TName` | extends `Object` = `any` |
|
|
71
|
+
| `TResult` | `any`[] |
|
|
72
|
+
|
|
73
|
+
#### Parameters
|
|
74
|
+
|
|
75
|
+
| Name | Type |
|
|
76
|
+
| :------ | :------ |
|
|
77
|
+
| `table` | `string` |
|
|
78
|
+
|
|
79
|
+
#### Returns
|
|
80
|
+
|
|
81
|
+
`K.QueryBuilder`<`TName`, `TResult`\>
|
|
82
|
+
|
|
83
|
+
___
|
|
84
|
+
|
|
85
|
+
### raw
|
|
86
|
+
|
|
87
|
+
▸ **raw**<`TResult`\>(`sql`, `bindings?`): `Promise`<`TResult`\>
|
|
88
|
+
|
|
89
|
+
#### Type parameters
|
|
90
|
+
|
|
91
|
+
| Name | Type |
|
|
92
|
+
| :------ | :------ |
|
|
93
|
+
| `TResult` | `any` |
|
|
94
|
+
|
|
95
|
+
#### Parameters
|
|
96
|
+
|
|
97
|
+
| Name | Type | Default value |
|
|
98
|
+
| :------ | :------ | :------ |
|
|
99
|
+
| `sql` | `string` | `undefined` |
|
|
100
|
+
| `bindings` | `ValueDict` \| `RawBinding`[] | `[]` |
|
|
101
|
+
|
|
102
|
+
#### Returns
|
|
103
|
+
|
|
104
|
+
`Promise`<`TResult`\>
|
|
105
|
+
|
|
106
|
+
___
|
|
107
|
+
|
|
108
|
+
### transaction
|
|
109
|
+
|
|
110
|
+
▸ **transaction**<`TResult`\>(`scope`, `config?`): `Promise`<`TResult`\>
|
|
111
|
+
|
|
112
|
+
#### Type parameters
|
|
113
|
+
|
|
114
|
+
| Name | Type |
|
|
115
|
+
| :------ | :------ |
|
|
116
|
+
| `TResult` | `any` |
|
|
117
|
+
|
|
118
|
+
#### Parameters
|
|
119
|
+
|
|
120
|
+
| Name | Type |
|
|
121
|
+
| :------ | :------ |
|
|
122
|
+
| `scope` | (`trx`: `Transaction`<`any`, `any`\>) => `void` \| `Promise`<`TResult`\> |
|
|
123
|
+
| `config?` | `any` |
|
|
124
|
+
|
|
125
|
+
#### Returns
|
|
126
|
+
|
|
127
|
+
`Promise`<`TResult`\>
|
|
128
|
+
|
|
129
|
+
___
|
|
130
|
+
|
|
131
|
+
### useKnex
|
|
132
|
+
|
|
133
|
+
▸ **useKnex**(`config?`): [`Knex`](classes/Knex.md) & `UseifyPlugin`
|
|
134
|
+
|
|
135
|
+
#### Parameters
|
|
136
|
+
|
|
137
|
+
| Name | Type |
|
|
138
|
+
| :------ | :------ |
|
|
139
|
+
| `config?` | [`KnexConfig`](#knexconfig) |
|
|
140
|
+
|
|
141
|
+
#### Returns
|
|
142
|
+
|
|
143
|
+
[`Knex`](classes/Knex.md) & `UseifyPlugin`
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Plugin, DeployData, Next, MountData, InvokeData, UseifyPlugin } from '@faasjs/func';
|
|
2
|
+
import { Knex as Knex$1 } from 'knex';
|
|
3
|
+
export { Knex as K } from 'knex';
|
|
4
|
+
|
|
5
|
+
type KnexConfig = {
|
|
6
|
+
name?: string;
|
|
7
|
+
config?: Knex$1.Config;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
declare class Knex implements Plugin {
|
|
11
|
+
readonly type: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
config: Knex$1.Config;
|
|
14
|
+
adapter: Knex$1;
|
|
15
|
+
query: Knex$1;
|
|
16
|
+
private logger;
|
|
17
|
+
constructor(config?: KnexConfig);
|
|
18
|
+
onDeploy(data: DeployData, next: Next): Promise<void>;
|
|
19
|
+
onMount(data: MountData, next: Next): Promise<void>;
|
|
20
|
+
onInvoke(data: InvokeData<any, any, any>, next: Next): Promise<void>;
|
|
21
|
+
raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<Knex$1.Raw<TResult>>;
|
|
22
|
+
transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult> | void, config?: any): Promise<TResult>;
|
|
23
|
+
schema(): Knex$1.SchemaBuilder;
|
|
24
|
+
quit(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
declare function useKnex(config?: KnexConfig): Knex & UseifyPlugin;
|
|
27
|
+
declare function query<TName extends Knex$1.TableNames>(table: TName): Knex$1.QueryBuilder<Knex$1.TableType<TName>, {
|
|
28
|
+
_base: Knex$1.ResolveTableType<Knex$1.TableType<TName>, 'base'>;
|
|
29
|
+
_hasSelection: false;
|
|
30
|
+
_keys: never;
|
|
31
|
+
_aliases: {};
|
|
32
|
+
_single: false;
|
|
33
|
+
_intersectProps: {};
|
|
34
|
+
_unionProps: never;
|
|
35
|
+
}[]>;
|
|
36
|
+
declare function query<TName extends {} = any, TResult = any[]>(table: string): Knex$1.QueryBuilder<TName, TResult>;
|
|
37
|
+
declare function transaction<TResult = any>(scope: (trx: Knex$1.Transaction<any, any>) => Promise<TResult> | void, config?: any): Promise<TResult>;
|
|
38
|
+
declare function raw<TResult = any>(sql: string, bindings?: Knex$1.RawBinding[] | Knex$1.ValueDict): Promise<TResult>;
|
|
39
|
+
|
|
40
|
+
export { Knex, KnexConfig, query, raw, transaction, useKnex };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var src_exports = {};
|
|
28
|
+
__export(src_exports, {
|
|
29
|
+
Knex: () => Knex,
|
|
30
|
+
query: () => query,
|
|
31
|
+
raw: () => raw,
|
|
32
|
+
transaction: () => transaction,
|
|
33
|
+
useKnex: () => useKnex
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var import_func = require("@faasjs/func");
|
|
37
|
+
var import_logger = require("@faasjs/logger");
|
|
38
|
+
var import_deep_merge = require("@faasjs/deep_merge");
|
|
39
|
+
var import_knex = __toESM(require("knex"));
|
|
40
|
+
var Name = "knex";
|
|
41
|
+
if (!global["FaasJS_Knex"]) {
|
|
42
|
+
global.FaasJS_Knex = {};
|
|
43
|
+
}
|
|
44
|
+
var Knex = class {
|
|
45
|
+
constructor(config) {
|
|
46
|
+
this.type = Name;
|
|
47
|
+
this.name = Name;
|
|
48
|
+
if (config) {
|
|
49
|
+
this.name = config.name || this.type;
|
|
50
|
+
this.config = config.config || /* @__PURE__ */ Object.create(null);
|
|
51
|
+
} else {
|
|
52
|
+
this.name = this.type;
|
|
53
|
+
this.config = /* @__PURE__ */ Object.create(null);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async onDeploy(data, next) {
|
|
57
|
+
const client = data.config.plugins[this.name].config.client;
|
|
58
|
+
if (!client)
|
|
59
|
+
throw Error("[Knex] client required.");
|
|
60
|
+
data.dependencies["@faasjs/knex"] = "*";
|
|
61
|
+
if (client === "sqlite3")
|
|
62
|
+
data.dependencies["better-sqlite3"] = "*";
|
|
63
|
+
else
|
|
64
|
+
data.dependencies[client] = "*";
|
|
65
|
+
new import_logger.Logger(this.name).debug("add dependencies: " + client);
|
|
66
|
+
await next();
|
|
67
|
+
}
|
|
68
|
+
async onMount(data, next) {
|
|
69
|
+
var _a;
|
|
70
|
+
this.logger = data.logger;
|
|
71
|
+
if (global.FaasJS_Knex[this.name]) {
|
|
72
|
+
this.config = global.FaasJS_Knex[this.name].config;
|
|
73
|
+
this.adapter = global.FaasJS_Knex[this.name].adapter;
|
|
74
|
+
this.query = this.adapter;
|
|
75
|
+
await next();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
79
|
+
for (let key in process.env)
|
|
80
|
+
if (key.startsWith(prefix)) {
|
|
81
|
+
const value = process.env[key];
|
|
82
|
+
key = key.replace(prefix, "").toLowerCase();
|
|
83
|
+
if (typeof this.config[key] === "undefined")
|
|
84
|
+
if (key.startsWith("connection_")) {
|
|
85
|
+
if (!this.config.connection) {
|
|
86
|
+
this.config.connection = /* @__PURE__ */ Object.create(null);
|
|
87
|
+
}
|
|
88
|
+
this.config.connection[key.replace("connection_", "")] = value;
|
|
89
|
+
} else
|
|
90
|
+
this.config[key] = value;
|
|
91
|
+
}
|
|
92
|
+
if (data.config.plugins && ((_a = data.config.plugins[this.name]) == null ? void 0 : _a.config))
|
|
93
|
+
this.config = (0, import_deep_merge.deepMerge)(data.config.plugins[this.name].config, this.config);
|
|
94
|
+
if (this.config.client === "sqlite3")
|
|
95
|
+
this.config.client = "better-sqlite3";
|
|
96
|
+
this.adapter = (0, import_knex.default)(this.config);
|
|
97
|
+
if (this.config.client === "pg") {
|
|
98
|
+
const pg = require("pg");
|
|
99
|
+
const intTypes = [
|
|
100
|
+
"INT2",
|
|
101
|
+
"INT4",
|
|
102
|
+
"INT8"
|
|
103
|
+
];
|
|
104
|
+
intTypes.forEach((t) => pg.types.setTypeParser(
|
|
105
|
+
pg.types.builtins[t],
|
|
106
|
+
(v) => parseInt(v)
|
|
107
|
+
));
|
|
108
|
+
const floatTypes = [
|
|
109
|
+
"FLOAT4",
|
|
110
|
+
"FLOAT8",
|
|
111
|
+
"NUMERIC"
|
|
112
|
+
];
|
|
113
|
+
floatTypes.forEach((t) => pg.types.setTypeParser(
|
|
114
|
+
pg.types.builtins[t],
|
|
115
|
+
(v) => parseFloat(v)
|
|
116
|
+
));
|
|
117
|
+
}
|
|
118
|
+
this.query = this.adapter;
|
|
119
|
+
this.query.on("query", ({
|
|
120
|
+
sql,
|
|
121
|
+
__knexQueryUid,
|
|
122
|
+
bindings
|
|
123
|
+
}) => {
|
|
124
|
+
this.logger.time(`Knex${__knexQueryUid}`);
|
|
125
|
+
this.logger.debug("[%s] query begin: %s %j", this.name, sql, bindings);
|
|
126
|
+
}).on("query-response", (response, {
|
|
127
|
+
sql,
|
|
128
|
+
__knexQueryUid,
|
|
129
|
+
bindings
|
|
130
|
+
}) => {
|
|
131
|
+
this.logger.timeEnd(`Knex${__knexQueryUid}`, "[%s] query done: %s %j %j", this.name, sql, bindings, response);
|
|
132
|
+
}).on("query-error", (_, {
|
|
133
|
+
__knexQueryUid,
|
|
134
|
+
sql,
|
|
135
|
+
bindings
|
|
136
|
+
}) => {
|
|
137
|
+
this.logger.timeEnd(`Knex${__knexQueryUid}`, "[%s] query failed: %s %j", this.name, sql, bindings);
|
|
138
|
+
});
|
|
139
|
+
data.logger.debug("[%s] connected", this.name);
|
|
140
|
+
global.FaasJS_Knex[this.name] = this;
|
|
141
|
+
await next();
|
|
142
|
+
}
|
|
143
|
+
async onInvoke(data, next) {
|
|
144
|
+
this.logger = data.logger;
|
|
145
|
+
await next();
|
|
146
|
+
}
|
|
147
|
+
async raw(sql, bindings = []) {
|
|
148
|
+
if (!this.adapter)
|
|
149
|
+
throw Error("[Knex] Client not initialized.");
|
|
150
|
+
return this.adapter.raw(sql, bindings);
|
|
151
|
+
}
|
|
152
|
+
async transaction(scope, config) {
|
|
153
|
+
if (!this.adapter)
|
|
154
|
+
throw Error(`[${this.name}] Client not initialized.`);
|
|
155
|
+
return this.adapter.transaction(scope, config);
|
|
156
|
+
}
|
|
157
|
+
schema() {
|
|
158
|
+
if (!this.adapter)
|
|
159
|
+
throw Error(`[${this.name}] Client not initialized.`);
|
|
160
|
+
return this.adapter.schema;
|
|
161
|
+
}
|
|
162
|
+
async quit() {
|
|
163
|
+
try {
|
|
164
|
+
await global.FaasJS_Knex[this.name].adapter.destroy();
|
|
165
|
+
delete global.FaasJS_Knex[this.name];
|
|
166
|
+
} catch (error) {
|
|
167
|
+
console.error(error);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
function useKnex(config) {
|
|
172
|
+
const name = (config == null ? void 0 : config.name) || Name;
|
|
173
|
+
if (global.FaasJS_Knex[name])
|
|
174
|
+
return (0, import_func.usePlugin)(global.FaasJS_Knex[name]);
|
|
175
|
+
return (0, import_func.usePlugin)(new Knex(config));
|
|
176
|
+
}
|
|
177
|
+
function query(table) {
|
|
178
|
+
return useKnex().query(table);
|
|
179
|
+
}
|
|
180
|
+
async function transaction(scope, config) {
|
|
181
|
+
return useKnex().transaction(scope, config);
|
|
182
|
+
}
|
|
183
|
+
async function raw(sql, bindings = []) {
|
|
184
|
+
return useKnex().raw(sql, bindings);
|
|
185
|
+
}
|
|
186
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
187
|
+
0 && (module.exports = {
|
|
188
|
+
Knex,
|
|
189
|
+
query,
|
|
190
|
+
raw,
|
|
191
|
+
transaction,
|
|
192
|
+
useKnex
|
|
193
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/index.ts
|
|
10
|
+
import {
|
|
11
|
+
usePlugin
|
|
12
|
+
} from "@faasjs/func";
|
|
13
|
+
import { Logger } from "@faasjs/logger";
|
|
14
|
+
import { deepMerge } from "@faasjs/deep_merge";
|
|
15
|
+
import knex from "knex";
|
|
16
|
+
var Name = "knex";
|
|
17
|
+
if (!global["FaasJS_Knex"]) {
|
|
18
|
+
global.FaasJS_Knex = {};
|
|
19
|
+
}
|
|
20
|
+
var Knex = class {
|
|
21
|
+
constructor(config) {
|
|
22
|
+
this.type = Name;
|
|
23
|
+
this.name = Name;
|
|
24
|
+
if (config) {
|
|
25
|
+
this.name = config.name || this.type;
|
|
26
|
+
this.config = config.config || /* @__PURE__ */ Object.create(null);
|
|
27
|
+
} else {
|
|
28
|
+
this.name = this.type;
|
|
29
|
+
this.config = /* @__PURE__ */ Object.create(null);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async onDeploy(data, next) {
|
|
33
|
+
const client = data.config.plugins[this.name].config.client;
|
|
34
|
+
if (!client)
|
|
35
|
+
throw Error("[Knex] client required.");
|
|
36
|
+
data.dependencies["@faasjs/knex"] = "*";
|
|
37
|
+
if (client === "sqlite3")
|
|
38
|
+
data.dependencies["better-sqlite3"] = "*";
|
|
39
|
+
else
|
|
40
|
+
data.dependencies[client] = "*";
|
|
41
|
+
new Logger(this.name).debug("add dependencies: " + client);
|
|
42
|
+
await next();
|
|
43
|
+
}
|
|
44
|
+
async onMount(data, next) {
|
|
45
|
+
var _a;
|
|
46
|
+
this.logger = data.logger;
|
|
47
|
+
if (global.FaasJS_Knex[this.name]) {
|
|
48
|
+
this.config = global.FaasJS_Knex[this.name].config;
|
|
49
|
+
this.adapter = global.FaasJS_Knex[this.name].adapter;
|
|
50
|
+
this.query = this.adapter;
|
|
51
|
+
await next();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
55
|
+
for (let key in process.env)
|
|
56
|
+
if (key.startsWith(prefix)) {
|
|
57
|
+
const value = process.env[key];
|
|
58
|
+
key = key.replace(prefix, "").toLowerCase();
|
|
59
|
+
if (typeof this.config[key] === "undefined")
|
|
60
|
+
if (key.startsWith("connection_")) {
|
|
61
|
+
if (!this.config.connection) {
|
|
62
|
+
this.config.connection = /* @__PURE__ */ Object.create(null);
|
|
63
|
+
}
|
|
64
|
+
this.config.connection[key.replace("connection_", "")] = value;
|
|
65
|
+
} else
|
|
66
|
+
this.config[key] = value;
|
|
67
|
+
}
|
|
68
|
+
if (data.config.plugins && ((_a = data.config.plugins[this.name]) == null ? void 0 : _a.config))
|
|
69
|
+
this.config = deepMerge(data.config.plugins[this.name].config, this.config);
|
|
70
|
+
if (this.config.client === "sqlite3")
|
|
71
|
+
this.config.client = "better-sqlite3";
|
|
72
|
+
this.adapter = knex(this.config);
|
|
73
|
+
if (this.config.client === "pg") {
|
|
74
|
+
const pg = __require("pg");
|
|
75
|
+
const intTypes = [
|
|
76
|
+
"INT2",
|
|
77
|
+
"INT4",
|
|
78
|
+
"INT8"
|
|
79
|
+
];
|
|
80
|
+
intTypes.forEach((t) => pg.types.setTypeParser(
|
|
81
|
+
pg.types.builtins[t],
|
|
82
|
+
(v) => parseInt(v)
|
|
83
|
+
));
|
|
84
|
+
const floatTypes = [
|
|
85
|
+
"FLOAT4",
|
|
86
|
+
"FLOAT8",
|
|
87
|
+
"NUMERIC"
|
|
88
|
+
];
|
|
89
|
+
floatTypes.forEach((t) => pg.types.setTypeParser(
|
|
90
|
+
pg.types.builtins[t],
|
|
91
|
+
(v) => parseFloat(v)
|
|
92
|
+
));
|
|
93
|
+
}
|
|
94
|
+
this.query = this.adapter;
|
|
95
|
+
this.query.on("query", ({
|
|
96
|
+
sql,
|
|
97
|
+
__knexQueryUid,
|
|
98
|
+
bindings
|
|
99
|
+
}) => {
|
|
100
|
+
this.logger.time(`Knex${__knexQueryUid}`);
|
|
101
|
+
this.logger.debug("[%s] query begin: %s %j", this.name, sql, bindings);
|
|
102
|
+
}).on("query-response", (response, {
|
|
103
|
+
sql,
|
|
104
|
+
__knexQueryUid,
|
|
105
|
+
bindings
|
|
106
|
+
}) => {
|
|
107
|
+
this.logger.timeEnd(`Knex${__knexQueryUid}`, "[%s] query done: %s %j %j", this.name, sql, bindings, response);
|
|
108
|
+
}).on("query-error", (_, {
|
|
109
|
+
__knexQueryUid,
|
|
110
|
+
sql,
|
|
111
|
+
bindings
|
|
112
|
+
}) => {
|
|
113
|
+
this.logger.timeEnd(`Knex${__knexQueryUid}`, "[%s] query failed: %s %j", this.name, sql, bindings);
|
|
114
|
+
});
|
|
115
|
+
data.logger.debug("[%s] connected", this.name);
|
|
116
|
+
global.FaasJS_Knex[this.name] = this;
|
|
117
|
+
await next();
|
|
118
|
+
}
|
|
119
|
+
async onInvoke(data, next) {
|
|
120
|
+
this.logger = data.logger;
|
|
121
|
+
await next();
|
|
122
|
+
}
|
|
123
|
+
async raw(sql, bindings = []) {
|
|
124
|
+
if (!this.adapter)
|
|
125
|
+
throw Error("[Knex] Client not initialized.");
|
|
126
|
+
return this.adapter.raw(sql, bindings);
|
|
127
|
+
}
|
|
128
|
+
async transaction(scope, config) {
|
|
129
|
+
if (!this.adapter)
|
|
130
|
+
throw Error(`[${this.name}] Client not initialized.`);
|
|
131
|
+
return this.adapter.transaction(scope, config);
|
|
132
|
+
}
|
|
133
|
+
schema() {
|
|
134
|
+
if (!this.adapter)
|
|
135
|
+
throw Error(`[${this.name}] Client not initialized.`);
|
|
136
|
+
return this.adapter.schema;
|
|
137
|
+
}
|
|
138
|
+
async quit() {
|
|
139
|
+
try {
|
|
140
|
+
await global.FaasJS_Knex[this.name].adapter.destroy();
|
|
141
|
+
delete global.FaasJS_Knex[this.name];
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.error(error);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
function useKnex(config) {
|
|
148
|
+
const name = (config == null ? void 0 : config.name) || Name;
|
|
149
|
+
if (global.FaasJS_Knex[name])
|
|
150
|
+
return usePlugin(global.FaasJS_Knex[name]);
|
|
151
|
+
return usePlugin(new Knex(config));
|
|
152
|
+
}
|
|
153
|
+
function query(table) {
|
|
154
|
+
return useKnex().query(table);
|
|
155
|
+
}
|
|
156
|
+
async function transaction(scope, config) {
|
|
157
|
+
return useKnex().transaction(scope, config);
|
|
158
|
+
}
|
|
159
|
+
async function raw(sql, bindings = []) {
|
|
160
|
+
return useKnex().raw(sql, bindings);
|
|
161
|
+
}
|
|
162
|
+
export {
|
|
163
|
+
Knex,
|
|
164
|
+
query,
|
|
165
|
+
raw,
|
|
166
|
+
transaction,
|
|
167
|
+
useKnex
|
|
168
|
+
};
|
package/package.json
CHANGED
|
@@ -1,35 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/knex",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3-beta.2",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"main": "
|
|
6
|
-
"types": "
|
|
7
|
-
"
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"homepage": "https://faasjs.com/doc/knex",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/faasjs/faasjs.git",
|
|
11
|
+
"directory": "packages/knex"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/faasjs/faasjs/issues"
|
|
15
|
+
},
|
|
16
|
+
"funding": "https://github.com/sponsors/faasjs",
|
|
8
17
|
"scripts": {
|
|
9
|
-
"
|
|
18
|
+
"build": "tsup-node src/index.ts --format esm,cjs",
|
|
19
|
+
"build:types": "tsup-node src/index.ts --dts-only"
|
|
10
20
|
},
|
|
11
21
|
"files": [
|
|
12
|
-
"
|
|
22
|
+
"dist"
|
|
13
23
|
],
|
|
14
24
|
"dependencies": {
|
|
15
|
-
"@faasjs/deep_merge": "^0.0.
|
|
16
|
-
"@faasjs/
|
|
25
|
+
"@faasjs/deep_merge": "^0.0.3-beta.2",
|
|
26
|
+
"@faasjs/func": "^0.0.3-beta.2",
|
|
27
|
+
"@faasjs/logger": "^0.0.3-beta.2",
|
|
17
28
|
"knex": "*"
|
|
18
29
|
},
|
|
19
30
|
"devDependencies": {
|
|
20
|
-
"@faasjs/func": "^0.0.2-beta.99",
|
|
21
|
-
"@types/debug": "*",
|
|
22
|
-
"@types/jest": "*",
|
|
23
31
|
"@types/mysql": "*",
|
|
24
32
|
"@types/node": "*",
|
|
25
33
|
"@types/pg": "*",
|
|
26
|
-
"@types/sqlite3": "*",
|
|
27
34
|
"mysql": "*",
|
|
28
35
|
"pg": "*",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"sqlite3": "*",
|
|
32
|
-
"typescript": "*"
|
|
36
|
+
"better-sqlite3": "*",
|
|
37
|
+
"tsd": "*"
|
|
33
38
|
},
|
|
34
|
-
"
|
|
39
|
+
"engines": {
|
|
40
|
+
"npm": ">=8.0.0",
|
|
41
|
+
"node": ">=16.0.0"
|
|
42
|
+
}
|
|
35
43
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Plugin, Next, DeployData, MountData, UseifyPlugin } from '@faasjs/func';
|
|
2
|
-
import Logger from '@faasjs/logger';
|
|
3
|
-
import knex, { Config, Transaction, QueryBuilder, Raw, AliasDict, Value, SchemaBuilder } from 'knex';
|
|
4
|
-
export declare type KnexConfig = {
|
|
5
|
-
name?: string;
|
|
6
|
-
config?: Config;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* TypeORM 插件
|
|
10
|
-
*/
|
|
11
|
-
export declare class Knex implements Plugin {
|
|
12
|
-
readonly type: string;
|
|
13
|
-
readonly name: string;
|
|
14
|
-
config: Config;
|
|
15
|
-
adapter: knex;
|
|
16
|
-
logger: Logger;
|
|
17
|
-
/**
|
|
18
|
-
* 创建插件实例
|
|
19
|
-
* @param config {object} 配置
|
|
20
|
-
* @param config.name {string} 配置名
|
|
21
|
-
* @param config.config {object} 数据库配置
|
|
22
|
-
*/
|
|
23
|
-
constructor(config?: KnexConfig);
|
|
24
|
-
onDeploy(data: DeployData, next: Next): Promise<void>;
|
|
25
|
-
onMount(data: MountData, next: Next): Promise<void>;
|
|
26
|
-
query<TRecord = any, TResult = any>(tableName?: string | Raw<TResult> | QueryBuilder<TRecord, TResult> | AliasDict): QueryBuilder<TRecord, TResult>;
|
|
27
|
-
raw<TResult = any>(value: Value): Promise<Raw<TResult>>;
|
|
28
|
-
transaction<TResult = any>(scope: (trx: Transaction) => Promise<TResult> | void): Promise<TResult>;
|
|
29
|
-
schema(): SchemaBuilder;
|
|
30
|
-
quit(): Promise<void>;
|
|
31
|
-
}
|
|
32
|
-
export declare function useKnex(config?: KnexConfig): Knex & UseifyPlugin;
|
package/lib/index.es.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { usePlugin } from '@faasjs/func';
|
|
2
|
-
import Logger from '@faasjs/logger';
|
|
3
|
-
import deepMerge from '@faasjs/deep_merge';
|
|
4
|
-
import knex from 'knex';
|
|
5
|
-
|
|
6
|
-
const Name = 'knex';
|
|
7
|
-
const globals = {};
|
|
8
|
-
/**
|
|
9
|
-
* TypeORM 插件
|
|
10
|
-
*/
|
|
11
|
-
class Knex {
|
|
12
|
-
/**
|
|
13
|
-
* 创建插件实例
|
|
14
|
-
* @param config {object} 配置
|
|
15
|
-
* @param config.name {string} 配置名
|
|
16
|
-
* @param config.config {object} 数据库配置
|
|
17
|
-
*/
|
|
18
|
-
constructor(config) {
|
|
19
|
-
this.type = Name;
|
|
20
|
-
this.name = Name;
|
|
21
|
-
if (config) {
|
|
22
|
-
this.name = config.name || this.type;
|
|
23
|
-
this.config = config.config || Object.create(null);
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
this.name = this.type;
|
|
27
|
-
this.config = Object.create(null);
|
|
28
|
-
}
|
|
29
|
-
this.logger = new Logger(this.name);
|
|
30
|
-
}
|
|
31
|
-
async onDeploy(data, next) {
|
|
32
|
-
const client = data.config.plugins[this.name].config.client;
|
|
33
|
-
data.dependencies[client] = '*';
|
|
34
|
-
await next();
|
|
35
|
-
}
|
|
36
|
-
async onMount(data, next) {
|
|
37
|
-
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
38
|
-
for (let key in process.env)
|
|
39
|
-
if (key.startsWith(prefix)) {
|
|
40
|
-
const value = process.env[key];
|
|
41
|
-
key = key.replace(prefix, '').toLowerCase();
|
|
42
|
-
if (typeof this.config[key] === 'undefined')
|
|
43
|
-
if (key.startsWith('connection_')) {
|
|
44
|
-
if (!this.config.connection)
|
|
45
|
-
this.config.connection = {};
|
|
46
|
-
this.config.connection[key.replace('connection_', '')] = value;
|
|
47
|
-
}
|
|
48
|
-
else
|
|
49
|
-
this.config[key] = value;
|
|
50
|
-
}
|
|
51
|
-
if (data.config.plugins[this.name] && data.config.plugins[this.name].config)
|
|
52
|
-
this.config = deepMerge(data.config.plugins[this.name].config, this.config);
|
|
53
|
-
this.adapter = knex(this.config);
|
|
54
|
-
this.adapter
|
|
55
|
-
.on('query', ({ sql, __knexQueryUid, bindings }) => {
|
|
56
|
-
this.logger.time(`Knex${__knexQueryUid}`);
|
|
57
|
-
this.logger.debug('query begin: %s %O', sql, bindings);
|
|
58
|
-
})
|
|
59
|
-
.on('query-response', (response, { sql, __knexQueryUid, bindings }) => {
|
|
60
|
-
this.logger.timeEnd(`Knex${__knexQueryUid}`, 'query done: %s %O %O', sql, bindings, response);
|
|
61
|
-
})
|
|
62
|
-
.on('query-error', (_, { __knexQueryUid, sql, bindings }) => {
|
|
63
|
-
this.logger.timeEnd(`Knex${__knexQueryUid}`, 'query failed: %s %O', sql, bindings);
|
|
64
|
-
});
|
|
65
|
-
this.logger.debug('connected');
|
|
66
|
-
globals[this.name] = this;
|
|
67
|
-
await next();
|
|
68
|
-
}
|
|
69
|
-
query(tableName) {
|
|
70
|
-
return this.adapter(tableName);
|
|
71
|
-
}
|
|
72
|
-
async raw(value) {
|
|
73
|
-
return this.adapter.raw(value);
|
|
74
|
-
}
|
|
75
|
-
async transaction(scope) {
|
|
76
|
-
return this.adapter.transaction(scope);
|
|
77
|
-
}
|
|
78
|
-
schema() {
|
|
79
|
-
return this.adapter.schema;
|
|
80
|
-
}
|
|
81
|
-
async quit() {
|
|
82
|
-
try {
|
|
83
|
-
await globals[this.name].adapter.destroy();
|
|
84
|
-
delete globals[this.name];
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
console.error(error);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
function useKnex(config) {
|
|
92
|
-
const name = (config === null || config === void 0 ? void 0 : config.name) || Name;
|
|
93
|
-
if (globals[name])
|
|
94
|
-
return usePlugin(globals[name]);
|
|
95
|
-
return usePlugin(new Knex(config));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export { Knex, useKnex };
|
package/lib/index.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
-
|
|
7
|
-
var func = require('@faasjs/func');
|
|
8
|
-
var Logger = _interopDefault(require('@faasjs/logger'));
|
|
9
|
-
var deepMerge = _interopDefault(require('@faasjs/deep_merge'));
|
|
10
|
-
var knex = _interopDefault(require('knex'));
|
|
11
|
-
|
|
12
|
-
const Name = 'knex';
|
|
13
|
-
const globals = {};
|
|
14
|
-
/**
|
|
15
|
-
* TypeORM 插件
|
|
16
|
-
*/
|
|
17
|
-
class Knex {
|
|
18
|
-
/**
|
|
19
|
-
* 创建插件实例
|
|
20
|
-
* @param config {object} 配置
|
|
21
|
-
* @param config.name {string} 配置名
|
|
22
|
-
* @param config.config {object} 数据库配置
|
|
23
|
-
*/
|
|
24
|
-
constructor(config) {
|
|
25
|
-
this.type = Name;
|
|
26
|
-
this.name = Name;
|
|
27
|
-
if (config) {
|
|
28
|
-
this.name = config.name || this.type;
|
|
29
|
-
this.config = config.config || Object.create(null);
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
this.name = this.type;
|
|
33
|
-
this.config = Object.create(null);
|
|
34
|
-
}
|
|
35
|
-
this.logger = new Logger(this.name);
|
|
36
|
-
}
|
|
37
|
-
async onDeploy(data, next) {
|
|
38
|
-
const client = data.config.plugins[this.name].config.client;
|
|
39
|
-
data.dependencies[client] = '*';
|
|
40
|
-
await next();
|
|
41
|
-
}
|
|
42
|
-
async onMount(data, next) {
|
|
43
|
-
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
44
|
-
for (let key in process.env)
|
|
45
|
-
if (key.startsWith(prefix)) {
|
|
46
|
-
const value = process.env[key];
|
|
47
|
-
key = key.replace(prefix, '').toLowerCase();
|
|
48
|
-
if (typeof this.config[key] === 'undefined')
|
|
49
|
-
if (key.startsWith('connection_')) {
|
|
50
|
-
if (!this.config.connection)
|
|
51
|
-
this.config.connection = {};
|
|
52
|
-
this.config.connection[key.replace('connection_', '')] = value;
|
|
53
|
-
}
|
|
54
|
-
else
|
|
55
|
-
this.config[key] = value;
|
|
56
|
-
}
|
|
57
|
-
if (data.config.plugins[this.name] && data.config.plugins[this.name].config)
|
|
58
|
-
this.config = deepMerge(data.config.plugins[this.name].config, this.config);
|
|
59
|
-
this.adapter = knex(this.config);
|
|
60
|
-
this.adapter
|
|
61
|
-
.on('query', ({ sql, __knexQueryUid, bindings }) => {
|
|
62
|
-
this.logger.time(`Knex${__knexQueryUid}`);
|
|
63
|
-
this.logger.debug('query begin: %s %O', sql, bindings);
|
|
64
|
-
})
|
|
65
|
-
.on('query-response', (response, { sql, __knexQueryUid, bindings }) => {
|
|
66
|
-
this.logger.timeEnd(`Knex${__knexQueryUid}`, 'query done: %s %O %O', sql, bindings, response);
|
|
67
|
-
})
|
|
68
|
-
.on('query-error', (_, { __knexQueryUid, sql, bindings }) => {
|
|
69
|
-
this.logger.timeEnd(`Knex${__knexQueryUid}`, 'query failed: %s %O', sql, bindings);
|
|
70
|
-
});
|
|
71
|
-
this.logger.debug('connected');
|
|
72
|
-
globals[this.name] = this;
|
|
73
|
-
await next();
|
|
74
|
-
}
|
|
75
|
-
query(tableName) {
|
|
76
|
-
return this.adapter(tableName);
|
|
77
|
-
}
|
|
78
|
-
async raw(value) {
|
|
79
|
-
return this.adapter.raw(value);
|
|
80
|
-
}
|
|
81
|
-
async transaction(scope) {
|
|
82
|
-
return this.adapter.transaction(scope);
|
|
83
|
-
}
|
|
84
|
-
schema() {
|
|
85
|
-
return this.adapter.schema;
|
|
86
|
-
}
|
|
87
|
-
async quit() {
|
|
88
|
-
try {
|
|
89
|
-
await globals[this.name].adapter.destroy();
|
|
90
|
-
delete globals[this.name];
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
console.error(error);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
function useKnex(config) {
|
|
98
|
-
const name = (config === null || config === void 0 ? void 0 : config.name) || Name;
|
|
99
|
-
if (globals[name])
|
|
100
|
-
return func.usePlugin(globals[name]);
|
|
101
|
-
return func.usePlugin(new Knex(config));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
exports.Knex = Knex;
|
|
105
|
-
exports.useKnex = useKnex;
|