db.from('capcodes') .del() .where('id', 'in', idList) .then((result) => { res.status(200).send({ 'status': 'ok' }); if (!updateRequired || updateRequired == 0) { nconf.set('database:aliasRefreshRequired', 1); nconf.save(); } }).catch((err) => { res.status(500).send(err); })
//save a new cat, cat is an object so I can insert directly function saveOne(cat) { return db("cats") .insert(cat) .then(data => { // bring some data back otherwise 'data' logs as the ID of our newly saved cat return db("cats").select(); }); }
router.put('/:id', function(req, res) { db('todo').where({id: req.params.id}).update({ title: req.body.title || null, is_done: req.body.is_done || false }).returning('*').then(function(data) { res.send(data); }); console.log(req.params.id); // UPDATE table_name SET title='', is_done='' WHERE id = '' });
router.route('/capcodes/agency') .get(authHelper.isAdmin, function (req, res, next) { db.from('capcodes') .distinct('agency') .then((rows) => { res.status(200); res.json(rows); }) .catch((err) => { res.status(500); res.send(err); }) });
describe('legacyListMap', () => { it('converts a legacy ID to the target ID correctly', done => { db.select().from('legacy_ids').limit(1).then(legacy => { const id = legacy[0].id; const target = legacy[0].target; legacyListMap(db, id).then(test => { expect(test).to.equal(target); done(); }); }); }); });
//edit cat (cat data here includes everything we need to edit). Tip: you need the ID of cat to update otherwise //we have no idea which cat we're updating. function editOne(cat) { return db("cats") .where({ id: cat.id }) // find the cat to update .update({ name: cat.name, location: cat.location, age: cat.age }) //change the cat info to the new stuff .then(data => { return db("cats").where({ id: cat.id }); //return the cat we updated }); }
router.post('/', function(req, res) { db.insert(req.body).returning('*').into('todo').then(function(data) { res.send(data); }) console.log(req.body); // INSERT INTO table_name(column1, column2) VALUES(value_1, value_2) // SELECT * FROM table WHERE id = inserted_row });
router.get("/:id", (req, res) => { //req.params.id will get the ID from our route above ^ db.getOne(req.params.id).then(cats => res.json([cats])); });
before('fetch list data', done => { db.select().from('lists').where({ id: listId }).then(lists => { data = lists[0]; done(); }); });
//delete cat by ID function deleteOne(id) { return db("cats") .where({ id }) .del() .then(data => { // the data callback here doesn't return any cats as we've deleted the cat. So returning whole DB. return db("cats").select(); }); }
router.get('/', function(req, res) { db.select().from('todo').then(function(data) { res.send(data); }); // SELECT * FROM table_name });
router.patch('/:id', function(req, res) { db('todo').where({id: req.params.id}).update(req.body).returning('*').then(function(data) { res.send(data); }); console.log(req.params.id); });
function addUser(body) { return db('users') .insert(body) .then(ids => ids[0]); }
db.from('users') .del() .where('id', id) .then((result) => { res.status(200).send({ 'status': 'ok' }); }) .catch((err) => { res.status(500).send(err); logger.main.error(err) })
db.from('capcodes') .del() .where('id', id) .then((result) => { res.status(200).send({ 'status': 'ok' }); if (!updateRequired || updateRequired == 0) { nconf.set('database:aliasRefreshRequired', 1); nconf.save(); } }) .catch((err) => { res.status(500).send(err); })