Skip to content

Commit 6849a68

Browse files
authored
docs: add readme (#20)
1 parent fc4b8db commit 6849a68

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Node Migrate TS
2+
3+
Abstract migration framework using TypeScript.
4+
5+
## Creating a migration
6+
7+
A migration is a simple `Migration` object containing a unique `id` and implementations for `up` and `down`.
8+
If necessary `up` and `down` can be async.
9+
10+
```ts
11+
// exampleMigration.ts
12+
import { Migration } from '@geprog/node-migrate-ts';
13+
14+
export const exampleMigration: Migration = {
15+
id: 'exampleMigration', // unique migration identifier
16+
up() {
17+
console.log('Do some migrations in here');
18+
},
19+
down() {
20+
console.log('Revert your migration in here');
21+
},
22+
};
23+
```
24+
25+
## State Storage
26+
27+
The state of migrations can be stored anywhere by passing a `MigrationStore` implementation to [up](src/up.ts)
28+
29+
Currently implemented `MigrationStore`s are:
30+
31+
- [MongoMigrationStore](src/MongoMigrationStore.ts)
32+
33+
Contributions are very welcome! :wink:
34+
35+
## Applying Migrations
36+
37+
This example uses `MongoMigrationStore` as `MigrationStore`.
38+
39+
```ts
40+
import { Migration, MongoMigrationStore, up } from '@geprog/node-migrate-ts';
41+
import { MongoClient } from 'mongodb';
42+
import { exampleMigration } from './exampleMigration';
43+
44+
// migrations are applied in the order defined here
45+
const migrations: Migration[] = [exampleMigration];
46+
47+
const client = await MongoClient.connect('mongodb://user:password@localhost:27017/database?authSource=admin', {
48+
useUnifiedTopology: true,
49+
});
50+
const db = client.db();
51+
52+
const migrationStore = new MongoMigrationStore();
53+
migrationStore.init({
54+
db,
55+
migrationsCollection: 'migrations',
56+
});
57+
58+
await up({ migrations, migrationStore });
59+
60+
await client.close();
61+
```
62+
63+
## Passing a migration context
64+
65+
To avoid a lot of boilerplate in the migrations, a context can be passed to [up](src/up.ts).
66+
This context will then be available in the migrations `up` and `down` functions.
67+
68+
To clarify, here is a modification of the above example to pass the database connection to all migrations:
69+
70+
```diff
71+
import { Migration, MongoMigrationStore, up } from '@geprog/node-migrate-ts';
72+
-import { MongoClient } from 'mongodb';
73+
+import { Db, MongoClient } from 'mongodb';
74+
import { exampleMigration } from './exampleMigration';
75+
76+
+declare module '@geprog/node-migrate-ts' {
77+
+ interface MigrationContext {
78+
+ db: Db;
79+
+ }
80+
+}
81+
+
82+
// migrations are applied in the order defined here
83+
const migrations: Migration[] = [exampleMigration];
84+
85+
const client = await MongoClient.connect('mongodb://user:password@localhost:27017/database?authSource=admin', {
86+
useUnifiedTopology: true,
87+
});
88+
const db = client.db();
89+
90+
const migrationStore = new MongoMigrationStore();
91+
migrationStore.init({
92+
db,
93+
migrationsCollection: 'migrations',
94+
});
95+
96+
-await up({ migrations, migrationStore });
97+
+await up({ migrations, migrationStore, context: { db } });
98+
99+
await client.close();
100+
```
101+
102+
The migrations can now use the database connection from the context.
103+
104+
```ts
105+
import { Migration } from '@geprog/node-migrate-ts';
106+
import { ObjectId } from 'mongodb';
107+
108+
export const exampleMigration: Migration = {
109+
id: 'exampleMigration',
110+
async up(context) {
111+
if (!context || !context.db) {
112+
throw new Error('Please pass a context with a db object');
113+
}
114+
const { db } = context;
115+
116+
console.log('Migrate some mongodb here');
117+
},
118+
async down(context) {
119+
if (!context || !context.db) {
120+
throw new Error('Please pass a context with a db object');
121+
}
122+
const { db } = context;
123+
124+
console.log('Migrate some mongodb here');
125+
},
126+
};
127+
```
128+
129+
## Attribution
130+
131+
This project is based on:
132+
133+
- <https://github.com/tj/node-migrate>
134+
- <https://github.com/mycodeself/mongo-migrate-ts>

0 commit comments

Comments
 (0)