1
1
# Replace ENUM PostgreSQL
2
2
3
- [ ![ Build Status] ( https://travis-ci.org/abelosorio/sequelize_replace_enum_postgres.svg?branch=master )] ( https://travis-ci.org/abelosorio/sequelize_replace_enum_postgres ) [ ![ NPM version] ( https://img.shields.io/npm/v/sequelize-replace-enum-postgres.svg )] ( https://www.npmjs.com/package/sequelize-replace-enum-postgres )
4
-
5
3
This package provides the methods needed to replace a ** PostgreSQL** ENUM in ** Sequelize** migrations.
6
4
7
5
## Install
8
6
9
- ```
7
+ ``` bash
10
8
npm install --save sequelize-replace-enum-postgres
9
+ # OR
10
+ yarn add sequelize-replace-enum-postgres
11
11
```
12
12
13
13
## How to use
14
14
15
+ ### Basic
16
+
15
17
In this migration we are adding the ` on-demand ` value to the ` recurrenceType ` field of ` eventRecurrence ` :
16
18
17
19
``` js
18
- const replaceEnum = require (' sequelize-replace-enum-postgres' );
20
+ const replaceEnum = require (' sequelize-replace-enum-postgres' )
19
21
20
22
module .exports = {
21
23
up : (queryInterface , Sequelize ) => {
@@ -24,8 +26,8 @@ module.exports = {
24
26
columnName: ' recurrenceType' ,
25
27
defaultValue: ' weekly' ,
26
28
newValues: [' weekly' , ' monthly' , ' yearly' , ' on-demand' ],
27
- enumName: ' enum_event_recurrence_recurrence_type'
28
- });
29
+ enumName: ' enum_event_recurrence_recurrence_type' ,
30
+ })
29
31
},
30
32
31
33
down : (queryInterface , Sequelize ) => {
@@ -34,13 +36,95 @@ module.exports = {
34
36
columnName: ' recurrenceType' ,
35
37
defaultValue: ' weekly' ,
36
38
newValues: [' weekly' , ' monthly' , ' yearly' ],
37
- enumName: ' enum_event_recurrence_recurrence_type'
38
- });
39
+ enumName: ' enum_event_recurrence_recurrence_type' ,
40
+ })
41
+ }
42
+ }
43
+ ```
44
+
45
+ ### Advanced
46
+
47
+ #### Typescript
48
+
49
+ The library is build, has full and first class typescript support.
50
+
51
+ ``` ts
52
+ import { QueryInterface , Sequelize } from ' sequelize'
53
+ import replaceEnum from ' sequelize-replace-enum-postgres'
54
+
55
+ module .exports = {
56
+ up : (queryInterface : QueryInterface , Sequelize : typeof Sequelize ) => {
57
+ return replaceEnum (queryInterface )({
58
+ tableName: ' event_recurrence' ,
59
+ columnName: ' recurrence_type' ,
60
+ defaultValue: ' weekly' ,
61
+ newValues: [' weekly' , ' monthly' , ' yearly' , ' on-demand' ],
62
+ enumName: ' enum_event_recurrence_recurrence_type' ,
63
+ })
64
+ },
65
+
66
+ down : (queryInterface : QueryInterface , Sequelize : typeof Sequelize ) => {
67
+ return replaceEnum (queryInterface )({
68
+ tableName: ' event_recurrence' ,
69
+ columnName: ' recurrence_type' ,
70
+ defaultValue: ' weekly' ,
71
+ newValues: [' weekly' , ' monthly' , ' yearly' ],
72
+ enumName: ' enum_event_recurrence_recurrence_type' ,
73
+ })
39
74
}
40
- };
75
+ }
41
76
```
42
77
43
- ## Mantainers
78
+ #### Transactions
79
+
80
+ By default, ` replaceEnum ` will create a transaction for you (it actually runs several queries, and does it safely within a transaction).
81
+
82
+ However — for example when writing migrations — you might already have a transaction instance. In such a case, you can pass it to ` transactionOptions ` and the function will create a nested sub-transaction, allowing it to fail gracefully ! You can also pass any other option inside id, it will be passed down to the created sub-transaction.
83
+
84
+ ``` ts
85
+ await queryInterface .sequelize .transaction (async t => {
86
+ // If it fails, the enclosing
87
+ // transaction (t) will waterfally fail
88
+ await replaceEnum ({
89
+ tableName: ' event_recurrence' ,
90
+ columnName: ' recurrence_type' ,
91
+ defaultValue: ' weekly' ,
92
+ newValues: [' weekly' , ' monthly' , ' yearly' , ' on-demand' ],
93
+ enumName: ' enum_event_recurrence_recurrence_type' ,
94
+ transactionOptions: { transaction: t } // Transaction is passed here
95
+ })
96
+
97
+ await sqlQueryInterface .bulkUpdate (
98
+ ' event_recurrence' ,
99
+ { /* some update... */ },
100
+ { /* some select... */ },
101
+ { transaction: t }, // Use same transaction
102
+ )
103
+ })
104
+ ```
105
+
106
+ #### Options
107
+
108
+ You can pass any options you'd like to subqueries, with ` queryOptions ` argument. They will be passed to to all subqueries.
109
+
110
+ ``` ts
111
+ await queryInterface .sequelize .transaction (async t => {
112
+ // If it fails, the enclosing
113
+ // transaction (t) will waterfally fail
114
+ await replaceEnum ({
115
+ tableName: ' event_recurrence' ,
116
+ columnName: ' recurrence_type' ,
117
+ defaultValue: ' weekly' ,
118
+ newValues: [' weekly' , ' monthly' , ' yearly' , ' on-demand' ],
119
+ enumName: ' enum_event_recurrence_recurrence_type' ,
120
+ queryOptions: { logging: true } // Pass some query options
121
+ })
122
+ })
123
+ ```
124
+
125
+ > ⚠️ Don't try to pass a transaction inside ` queryOptions ` , as it would be overwritten by the underlying generated transaction. The recommended way to run ` replaceEnum ` within a transaction is passing a transaction instance to ` transactionOptions `
126
+
127
+ ## Maintainers
44
128
45
129
* ** [ Abel M. Osorio] ( https://github.com/abelosorio ) **
46
130
* ** [ Cyril CHAPON] ( https://github.com/cyrilchapon ) **
0 commit comments