router.get('/users/:id', (req, res) => { let id = parseInt(req.params.id) User.query() .where('id', id) .eager('messages') .then(user => { res.json(user) }) })
whereByAnd(obj) { this.orWhere((q) => { _.forEach(obj, (value, key) => { q.where(key, value); }); }); return this; }
async function main() { await Person.query().insert({ name: 'John' }) console.log(await Person.query().where('name', 'John')) console.log(await knex.raw('SELECT * FROM persons')) }
async findUserByEmail(email) { return await User.query().first().where({ email }) }
static getFindOneResolver(options = {}) { return (async (root, args) => { if (args[this.idColumn]) { return this.loadById(args[this.idColumn], options); } const keys = Object.keys(args); if (!keys.length) return null; if (keys.length === 1) { return this.loadByColumn(keys[0], args[keys[0]], options); } const query = this.query(); keys.forEach((key) => { query.where(key, args[key]); }); return query.first(); }); }
var requireAuthToken = () => { return async(ctx, next) => { if (!ctx.header || !ctx.header.authorization) { ctx.throw(401, 'Authorization header not found') }; const parts = ctx.header.authorization.split(' '); if (parts.length === 2) { const scheme = parts[0]; const credentials = parts[1]; let accessGrant = await AccessGrant.query().where('token', credentials).eager('user') if (accessGrant.length > 0 && accessGrant[0].active) { ctx.state['user'] = accessGrant[0].user; return next(); } else { ctx.throw(401) } } else { ctx.throw(401, 'Bad Authorization header format. Format is "Authorization: Bearer <token>"'); } } }
if (options.modify) { if (_.isPlainObject(options.modify)) { query.where(options.modify);
static where(...args) { return this.query().where(...args); }