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