Sequelize queries #234
2 changed files with 63 additions and 51 deletions
|
@ -1,4 +1,5 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
I think Certificate is accurate because it is actually storing the certificate claims from the blockchain. We have a Channel table for the Channels that are created on spee.ch. It parallels how we have a Claim table for all the stream claims in the blockchain (i guess this table should be Streams to be fully parallel) and a File table for all the files held locally by spee.ch. I think Certificate is accurate because it is actually storing the certificate claims from the blockchain. We have a Channel table for the Channels that are created on spee.ch. It parallels how we have a Claim table for all the stream claims in the blockchain (i guess this table should be Streams to be fully parallel) and a File table for all the files held locally by spee.ch.
@billbitt got it @billbitt got it
|
|||||||
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
function sortResult (result, longId) {
|
function sortResult (result, longId) {
|
||||||
let claimIndex;
|
let claimIndex;
|
||||||
This function is called This function is called `sortResult` but it appears to return a short Id.
|
|||||||
|
@ -24,7 +25,7 @@ function sortResult (result, longId) {
|
||||||
return shortId;
|
return shortId;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => {
|
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE, Op }) => {
|
||||||
const Certificate = sequelize.define(
|
const Certificate = sequelize.define(
|
||||||
'Certificate',
|
'Certificate',
|
||||||
{
|
{
|
||||||
|
@ -145,5 +146,66 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' AND claimId LIKE '${channelId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
|
Certificate.getLongChannelIdFromShortChannelId = function (channelName, channelId) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this
|
||||||
|
.findAll({
|
||||||
|
where: {
|
||||||
|
name : channelName,
|
||||||
|
claimId: {
|
||||||
|
[Op.like]: `${channelId}%`,
|
||||||
|
},
|
||||||
|
order: [['height', 'ASC']],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then(result => {
|
||||||
|
switch (result.length) {
|
||||||
|
case 0:
|
||||||
|
return resolve(NO_CHANNEL);
|
||||||
|
default: // note results must be sorted
|
||||||
|
return resolve(result[0].claimId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
|
Certificate.getLongChannelIdFromChannelName = function (channelName) {
|
||||||
|
logger.debug(`getLongChannelIdFromChannelName(${channelName})`);
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this
|
||||||
|
.findAll({
|
||||||
|
where: { name: channelName },
|
||||||
|
order: [['effectiveAmount', 'DESC'], ['height', 'ASC']],
|
||||||
|
})
|
||||||
|
.then(result => {
|
||||||
|
switch (result.length) {
|
||||||
|
case 0:
|
||||||
|
return resolve(NO_CHANNEL);
|
||||||
|
default:
|
||||||
|
return resolve(result[0].claimId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Certificate.getLongChannelId = function (channelName, channelId) {
|
||||||
|
logger.debug(`getLongChannelId(${channelName}, ${channelId})`);
|
||||||
|
if (channelId && (channelId.length === 40)) { // if a full channel id is provided
|
||||||
|
return new Promise((resolve, reject) => resolve(channelId));
|
||||||
|
} else if (channelId && channelId.length < 40) { // if a short channel id is provided
|
||||||
|
return this.getLongChannelIdFromShortChannelId(channelName, channelId);
|
||||||
|
} else {
|
||||||
|
return this.getLongChannelIdFromChannelName(channelName); // if no channel id provided
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return Certificate;
|
return Certificate;
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,8 +6,6 @@ const config = require('config');
|
||||||
const db = {};
|
const db = {};
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
|
||||||
const NO_CHANNEL = 'NO_CHANNEL';
|
|
||||||
|
|
||||||
const database = config.get('Database.Database');
|
const database = config.get('Database.Database');
|
||||||
const username = config.get('Database.Username');
|
const username = config.get('Database.Username');
|
||||||
const password = config.get('Database.Password');
|
const password = config.get('Database.Password');
|
||||||
|
@ -24,43 +22,6 @@ const sequelize = new Sequelize(database, username, password, {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
function getLongChannelIdFromShortChannelId (channelName, channelId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
db
|
|
||||||
.sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' AND claimId LIKE '${channelId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
|
||||||
.then(result => {
|
|
||||||
switch (result.length) {
|
|
||||||
case 0:
|
|
||||||
return resolve(NO_CHANNEL);
|
|
||||||
default: // note results must be sorted
|
|
||||||
return resolve(result[0].claimId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLongChannelIdFromChannelName (channelName) {
|
|
||||||
logger.debug(`getLongChannelIdFromChannelName(${channelName})`);
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
db
|
|
||||||
.sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
|
||||||
.then(result => {
|
|
||||||
switch (result.length) {
|
|
||||||
case 0:
|
|
||||||
return resolve(NO_CHANNEL);
|
|
||||||
default:
|
|
||||||
return resolve(result[0].claimId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
sequelize
|
sequelize
|
||||||
.authenticate()
|
.authenticate()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
@ -135,15 +96,4 @@ db['resolveClaim'] = (name, claimId) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
db['getLongChannelId'] = (channelName, channelId) => {
|
|
||||||
logger.debug(`getLongChannelId (${channelName}, ${channelId})`);
|
|
||||||
if (channelId && (channelId.length === 40)) { // if a full channel id is provided
|
|
||||||
return new Promise((resolve, reject) => resolve(channelId));
|
|
||||||
} else if (channelId && channelId.length < 40) { // if a short channel id is provided
|
|
||||||
return getLongChannelIdFromShortChannelId(channelName, channelId);
|
|
||||||
} else {
|
|
||||||
return getLongChannelIdFromChannelName(channelName); // if no channel id provided
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = db;
|
module.exports = db;
|
||||||
|
|
Loading…
Reference in a new issue
Would it make sense for this to be
Channel
rather thanCertificate
?