@faasjs/knex 0.0.2-beta.451 → 0.0.2-beta.453
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.ts +1 -1
- package/dist/index.js +156 -7
- package/dist/index.mjs +150 -5
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -33,9 +33,158 @@ __export(src_exports, {
|
|
|
33
33
|
useKnex: () => useKnex
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(src_exports);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
var
|
|
36
|
+
|
|
37
|
+
// ../logger/src/index.ts
|
|
38
|
+
var import_util = require("util");
|
|
39
|
+
var LevelColor = ((LevelColor2) => {
|
|
40
|
+
LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
|
|
41
|
+
LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
|
|
42
|
+
LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
|
|
43
|
+
LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
|
|
44
|
+
return LevelColor2;
|
|
45
|
+
})(LevelColor || {});
|
|
46
|
+
var LevelPriority = {
|
|
47
|
+
debug: 0,
|
|
48
|
+
info: 1,
|
|
49
|
+
warn: 2,
|
|
50
|
+
error: 3
|
|
51
|
+
};
|
|
52
|
+
var Logger = class {
|
|
53
|
+
constructor(label) {
|
|
54
|
+
this.colorfyOutput = true;
|
|
55
|
+
if (label)
|
|
56
|
+
this.label = label;
|
|
57
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
58
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
59
|
+
this.colorfyOutput = false;
|
|
60
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
61
|
+
this.cachedTimers = {};
|
|
62
|
+
this.size = 500;
|
|
63
|
+
this.stdout = console.log;
|
|
64
|
+
this.stderr = console.error;
|
|
65
|
+
}
|
|
66
|
+
debug(message, ...args) {
|
|
67
|
+
this.log("debug", message, ...args);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
info(message, ...args) {
|
|
71
|
+
this.log("info", message, ...args);
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
warn(message, ...args) {
|
|
75
|
+
this.log("warn", message, ...args);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
error(message, ...args) {
|
|
79
|
+
let stack = false;
|
|
80
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
81
|
+
if (e.stack) {
|
|
82
|
+
stack = true;
|
|
83
|
+
this.log("error", e.stack);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
if (!stack)
|
|
87
|
+
this.log("error", message, ...args);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
time(key, level = "debug") {
|
|
91
|
+
this.cachedTimers[key] = {
|
|
92
|
+
level,
|
|
93
|
+
time: new Date().getTime()
|
|
94
|
+
};
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
timeEnd(key, message, ...args) {
|
|
98
|
+
if (this.cachedTimers[key]) {
|
|
99
|
+
const timer = this.cachedTimers[key];
|
|
100
|
+
message = message + " +%ims";
|
|
101
|
+
args.push(new Date().getTime() - timer.time);
|
|
102
|
+
this[timer.level](message, ...args);
|
|
103
|
+
delete this.cachedTimers[key];
|
|
104
|
+
} else {
|
|
105
|
+
this.warn("timeEnd not found key %s", key);
|
|
106
|
+
this.debug(message);
|
|
107
|
+
}
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
raw(message, ...args) {
|
|
111
|
+
if (this.silent)
|
|
112
|
+
return this;
|
|
113
|
+
this.stdout((0, import_util.format)(message, ...args));
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
colorfy(color, message) {
|
|
117
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
118
|
+
}
|
|
119
|
+
log(level, message, ...args) {
|
|
120
|
+
if (this.silent)
|
|
121
|
+
return this;
|
|
122
|
+
if (LevelPriority[level] < this.level)
|
|
123
|
+
return this;
|
|
124
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
125
|
+
if (this.colorfyOutput && level !== "error")
|
|
126
|
+
output = this.colorfy(LevelColor[level], output);
|
|
127
|
+
else if (!this.colorfyOutput)
|
|
128
|
+
output = output.replace(/\n/g, "");
|
|
129
|
+
if (!output)
|
|
130
|
+
return this;
|
|
131
|
+
if (output.length > this.size)
|
|
132
|
+
output = output.slice(0, this.size) + "...";
|
|
133
|
+
if (level === "error")
|
|
134
|
+
this.stderr(output);
|
|
135
|
+
else
|
|
136
|
+
this.stdout(output);
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// ../func/src/index.ts
|
|
142
|
+
var import_crypto = require("crypto");
|
|
143
|
+
var plugins = [];
|
|
144
|
+
function usePlugin(plugin) {
|
|
145
|
+
if (!plugins.find((p) => p.name === plugin.name))
|
|
146
|
+
plugins.push(plugin);
|
|
147
|
+
if (!plugin.mount)
|
|
148
|
+
plugin.mount = async function({ config }) {
|
|
149
|
+
if (plugin.onMount)
|
|
150
|
+
await plugin.onMount({
|
|
151
|
+
config,
|
|
152
|
+
event: {},
|
|
153
|
+
context: {},
|
|
154
|
+
logger: new Logger(plugin.name)
|
|
155
|
+
}, async () => Promise.resolve());
|
|
156
|
+
};
|
|
157
|
+
return plugin;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ../deep_merge/src/index.ts
|
|
161
|
+
var shouldMerge = function(item) {
|
|
162
|
+
const type = Object.prototype.toString.call(item);
|
|
163
|
+
return type === "[object Object]" || type === "[object Array]";
|
|
164
|
+
};
|
|
165
|
+
function deepMerge(...sources) {
|
|
166
|
+
let acc = /* @__PURE__ */ Object.create(null);
|
|
167
|
+
for (const source of sources)
|
|
168
|
+
if (source instanceof Array) {
|
|
169
|
+
if (!(acc instanceof Array))
|
|
170
|
+
acc = [];
|
|
171
|
+
acc = [...new Set(source.concat(...acc))];
|
|
172
|
+
} else if (shouldMerge(source))
|
|
173
|
+
for (const [key, value] of Object.entries(source)) {
|
|
174
|
+
let val;
|
|
175
|
+
if (shouldMerge(value))
|
|
176
|
+
val = deepMerge(acc[key], value);
|
|
177
|
+
else
|
|
178
|
+
val = value;
|
|
179
|
+
acc = {
|
|
180
|
+
...acc,
|
|
181
|
+
[key]: val
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
return acc;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/index.ts
|
|
39
188
|
var import_knex = __toESM(require("knex"));
|
|
40
189
|
var Name = "knex";
|
|
41
190
|
if (!global["FaasJS_Knex"]) {
|
|
@@ -62,7 +211,7 @@ var Knex = class {
|
|
|
62
211
|
data.dependencies["better-sqlite3"] = "*";
|
|
63
212
|
else
|
|
64
213
|
data.dependencies[client] = "*";
|
|
65
|
-
new
|
|
214
|
+
new Logger(this.name).debug("add dependencies: " + client);
|
|
66
215
|
await next();
|
|
67
216
|
}
|
|
68
217
|
async onMount(data, next) {
|
|
@@ -90,7 +239,7 @@ var Knex = class {
|
|
|
90
239
|
this.config[key] = value;
|
|
91
240
|
}
|
|
92
241
|
if (data.config.plugins && ((_a = data.config.plugins[this.name]) == null ? void 0 : _a.config))
|
|
93
|
-
this.config =
|
|
242
|
+
this.config = deepMerge(data.config.plugins[this.name].config, this.config);
|
|
94
243
|
if (this.config.client === "sqlite3")
|
|
95
244
|
this.config.client = "better-sqlite3";
|
|
96
245
|
this.adapter = (0, import_knex.default)(this.config);
|
|
@@ -171,8 +320,8 @@ var Knex = class {
|
|
|
171
320
|
function useKnex(config) {
|
|
172
321
|
const name = (config == null ? void 0 : config.name) || Name;
|
|
173
322
|
if (global.FaasJS_Knex[name])
|
|
174
|
-
return
|
|
175
|
-
return
|
|
323
|
+
return usePlugin(global.FaasJS_Knex[name]);
|
|
324
|
+
return usePlugin(new Knex(config));
|
|
176
325
|
}
|
|
177
326
|
function query(table) {
|
|
178
327
|
return useKnex().query(table);
|
package/dist/index.mjs
CHANGED
|
@@ -6,12 +6,157 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
6
6
|
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
+
// ../logger/src/index.ts
|
|
10
|
+
import { format } from "util";
|
|
11
|
+
var LevelColor = ((LevelColor2) => {
|
|
12
|
+
LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
|
|
13
|
+
LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
|
|
14
|
+
LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
|
|
15
|
+
LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
|
|
16
|
+
return LevelColor2;
|
|
17
|
+
})(LevelColor || {});
|
|
18
|
+
var LevelPriority = {
|
|
19
|
+
debug: 0,
|
|
20
|
+
info: 1,
|
|
21
|
+
warn: 2,
|
|
22
|
+
error: 3
|
|
23
|
+
};
|
|
24
|
+
var Logger = class {
|
|
25
|
+
constructor(label) {
|
|
26
|
+
this.colorfyOutput = true;
|
|
27
|
+
if (label)
|
|
28
|
+
this.label = label;
|
|
29
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
30
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
31
|
+
this.colorfyOutput = false;
|
|
32
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
33
|
+
this.cachedTimers = {};
|
|
34
|
+
this.size = 500;
|
|
35
|
+
this.stdout = console.log;
|
|
36
|
+
this.stderr = console.error;
|
|
37
|
+
}
|
|
38
|
+
debug(message, ...args) {
|
|
39
|
+
this.log("debug", message, ...args);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
info(message, ...args) {
|
|
43
|
+
this.log("info", message, ...args);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
warn(message, ...args) {
|
|
47
|
+
this.log("warn", message, ...args);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
error(message, ...args) {
|
|
51
|
+
let stack = false;
|
|
52
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
53
|
+
if (e.stack) {
|
|
54
|
+
stack = true;
|
|
55
|
+
this.log("error", e.stack);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
if (!stack)
|
|
59
|
+
this.log("error", message, ...args);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
time(key, level = "debug") {
|
|
63
|
+
this.cachedTimers[key] = {
|
|
64
|
+
level,
|
|
65
|
+
time: new Date().getTime()
|
|
66
|
+
};
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
timeEnd(key, message, ...args) {
|
|
70
|
+
if (this.cachedTimers[key]) {
|
|
71
|
+
const timer = this.cachedTimers[key];
|
|
72
|
+
message = message + " +%ims";
|
|
73
|
+
args.push(new Date().getTime() - timer.time);
|
|
74
|
+
this[timer.level](message, ...args);
|
|
75
|
+
delete this.cachedTimers[key];
|
|
76
|
+
} else {
|
|
77
|
+
this.warn("timeEnd not found key %s", key);
|
|
78
|
+
this.debug(message);
|
|
79
|
+
}
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
raw(message, ...args) {
|
|
83
|
+
if (this.silent)
|
|
84
|
+
return this;
|
|
85
|
+
this.stdout(format(message, ...args));
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
colorfy(color, message) {
|
|
89
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
90
|
+
}
|
|
91
|
+
log(level, message, ...args) {
|
|
92
|
+
if (this.silent)
|
|
93
|
+
return this;
|
|
94
|
+
if (LevelPriority[level] < this.level)
|
|
95
|
+
return this;
|
|
96
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + format(message, ...args);
|
|
97
|
+
if (this.colorfyOutput && level !== "error")
|
|
98
|
+
output = this.colorfy(LevelColor[level], output);
|
|
99
|
+
else if (!this.colorfyOutput)
|
|
100
|
+
output = output.replace(/\n/g, "");
|
|
101
|
+
if (!output)
|
|
102
|
+
return this;
|
|
103
|
+
if (output.length > this.size)
|
|
104
|
+
output = output.slice(0, this.size) + "...";
|
|
105
|
+
if (level === "error")
|
|
106
|
+
this.stderr(output);
|
|
107
|
+
else
|
|
108
|
+
this.stdout(output);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// ../func/src/index.ts
|
|
114
|
+
import { randomBytes } from "crypto";
|
|
115
|
+
var plugins = [];
|
|
116
|
+
function usePlugin(plugin) {
|
|
117
|
+
if (!plugins.find((p) => p.name === plugin.name))
|
|
118
|
+
plugins.push(plugin);
|
|
119
|
+
if (!plugin.mount)
|
|
120
|
+
plugin.mount = async function({ config }) {
|
|
121
|
+
if (plugin.onMount)
|
|
122
|
+
await plugin.onMount({
|
|
123
|
+
config,
|
|
124
|
+
event: {},
|
|
125
|
+
context: {},
|
|
126
|
+
logger: new Logger(plugin.name)
|
|
127
|
+
}, async () => Promise.resolve());
|
|
128
|
+
};
|
|
129
|
+
return plugin;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ../deep_merge/src/index.ts
|
|
133
|
+
var shouldMerge = function(item) {
|
|
134
|
+
const type = Object.prototype.toString.call(item);
|
|
135
|
+
return type === "[object Object]" || type === "[object Array]";
|
|
136
|
+
};
|
|
137
|
+
function deepMerge(...sources) {
|
|
138
|
+
let acc = /* @__PURE__ */ Object.create(null);
|
|
139
|
+
for (const source of sources)
|
|
140
|
+
if (source instanceof Array) {
|
|
141
|
+
if (!(acc instanceof Array))
|
|
142
|
+
acc = [];
|
|
143
|
+
acc = [...new Set(source.concat(...acc))];
|
|
144
|
+
} else if (shouldMerge(source))
|
|
145
|
+
for (const [key, value] of Object.entries(source)) {
|
|
146
|
+
let val;
|
|
147
|
+
if (shouldMerge(value))
|
|
148
|
+
val = deepMerge(acc[key], value);
|
|
149
|
+
else
|
|
150
|
+
val = value;
|
|
151
|
+
acc = {
|
|
152
|
+
...acc,
|
|
153
|
+
[key]: val
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return acc;
|
|
157
|
+
}
|
|
158
|
+
|
|
9
159
|
// 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
160
|
import knex from "knex";
|
|
16
161
|
var Name = "knex";
|
|
17
162
|
if (!global["FaasJS_Knex"]) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/knex",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.453",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@faasjs/deep_merge": "^0.0.2-beta.
|
|
26
|
-
"@faasjs/func": "^0.0.2-beta.
|
|
27
|
-
"@faasjs/logger": "^0.0.2-beta.
|
|
25
|
+
"@faasjs/deep_merge": "^0.0.2-beta.453",
|
|
26
|
+
"@faasjs/func": "^0.0.2-beta.453",
|
|
27
|
+
"@faasjs/logger": "^0.0.2-beta.453",
|
|
28
28
|
"knex": "*"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|