Skip to content

Commit

Permalink
chore: db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
incredible-phoenix246 committed Jul 22, 2024
1 parent 7b0f906 commit a4b0504
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ AUTH_SECRET=
DB_USER=
DB_HOST=
DB_PASSWORD=
DB_NAME=
DB_NAME=
NODE_ENV=development
11 changes: 11 additions & 0 deletions db/migrations/1721611425287-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Migration1721611425287 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
}

public async down(queryRunner: QueryRunner): Promise<void> {
}

}
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"scripts": {
"start:dev": "ts-node-dev --respawn --transpile-only ./src/index",
"start": "ts-node --transpile-only src/index.ts",
"test": "jest ",
"typeorm": "typeorm-ts-node-commonjs",
"build": "tsc",
"prod": "node dist/index.js",
"migrate": "typeorm migration:run -d src/data-source.ts"
"migrate": "typeorm-ts-node-commonjs migration:run -d src/data-source",
"migration:create": "typeorm-ts-node-commonjs migration:create db/migrations/migration",
"migration:generate": "typeorm-ts-node-commonjs migration:generate db/migrations/migration -d src/data-source",
"migration:revert": "typeorm-ts-node-commonjs migration:revert -d src/data-source"
},
"repository": {
"type": "git",
Expand All @@ -29,7 +31,7 @@
"@types/supertest": "^6.0.2",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"ts-node": "^10.9.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.3"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const config = {
SMTP_PASSWORD: process.env.SMTP_PASSWORD,
SMTP_HOST: process.env.SMTP_HOST,
SMTP_SERVICE: process.env.SMTP_SERVICE,
NODE_ENV: process.env.NODE_ENV,
};

export default config;
16 changes: 14 additions & 2 deletions src/data-source.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import "reflect-metadata";
import { DataSource, Tree } from "typeorm";
import { DataSource } from "typeorm";
import config from "./config";

const isDevelopment = config.NODE_ENV === "development";

const AppDataSource = new DataSource({
type: "postgres",
host: config.DB_HOST,
port: 5432,
username: config.DB_USER,
password: config.DB_PASSWORD,
database: config.DB_NAME,
synchronize: true,
synchronize: isDevelopment,
logging: false,
entities: ["src/models/**/*.ts"],
migrations: ["src/migrations/**/*.ts"],
migrationsTableName: "migrations",
ssl: false,
extra: {
ssl: {
Expand All @@ -20,4 +24,12 @@ const AppDataSource = new DataSource({
},
});

export async function initializeDataSource() {
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
}
return AppDataSource;
}

export default AppDataSource;

10 changes: 6 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"compilerOptions": {
"lib": ["es5", "es6"],
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2021",
"module": "commonjs",
"removeComments": true,
"allowSyntheticDefaultImports": true,
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
Expand All @@ -13,5 +14,6 @@
"skipLibCheck": true,
"typeRoots": ["src/types"]
},
"include": ["src/**/*", "config"]
"include": ["src/**/*", "config"],
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3813,7 +3813,7 @@ ts-node-dev@^2.0.0:
ts-node "^10.4.0"
tsconfig "^7.0.0"

ts-node@^10.4.0, ts-node@^10.9.1:
ts-node@^10.4.0, ts-node@^10.9.2:
version "10.9.2"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.2.tgz#70f021c9e185bccdca820e26dc413805c101c71f"
integrity sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==
Expand Down

0 comments on commit a4b0504

Please sign in to comment.