@quillsql/node 0.9.16 → 0.9.18
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/cjs/db/CachedConnection.d.ts +1 -5
- package/dist/cjs/db/CachedConnection.js +20 -92
- package/dist/cjs/db/CachedConnection.js.map +1 -1
- package/dist/cjs/index.d.ts +17 -3
- package/dist/cjs/index.js +188 -131
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/models/Cache.d.ts +7 -12
- package/dist/cjs/utils/Lock.d.ts +13 -0
- package/dist/cjs/utils/Lock.js +77 -0
- package/dist/cjs/utils/Lock.js.map +1 -0
- package/dist/esm/db/CachedConnection.d.ts +1 -5
- package/dist/esm/db/CachedConnection.js +20 -92
- package/dist/esm/db/CachedConnection.js.map +1 -1
- package/dist/esm/index.d.ts +17 -3
- package/dist/esm/index.js +188 -131
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/models/Cache.d.ts +7 -12
- package/dist/esm/utils/Lock.d.ts +13 -0
- package/dist/esm/utils/Lock.js +73 -0
- package/dist/esm/utils/Lock.js.map +1 -0
- package/package.json +2 -2
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { Mappable, CacheCredentials } from "../models/Cache";
|
|
2
2
|
import { DatabaseConnection, DatabaseConnectionConfig, DatabaseConnectionType } from "./DatabaseHelper";
|
|
3
3
|
export declare class CachedConnection {
|
|
4
|
-
private static sharedRedisClients;
|
|
5
4
|
databaseType: DatabaseConnectionType;
|
|
6
5
|
readonly pool: DatabaseConnection;
|
|
7
|
-
tenantIds: (string | number)[] | null;
|
|
8
6
|
ttl: number;
|
|
9
7
|
cache: Mappable | null;
|
|
8
|
+
isCacheJobRunning: boolean;
|
|
10
9
|
private _isClosed;
|
|
11
10
|
get isClosed(): boolean;
|
|
12
11
|
/** Private constructor; use CachedConnection.create(...) */
|
|
@@ -14,9 +13,6 @@ export declare class CachedConnection {
|
|
|
14
13
|
/** Async factory initializer */
|
|
15
14
|
static create(databaseType: DatabaseConnectionType, config: DatabaseConnectionConfig, cacheConfig?: Partial<CacheCredentials>): Promise<CachedConnection>;
|
|
16
15
|
query(text: string, overwriteCache?: boolean): Promise<any>;
|
|
17
|
-
getCachedValue(key: string): Promise<string | null>;
|
|
18
|
-
setCachedValue(key: string, value: string): Promise<void>;
|
|
19
|
-
deleteCachedValue(key: string): Promise<void>;
|
|
20
16
|
/**
|
|
21
17
|
* Configures and returns a cache instance or null if none could be created.
|
|
22
18
|
*/
|
|
@@ -22,7 +22,7 @@ class CachedConnection {
|
|
|
22
22
|
/** Private constructor; use CachedConnection.create(...) */
|
|
23
23
|
constructor(databaseType, config, cacheConfig = {}) {
|
|
24
24
|
var _a;
|
|
25
|
-
this.
|
|
25
|
+
this.isCacheJobRunning = false;
|
|
26
26
|
this._isClosed = false;
|
|
27
27
|
this.databaseType = databaseType;
|
|
28
28
|
this.pool = (0, DatabaseHelper_1.connectToDatabase)(databaseType, config);
|
|
@@ -39,7 +39,6 @@ class CachedConnection {
|
|
|
39
39
|
}
|
|
40
40
|
query(text_1) {
|
|
41
41
|
return __awaiter(this, arguments, void 0, function* (text, overwriteCache = false) {
|
|
42
|
-
var _a;
|
|
43
42
|
try {
|
|
44
43
|
if (this.isClosed) {
|
|
45
44
|
throw new Error("Connection is closed");
|
|
@@ -47,63 +46,27 @@ class CachedConnection {
|
|
|
47
46
|
if (!this.cache) {
|
|
48
47
|
return yield (0, DatabaseHelper_1.runQueryByDatabase)(this.databaseType, this.pool, text);
|
|
49
48
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
49
|
+
// `text` must be a tenant-scoped SQL query.
|
|
50
|
+
// Since runQueryByDatabase doesn't receive tenant information from any
|
|
51
|
+
// of its other parameters
|
|
52
|
+
const key = text;
|
|
54
53
|
if (!overwriteCache) {
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
const parsedMeta = JSON.parse(cachedMeta);
|
|
59
|
-
const [cachedFields, cachedRows] = yield Promise.all([
|
|
60
|
-
this.cache.lRange(fieldsKey, 0, -1),
|
|
61
|
-
this.cache.lRange(rowsKey, 0, -1),
|
|
62
|
-
]);
|
|
63
|
-
if (parsedMeta.fieldsCount === cachedFields.length &&
|
|
64
|
-
parsedMeta.rowsCount === cachedRows.length) {
|
|
65
|
-
return {
|
|
66
|
-
fields: cachedFields.map((item) => JSON.parse(item)),
|
|
67
|
-
rows: cachedRows.map((item) => JSON.parse(item)),
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
catch (_b) {
|
|
72
|
-
// Any parse/shape failure should invalidate this cache set.
|
|
73
|
-
}
|
|
74
|
-
yield this.cache.del(metaKey, fieldsKey, rowsKey);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const newResult = yield (0, DatabaseHelper_1.runQueryByDatabase)(this.databaseType, this.pool, text);
|
|
78
|
-
if (!newResult) {
|
|
79
|
-
return newResult;
|
|
54
|
+
let cachedResult = yield this.cache.get(key);
|
|
55
|
+
if (cachedResult)
|
|
56
|
+
return JSON.parse(cachedResult);
|
|
80
57
|
}
|
|
58
|
+
const result = yield (0, DatabaseHelper_1.runQueryByDatabase)(this.databaseType, this.pool, text);
|
|
81
59
|
try {
|
|
82
|
-
|
|
83
|
-
const rows = Array.isArray(newResult.rows) ? newResult.rows : [];
|
|
84
|
-
const fieldsPayload = fields.map((field) => JSON.stringify(field));
|
|
85
|
-
const rowsPayload = rows.map((row) => JSON.stringify(row));
|
|
86
|
-
const metaPayload = JSON.stringify({
|
|
87
|
-
fieldsCount: fieldsPayload.length,
|
|
88
|
-
rowsCount: rowsPayload.length,
|
|
89
|
-
});
|
|
90
|
-
const tx = this.cache.multi();
|
|
91
|
-
tx.del(metaKey, fieldsKey, rowsKey);
|
|
92
|
-
if (fieldsPayload.length) {
|
|
93
|
-
tx.rPush(fieldsKey, fieldsPayload);
|
|
94
|
-
tx.expire(fieldsKey, this.ttl);
|
|
95
|
-
}
|
|
96
|
-
if (rowsPayload.length) {
|
|
97
|
-
tx.rPush(rowsKey, rowsPayload);
|
|
98
|
-
tx.expire(rowsKey, this.ttl);
|
|
99
|
-
}
|
|
100
|
-
tx.set(metaKey, metaPayload, "EX", this.ttl);
|
|
101
|
-
yield tx.exec();
|
|
60
|
+
yield this.cache.set(key, JSON.stringify(result), "EX", this.ttl);
|
|
102
61
|
}
|
|
103
62
|
catch (error) {
|
|
104
|
-
|
|
63
|
+
const message = "Redis Cache Set Failed: " + String(error);
|
|
64
|
+
if (this.isCacheJobRunning) {
|
|
65
|
+
throw new Error(message);
|
|
66
|
+
}
|
|
67
|
+
console.warn(message); // This runs for general data fetching by customers
|
|
105
68
|
}
|
|
106
|
-
return
|
|
69
|
+
return result;
|
|
107
70
|
}
|
|
108
71
|
catch (err) {
|
|
109
72
|
if ((0, Error_1.isSuperset)(err, Error_1.PgError)) {
|
|
@@ -115,56 +78,22 @@ class CachedConnection {
|
|
|
115
78
|
}
|
|
116
79
|
});
|
|
117
80
|
}
|
|
118
|
-
getCachedValue(key) {
|
|
119
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
-
if (!this.cache || this.isClosed)
|
|
121
|
-
return null;
|
|
122
|
-
return yield this.cache.get(key);
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
setCachedValue(key, value) {
|
|
126
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
127
|
-
if (!this.cache || this.isClosed)
|
|
128
|
-
return;
|
|
129
|
-
yield this.cache.set(key, value, "EX", this.ttl);
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
deleteCachedValue(key) {
|
|
133
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
-
if (!this.cache || this.isClosed)
|
|
135
|
-
return;
|
|
136
|
-
yield this.cache.del(key);
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
81
|
/**
|
|
140
82
|
* Configures and returns a cache instance or null if none could be created.
|
|
141
83
|
*/
|
|
142
84
|
getCache(_a) {
|
|
143
85
|
return __awaiter(this, arguments, void 0, function* ({ username, password, host, port, cacheType, }) {
|
|
144
86
|
if (cacheType === "redis" || cacheType === "rediss") {
|
|
145
|
-
const sharedKey = JSON.stringify({
|
|
146
|
-
cacheType,
|
|
147
|
-
username: username || "",
|
|
148
|
-
password: password || "",
|
|
149
|
-
host: host || "",
|
|
150
|
-
port: port || "",
|
|
151
|
-
});
|
|
152
|
-
const existingClient = CachedConnection.sharedRedisClients.get(sharedKey);
|
|
153
|
-
if (existingClient) {
|
|
154
|
-
return existingClient;
|
|
155
|
-
}
|
|
156
87
|
const client = (0, redis_1.createClient)({
|
|
157
|
-
username: username ||
|
|
158
|
-
password: password ||
|
|
88
|
+
username: username || '',
|
|
89
|
+
password: password || '',
|
|
159
90
|
socket: {
|
|
160
|
-
host: host ||
|
|
161
|
-
port: port ? parseInt(port) : 0
|
|
91
|
+
host: host || '',
|
|
92
|
+
port: port ? parseInt(port) : 0
|
|
162
93
|
},
|
|
163
94
|
});
|
|
164
95
|
client.on("error", (err) => console.log("Redis Client Error", err));
|
|
165
96
|
yield client.connect();
|
|
166
|
-
// console.log("Redis connected to host:", host)
|
|
167
|
-
CachedConnection.sharedRedisClients.set(sharedKey, client);
|
|
168
97
|
return client;
|
|
169
98
|
}
|
|
170
99
|
return null;
|
|
@@ -181,5 +110,4 @@ class CachedConnection {
|
|
|
181
110
|
}
|
|
182
111
|
}
|
|
183
112
|
exports.CachedConnection = CachedConnection;
|
|
184
|
-
CachedConnection.sharedRedisClients = new Map();
|
|
185
113
|
//# sourceMappingURL=CachedConnection.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CachedConnection.js","sourceRoot":"","sources":["../../../src/db/CachedConnection.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,iCAAqC;AACrC,0CAAqD;AACrD,qDAO0B;AAE1B,mDAAmD;AACnD,MAAM,iBAAiB,GAAG,EAAE,GAAG,EAAE,G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@@ -19,6 +19,21 @@ export interface QuillQueryResult {
|
|
|
19
19
|
fields: any[];
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
+
export type QuillCacheResult = {
|
|
23
|
+
status: "failed";
|
|
24
|
+
error: string;
|
|
25
|
+
} | {
|
|
26
|
+
status: "complete";
|
|
27
|
+
cachedDashboards: string[];
|
|
28
|
+
failedDashboards: string[];
|
|
29
|
+
};
|
|
30
|
+
export interface QuillCacheParams {
|
|
31
|
+
tenants: QuillQueryParams["tenants"];
|
|
32
|
+
dashboards?: string[];
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
parallelQueries?: number;
|
|
35
|
+
maxRowsPerReport?: number;
|
|
36
|
+
}
|
|
22
37
|
export type QuillStreamEvent = {
|
|
23
38
|
type: "start";
|
|
24
39
|
} | {
|
|
@@ -74,6 +89,7 @@ export declare class Quill implements AsyncDisposable {
|
|
|
74
89
|
private baseUrl;
|
|
75
90
|
private config;
|
|
76
91
|
private initPromise;
|
|
92
|
+
private lock;
|
|
77
93
|
constructor(data: {
|
|
78
94
|
privateKey: string;
|
|
79
95
|
databaseType: "snowflake";
|
|
@@ -107,9 +123,7 @@ export declare class Quill implements AsyncDisposable {
|
|
|
107
123
|
metadataServerURL?: string;
|
|
108
124
|
});
|
|
109
125
|
private ensureReady;
|
|
110
|
-
|
|
111
|
-
private getTenantMappedFlagsFromCache;
|
|
112
|
-
private setTenantMappedFlagsCache;
|
|
126
|
+
cache({ tenants, dashboards, signal, parallelQueries, maxRowsPerReport, }: QuillCacheParams): Promise<QuillCacheResult>;
|
|
113
127
|
query({ tenants, flags, metadata, filters, adminEnabled, }: QuillQueryParams): Promise<QuillQueryResult>;
|
|
114
128
|
stream({ tenants, flags, metadata, filters, adminEnabled, signal, }: QuillStreamOptions): Promise<AsyncIterable<QuillStreamEvent>>;
|
|
115
129
|
private applyLimit;
|