@openhorizontal/dataloader-cache 1.0.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/index.cjs +82 -0
- package/dist/index.d.cts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.mjs +47 -0
- package/package.json +31 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
createDataLoader: () => createDataLoader
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_dataloader = __toESM(require("dataloader"), 1);
|
|
37
|
+
var createDataLoader = ({ cache, namespace, ttl }, { model, key }) => {
|
|
38
|
+
return new import_dataloader.default(async (values) => {
|
|
39
|
+
const results = /* @__PURE__ */ new Map();
|
|
40
|
+
const cacheMisses = [];
|
|
41
|
+
await Promise.all(
|
|
42
|
+
values.map(async (value) => {
|
|
43
|
+
try {
|
|
44
|
+
const cacheHit = await cache.get(`${namespace}:${key}:${value}`);
|
|
45
|
+
if (cacheHit !== void 0) {
|
|
46
|
+
results.set(value, JSON.parse(cacheHit));
|
|
47
|
+
} else {
|
|
48
|
+
cacheMisses.push(value);
|
|
49
|
+
}
|
|
50
|
+
} catch {
|
|
51
|
+
cacheMisses.push(value);
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
if (cacheMisses.length > 0) {
|
|
56
|
+
const rows = await model.findMany({
|
|
57
|
+
where: { [key]: { in: cacheMisses } }
|
|
58
|
+
});
|
|
59
|
+
await Promise.all(
|
|
60
|
+
rows.map(async (row) => {
|
|
61
|
+
const value = String(row[key]);
|
|
62
|
+
results.set(value, row);
|
|
63
|
+
try {
|
|
64
|
+
await cache.set(
|
|
65
|
+
`${namespace}:${key}:${value}`,
|
|
66
|
+
JSON.stringify(row),
|
|
67
|
+
{
|
|
68
|
+
ttl
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
} catch {
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return values.map((id) => results.get(id) ?? null);
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
createDataLoader
|
|
82
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import DataLoader from 'dataloader';
|
|
2
|
+
|
|
3
|
+
type Cache = {
|
|
4
|
+
get(key: string): Promise<string | undefined>;
|
|
5
|
+
set(key: string, value: string, options?: {
|
|
6
|
+
ttl?: number;
|
|
7
|
+
}): Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
type LookupDefinition<T> = {
|
|
10
|
+
model: {
|
|
11
|
+
findMany(args: {
|
|
12
|
+
where?: Record<string, unknown>;
|
|
13
|
+
}): Promise<T[]>;
|
|
14
|
+
};
|
|
15
|
+
key: keyof T & string;
|
|
16
|
+
};
|
|
17
|
+
type CacheOptions = {
|
|
18
|
+
ttl: number;
|
|
19
|
+
cache: Cache;
|
|
20
|
+
namespace: string;
|
|
21
|
+
};
|
|
22
|
+
declare const createDataLoader: <T>({ cache, namespace, ttl }: CacheOptions, { model, key }: LookupDefinition<T>) => DataLoader<string, T | null, string>;
|
|
23
|
+
|
|
24
|
+
export { createDataLoader };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import DataLoader from 'dataloader';
|
|
2
|
+
|
|
3
|
+
type Cache = {
|
|
4
|
+
get(key: string): Promise<string | undefined>;
|
|
5
|
+
set(key: string, value: string, options?: {
|
|
6
|
+
ttl?: number;
|
|
7
|
+
}): Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
type LookupDefinition<T> = {
|
|
10
|
+
model: {
|
|
11
|
+
findMany(args: {
|
|
12
|
+
where?: Record<string, unknown>;
|
|
13
|
+
}): Promise<T[]>;
|
|
14
|
+
};
|
|
15
|
+
key: keyof T & string;
|
|
16
|
+
};
|
|
17
|
+
type CacheOptions = {
|
|
18
|
+
ttl: number;
|
|
19
|
+
cache: Cache;
|
|
20
|
+
namespace: string;
|
|
21
|
+
};
|
|
22
|
+
declare const createDataLoader: <T>({ cache, namespace, ttl }: CacheOptions, { model, key }: LookupDefinition<T>) => DataLoader<string, T | null, string>;
|
|
23
|
+
|
|
24
|
+
export { createDataLoader };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import DataLoader from "dataloader";
|
|
3
|
+
var createDataLoader = ({ cache, namespace, ttl }, { model, key }) => {
|
|
4
|
+
return new DataLoader(async (values) => {
|
|
5
|
+
const results = /* @__PURE__ */ new Map();
|
|
6
|
+
const cacheMisses = [];
|
|
7
|
+
await Promise.all(
|
|
8
|
+
values.map(async (value) => {
|
|
9
|
+
try {
|
|
10
|
+
const cacheHit = await cache.get(`${namespace}:${key}:${value}`);
|
|
11
|
+
if (cacheHit !== void 0) {
|
|
12
|
+
results.set(value, JSON.parse(cacheHit));
|
|
13
|
+
} else {
|
|
14
|
+
cacheMisses.push(value);
|
|
15
|
+
}
|
|
16
|
+
} catch {
|
|
17
|
+
cacheMisses.push(value);
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
);
|
|
21
|
+
if (cacheMisses.length > 0) {
|
|
22
|
+
const rows = await model.findMany({
|
|
23
|
+
where: { [key]: { in: cacheMisses } }
|
|
24
|
+
});
|
|
25
|
+
await Promise.all(
|
|
26
|
+
rows.map(async (row) => {
|
|
27
|
+
const value = String(row[key]);
|
|
28
|
+
results.set(value, row);
|
|
29
|
+
try {
|
|
30
|
+
await cache.set(
|
|
31
|
+
`${namespace}:${key}:${value}`,
|
|
32
|
+
JSON.stringify(row),
|
|
33
|
+
{
|
|
34
|
+
ttl
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
} catch {
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return values.map((id) => results.get(id) ?? null);
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
export {
|
|
46
|
+
createDataLoader
|
|
47
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openhorizontal/dataloader-cache",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.cjs",
|
|
12
|
+
"import": "./dist/index.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "^26.0.1",
|
|
25
|
+
"tsup": "^8.5.1",
|
|
26
|
+
"typescript": "^6.0.3"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"dataloader": "^2.2.3"
|
|
30
|
+
}
|
|
31
|
+
}
|