Sequelize queries #234

Merged
bones7242 merged 10 commits from sequelize-queries into master 2017-11-03 16:35:42 +01:00
2 changed files with 63 additions and 51 deletions
Showing only changes of commit 913c24c3a9 - Show all commits

View file

@ -1,4 +1,5 @@
const logger = require('winston'); const logger = require('winston');
kauffj commented 2017-11-01 14:33:06 +01:00 (Migrated from github.com)
Review

Would it make sense for this to be Channel rather than Certificate?

Would it make sense for this to be `Channel` rather than `Certificate`?
bones7242 commented 2017-11-04 00:43:10 +01:00 (Migrated from github.com)
Review

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.
kauffj commented 2017-11-06 17:58:11 +01:00 (Migrated from github.com)
Review

@billbitt got it

@billbitt got it
const NO_CHANNEL = 'NO_CHANNEL';
function sortResult (result, longId) { function sortResult (result, longId) {
let claimIndex; let claimIndex;
kauffj commented 2017-11-01 14:29:47 +01:00 (Migrated from github.com)
Review

This function is called sortResult but it appears to return a short Id.

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;
}; };

View file

@ -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;