How to use the transaction function from knex

Find comprehensive JavaScript knex.transaction code examples handpicked from public code repositorys.

27
28
29
30
31
32
33
34
35
36

// empty data
await knex('test_table').truncate()

console.log('See Transaction...')
await knex.transaction(async trx => {  
  await knex('test_table').insert({ name: 'aa' }).transacting(trx)
  // await knex.schema.raw('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;')
  let rv = await knex.select().from('test_table')
  console.log(rv)
fork icon116
star icon447
watch icon29

+ 7 other calls in file

38
39
40
41
42
43
44
45
46
47
worker[i].send(links.slice(start, end));

worker[i].on('message', async (msg) => {

  if (msg.data[0] !== undefined) {
    const changedInfo = await knex.transaction(async (trx) => {
      return await trx
        .from('urls')
        .where('updated_at_for_status', '<', trx.raw('NOW() - INTERVAL \'1 DAY\''))
        .orWhere('created_at', '<', trx.raw('NOW() - INTERVAL \'1 DAY\''))
fork icon0
star icon0
watch icon1

+ 9 other calls in file

42
43
44
45
46
47
48
49
50
51

if (msg.data !== undefined) {


  (async () => {
    const trx = await knex.transaction();
    try {
      // Perform the updates on the links table
      await trx('links')
        .where('updated_at', '<', knex.raw('NOW() - INTERVAL \'1 DAY\''))
fork icon0
star icon0
watch icon1

+ 3 other calls in file

1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
  .then((inserts) => console.log(inserts.length + ' new books saved.'))
  .catch((error) => console.error(error));

// 1й вариант async:
try {
  await knex.transaction(async (trx) => {
    const ids = await trx('catalogues').insert(
      {
        name: 'Old Books',
      },
fork icon0
star icon0
watch icon1

+ 14 other calls in file

73
74
75
76
77
78
79
80
81
82
//     console.log(data);
// });


const hashedPassword = await bcrypt.hash(req.body.userPassword, 10)
knex.transaction(trx => {
    return trx('users')
      .insert({
        name: req.body.userName,
        email: req.body.userEmail,
fork icon0
star icon0
watch icon0