Skip to content

Commit 0092a6e

Browse files
committed
[FEATURE]: add basic migration script
1 parent ebb134d commit 0092a6e

File tree

5 files changed

+222
-2
lines changed

5 files changed

+222
-2
lines changed

package-lock.json

Lines changed: 159 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"postbuild": "npm run copy-files",
1010
"copy-files": "copyfiles -u 1 src/**/*.yml lib",
1111
"dev": "env-cmd -f ./src/config/dev.env nodemon ./src/index.ts",
12-
"test": "env-cmd -f ./src/config/test.env jest --detectOpenHandles"
12+
"test": "env-cmd -f ./src/config/test.env jest --detectOpenHandles",
13+
"migrate:create": "migrate create --template-file ./src/migrations/utils/template.ts --migrations-dir=\"./src/migrations/db\"",
14+
"migrate:up": "migrate --migrations-dir=\"./lib/migrations/db\" up",
15+
"migrate:down": "migrate --migrations-dir=\"./lib/migrations/db\" down"
1316
},
1417
"author": "",
1518
"license": "ISC",
@@ -35,6 +38,7 @@
3538
"http-status": "^1.6.2",
3639
"joi": "^17.7.0",
3740
"jsonwebtoken": "^9.0.0",
41+
"migrate": "^2.0.1",
3842
"moment": "^2.29.4",
3943
"moment-timezone": "^0.5.40",
4044
"mongodb": "^4.12.1",
@@ -76,4 +80,4 @@
7680
"ts-node": "^10.9.1",
7781
"typescript": "^4.9.3"
7882
}
79-
}
83+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import mongoose from "mongoose";
2+
import config from '../../config';
3+
import logger from '../../config/logger';
4+
import 'dotenv/config';
5+
import { DatabaseManager } from '@togethercrew.dev/db';
6+
import { User } from "@togethercrew.dev/db";
7+
8+
const connectToMongoDB = async () => {
9+
try {
10+
await mongoose.connect(config.mongoose.serverURL);
11+
logger.info({ url: config.mongoose.serverURL }, 'Connected to MongoDB!');
12+
} catch (error) {
13+
logger.fatal({ url: config.mongoose.serverURL, error }, 'Failed to connect to MongoDB!')
14+
}
15+
};
16+
17+
export const up = async () => {
18+
// Connect to MongoDB
19+
await connectToMongoDB();
20+
const connection = DatabaseManager.getInstance().getTenantDb("RnDAO");
21+
console.log(await connection.models.Guild.find({}));
22+
23+
console.log(await User.find({}))
24+
};
25+
26+
export const down = async () => {
27+
// Do something
28+
};

src/migrations/utils/template.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import mongoose from "mongoose";
2+
import config from '../../config';
3+
import logger from '../../config/logger';
4+
import 'dotenv/config';
5+
import { DatabaseManager } from '@togethercrew.dev/db';
6+
7+
const connectToMongoDB = async () => {
8+
try {
9+
await mongoose.connect(config.mongoose.serverURL);
10+
logger.info({ url: config.mongoose.serverURL }, 'Connected to MongoDB!');
11+
} catch (error) {
12+
logger.fatal({ url: config.mongoose.serverURL, error }, 'Failed to connect to MongoDB!')
13+
}
14+
};
15+
16+
export const up = async () => {
17+
// Connect to MongoDB
18+
await connectToMongoDB();
19+
const connection = DatabaseManager.getInstance().getTenantDb("database");
20+
await connection.createCollection('my_collection');
21+
};
22+
23+
export const down = async () => {
24+
// Do something
25+
};

src/migrations/utils/ts-compiler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
/* eslint-disable no-undef */
3+
const tsNode = require('ts-node');
4+
module.exports = tsNode.register;

0 commit comments

Comments
 (0)