@oliasoft-open-source/node-json-migrator 3.0.0-beta-2 → 3.0.0-beta-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -169
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oliasoft-open-source/node-json-migrator",
|
|
3
|
-
"version": "3.0.0-beta-
|
|
3
|
+
"version": "3.0.0-beta-3",
|
|
4
4
|
"description": "A library for JSON migrations",
|
|
5
5
|
"homepage": "https://oliasoft-open-source.gitlab.io/node-postgresql-migrator",
|
|
6
6
|
"bugs": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"scripts": {
|
|
33
|
-
"build": "pkgroll --clean-dist
|
|
33
|
+
"build": "pkgroll --clean-dist",
|
|
34
34
|
"lint:check": "npx eslint '**/*.{js,ts,json}'",
|
|
35
35
|
"lint:fix": "npx eslint --fix '**/*.{js,ts,json}'",
|
|
36
36
|
"prepare": "husky",
|
package/dist/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 Oliasoft AS and contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/README.md
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
# Node JSON Migrator
|
|
2
|
-
|
|
3
|
-
## A simple JSON change management tool for Node.js
|
|
4
|
-
|
|
5
|
-
- Lightweight JSON change management tool for `Node.js` applications
|
|
6
|
-
- Written in JavaScript
|
|
7
|
-
- Pre-transpiled (no extra build steps)
|
|
8
|
-
- Not tied to any framework
|
|
9
|
-
- Open source (MIT License)
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- Versioning of JSON payloads
|
|
14
|
-
- Write change scripts (migrations) in JavaScript
|
|
15
|
-
- Apply changes to JSON payloads using a convenient `migrate()` function
|
|
16
|
-
- Works great in a team setting (multiple developers can track and handle changes)
|
|
17
|
-
- Supports automated change deployment across multiple environments
|
|
18
|
-
- Automatically tracks and applies only new (pending) change scripts
|
|
19
|
-
- Automatically detects and warns when previous scripts have been altered
|
|
20
|
-
- Optionally track version history in a PostgreSQL database (pg-promise interface)
|
|
21
|
-
|
|
22
|
-
# How to use
|
|
23
|
-
|
|
24
|
-
## Installation and setup
|
|
25
|
-
|
|
26
|
-
The database (if used) must already exist and have permissions granted.
|
|
27
|
-
|
|
28
|
-
Install the tool directly from Gitlab (published NPM packages may be available in future):
|
|
29
|
-
|
|
30
|
-
```
|
|
31
|
-
npm install --save git+https://gitlab.com/oliasoft-open-source/node-json-migrator.git
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
## Defining migrations (change scripts)
|
|
35
|
-
|
|
36
|
-
Use the `createMigration()` function to generate a skeleton migration script, for example:
|
|
37
|
-
|
|
38
|
-
```javascript
|
|
39
|
-
import {createMigration} from 'node-json-migrator';
|
|
40
|
-
|
|
41
|
-
const directory = 'json-migrations';
|
|
42
|
-
const description = 'add-panda';
|
|
43
|
-
await createMigration(directory, description);
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
This will create and populate skeleton files:
|
|
47
|
-
|
|
48
|
-
- `add-panda/add-panda.js` is the skeleton change script (alter this to describe the desired payload change)
|
|
49
|
-
- `plan.json` is the execution plan (defines the execution order / sequence for migration scripts)
|
|
50
|
-
|
|
51
|
-
Migration scripts export default functions that receive a payload and return the next payload.
|
|
52
|
-
They must use immutable update patterns, and [Immer](https://github.com/immerjs/immer) is recommended for this.
|
|
53
|
-
For example, after editing, the scripts might look like this:
|
|
54
|
-
|
|
55
|
-
**json-migrations/add-panda/add-panda.js** (adds a property to the payload)
|
|
56
|
-
|
|
57
|
-
```javascript
|
|
58
|
-
import produce from 'immer';
|
|
59
|
-
|
|
60
|
-
export default (payload) = produce(state, (draft) => {
|
|
61
|
-
draft.panda = 'Elliot';
|
|
62
|
-
});
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
**json-migrations/plan.json** (defines the migration execution plan)
|
|
66
|
-
|
|
67
|
-
```json
|
|
68
|
-
[
|
|
69
|
-
{
|
|
70
|
-
"fileHash": "70fd80fd19828435b8656b8d01f99b840381050ded4aec2dd126e2f0c9864c88",
|
|
71
|
-
"fileName": "add-panda.js",
|
|
72
|
-
"sequence": "1"
|
|
73
|
-
}
|
|
74
|
-
]
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
Note: the `fileHash` property is the SHA256 hash of the migration file content. You don't have to manually maintain
|
|
78
|
-
that, the tool will sync it automatically for you.
|
|
79
|
-
|
|
80
|
-
## Running migrations
|
|
81
|
-
|
|
82
|
-
In your application source code, import and call the `migrate()` function. The simplest usage (runs all migrations)
|
|
83
|
-
is:
|
|
84
|
-
|
|
85
|
-
```javascript
|
|
86
|
-
import {migrate} from 'node-json-migrator';
|
|
87
|
-
|
|
88
|
-
const {nextPayload} = await migrate({
|
|
89
|
-
payload: {},
|
|
90
|
-
config: {
|
|
91
|
-
directory: 'json-migrations'
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
Normally you want to track payload versions, and only execute pending migrations. To do this, you need to connect to a
|
|
97
|
-
PostgreSQL database and pass a
|
|
98
|
-
connected [pg-promise database object](http://vitaly-t.github.io/pg-promise/). Then it looks like this:
|
|
99
|
-
|
|
100
|
-
```javascript
|
|
101
|
-
import pgPromise from 'pg-promise';
|
|
102
|
-
import {migrate} from 'node-json-migrator';
|
|
103
|
-
|
|
104
|
-
//connect to the database somewhere in the database layer
|
|
105
|
-
const pgp = pgPromise({});
|
|
106
|
-
const pgpHelpers = pgp.helpers;
|
|
107
|
-
const database = pgp(/*connection string*/);
|
|
108
|
-
database.connect();
|
|
109
|
-
|
|
110
|
-
//if we know the current payload version, we can run only the newer, pending migration scripts
|
|
111
|
-
const currentPayloadVersion = undefined;
|
|
112
|
-
|
|
113
|
-
const {nextPayload, nextVersion} = await migrate({
|
|
114
|
-
payload: {},
|
|
115
|
-
config: {
|
|
116
|
-
directory: 'json-migrations',
|
|
117
|
-
database, //connected database object
|
|
118
|
-
pgpHelpers, //pg-promise helpers: https://vitaly-t.github.io/pg-promise/helpers.html
|
|
119
|
-
version: currentPayloadVersion
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
**Other config options**
|
|
125
|
-
|
|
126
|
-
```javascript
|
|
127
|
-
config = {
|
|
128
|
-
dry: true, //execute without writing to database tables or files
|
|
129
|
-
force: true, //execute bypassing some validation warnings
|
|
130
|
-
}
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
## Usage Rules
|
|
134
|
-
|
|
135
|
-
- files in the plan must exist
|
|
136
|
-
- filenames must be unique and follow formatting rules
|
|
137
|
-
- sequences must be unique
|
|
138
|
-
- not allowed to change sequence numbers after releasing (merging) a migration
|
|
139
|
-
- not allowed to remove released files from the plan
|
|
140
|
-
- should release only one migration script at a time
|
|
141
|
-
- not allowed to alter/modify released migration files (instead, write a new migration to replace it)
|
|
142
|
-
- migration scripts should be repeatable (not fail if they run twice on the same payload)
|
|
143
|
-
|
|
144
|
-
## Backfitting bugfixes
|
|
145
|
-
|
|
146
|
-
If you release a bad migration script, there is an escape-hatch for backfitting bugfixes:
|
|
147
|
-
|
|
148
|
-
- rename the faulty script to `.skip.js` (or `.skip2.js` if it happened more than once)
|
|
149
|
-
- add a new migration file with the original filename, to replace it (fix bug in new file)
|
|
150
|
-
- the replacement script will *not* re-execute on payloads that already had the original script applied
|
|
151
|
-
|
|
152
|
-
Or, you can backfit a script earlier in the sequence
|
|
153
|
-
|
|
154
|
-
- add a new migration file
|
|
155
|
-
- set its sequence to for example `1.1` (subsequence number)
|
|
156
|
-
- the backfitted script *will* execute one time on all payloads
|
|
157
|
-
|
|
158
|
-
# How to contribute
|
|
159
|
-
|
|
160
|
-
Contribution is welcome via [issue tickets](https://gitlab.com/oliasoft-open-source/node-json-migrator/-/issues)
|
|
161
|
-
and [merge requests](https://gitlab.com/oliasoft-open-source/node-json-migrator/-/merge_requests).
|
|
162
|
-
|
|
163
|
-
- coding style (ESLint and prettier) is mandatory (applied via pre-commit hook)
|
|
164
|
-
- to test: `npm run test`
|
|
165
|
-
- database is mocked with [pg-mem](https://github.com/oguimbal/pg-mem) (in-memory database)
|
|
166
|
-
- filesystem is mocked [mock-fs](https://github.com/tschaub/mock-fs)
|
|
167
|
-
- coverage reports are in `test/coverage` (not committed)
|
|
168
|
-
- to build: `npm run buld` (this transpile a production build to the `dist` directory)
|
|
169
|
-
- the build `dist` directory should be committed
|