Skip to content

Instantly share code, notes, and snippets.

@dsternlicht
Last active January 29, 2019 13:25
Show Gist options
  • Select an option

  • Save dsternlicht/7a21172970f6f9cde61eb4a8cfee30d5 to your computer and use it in GitHub Desktop.

Select an option

Save dsternlicht/7a21172970f6f9cde61eb4a8cfee30d5 to your computer and use it in GitHub Desktop.
Feeds model before cache
// Feeds model before cache
import DB from '../db';
const FeedModel = {
getUserFeeds(userID) {
const selectQuery = `SELECT * FROM feeds WHERE userID = ${userID}`;
return DB.then((connection) =>
connection.query(selectQuery).then((rows) => Promise.resolve(rows))
);
},
getFeedById(feedID, allFields = false) {
const selectQuery = `SELECT * FROM feeds WHERE feedID = ${feedID}`;
return DB.then((connection) =>
connection.query(selectQuery).then((rows) => {
return rows[0];
})
);
},
countFeeds(userID) {
const selectQuery = `SELECT COUNT(*) as count FROM feeds WHERE userID = ${userID}`;
return DB.then((connection) =>
connection.query(selectQuery).then((rows) => rows[0].count)
);
},
createFeed(userID, feedData = {}) {
const insertQuery = 'INSERT INTO feeds SET ?';
feedData.userID = userID;
return DB.then((connection) => {
return connection.query(insertQuery, feedData).then(() =>
Promise.resolve(feedData)
);
});
},
update(userID, feedID, feedData) {
const whereQuery = `WHERE feedID = ${feedID} AND userID = ${userID}`;
const updateQuery = `UPDATE feeds SET ? ${whereQuery}`;
return DB.then((connection) => connection.query(updateQuery, feedData).then(() => true));
},
delete(userID, feedID) {
const deleteQuery = `DELETE from feeds WHERE userID = ${userID} AND feedID = ${feedID}`;
return DB.then((connection) => connection.query(deleteQuery).then(() => true));
}
};
export default FeedModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment