jobscraper/lib/dbmanager.js
Martin Donnelly 5001bbd798 init
2020-05-19 10:05:04 +01:00

67 lines
1.4 KiB
JavaScript

/**
* Created by WebStorm.
* User: martin
* Date: 16/04/2020
* Time: 10:00
*/
const db = require('./connect');
function prepareData(_obj) {
const newObj = Object.assign({}, _obj);
newObj.isEasyApply = (_obj.isEasyApply) ? 1 : 0;
return newObj;
}
exports.insertOne = (data) => {
const sql = 'INSERT INTO jobs VALUES (?,?,?,?,?,?,?,?,?,?,?,?)';
const workObj = prepareData(data);
return new Promise((resolve, reject) => {
db.run(sql, [null, workObj.title, workObj.site, workObj.url, workObj.id, workObj.summary, workObj.company, workObj.location, workObj.postDate, workObj.salary, workObj.isEasyApply, workObj.timestamp], function(err) {
if (err)
reject(err);
resolve({ 'msg':'Row inserted', '_id': this.lastID });
});
});
};
exports.getList = () => {
const outgoing = [];
const sql = 'select _id, title, site, company, timestamp from jobs order by _id desc';
return new Promise((resolve, reject) => {
db.all(sql, [], (err, rows) => {
if (err)
reject(err);
rows.forEach((row) => {
outgoing.push(row);
});
resolve(outgoing) ;
});
});
};
exports.getOne = (id) => {
const sql = 'SELECT * FROM jobs WHERE _id = ?';
return new Promise((resolve, reject) => {
db.get(sql, [id], (err, row) => {
if (err)
reject(err);
if (!err) resolve(row);
});
});
};
// select _id, title, site, company, timestamp from jobs order by _id desc;