Sequelize queries #234

Merged
bones7242 merged 10 commits from sequelize-queries into master 2017-11-03 16:35:42 +01:00
3 changed files with 25 additions and 22 deletions
Showing only changes of commit 409a9346de - Show all commits

View file

@ -168,7 +168,7 @@ module.exports = {
return NO_CHANNEL;
}
shortChannelId = result;
return db.getAllChannelClaims(longChannelId);
return db.Claim.getAllChannelClaims(longChannelId);
})
.then(result => { // 4. add extra data not available from Claim table
if (result === NO_CHANNEL) {

View file

@ -181,8 +181,8 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
};
Claim.getShortClaimIdFromLongClaimId = function (claimId, claimName) {
logger.debug(`Claim.getShortClaimIdFromLongClaimId for ${claimId}#${claimId}`);
return new Promise((resolve, reject) => {
logger.debug(`finding short claim id for ${claimId}#${claimId}`);
this
.findAll({
where: { name: claimName },
@ -202,5 +202,27 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
});
};
Claim.getAllChannelClaims = function (channelId) {
logger.debug(`Claim.getAllChannelClaims for ${channelId}`);
return new Promise((resolve, reject) => {
this
.findAll({
where: { certificateId: channelId },
order: [['height', 'ASC']],
})
.then(result => {
switch (result.length) {
case 0:
return resolve(null);
default:
return resolve(result);
}
})
.catch(error => {
reject(error);
});
});
};
return Claim;
};

View file

@ -209,25 +209,6 @@ db['getClaimIdByLongChannelId'] = (channelId, claimName) => {
});
};
db['getAllChannelClaims'] = (channelId) => {
return new Promise((resolve, reject) => {
logger.debug(`finding all claims in channel "${channelId}"`);
db
.sequelize.query(`SELECT name, claimId, outpoint, height, address, contentType, title, description, license, thumbnail FROM Claim WHERE certificateId = '${channelId}' ORDER BY height DESC;`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return resolve(null);
default:
return resolve(result);
}
})
.catch(error => {
reject(error);
});
});
};
db['getLongClaimId'] = (claimName, claimId) => {
logger.debug(`getLongClaimId(${claimName}, ${claimId})`);
if (claimId && (claimId.length === 40)) { // if a full claim id is provided