Skip to content

Commit 897132c

Browse files
fix(set-head): query for speicific version of the object in the index when verifying existence of the object version before setting head version (#151)
1 parent 4f53d09 commit 897132c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/plugins/object.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,38 @@ module.exports = fp(
457457
}
458458
);
459459

460+
/**
461+
* Check if an Object version exists
462+
*
463+
* @param {Object} opts Method parameters
464+
* @param {string} opts.name Object name
465+
* @param {string} opts.env Environment
466+
* @param {string} opts.version Object versions
467+
* @returns {Promise<boolean>} True if the object version exists, false otherwise
468+
*/
469+
fastify.decorate(
470+
'hasObjectVersion',
471+
async function hasObjectVersion({ name, env, version }) {
472+
const params = {
473+
IndexName: 'keyname-env-index',
474+
KeyConditionExpression: 'keyname = :name AND env = :env',
475+
FilterExpression: 'version = :version',
476+
ExpressionAttributeValues: {
477+
':name': name,
478+
':env': env,
479+
':version': version
480+
},
481+
TableName: OBJECT_VARIANTS_TABLE
482+
};
483+
const { Items: items } = await dynamo.query(params).promise();
484+
if (!items || items.length === 0) {
485+
return false;
486+
}
487+
// Check if the version exists in the list
488+
return items.some((item) => item.version === version);
489+
}
490+
);
491+
460492
fastify.decorate(
461493
'putObjectVariant',
462494
/**

lib/routes/objects.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,9 @@ module.exports =
647647
headVersion = fromEnvVersion.headVersion;
648648
}
649649

650-
const [obj, objVersions] = await Promise.all([
650+
const [obj, hasObjectVersion = false] = await Promise.all([
651651
fastify.getObject({ name, env }),
652-
fastify.getAllObjectVersions({ name, env })
652+
fastify.hasObjectVersion({ name, env, version: headVersion })
653653
]);
654654

655655
if (!obj) {
@@ -658,7 +658,7 @@ module.exports =
658658
);
659659
}
660660

661-
if (!objVersions.includes(headVersion)) {
661+
if (!hasObjectVersion) {
662662
throw fastify.httpErrors.notFound(
663663
`Version ${headVersion} not found in '${env}'`
664664
);

0 commit comments

Comments
 (0)