async loginSocialNetwork(userReq, request) { let _this = this; return objection.transaction(User, function (User) { return User.query().insert(userReq) .then(userReturn => { let gender; if (request.gender == 'male') gender = 0; else gender = 1; let profile = { user_id: userReturn.id, name: request.name, gender: gender, avatar_url: request.avatar_url, created_at: userReturn.created_at, updated_at: userReturn.updated_at }; return _this.createProfile(userReq, userReturn, profile); }) .catch(err => { console.log(err); }); }); }
(async function () { await db.loaded var entry = { version: '0', parentHash: null, data: { hello: 'world!', pubkey: 'foobarbaz' } } var content = await HashToData.query().insert({value: JSON.stringify(entry)}) console.log('inserted:', content) var groupId = content.hash var res = await transaction(Group, Log, async function (Group, Log) { await Log.query().insert({groupId, entry, entryNum: 0}) await Group.query().insert({id: groupId, lastEntryNum: 0}) }) console.log('transaction completed with result:', res) entry.parentHash = groupId entry.data = {its: 'a new world'} res = await transaction(Group, Log, async function (Group, Log) { var group = await Group.query().updateAndFetchById(groupId, { lastEntryNum: Group.raw('lastEntryNum + 1') }) var log = await Log.query().insert({groupId, entry, entryNum: group.lastEntryNum}) console.log('last log:', log) }) console.log('transaction completed with result:', res) // and now to fetch the latest transaction res = await Group.query().first().where('id', groupId) console.log('got group:', res) res = await res.$relatedQuery('lastEntry').first() console.log('last log again:', res) console.log('last log data:', res.entry.data) // another way, using one query, and without relations. res = await Log.query().first().where('groupId', groupId).andWhere('entryNum', 'in', Group.query().select('lastEntryNum').where('id', groupId)) console.log('last log again:', res) knex.destroy() process.exit(0) })()
router.post('/persons/:id/movies', async (req, res) => { // Inserting a movie for a person creates two queries: the movie insert query // and the join table row insert query. It is wise to use a transaction here. const movie = await transaction(Person.knex(), async function (trx) { const person = await Person .query(trx) .findById(req.params.id); if (!person) { throwNotFound(); } return person .$relatedQuery('movies', trx) .insert(req.body); }); res.send(movie); });
createUser(user) { let self = this; user.created_at = Utils.generateTimestampLong(); user.updated_at = Utils.generateTimestampLong(); let profileName = user.name; delete user.name; return objection.transaction(User, function (User) { return User.query().insert(user) .then(function (userReturn) { let profile = { user_id: user.id, name: profileName, created_at: user.created_at, updated_at: user.updated_at }; return self.createProfile(user, userReturn, profile); }) }); }
} } } } // ======================= // wrapper methods to add log entries / create groups // ======================= export default { async createGroup (entry) { var groupId var res = await transaction(HashToData, Group, Log, async function (HashToData, Group, Log) { var content = await HashToData.query().insert({value: JSON.stringify(entry)}) console.log('inserted:', content) groupId = content.hash await Log.query().insert({groupId, entry, entryNum: 0}) await Group.query().insert({id: groupId, lastEntryNum: 0}) }) // TODO: get rid of this debugging or convert it to events using Good console.log('transaction completed with result:', res) return Promise.resolve(groupId) }, async appendLogEntry (groupId, entry) { var res = await transaction(Group, Log, async function (Group, Log) { var group = await Group.query().updateAndFetchById(groupId, { lastEntryNum: Group.raw('lastEntryNum + 1') }) var log = await Log.query().insert({groupId, entry, entryNum: group.lastEntryNum}) console.log('last log:', log)