added a migration for File table

This commit is contained in:
bill bittner 2018-07-25 17:21:02 -07:00
parent 0f6193b450
commit 2d7dbdc8a0
5 changed files with 116 additions and 63 deletions

View file

@ -1,5 +1,7 @@
module.exports = (fileInfo, getResult) => { const addGetResultsToFileData = (fileInfo, getResult) => {
fileInfo.fileName = getResult.file_name; fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path; fileInfo.filePath = getResult.download_path;
return fileInfo; return fileInfo;
}; };
module.exports = addGetResultsToFileData;

View file

@ -1,4 +1,4 @@
module.exports = ({ name, claimId, outpoint, height, address, nsfw, contentType }) => { const createFileData = ({ name, claimId, outpoint, height, address, nsfw, contentType }) => {
return { return {
name, name,
claimId, claimId,
@ -11,3 +11,5 @@ module.exports = ({ name, claimId, outpoint, height, address, nsfw, contentType
nsfw, nsfw,
}; };
}; };
module.exports = createFileData;

View file

@ -13,23 +13,35 @@ const db = require('../../../../models');
const claimGet = ({ ip, originalUrl, params }, res) => { const claimGet = ({ ip, originalUrl, params }, res) => {
const name = params.name; const name = params.name;
const claimId = params.claimId; const claimId = params.claimId;
let fileData;
let resolveResult;
let getResult;
let message;
let completed;
// resolve the claim // resolve the claim
db.Claim.resolveClaim(name, claimId) db.Claim.resolveClaim(name, claimId)
.then(resolveResult => { .then(result => {
// make sure a claim actually exists at that uri // make sure a claim actually exists at that uri
if (!resolveResult) { if (!result) {
throw new Error('No matching uri found in Claim table'); throw new Error('No matching uri found in Claim table');
} }
let fileData = createFileData(resolveResult); resolveResult = result;
// get the claim return getClaim(`${name}#${claimId}`);
return Promise.all([fileData, getClaim(`${name}#${claimId}`)]);
}) })
.then(([ fileData, getResult ]) => { .then(result => {
getResult = result;
fileData = createFileData(resolveResult);
fileData = addGetResultsToFileData(fileData, getResult); fileData = addGetResultsToFileData(fileData, getResult);
return Promise.all([db.upsert(db.File, fileData, {name, claimId}, 'File'), getResult]); const upsertCriteria = { name, claimId};
return db.upsert(db.File, fileData, upsertCriteria, 'File');
}) })
.then(([ fileRecord, {message, completed} ]) => { .then(() => {
res.status(200).json({ success: true, message, completed }); ({ message, completed } = getResult);
res.status(200).json({
success: true,
message,
completed,
});
}) })
.catch(error => { .catch(error => {
handleErrorResponse(originalUrl, ip, error, res); handleErrorResponse(originalUrl, ip, error, res);

View file

@ -1,34 +1,88 @@
module.exports = { module.exports = {
up: (queryInterface, Sequelize) => { up: (queryInterface, { INTEGER }) => {
// logic for transforming into the new state // logic for transforming into the new state
const changeOne = queryInterface.addColumn( return Promise.all([
queryInterface.addColumn(
'File',
'fileHeight',
{
type : INTEGER,
allowNull: false,
default : 0,
}
),
queryInterface.addColumn(
'File',
'fileWidth',
{
type : INTEGER,
allowNull: false,
default : 0,
}
),
queryInterface.removeColumn(
'File',
'address',
),
queryInterface.removeColumn(
'File',
'height',
),
queryInterface.removeColumn(
'File',
'nsfw',
),
queryInterface.removeColumn(
'File',
'trendingEligible',
),
]);
},
down: (queryInterface, { BOOLEAN, INTEGER, STRING }) => {
return Promise.all([
queryInterface.removeColumn(
'File',
'fileHeight',
),
queryInterface.removeColumn(
'File',
'fileWidth',
),
queryInterface.addColumn(
'File',
'address',
{
type : STRING,
allowNull: false,
}
),
queryInterface.addColumn(
'File', 'File',
'height', 'height',
{ {
type : Sequelize.INTEGER, type : INTEGER,
allowNull: false, allowNull: false,
default : 0,
} }
); ),
const changeTwo = queryInterface.addColumn( queryInterface.addColumn(
'File', 'File',
'width', 'nsfw',
{ {
type : Sequelize.INTEGER, type : BOOLEAN,
allowNull: false, allowNull : false,
defaultValue: false,
} }
); ),
return Promise.all([changeOne, changeTwo]); queryInterface.addColumn(
},
down: (queryInterface, Sequelize) => {
// logic for reverting the changes
const reversionOne = queryInterface.removeColumn(
'File', 'File',
'height' 'trendingEligible',
); {
const reversionTwo = queryInterface.removeColumn( type : BOOLEAN,
'File', allowNull : false,
'width', defaultValue: true,
); }
return Promise.all([reversionOne, reversionTwo]); ),
]);
}, },
}; };

View file

@ -10,15 +10,16 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
address: {
type : STRING,
allowNull: false,
},
outpoint: { outpoint: {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
height: { fileHeight: {
type : INTEGER,
allowNull: false,
default : 0,
},
fileWidth: {
type : INTEGER, type : INTEGER,
allowNull: false, allowNull: false,
default : 0, default : 0,
@ -34,16 +35,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
fileType: { fileType: {
type: STRING, type: STRING,
}, },
nsfw: {
type : BOOLEAN,
allowNull : false,
defaultValue: false,
},
trendingEligible: {
type : BOOLEAN,
allowNull : false,
defaultValue: true,
},
}, },
{ {
freezeTableName: true, freezeTableName: true,
@ -55,13 +46,5 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
File.hasOne(db.Claim); File.hasOne(db.Claim);
}; };
File.getRecentClaims = function () {
return this.findAll({
where: { nsfw: false, trendingEligible: true },
order: [['createdAt', 'DESC']],
limit: 25,
});
};
return File; return File;
}; };