Skip to content

Commit f9dbc11

Browse files
authored
Merge pull request #43 from DxCx/typings
Added embeded typescript typings
2 parents 68a2a2e + ef4d6b4 commit f9dbc11

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

dataloader.d.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// If a custom cache is provided, it must be of this type (a subset of ES6 Map).
2+
type CacheMap<K, V> = {
3+
get(key: K): V | void;
4+
set(key: K, value: V): any;
5+
delete(key: K): any;
6+
clear(): any;
7+
}
8+
9+
// A Function, which when given an Array of keys, returns a Promise of an Array
10+
// of values or Errors.
11+
type BatchLoadFn<K, V> = (keys: K[]) => Promise<Array<V | Error>>;
12+
13+
// Optionally turn off batching or caching or provide a cache key function or a
14+
// custom cache instance.
15+
type Options<K, V> = {
16+
17+
/**
18+
* Default `true`. Set to `false` to disable batching,
19+
* instead immediately invoking `batchLoadFn` with a
20+
* single load key.
21+
*/
22+
batch?: boolean,
23+
24+
/**
25+
* Default `true`. Set to `false` to disable caching,
26+
* instead creating a new Promise and new key in
27+
* the `batchLoadFn` for every load.
28+
*/
29+
cache?: boolean,
30+
31+
/**
32+
* A function to produce a cache key for a given load key.
33+
* Defaults to `key => key`. Useful to provide when JavaScript
34+
* objects are keys and two similarly shaped objects should
35+
* be considered equivalent.
36+
*/
37+
cacheKeyFn?: (key: any) => any,
38+
39+
/**
40+
* An instance of Map (or an object with a similar API) to
41+
* be used as the underlying cache for this loader.
42+
* Default `new Map()`.
43+
*/
44+
cacheMap?: CacheMap<K, Promise<V>>;
45+
}
46+
47+
/**
48+
* DataLoader creates a public API for loading data from a particular
49+
* data back-end with unique keys such as the id column of a SQL table
50+
* or document name in a MongoDB database, given a batch loading function.
51+
*
52+
* Each DataLoader instance contains a unique memoized cache. Use caution
53+
* when used in long-lived applications or those which serve many users
54+
* with different access permissions and consider creating a new instance
55+
* per web request.
56+
*/
57+
export class DataLoader<K, V> {
58+
59+
constructor(batchLoadFn: BatchLoadFn<K, V>, options?: Options<K, V>);
60+
61+
/**
62+
* Loads a key, returning a `Promise` for the value represented by that key.
63+
*/
64+
load(key: K): Promise<V>;
65+
66+
/**
67+
* Loads multiple keys, promising an array of values:
68+
*
69+
* var [ a, b ] = await myLoader.loadMany([ 'a', 'b' ]);
70+
*
71+
* This is equivalent to the more verbose:
72+
*
73+
* var [ a, b ] = await Promise.all([
74+
* myLoader.load('a'),
75+
* myLoader.load('b')
76+
* ]);
77+
*
78+
*/
79+
loadMany(keys: K[]): Promise<V[]>;
80+
81+
/**
82+
* Clears the value at `key` from the cache, if it exists. Returns itself for
83+
* method chaining.
84+
*/
85+
clear(key: K): DataLoader<K, V>;
86+
87+
/**
88+
* Clears the entire cache. To be used when some event results in unknown
89+
* invalidations across this particular `DataLoader`. Returns itself for
90+
* method chaining.
91+
*/
92+
clearAll(): DataLoader<K, V>;
93+
94+
/**
95+
* Adds the provied key and value to the cache. If the key already exists, no
96+
* change is made. Returns itself for method chaining.
97+
*/
98+
prime(key: K, value: V): DataLoader<K, V>;
99+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
},
3535
"files": [
3636
"dist",
37+
"dataloader.d.ts",
3738
"README.md",
3839
"LICENSE",
3940
"PATENTS"
@@ -51,5 +52,6 @@
5152
"isparta": "3.0.3",
5253
"mocha": "2.3.4",
5354
"sane": "1.3.0"
54-
}
55+
},
56+
"typings": "dataloader.d.ts"
5557
}

0 commit comments

Comments
 (0)