|
| 1 | +/** |
| 2 | + * Transactions Support in Tryber API |
| 3 | + * |
| 4 | + * This file provides examples of how to use Knex transactions in the codebase. |
| 5 | + * Transactions ensure that multiple database operations are executed atomically: |
| 6 | + * either all succeed or all are rolled back. |
| 7 | + */ |
| 8 | + |
| 9 | +import * as db from "@src/features/db"; |
| 10 | +import { tryber } from "@src/features/database"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Example 1: Using db.transaction() helper with raw queries |
| 14 | + * |
| 15 | + * The transaction() helper automatically handles commit/rollback: |
| 16 | + * - If the callback completes successfully, the transaction is committed |
| 17 | + * - If an error is thrown, the transaction is rolled back |
| 18 | + */ |
| 19 | +export async function exampleRawQueries() { |
| 20 | + await db.transaction(async (trx) => { |
| 21 | + // Execute multiple queries in the same transaction |
| 22 | + await db.query("INSERT INTO users (name) VALUES ('John')", trx); |
| 23 | + await db.query( |
| 24 | + "INSERT INTO profiles (user_id, bio) VALUES (1, 'Bio')", |
| 25 | + trx |
| 26 | + ); |
| 27 | + |
| 28 | + // If any query fails, all changes are rolled back |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Example 2: Using transactions with Database class |
| 34 | + * |
| 35 | + * All methods in the Database class now accept an optional `trx` parameter |
| 36 | + */ |
| 37 | +export async function exampleDatabaseClass() { |
| 38 | + const Experience = new ( |
| 39 | + await import("@src/features/db/class/Experience") |
| 40 | + ).default(); |
| 41 | + |
| 42 | + await db.transaction(async (trx) => { |
| 43 | + // Insert operations within a transaction |
| 44 | + const result1 = await Experience.insert( |
| 45 | + { |
| 46 | + tester_id: 1, |
| 47 | + amount: 100, |
| 48 | + creation_date: new Date().toISOString(), |
| 49 | + activity_id: 1, |
| 50 | + reason: "Campaign participation", |
| 51 | + campaign_id: 1, |
| 52 | + pm_id: 1, |
| 53 | + }, |
| 54 | + trx |
| 55 | + ); |
| 56 | + |
| 57 | + const result2 = await Experience.insert( |
| 58 | + { |
| 59 | + tester_id: 1, |
| 60 | + amount: 50, |
| 61 | + creation_date: new Date().toISOString(), |
| 62 | + activity_id: 2, |
| 63 | + reason: "Bug report", |
| 64 | + campaign_id: 1, |
| 65 | + pm_id: 1, |
| 66 | + }, |
| 67 | + trx |
| 68 | + ); |
| 69 | + |
| 70 | + // Query within the same transaction |
| 71 | + const records = await Experience.query({ |
| 72 | + where: [{ tester_id: 1 }], |
| 73 | + trx, |
| 74 | + }); |
| 75 | + |
| 76 | + // Update within the same transaction |
| 77 | + await Experience.update({ |
| 78 | + data: { amount: 150 }, |
| 79 | + where: [{ id: result1.insertId }], |
| 80 | + trx, |
| 81 | + }); |
| 82 | + |
| 83 | + // Delete within the same transaction |
| 84 | + await Experience.delete([{ id: result2.insertId }], trx); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Example 3: Using transactions with tryber tables (Knex query builder) |
| 90 | + * |
| 91 | + * You can also use the tryber instance directly with transactions |
| 92 | + */ |
| 93 | +export async function exampleKnexQueryBuilder() { |
| 94 | + await db.transaction(async (trx) => { |
| 95 | + // Using Knex query builder with transaction |
| 96 | + await tryber.tables.WpUsers.do().transacting(trx).insert({ |
| 97 | + ID: 123, |
| 98 | + user_login: "test_user", |
| 99 | + user_email: "test@example.com", |
| 100 | + }); |
| 101 | + |
| 102 | + // Multiple operations in the same transaction |
| 103 | + const user = await tryber.tables.WpUsers.do() |
| 104 | + .transacting(trx) |
| 105 | + .where({ user_email: "test@example.com" }) |
| 106 | + .first(); |
| 107 | + |
| 108 | + if (user) { |
| 109 | + await tryber.tables.WpAppqEvdProfile.do().transacting(trx).insert({ |
| 110 | + wp_user_id: user.ID, |
| 111 | + id: 1, |
| 112 | + email: user.user_email, |
| 113 | + education_id: 1, |
| 114 | + employment_id: 1, |
| 115 | + }); |
| 116 | + } |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Example 4: Error handling and rollback |
| 122 | + * |
| 123 | + * When an error occurs, the transaction is automatically rolled back |
| 124 | + */ |
| 125 | +export async function exampleErrorHandling() { |
| 126 | + try { |
| 127 | + await db.transaction(async (trx) => { |
| 128 | + await db.query("INSERT INTO users (name) VALUES ('John')", trx); |
| 129 | + |
| 130 | + // This will cause an error and rollback all changes |
| 131 | + throw new Error("Something went wrong"); |
| 132 | + |
| 133 | + // This line will never be executed |
| 134 | + await db.query( |
| 135 | + "INSERT INTO profiles (user_id, bio) VALUES (1, 'Bio')", |
| 136 | + trx |
| 137 | + ); |
| 138 | + }); |
| 139 | + } catch (error) { |
| 140 | + console.error("Transaction failed:", error); |
| 141 | + // All database changes have been rolled back |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Example 5: Mixed usage - transaction with both Database class and raw queries |
| 147 | + */ |
| 148 | +export async function exampleMixedUsage() { |
| 149 | + const Experience = new ( |
| 150 | + await import("@src/features/db/class/Experience") |
| 151 | + ).default(); |
| 152 | + |
| 153 | + await db.transaction(async (trx) => { |
| 154 | + // Use Database class |
| 155 | + const expResult = await Experience.insert( |
| 156 | + { |
| 157 | + tester_id: 1, |
| 158 | + amount: 100, |
| 159 | + creation_date: new Date().toISOString(), |
| 160 | + activity_id: 1, |
| 161 | + reason: "Test", |
| 162 | + campaign_id: 1, |
| 163 | + pm_id: 1, |
| 164 | + }, |
| 165 | + trx |
| 166 | + ); |
| 167 | + |
| 168 | + // Use raw query |
| 169 | + await db.query( |
| 170 | + `UPDATE wp_appq_user SET total_exp = total_exp + 100 WHERE id = 1`, |
| 171 | + trx |
| 172 | + ); |
| 173 | + |
| 174 | + // Use Knex query builder |
| 175 | + await tryber.tables.WpAppqEventTransactionalMail.do() |
| 176 | + .transacting(trx) |
| 177 | + .insert({ |
| 178 | + event_name: "experience_added", |
| 179 | + template_id: 1, |
| 180 | + last_editor_tester_id: 1, |
| 181 | + }); |
| 182 | + }); |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * IMPORTANT NOTES: |
| 187 | + * |
| 188 | + * 1. Always pass the `trx` parameter to ALL database operations within the transaction |
| 189 | + * 2. Don't mix transactional and non-transactional operations in the same logical flow |
| 190 | + * 3. Keep transactions short to avoid locking issues |
| 191 | + * 4. The transaction is automatically committed if the callback completes without errors |
| 192 | + * 5. The transaction is automatically rolled back if an error is thrown |
| 193 | + * 6. All methods are backward compatible - the `trx` parameter is optional |
| 194 | + */ |
0 commit comments