@technicity/data-service-generator 0.11.0-next.1 → 0.11.0-next.3
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/runtime/Cache.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { RedisClientType } from 'redis';
|
|
2
2
|
import { TResolveParams } from './IRuntime';
|
|
3
|
+
import Stats from './Stats';
|
|
3
4
|
declare class Cache {
|
|
4
|
-
protected logs?: boolean | undefined;
|
|
5
5
|
client: RedisClientType;
|
|
6
6
|
waiting?: Set<() => void> | undefined;
|
|
7
|
-
|
|
7
|
+
logs?: boolean;
|
|
8
|
+
stats?: Stats;
|
|
9
|
+
constructor(url: string, debug?: string[]);
|
|
8
10
|
log(message: string): void;
|
|
9
11
|
pending(): Promise<void>;
|
|
10
12
|
from(input: TResolveParams): Promise<{
|
package/dist/runtime/Cache.js
CHANGED
|
@@ -3,21 +3,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const crypto_1 = require("crypto");
|
|
4
4
|
const redis_1 = require("redis");
|
|
5
5
|
const utility_1 = require("./lib/utility");
|
|
6
|
+
const Stats_1 = require("./Stats");
|
|
6
7
|
class Cache {
|
|
7
|
-
constructor(url,
|
|
8
|
-
this.logs = logs;
|
|
8
|
+
constructor(url, debug) {
|
|
9
9
|
this.waiting = new Set();
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.client
|
|
10
|
+
if (debug?.includes("Cache"))
|
|
11
|
+
this.logs = true;
|
|
12
|
+
const client = this.client =
|
|
13
|
+
(0, redis_1.createClient)({ url: `redis://${url}` });
|
|
14
|
+
client.connect();
|
|
15
|
+
client.on("connect", () => {
|
|
13
16
|
if (this.waiting)
|
|
14
17
|
this.waiting.forEach(x => x());
|
|
15
18
|
this.waiting = undefined;
|
|
16
19
|
});
|
|
20
|
+
if (debug?.includes("Stats"))
|
|
21
|
+
this.stats = new Stats_1.default(client);
|
|
17
22
|
}
|
|
18
23
|
log(message) {
|
|
19
24
|
if (this.logs)
|
|
20
|
-
console.log(message);
|
|
25
|
+
console.log(`\n-- CACHE: ${message}\n`);
|
|
21
26
|
}
|
|
22
27
|
async pending() {
|
|
23
28
|
if (this.waiting)
|
|
@@ -50,7 +55,7 @@ class Cache {
|
|
|
50
55
|
}
|
|
51
56
|
if (!pending.length)
|
|
52
57
|
return;
|
|
53
|
-
this.log(`
|
|
58
|
+
this.log(`insert: ${key.substring(0, 6)}`);
|
|
54
59
|
await Promise.all([
|
|
55
60
|
redis.set(`cache:${key}`, json),
|
|
56
61
|
...pending
|
|
@@ -61,11 +66,11 @@ class Cache {
|
|
|
61
66
|
const data = await this.client.get(`cache:${key}`);
|
|
62
67
|
const shorthand = key.substring(0, 6);
|
|
63
68
|
if (data) {
|
|
64
|
-
this.log(`
|
|
69
|
+
this.log(`hit: ${shorthand}`);
|
|
65
70
|
return JSON.parse(data);
|
|
66
71
|
}
|
|
67
72
|
else {
|
|
68
|
-
this.log(`
|
|
73
|
+
this.log(`miss: ${shorthand}`);
|
|
69
74
|
return undefined;
|
|
70
75
|
}
|
|
71
76
|
}
|
|
@@ -24,9 +24,8 @@ class RuntimeMySQL {
|
|
|
24
24
|
_RuntimeMySQL_clientCache.set(this, void 0);
|
|
25
25
|
_RuntimeMySQL_middlewareHandler.set(this, void 0);
|
|
26
26
|
__classPrivateFieldSet(this, _RuntimeMySQL_middlewareHandler, new shared_1.MiddlewareHandler(), "f");
|
|
27
|
-
const debugCache = clientOpts?.debug?.includes("Cache");
|
|
28
27
|
if (otherOpts.redisHost)
|
|
29
|
-
__classPrivateFieldSet(this, _RuntimeMySQL_clientCache, new Cache_1.default(otherOpts.redisHost,
|
|
28
|
+
__classPrivateFieldSet(this, _RuntimeMySQL_clientCache, new Cache_1.default(otherOpts.redisHost, clientOpts?.debug), "f");
|
|
30
29
|
if (otherOpts.supplementClientOpts) {
|
|
31
30
|
clientOpts = {
|
|
32
31
|
supportBigNumbers: true,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RedisClientType } from '@redis/client';
|
|
2
|
+
declare class Stats {
|
|
3
|
+
client: RedisClientType<any>;
|
|
4
|
+
constructor(client: RedisClientType<any>);
|
|
5
|
+
updateStats: (ms: number, category: string) => Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
declare namespace timer {
|
|
8
|
+
type Callback = (time: number, ...params: any[]) => void;
|
|
9
|
+
type Params<T> = T extends (ms: number, ...params: infer U) => any ? U : never;
|
|
10
|
+
}
|
|
11
|
+
export declare function timer(): <T extends timer.Callback>(callback: T, ...args: timer.Params<T>) => void;
|
|
12
|
+
export default Stats;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timer = void 0;
|
|
4
|
+
const perf_hooks_1 = require("perf_hooks");
|
|
5
|
+
class Stats {
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
this.updateStats = async (ms, category) => {
|
|
9
|
+
const redis = this.client;
|
|
10
|
+
const requests = Number(await redis.get(`stats:${category}_requests`)) || 0;
|
|
11
|
+
const average = Number(await redis.get(`stats:${category}_avg`)) || 0;
|
|
12
|
+
const max = Number(await redis.get(`stats:${category}_max`)) || 0;
|
|
13
|
+
const min = Number(await redis.get(`stats:${category}_min`)) || Infinity;
|
|
14
|
+
const newTotalRequests = requests + 1;
|
|
15
|
+
const newAverage = (average * requests + ms) / newTotalRequests;
|
|
16
|
+
await redis.set(`stats:${category}_requests`, newTotalRequests);
|
|
17
|
+
await redis.set(`stats:${category}_avg`, newAverage);
|
|
18
|
+
if (ms > max)
|
|
19
|
+
await redis.set(`stats:${category}_max`, ms);
|
|
20
|
+
if (ms < min)
|
|
21
|
+
await redis.set(`stats:${category}_min`, ms);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function timer() {
|
|
26
|
+
const start = perf_hooks_1.performance.now();
|
|
27
|
+
return (callback, ...args) => {
|
|
28
|
+
callback(perf_hooks_1.performance.now() - start, ...args);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
exports.timer = timer;
|
|
32
|
+
exports.default = Stats;
|
|
@@ -22,6 +22,7 @@ const SDKNotFoundError_1 = require("./SDKNotFoundError");
|
|
|
22
22
|
const SDKBadWhereError_1 = require("./SDKBadWhereError");
|
|
23
23
|
const stringifyWhere_1 = require("./stringifyWhere");
|
|
24
24
|
const getOrderBy_1 = require("./getOrderBy");
|
|
25
|
+
const Stats_1 = require("../Stats");
|
|
25
26
|
async function resolve(input, dbCall, formatQuery, beginTransaction, dialect, middlewareHandler, context, cache) {
|
|
26
27
|
// https://github.com/prisma/prisma/blob/822198e5ba21535364d20c86901b8c3778ebf6a3/packages/client/src/runtime/getPrismaClient.ts#L1087
|
|
27
28
|
let index = -1;
|
|
@@ -266,12 +267,24 @@ async function getData(input, dbCall, formatQuery, dialect) {
|
|
|
266
267
|
return data;
|
|
267
268
|
}
|
|
268
269
|
async function getCached(input, dbCall, formatQuery, dialect, cache) {
|
|
270
|
+
const done = (0, Stats_1.timer)();
|
|
271
|
+
const { stats } = cache;
|
|
269
272
|
const { request, cached } = await cache.from(input);
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
273
|
+
let results;
|
|
274
|
+
if (cached) {
|
|
275
|
+
if (stats)
|
|
276
|
+
done(stats.updateStats, "get_redis");
|
|
277
|
+
results = cache;
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
ensureUuidSelect(input);
|
|
281
|
+
results = await getData(input, dbCall, formatQuery, dialect);
|
|
282
|
+
cache.insert(request, results);
|
|
283
|
+
if (stats)
|
|
284
|
+
done(stats.updateStats, "get_mysql");
|
|
285
|
+
}
|
|
286
|
+
if (stats)
|
|
287
|
+
done(stats.updateStats, "get");
|
|
275
288
|
return results;
|
|
276
289
|
}
|
|
277
290
|
function ensureUuidSelect(input) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@technicity/data-service-generator",
|
|
3
|
-
"version": "0.11.0-next.
|
|
3
|
+
"version": "0.11.0-next.3",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"compile": "rm -rf dist && tsc",
|
|
10
10
|
"publish:next": "npm version prerelease --preid next",
|
|
11
11
|
"preversion": "npm run compile",
|
|
12
|
+
"postversion": "npm publish",
|
|
12
13
|
"generate": "npm run compile && concurrently \"node ./test/mysql/generate.js\" \"node ./test/mysql8/generate.js\"",
|
|
13
14
|
"test": "npm run generate && mocha ./test/addNullFallbacks.test.js && mocha ./test/stringifyWhere.test.js && npm run test:sdk",
|
|
14
15
|
"test:prepare": "docker-compose down --volumes && docker-compose up -d --build",
|