Skip to content

Commit 6c758d0

Browse files
authored
feat: add name to DataLoader (#326)
1 parent 588a8b6 commit 6c758d0

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

.changeset/wet-melons-wash.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"dataloader": minor
3+
---
4+
5+
Add `name` property to `DataLoader`. Useful in APM tools.

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,15 @@ Create a new `DataLoader` given a batch loading function and options.
393393

394394
- *options*: An optional object of options:
395395

396-
| Option Key | Type | Default | Description |
397-
| ---------- | ---- | ------- | ----------- |
398-
| *batch* | Boolean | `true` | Set to `false` to disable batching, invoking `batchLoadFn` with a single load key. This is equivalent to setting `maxBatchSize` to `1`.
399-
| *maxBatchSize* | Number | `Infinity` | Limits the number of items that get passed in to the `batchLoadFn`. May be set to `1` to disable batching.
400-
| *batchScheduleFn* | Function | See [Batch scheduling](#batch-scheduling) | A function to schedule the later execution of a batch. The function is expected to call the provided callback in the immediate future.
401-
| *cache* | Boolean | `true` | Set to `false` to disable memoization caching, creating a new Promise and new key in the `batchLoadFn` for every load of the same key. This is equivalent to setting `cacheMap` to `null`.
402-
| *cacheKeyFn* | Function | `key => key` | Produces cache key for a given load key. Useful when objects are keys and two objects should be considered equivalent.
403-
| *cacheMap* | Object | `new Map()` | Instance of [Map][] (or an object with a similar API) to be used as cache. May be set to `null` to disable caching.
396+
| Option Key | Type | Default | Description |
397+
|-------------------|----------|-------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
398+
| `batch` | Boolean | `true` | Set to `false` to disable batching, invoking `batchLoadFn` with a single load key. This is equivalent to setting `maxBatchSize` to `1`. |
399+
| `maxBatchSize` | Number | `Infinity` | Limits the number of items that get passed in to the `batchLoadFn`. May be set to `1` to disable batching. |
400+
| `batchScheduleFn` | Function | See [Batch scheduling](#batch-scheduling) | A function to schedule the later execution of a batch. The function is expected to call the provided callback in the immediate future. |
401+
| `cache` | Boolean | `true` | Set to `false` to disable memoization caching, creating a new Promise and new key in the `batchLoadFn` for every load of the same key. This is equivalent to setting `cacheMap` to `null`. |
402+
| `cacheKeyFn` | Function | `key => key` | Produces cache key for a given load key. Useful when objects are keys and two objects should be considered equivalent. |
403+
| `cacheMap` | Object | `new Map()` | Instance of [Map][] (or an object with a similar API) to be used as cache. May be set to `null` to disable caching. |
404+
| `name` | String | `null` | The name given to this `DataLoader` instance. Useful for APM tools. |
404405

405406
##### `load(key)`
406407

src/__tests__/dataloader.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ describe('Primary API', () => {
412412
expect(loadCalls).toEqual([ [ 'B' ] ]);
413413
});
414414

415+
it('allows giving the loader a name', () => {
416+
expect(new DataLoader(() => []).name).toBeNull();
417+
expect(new DataLoader(() => [], {}).name).toBeNull();
418+
419+
expect(new DataLoader(() => [], { name: 'Some name' }).name).toBe(
420+
'Some name'
421+
);
422+
});
415423
});
416424

417425
describe('Represents Errors', () => {

src/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ declare namespace DataLoader {
115115
* to be used as cache. May be set to `null` to disable caching.
116116
*/
117117
cacheMap?: CacheMap<C, Promise<V>> | null;
118+
119+
/**
120+
* The name given to this `DataLoader` instance. Useful for APM tools.
121+
*
122+
* Is `null` if not set in the constructor.
123+
*/
124+
name: string | null;
118125
}
119126
}
120127

src/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type Options<K, V, C = K> = {
2121
cache?: boolean;
2222
cacheKeyFn?: (key: K) => C;
2323
cacheMap?: CacheMap<C, Promise<V>> | null;
24+
name?: string;
2425
};
2526

2627
// If a custom cache is provided, it must be of this type (a subset of ES6 Map).
@@ -58,6 +59,7 @@ class DataLoader<K, V, C = K> {
5859
this._cacheKeyFn = getValidCacheKeyFn(options);
5960
this._cacheMap = getValidCacheMap(options);
6061
this._batch = null;
62+
this.name = getValidName(options);
6163
}
6264

6365
// Private
@@ -201,6 +203,13 @@ class DataLoader<K, V, C = K> {
201203
}
202204
return this;
203205
}
206+
207+
/**
208+
* The name given to this `DataLoader` instance. Useful for APM tools.
209+
*
210+
* Is `null` if not set in the constructor.
211+
*/
212+
name: string | null;
204213
}
205214

206215
// Private: Enqueue a Job to be executed after all "PromiseJobs" Jobs.
@@ -456,6 +465,16 @@ function getValidCacheMap<K, V, C>(
456465
return cacheMap;
457466
}
458467

468+
function getValidName<K, V, C>(
469+
options: ?Options<K, V, C>
470+
): string | null {
471+
if (options && options.name) {
472+
return options.name;
473+
}
474+
475+
return null;
476+
}
477+
459478
// Private
460479
function isArrayLike(x: mixed): boolean {
461480
return (

0 commit comments

Comments
 (0)