Skip to content

Commit

Permalink
feat: add name to DataLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 20, 2023
1 parent 588a8b6 commit c5b96e6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-melons-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dataloader": minor
---

Add `name` property to `DataLoader`. Useful in APM tools.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,16 @@ Create a new `DataLoader` given a batch loading function and options.

- *options*: An optional object of options:

| Option Key | Type | Default | Description |
| ---------- | ---- | ------- | ----------- |
| *batch* | Boolean | `true` | Set to `false` to disable batching, invoking `batchLoadFn` with a single load key. This is equivalent to setting `maxBatchSize` to `1`.
| *maxBatchSize* | Number | `Infinity` | Limits the number of items that get passed in to the `batchLoadFn`. May be set to `1` to disable batching.
| *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.
| *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`.
| *cacheKeyFn* | Function | `key => key` | Produces cache key for a given load key. Useful when objects are keys and two objects should be considered equivalent.
| *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.
| Option Key | Type | Default | Description |
|-------------------|----------|-------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `batch` | Boolean | `true` | Set to `false` to disable batching, invoking `batchLoadFn` with a single load key. This is equivalent to setting `maxBatchSize` to `1`. |
| `maxBatchSize` | Number | `Infinity` | Limits the number of items that get passed in to the `batchLoadFn`. May be set to `1` to disable batching. |
| `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. |
| `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`. |
| `cacheKeyFn` | Function | `key => key` | Produces cache key for a given load key. Useful when objects are keys and two objects should be considered equivalent. |
| `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. |
| `name` | String | `null` | The name given to this `DataLoader` instance. Useful for APM tools. |


##### `load(key)`

Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/dataloader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ describe('Primary API', () => {
expect(loadCalls).toEqual([ [ 'B' ] ]);
});

it('allows giving the loader a name', () => {
expect(new DataLoader(() => []).name).toBeNull();
expect(new DataLoader(() => [], {}).name).toBeNull();

expect(new DataLoader(() => [], { name: 'Some name' }).name).toBe(
'Some name'
);
});
});

describe('Represents Errors', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ declare namespace DataLoader {
* to be used as cache. May be set to `null` to disable caching.
*/
cacheMap?: CacheMap<C, Promise<V>> | null;

/**
* The name given to this `DataLoader` instance. Useful for APM tools.
*
* Is `null` if not set in the constructor.
*/
name: string | null;
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Options<K, V, C = K> = {
cache?: boolean;
cacheKeyFn?: (key: K) => C;
cacheMap?: CacheMap<C, Promise<V>> | null;
name?: string;
};

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

// Private
Expand Down Expand Up @@ -201,6 +203,13 @@ class DataLoader<K, V, C = K> {
}
return this;
}

/**
* The name given to this `DataLoader` instance. Useful for APM tools.
*
* Is `null` if not set in the constructor.
*/
name: string | null;
}

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

function getValidName<K, V, C>(
options: ?Options<K, V, C>
): string | null {
if (options && options.name) {
return options.name;
}

return null;
}

// Private
function isArrayLike(x: mixed): boolean {
return (
Expand Down

0 comments on commit c5b96e6

Please sign in to comment.