fix caching for petTypeLoader

Oops, of course, we weren't actually taking proper advantage of the dataloader here! The queries got over-complicated, but more importantly, subsequent requests to the same loader would re-submit the query!

I noticed it in the SearchPanel operation, in this Honeycomb trace:
https://ui.honeycomb.io/openneo/datasets/dress-to-impress--2020-/trace/aMuhsTjQFZY
This commit is contained in:
Emi Matchu 2020-08-17 01:33:34 -07:00
parent 6fc508589a
commit d621b4c1a7
2 changed files with 26 additions and 27 deletions

View file

@ -196,32 +196,35 @@ const buildPetTypeLoader = (db) =>
}); });
const buildPetTypeBySpeciesAndColorLoader = (db, loaders) => const buildPetTypeBySpeciesAndColorLoader = (db, loaders) =>
new DataLoader(async (speciesAndColorPairs) => { new DataLoader(
const conditions = []; async (speciesAndColorPairs) => {
const values = []; const conditions = [];
for (const { speciesId, colorId } of speciesAndColorPairs) { const values = [];
conditions.push("(species_id = ? AND color_id = ?)"); for (const { speciesId, colorId } of speciesAndColorPairs) {
values.push(speciesId, colorId); conditions.push("(species_id = ? AND color_id = ?)");
} values.push(speciesId, colorId);
}
const [rows, _] = await db.execute( const [rows, _] = await db.execute(
`SELECT * FROM pet_types WHERE ${conditions.join(" OR ")}`, `SELECT * FROM pet_types WHERE ${conditions.join(" OR ")}`,
values values
); );
const entities = rows.map(normalizeRow); const entities = rows.map(normalizeRow);
const entitiesBySpeciesAndColorPair = new Map( const entitiesBySpeciesAndColorPair = new Map(
entities.map((e) => [`${e.speciesId},${e.colorId}`, e]) entities.map((e) => [`${e.speciesId},${e.colorId}`, e])
); );
for (const petType of entities) { for (const petType of entities) {
loaders.petTypeLoader.prime(petType.id, petType); loaders.petTypeLoader.prime(petType.id, petType);
} }
return speciesAndColorPairs.map(({ speciesId, colorId }) => return speciesAndColorPairs.map(({ speciesId, colorId }) =>
entitiesBySpeciesAndColorPair.get(`${speciesId},${colorId}`) entitiesBySpeciesAndColorPair.get(`${speciesId},${colorId}`)
); );
}); },
{ cacheKeyFn: ({ speciesId, colorId }) => `${speciesId},${colorId}` }
);
const buildSwfAssetLoader = (db) => const buildSwfAssetLoader = (db) =>
new DataLoader(async (swfAssetIds) => { new DataLoader(async (swfAssetIds) => {

View file

@ -111,14 +111,10 @@ describe("Item", () => {
], ],
], ],
Array [ Array [
"SELECT * FROM pet_types WHERE (species_id = ? AND color_id = ?) OR (species_id = ? AND color_id = ?) OR (species_id = ? AND color_id = ?)", "SELECT * FROM pet_types WHERE (species_id = ? AND color_id = ?)",
Array [ Array [
"54", "54",
"75", "75",
"54",
"75",
"54",
"75",
], ],
], ],
Array [ Array [