transaction.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. const uuidv4 = require('uuid').v4;
  3. const TransactionQueries = {
  4. /**
  5. * Returns a query that sets the transaction isolation level.
  6. *
  7. * @param {string} value The isolation level.
  8. * @param {object} options An object with options.
  9. * @returns {string} The generated sql query.
  10. * @private
  11. */
  12. setIsolationLevelQuery(value, options) {
  13. if (options.parent) {
  14. return;
  15. }
  16. return `SET TRANSACTION ISOLATION LEVEL ${value};`;
  17. },
  18. generateTransactionId() {
  19. return uuidv4();
  20. },
  21. /**
  22. * Returns a query that starts a transaction.
  23. *
  24. * @param {Transaction} transaction
  25. * @returns {string} The generated sql query.
  26. * @private
  27. */
  28. startTransactionQuery(transaction) {
  29. if (transaction.parent) {
  30. // force quoting of savepoint identifiers for postgres
  31. return `SAVEPOINT ${this.quoteIdentifier(transaction.name, true)};`;
  32. }
  33. return 'START TRANSACTION;';
  34. },
  35. deferConstraintsQuery() {},
  36. setConstraintQuery() {},
  37. setDeferredQuery() {},
  38. setImmediateQuery() {},
  39. /**
  40. * Returns a query that commits a transaction.
  41. *
  42. * @param {Transaction} transaction An object with options.
  43. * @returns {string} The generated sql query.
  44. * @private
  45. */
  46. commitTransactionQuery(transaction) {
  47. if (transaction.parent) {
  48. return;
  49. }
  50. return 'COMMIT;';
  51. },
  52. /**
  53. * Returns a query that rollbacks a transaction.
  54. *
  55. * @param {Transaction} transaction
  56. * @returns {string} The generated sql query.
  57. * @private
  58. */
  59. rollbackTransactionQuery(transaction) {
  60. if (transaction.parent) {
  61. // force quoting of savepoint identifiers for postgres
  62. return `ROLLBACK TO SAVEPOINT ${this.quoteIdentifier(transaction.name, true)};`;
  63. }
  64. return 'ROLLBACK;';
  65. }
  66. };
  67. module.exports = TransactionQueries;