whereByOr(obj) { this.where((q) => { _.forEach(obj, (value, key) => { q.orWhere(key, value); }); }); return this; }
restore() { return this.dontTouch() .withTrashed() .patch({ [this.modelClass().softDeleteColumn]: null, }); }
async show(req, res) { try { const user = await User.query().findById(parseInt(req.params.id)) if (!user) { return res.json(error(400, 'User not found')) } return res.json(success('User details fetched successfully.', user)) } catch (excepetion) { logger.error(excepetion) return res.json(error(500)) } }
updateById(id, fields) { const idColumn = this.modelClass().idColumn; const context = {[idColumn]: id}; return this.mergeContext(context).update(fields).whereComposite(idColumn, id); }
/** * this is equivalent to `this.where(column, values).orderByArrayPos(column, values)` * use this for returning results ordered in the way you gave them */ whereInOrdered(column, values) { this.whereIn(column, values).orderByArrayPos(column, values); return this; }
router.delete('podcasts.delete', '/podcasts/:id', api.version({'1.0.0': async (ctx, next) => { try { response = await Podcast.query().deleteById(ctx.params.id); ctx.body = response; } catch(err) { console.error(err); ctx.throw(422); }; }}));
router.post('/access_grants', async(ctx) => { const { body } = ctx.request; let token = uuidV4(); let accessGrant = {token: token, user_id: 1} try { let response = await AccessGrant.query().insert(accessGrant) ctx.body = response; } catch(err) { console.error(err); ctx.throw(422); } });
static getDeleteByIdResolver() { return (async (root, obj) => this.query().deleteById(obj[this.idColumn]) .then(() => ({id: obj[this.idColumn]}))); }
static get relationMappings() { const User = require('../users/model'); return { user: { relation: Model.HasOneRelation, modelClass: User, join: { from: 'access_grants.user_id', to: 'users.id' } } } }
/** * Create new user. */ async create(req, res, next) { try { const user = await User.query().insert(req.body) return res.json(success('Successfully added new user.', user)) } catch (excepetion) { console.log(excepetion) return res.json(error(500)) } }
$parseJson(json, opt) { json = super.$parseJson(json, opt); this.constructor.timestampColumns.forEach((column) => { if (json[column] && !(json[column] instanceof Date)) { json[column] = new Date(json[column]); } }); return json; }
delete() { if (!this.modelClass().softDelete) { return super.delete(); } return this.softDelete(); }
save(fields) { const id = this.modelClass().idColumn; if (!(id in fields)) { // modelClass has a generateId function, use that if (this.modelClass().generateId) { fields.id = this.modelClass().generateId(); } return this.insert(fields); } const patchFields = _.assign({}, fields); delete patchFields[id]; const context = {[id]: fields[id]}; return this.mergeContext(context).patch(patchFields).where(id, fields[id]); }
router.post('/subscriptions', async(ctx) => { const { body } = ctx.request; let subscription = { user_id: body.user_id, podcast_id: body.podcast_id } try { let response = await Subscription.query().insert(subscription) ctx.body = response; } catch(err) { console.error(err); ctx.throw(422); } });
deleteById(id) { const idColumn = this.modelClass().idColumn; const context = {[idColumn]: id}; return this.mergeContext(context).delete().whereComposite(idColumn, id); }