Skip to content

Commit d891dac

Browse files
author
Cyril CHAPON
committed
Upgrade documentation
1 parent 3839c76 commit d891dac

File tree

1 file changed

+94
-10
lines changed

1 file changed

+94
-10
lines changed

README.md

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# Replace ENUM PostgreSQL
22

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-
53
This package provides the methods needed to replace a **PostgreSQL** ENUM in **Sequelize** migrations.
64

75
## Install
86

9-
```
7+
```bash
108
npm install --save sequelize-replace-enum-postgres
9+
#OR
10+
yarn add sequelize-replace-enum-postgres
1111
```
1212

1313
## How to use
1414

15+
### Basic
16+
1517
In this migration we are adding the `on-demand` value to the `recurrenceType` field of `eventRecurrence`:
1618

1719
```js
18-
const replaceEnum = require('sequelize-replace-enum-postgres');
20+
const replaceEnum = require('sequelize-replace-enum-postgres')
1921

2022
module.exports = {
2123
up: (queryInterface, Sequelize) => {
@@ -24,8 +26,8 @@ module.exports = {
2426
columnName: 'recurrenceType',
2527
defaultValue: 'weekly',
2628
newValues: ['weekly', 'monthly', 'yearly', 'on-demand'],
27-
enumName: 'enum_event_recurrence_recurrence_type'
28-
});
29+
enumName: 'enum_event_recurrence_recurrence_type',
30+
})
2931
},
3032

3133
down: (queryInterface, Sequelize) => {
@@ -34,13 +36,95 @@ module.exports = {
3436
columnName: 'recurrenceType',
3537
defaultValue: 'weekly',
3638
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+
})
3974
}
40-
};
75+
}
4176
```
4277

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
44128

45129
* **[Abel M. Osorio](https://github.com/abelosorio)**
46130
* **[Cyril CHAPON](https://github.com/cyrilchapon)**

0 commit comments

Comments
 (0)