deferrable.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. const { classToInvokable } = require('./utils');
  3. class ABSTRACT {
  4. static toString(...args) {
  5. return new this().toString(...args);
  6. }
  7. toString(...args) {
  8. return this.toSql(...args);
  9. }
  10. toSql() {
  11. throw new Error('toSql implementation missing');
  12. }
  13. }
  14. class INITIALLY_DEFERRED extends ABSTRACT {
  15. toSql() {
  16. return 'DEFERRABLE INITIALLY DEFERRED';
  17. }
  18. }
  19. class INITIALLY_IMMEDIATE extends ABSTRACT {
  20. toSql() {
  21. return 'DEFERRABLE INITIALLY IMMEDIATE';
  22. }
  23. }
  24. class NOT extends ABSTRACT {
  25. toSql() {
  26. return 'NOT DEFERRABLE';
  27. }
  28. }
  29. class SET_DEFERRED extends ABSTRACT {
  30. constructor(constraints) {
  31. super();
  32. this.constraints = constraints;
  33. }
  34. toSql(queryGenerator) {
  35. return queryGenerator.setDeferredQuery(this.constraints);
  36. }
  37. }
  38. class SET_IMMEDIATE extends ABSTRACT {
  39. constructor(constraints) {
  40. super();
  41. this.constraints = constraints;
  42. }
  43. toSql(queryGenerator) {
  44. return queryGenerator.setImmediateQuery(this.constraints);
  45. }
  46. }
  47. /**
  48. * A collection of properties related to deferrable constraints. It can be used to
  49. * make foreign key constraints deferrable and to set the constraints within a
  50. * transaction. This is only supported in PostgreSQL.
  51. *
  52. * The foreign keys can be configured like this. It will create a foreign key
  53. * that will check the constraints immediately when the data was inserted.
  54. *
  55. * ```js
  56. * sequelize.define('Model', {
  57. * foreign_id: {
  58. * type: Sequelize.INTEGER,
  59. * references: {
  60. * model: OtherModel,
  61. * key: 'id',
  62. * deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
  63. * }
  64. * }
  65. * });
  66. * ```
  67. *
  68. * The constraints can be configured in a transaction like this. It will
  69. * trigger a query once the transaction has been started and set the constraints
  70. * to be checked at the very end of the transaction.
  71. *
  72. * ```js
  73. * sequelize.transaction({
  74. * deferrable: Sequelize.Deferrable.SET_DEFERRED
  75. * });
  76. * ```
  77. *
  78. * @property INITIALLY_DEFERRED Use when declaring a constraint. Allow and enable by default this constraint's checks to be deferred at the end of transactions.
  79. * @property INITIALLY_IMMEDIATE Use when declaring a constraint. Allow the constraint's checks to be deferred at the end of transactions.
  80. * @property NOT Use when declaring a constraint. Set the constraint to not deferred. This is the default in PostgreSQL and makes it impossible to dynamically defer the constraints within a transaction.
  81. * @property SET_DEFERRED Use when declaring a transaction. Defer the deferrable checks involved in this transaction at commit.
  82. * @property SET_IMMEDIATE Use when declaring a transaction. Execute the deferrable checks involved in this transaction immediately.
  83. */
  84. const Deferrable = {
  85. INITIALLY_DEFERRED: classToInvokable(INITIALLY_DEFERRED),
  86. INITIALLY_IMMEDIATE: classToInvokable(INITIALLY_IMMEDIATE),
  87. NOT: classToInvokable(NOT),
  88. SET_DEFERRED: classToInvokable(SET_DEFERRED),
  89. SET_IMMEDIATE: classToInvokable(SET_IMMEDIATE)
  90. };
  91. module.exports = Deferrable;