Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add name to DataLoader #326

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,15 @@ 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 |
Copy link
Contributor Author

@SimenB SimenB Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just passed it through prettier after adding my new field.

|-------------------|----------|-------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `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