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