@digitraffic/common 2022.12.1-1 → 2022.12.2-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/database/cached.d.ts +5 -0
- package/dist/database/cached.js +10 -4
- package/package.json +1 -1
- package/src/database/cached.ts +32 -13
@@ -1,7 +1,12 @@
|
|
1
1
|
import { DTDatabase, DTTransaction } from "./database";
|
2
|
+
export interface CachedValue<T> {
|
3
|
+
content: T;
|
4
|
+
last_updated: Date;
|
5
|
+
}
|
2
6
|
export declare enum JSON_CACHE_KEY {
|
3
7
|
NAUTICAL_WARNINGS_ACTIVE = "nautical-warnings-active",
|
4
8
|
NAUTICAL_WARNINGS_ARCHIVED = "nautical-warnings-archived"
|
5
9
|
}
|
6
10
|
export declare function updateCachedJson<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY, value: T): Promise<null>;
|
7
11
|
export declare function getJsonFromCache<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY): Promise<T | null>;
|
12
|
+
export declare function getFromCache<T>(db: DTDatabase | DTTransaction, cacheKey: JSON_CACHE_KEY): Promise<CachedValue<T> | null>;
|
package/dist/database/cached.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.getJsonFromCache = exports.updateCachedJson = exports.JSON_CACHE_KEY = void 0;
|
3
|
+
exports.getFromCache = exports.getJsonFromCache = exports.updateCachedJson = exports.JSON_CACHE_KEY = void 0;
|
4
4
|
const pg_promise_1 = require("pg-promise");
|
5
5
|
const SQL_UPDATE_CACHE_VALUE = `insert into cached_json(cache_id, content, last_updated)
|
6
6
|
values ($1, $2, now())
|
@@ -9,11 +9,11 @@ const SQL_UPDATE_CACHE_VALUE = `insert into cached_json(cache_id, content, last_
|
|
9
9
|
const SQL_GET_CACHE_VALUE = `select content, last_updated from cached_json
|
10
10
|
where cache_id = $1`;
|
11
11
|
const PS_UPDATE_CACHE_VALUE = new pg_promise_1.PreparedStatement({
|
12
|
-
name:
|
12
|
+
name: "update-cache-value",
|
13
13
|
text: SQL_UPDATE_CACHE_VALUE,
|
14
14
|
});
|
15
15
|
const PS_GET_CACHE_VALUE = new pg_promise_1.PreparedStatement({
|
16
|
-
name:
|
16
|
+
name: "get-cache-value",
|
17
17
|
text: SQL_GET_CACHE_VALUE,
|
18
18
|
});
|
19
19
|
var JSON_CACHE_KEY;
|
@@ -26,7 +26,13 @@ function updateCachedJson(db, cacheKey, value) {
|
|
26
26
|
}
|
27
27
|
exports.updateCachedJson = updateCachedJson;
|
28
28
|
function getJsonFromCache(db, cacheKey) {
|
29
|
-
return db
|
29
|
+
return db
|
30
|
+
.oneOrNone(PS_GET_CACHE_VALUE, [cacheKey])
|
31
|
+
.then((value) => value?.content ?? null);
|
30
32
|
}
|
31
33
|
exports.getJsonFromCache = getJsonFromCache;
|
34
|
+
function getFromCache(db, cacheKey) {
|
35
|
+
return db.oneOrNone(PS_GET_CACHE_VALUE, [cacheKey]);
|
36
|
+
}
|
37
|
+
exports.getFromCache = getFromCache;
|
32
38
|
//# sourceMappingURL=cached.js.map
|
package/package.json
CHANGED
package/src/database/cached.ts
CHANGED
@@ -1,35 +1,54 @@
|
|
1
|
-
import {PreparedStatement} from "pg-promise";
|
2
|
-
import {DTDatabase, DTTransaction} from "./database";
|
1
|
+
import { PreparedStatement } from "pg-promise";
|
2
|
+
import { DTDatabase, DTTransaction } from "./database";
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
export interface CachedValue<T> {
|
5
|
+
content: T;
|
6
|
+
last_updated: Date;
|
7
|
+
}
|
8
|
+
|
9
|
+
const SQL_UPDATE_CACHE_VALUE = `insert into cached_json(cache_id, content, last_updated)
|
6
10
|
values ($1, $2, now())
|
7
11
|
on conflict(cache_id) do
|
8
12
|
update set content = $2, last_updated = now()`;
|
9
13
|
|
10
|
-
const SQL_GET_CACHE_VALUE =
|
11
|
-
`select content, last_updated from cached_json
|
14
|
+
const SQL_GET_CACHE_VALUE = `select content, last_updated from cached_json
|
12
15
|
where cache_id = $1`;
|
13
16
|
|
14
17
|
const PS_UPDATE_CACHE_VALUE = new PreparedStatement({
|
15
|
-
name:
|
18
|
+
name: "update-cache-value",
|
16
19
|
text: SQL_UPDATE_CACHE_VALUE,
|
17
20
|
});
|
18
21
|
|
19
22
|
const PS_GET_CACHE_VALUE = new PreparedStatement({
|
20
|
-
name:
|
23
|
+
name: "get-cache-value",
|
21
24
|
text: SQL_GET_CACHE_VALUE,
|
22
25
|
});
|
23
26
|
|
24
27
|
export enum JSON_CACHE_KEY {
|
25
|
-
NAUTICAL_WARNINGS_ACTIVE =
|
26
|
-
NAUTICAL_WARNINGS_ARCHIVED =
|
28
|
+
NAUTICAL_WARNINGS_ACTIVE = "nautical-warnings-active",
|
29
|
+
NAUTICAL_WARNINGS_ARCHIVED = "nautical-warnings-archived",
|
27
30
|
}
|
28
31
|
|
29
|
-
export function updateCachedJson<T>(
|
32
|
+
export function updateCachedJson<T>(
|
33
|
+
db: DTDatabase | DTTransaction,
|
34
|
+
cacheKey: JSON_CACHE_KEY,
|
35
|
+
value: T
|
36
|
+
): Promise<null> {
|
30
37
|
return db.none(PS_UPDATE_CACHE_VALUE, [cacheKey, value]);
|
31
38
|
}
|
32
39
|
|
33
|
-
export function getJsonFromCache<T>(
|
34
|
-
|
40
|
+
export function getJsonFromCache<T>(
|
41
|
+
db: DTDatabase | DTTransaction,
|
42
|
+
cacheKey: JSON_CACHE_KEY
|
43
|
+
): Promise<T | null> {
|
44
|
+
return db
|
45
|
+
.oneOrNone<CachedValue<T>>(PS_GET_CACHE_VALUE, [cacheKey])
|
46
|
+
.then((value) => value?.content ?? null);
|
47
|
+
}
|
48
|
+
|
49
|
+
export function getFromCache<T>(
|
50
|
+
db: DTDatabase | DTTransaction,
|
51
|
+
cacheKey: JSON_CACHE_KEY
|
52
|
+
): Promise<CachedValue<T> | null> {
|
53
|
+
return db.oneOrNone<CachedValue<T>>(PS_GET_CACHE_VALUE, [cacheKey]);
|
35
54
|
}
|