Skip to content

Commit 5e21df6

Browse files
committed
Only allow priming keys that do not already exist. Update documentation
and add new tests to match.
1 parent a7be5f2 commit 5e21df6

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ method chaining.
198198

199199
##### `prime(key, value)`
200200

201-
Primes the cache with the provided key and value. If the key already exists, the
202-
value will be updated. Returns itself for method chaining.
201+
Primes the cache with the provided key and value. If the key already exists, no
202+
change is made. (To forcefully prime the cache, clear the key first with
203+
`loader.clear(key).prime(key, value)`.) Returns itself for method chaining.
203204

204205

205206
## Using with GraphQL

src/__tests__/dataloader-test.js

+42
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,48 @@ describe('Primary API', () => {
181181
expect(loadCalls).to.deep.equal([ [ 'B' ] ]);
182182
});
183183

184+
it('does not prime keys that already exist', async () => {
185+
var [ identityLoader, loadCalls ] = idLoader();
186+
187+
identityLoader.prime('A', 'X');
188+
189+
var a1 = await identityLoader.load('A');
190+
var b1 = await identityLoader.load('B');
191+
expect(a1).to.equal('X');
192+
expect(b1).to.equal('B');
193+
194+
identityLoader.prime('A', 'Y');
195+
identityLoader.prime('B', 'Y');
196+
197+
var a2 = await identityLoader.load('A');
198+
var b2 = await identityLoader.load('B');
199+
expect(a2).to.equal('X');
200+
expect(b2).to.equal('B');
201+
202+
expect(loadCalls).to.deep.equal([ [ 'B' ] ]);
203+
});
204+
205+
it('allows forcefully priming the cache', async () => {
206+
var [ identityLoader, loadCalls ] = idLoader();
207+
208+
identityLoader.prime('A', 'X');
209+
210+
var a1 = await identityLoader.load('A');
211+
var b1 = await identityLoader.load('B');
212+
expect(a1).to.equal('X');
213+
expect(b1).to.equal('B');
214+
215+
identityLoader.clear('A').prime('A', 'Y');
216+
identityLoader.clear('B').prime('B', 'Y');
217+
218+
var a2 = await identityLoader.load('A');
219+
var b2 = await identityLoader.load('B');
220+
expect(a2).to.equal('Y');
221+
expect(b2).to.equal('Y');
222+
223+
expect(loadCalls).to.deep.equal([ [ 'B' ] ]);
224+
});
225+
184226
});
185227

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

src/index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ export default class DataLoader<K, V> {
159159
}
160160

161161
/**
162-
* Adds/updates the provied key and value in the cache. Returns itself for
163-
* method chaining.
162+
* Adds the provied key and value to the cache. If the key already exists, no
163+
* change is made. Returns itself for method chaining.
164164
*/
165165
prime(key: K, value: V): DataLoader<K, V> {
166166
var cacheKeyFn = this._options && this._options.cacheKeyFn;
@@ -172,7 +172,11 @@ export default class DataLoader<K, V> {
172172
Promise.reject(value) :
173173
Promise.resolve(value);
174174

175-
this._promiseCache.set(cacheKey, promise);
175+
// Only add the key if it does not already exist.
176+
if (this._promiseCache.get(cacheKey) === undefined) {
177+
this._promiseCache.set(cacheKey, promise);
178+
}
179+
176180
return this;
177181
}
178182
}

0 commit comments

Comments
 (0)