Skip to content

Commit

Permalink
chore: configure TypeORM migrations and make docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
the-sabra committed Jul 23, 2024
1 parent 9fe7946 commit a312d8a
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
Dockerfile
.gitignore
README.md
.vscodew
dist
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ lerna-debug.log*
.env.test.local
.env.production.local
.env.local
.env.docker

# temp directory
.temp
Expand Down
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3.9'

services:
db:
image: mysql:latest
container_name: mostaqem_db
environment:
MYSQL_ROOT_PASSWORD: 'db_password'
MYSQL_DATABASE: 'mostaqem_db'
api_v1:
build:
context: .
dockerfile: Dockerfile
ports:
- '3000:3000'
expose:
- '3000'
env_file:
- .env.docker
volumes:
- ./src:/app/src
links:
- db
13 changes: 13 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:22-alpine

WORKDIR /app

COPY . .

RUN npm install -g pnpm

RUN pnpm install

EXPOSE 3000

CMD [ "pnpm" , "run", "start:prod"]
29 changes: 29 additions & 0 deletions migrations/1721758921061-init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Init1721758921061 implements MigrationInterface {
name = 'Init1721758921061'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`reciter\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name_english\` varchar(100) NOT NULL, \`name_arabic\` varchar(100) NOT NULL, \`image\` varchar(250) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`CREATE TABLE \`reciter_surah\` (\`reciter_id\` int NOT NULL, \`surah_id\` int NOT NULL, \`url\` varchar(255) NOT NULL, PRIMARY KEY (\`reciter_id\`, \`surah_id\`)) ENGINE=InnoDB`);
await queryRunner.query(`CREATE TABLE \`surah\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name_arabic\` varchar(100) NOT NULL, \`name_complex\` varchar(100) NOT NULL, \`verses_count\` int NOT NULL, \`revelation_place\` varchar(30) NOT NULL, \`image\` varchar(250) NULL, INDEX \`IDX_SURAH\` (\`id\`, \`name_arabic\`, \`name_complex\`, \`verses_count\`, \`revelation_place\`, \`image\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`CREATE TABLE \`verse\` (\`id\` int NOT NULL AUTO_INCREMENT, \`vers\` text NOT NULL, \`verse_number\` int NOT NULL, \`vers_lang\` varchar(3) NOT NULL, \`surah_id\` int NOT NULL, INDEX \`idx_surah_id\` (\`surah_id\`), INDEX \`SURAH_VERSE_UNIQUE\` (\`surah_id\`, \`verse_number\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`ALTER TABLE \`reciter_surah\` ADD CONSTRAINT \`FK_c083c2e64bff5563b0940b6ae9a\` FOREIGN KEY (\`reciter_id\`) REFERENCES \`reciter\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`reciter_surah\` ADD CONSTRAINT \`FK_faeb6bf0be06de7f316921c0068\` FOREIGN KEY (\`surah_id\`) REFERENCES \`surah\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`verse\` ADD CONSTRAINT \`FK_7da0f9ffbbe4ea8116f2f36610a\` FOREIGN KEY (\`surah_id\`) REFERENCES \`surah\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`verse\` DROP FOREIGN KEY \`FK_7da0f9ffbbe4ea8116f2f36610a\``);
await queryRunner.query(`ALTER TABLE \`reciter_surah\` DROP FOREIGN KEY \`FK_faeb6bf0be06de7f316921c0068\``);
await queryRunner.query(`ALTER TABLE \`reciter_surah\` DROP FOREIGN KEY \`FK_c083c2e64bff5563b0940b6ae9a\``);
await queryRunner.query(`DROP INDEX \`SURAH_VERSE_UNIQUE\` ON \`verse\``);
await queryRunner.query(`DROP INDEX \`idx_surah_id\` ON \`verse\``);
await queryRunner.query(`DROP TABLE \`verse\``);
await queryRunner.query(`DROP INDEX \`IDX_SURAH\` ON \`surah\``);
await queryRunner.query(`DROP TABLE \`surah\``);
await queryRunner.query(`DROP TABLE \`reciter_surah\``);
await queryRunner.query(`DROP TABLE \`reciter\``);
}

}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "ts-node --require tsconfig-paths/register ./node_modules/typeorm/cli",
"typeorm:generate-migration": "npm run typeorm -- -d ./typeorm.config.ts migration:generate ./migrations/$npm_config_name",
"typeorm:migrate": "npm run typeorm -- -d ./typeorm.config.ts migration:run"
},
"dependencies": {
"@nestjs/cache-manager": "^2.2.2",
Expand All @@ -33,6 +36,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.6",
"dotenv": "^16.4.5",
"mysql2": "^3.10.3",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { CacheModule } from '@nestjs/cache-manager';
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
autoLoadEntities: true,
synchronize: true,
synchronize: process.env.NODE_ENV == 'development',
connectTimeout: 60000,
retryDelay: 6000,
}),
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
},
"include": ["src/**/*", "test", "seed.ts"]
"include": ["src/**/*", "test", "seed.ts","typeorm.config.ts"]
}
15 changes: 15 additions & 0 deletions typeorm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DataSource } from 'typeorm';
import { config } from 'dotenv';
config({ path: '.env.docker' });

export default new DataSource({
type: 'mysql',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: ['dist/**/*.entity{.ts,.js}', 'src/**/*.entity{.ts,.js}'],
migrations: ['migrations/**'],
ssl: process.env.NODE_ENV == 'production' ? true : false,
});

0 comments on commit a312d8a

Please sign in to comment.