From c5b96e6f2598fc77434039cfdce08228ec988f26 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 20 Jan 2023 09:22:04 +0100 Subject: [PATCH] feat: add `name` to `DataLoader` --- .changeset/wet-melons-wash.md | 5 +++++ README.md | 18 ++++++++++-------- src/__tests__/dataloader.test.js | 8 ++++++++ src/index.d.ts | 7 +++++++ src/index.js | 19 +++++++++++++++++++ 5 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 .changeset/wet-melons-wash.md diff --git a/.changeset/wet-melons-wash.md b/.changeset/wet-melons-wash.md new file mode 100644 index 0000000..9228b18 --- /dev/null +++ b/.changeset/wet-melons-wash.md @@ -0,0 +1,5 @@ +--- +"dataloader": minor +--- + +Add `name` property to `DataLoader`. Useful in APM tools. diff --git a/README.md b/README.md index 5e4afbd..61c2e88 100644 --- a/README.md +++ b/README.md @@ -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)` diff --git a/src/__tests__/dataloader.test.js b/src/__tests__/dataloader.test.js index a32a60b..4f8d955 100644 --- a/src/__tests__/dataloader.test.js +++ b/src/__tests__/dataloader.test.js @@ -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', () => { diff --git a/src/index.d.ts b/src/index.d.ts index 6709279..2942ae4 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -115,6 +115,13 @@ declare namespace DataLoader { * to be used as cache. May be set to `null` to disable caching. */ cacheMap?: CacheMap> | null; + + /** + * The name given to this `DataLoader` instance. Useful for APM tools. + * + * Is `null` if not set in the constructor. + */ + name: string | null; } } diff --git a/src/index.js b/src/index.js index d2b6d02..b346d86 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,7 @@ export type Options = { cache?: boolean; cacheKeyFn?: (key: K) => C; cacheMap?: CacheMap> | null; + name?: string; }; // If a custom cache is provided, it must be of this type (a subset of ES6 Map). @@ -58,6 +59,7 @@ class DataLoader { this._cacheKeyFn = getValidCacheKeyFn(options); this._cacheMap = getValidCacheMap(options); this._batch = null; + this.name = getValidName(options); } // Private @@ -201,6 +203,13 @@ class DataLoader { } 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. @@ -456,6 +465,16 @@ function getValidCacheMap( return cacheMap; } +function getValidName( + options: ?Options +): string | null { + if (options && options.name) { + return options.name; + } + + return null; +} + // Private function isArrayLike(x: mixed): boolean { return (