moved upsert() to db module

This commit is contained in:
bill bittner 2017-08-04 11:32:21 -07:00
parent ea922f1365
commit f4cf6dada8
6 changed files with 27 additions and 38 deletions

View file

@ -3,22 +3,6 @@ const db = require('../models');
const lbryApi = require('../helpers/lbryApi.js');
const publishHelpers = require('../helpers/publishHelpers.js');
function upsert (Model, values, condition) {
return Model
.findOne({ where: condition })
.then(function (obj) {
if (obj) { // update
logger.silly(`updating ${values.name}:${values.claimId} in File db`);
return obj.update(values);
} else { // insert
logger.silly(`creating ${values.name}:${values.claimId} in File db`);
return Model.create(values);
}
}).catch(function (error) {
logger.error('Sequelize findOne error', error);
});
}
function checkNameAvailability (name) {
return new Promise((resolve, reject) => {
// find any records where the name is used
@ -65,7 +49,7 @@ module.exports = {
.then(result => {
logger.info(`Successfully published ${fileName}`, result);
// 3. update old record or create new one (update is in case the claim has been published before by this daemon)
upsert(
db.upsert(
db.File,
{
name : publishParams.name,

View file

@ -42,11 +42,6 @@ function getAssetByClaimId (fullClaimId, name) {
// 2. if a result was found, resolve the result
if (dataValues) {
logger.debug('found a local file for this claimId');
// trigger an update check for the file
/*
serveHelpers.updateFileIfNeeded(dataValues.name, dataValues.claimId, dataValues.outpoint, dataValues.height);
*/
// resolve promise
resolve(dataValues);
// 2. if not found locally, make a get request
} else {
@ -61,6 +56,9 @@ function getAssetByClaimId (fullClaimId, name) {
let fileInfo = formatGetResultsToFileInfo(getResult);
fileInfo['address'] = resolveResult.claim.address;
fileInfo['height'] = resolveResult.claim.height;
// insert a record in the File table
db.File.create(fileInfo);
// resolve the promise
resolve(fileInfo);
})
.catch(error => {

View file

@ -62,7 +62,7 @@ module.exports = {
});
},
getClaimsList (claimName) {
logger.debug(`lbryApi >> Getting Claim List for "${claimName}"`);
logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`);
return new Promise((resolve, reject) => {
axios
.post('http://localhost:5279/lbryapi', {

View file

@ -10,7 +10,7 @@ function determineShortUrl (claimId, claimList) {
claimList = claimList.filter(claim => { // remove this claim from the claim list
return claim.claim_id !== claimId;
});
console.log(claimList.length);
logger.debug('claim list length:', claimList.length);
if (claimList.length === 0) { // if there are no other claims, return the first letter of the claim id
return claimId.substring(0, 1);
} else {
@ -94,7 +94,7 @@ module.exports = {
},
getClaimIdFromShortUrl (shortUrl, name) {
return new Promise((resolve, reject) => {
logger.debug('getting claims list from lbrynet');
logger.debug('getting claim_id from short url');
// use the daemon to check for claims list
lbryApi.getClaimsList(name)
.then(({ claims }) => {
@ -135,7 +135,7 @@ module.exports = {
},
getShortUrlFromClaimId (claimId, name) {
return new Promise((resolve, reject) => {
console.log('getting short url');
logger.debug('finding short url from claim_id');
// get a list of all the claims
lbryApi.getClaimsList(name)
// find the smallest possible unique url for this claim

View file

@ -37,6 +37,22 @@ Object.keys(db).forEach(modelName => {
}
});
db['upsert'] = (Model, values, condition) => {
return Model
.findOne({ where: condition })
.then(function (obj) {
if (obj) { // update
logger.silly(`updating ${values.name}:${values.claimId} in File db`);
return obj.update(values);
} else { // insert
logger.silly(`creating ${values.name}:${values.claimId} in File db`);
return Model.create(values);
}
}).catch(function (error) {
logger.error('Sequelize findOne error', error);
});
};
db.sequelize = sequelize;
db.Sequelize = Sequelize;

View file

@ -3,7 +3,6 @@ const { serveFile, showFile, showFileLite, getShortUrlFromClaimId } = require('.
const { getAssetByChannel, getAssetByShortUrl, getAssetByClaimId, getAssetByName } = require('../controllers/serveController.js');
const { handleRequestError } = require('../helpers/errorHandlers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
// const db = require('../models');
const SERVE = 'SERVE';
const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE';
@ -27,17 +26,9 @@ function getAsset (claimType, channelName, shortUrl, fullClaimId, name) {
}
}
function updateFileDb (fileInfo) {
logger.debug('update db / create new record');
// 1. if asset was found locally (i.e. a record exists) then check to see if we should update the file in the db
// 2. upsert the file info into the file db
}
function serveOrShowAsset (fileInfo, method, headers, originalUrl, ip, res) {
// add file extension to the file info
fileInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.'));
// test logging
logger.debug(fileInfo);
// serve or show
switch (method) {
case SERVE:
@ -145,9 +136,9 @@ module.exports = (app) => {
return serveOrShowAsset(fileInfo, method, headers, originalUrl, ip, res);
}
})
// 3. update the database
// 3. update the file
.then(fileInfoForUpdate => {
return updateFileDb(fileInfoForUpdate);
// if needed, this is where we would update the file
})
.catch(error => {
handleRequestError('serve', originalUrl, ip, error, res);
@ -187,7 +178,7 @@ module.exports = (app) => {
})
// 3. update the database
.then(fileInfoForUpdate => {
return updateFileDb(fileInfoForUpdate);
// if needed, this is where we would update the file
})
.catch(error => {
handleRequestError('serve', originalUrl, ip, error, res);