Skip to content

Commit c5e102e

Browse files
committed
migration setup
1 parent 5b71b70 commit c5e102e

File tree

10 files changed

+58
-15
lines changed

10 files changed

+58
-15
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
tsconfigRootDir: __dirname,
66
sourceType: 'module',
77
},
8+
exports: ["migrations/**/*"],
89
plugins: ['@typescript-eslint/eslint-plugin'],
910
extends: [
1011
'plugin:@typescript-eslint/recommended',
@@ -15,7 +16,8 @@ module.exports = {
1516
node: true,
1617
jest: true,
1718
},
18-
ignorePatterns: ['.eslintrc.js'],
19+
20+
ignorePatterns: ['.eslintrc.js', 'typings', "migrations/**/*"],
1921
rules: {
2022
'@typescript-eslint/interface-name-prefix': 'off',
2123
'@typescript-eslint/explicit-function-return-type': 'off',

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ RUN --mount=type=bind,source=package.json,target=package.json \
2828
USER node
2929

3030
# Copy the rest of the source files into the image.
31-
COPY . .
31+
# COPY . .
3232

3333
# Expose the port that the application listens on.
3434
EXPOSE 8100
3535

3636
# Run the application.
37-
CMD yarn run start:dev
37+
# CMD yarn run start:dev

compose.yaml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
# You can add other services your application may depend on here, such as a
88
# database or a cache. For examples, see the Awesome Compose repository:
99
# https://github.com/docker/awesome-compose
10-
name: ${PROJECT_NAME}
10+
name: starter-kit
1111
services:
1212
app:
1313
container_name: "starter-kit-app"
14+
image: starter-kit/backend:latest
1415
build:
1516
context: .
1617
environment:
@@ -31,7 +32,6 @@ services:
3132
- -c
3233
- |
3334
yarn install
34-
yarn run build
3535
yarn run start:dev
3636
3737
pgsql:
@@ -45,6 +45,17 @@ services:
4545
- ${DB_EXPOSED_PORT:-8611}:5432
4646
networks:
4747
- network
48+
healthcheck:
49+
test:
50+
- CMD
51+
- pg_isready
52+
- '-q'
53+
- '-d'
54+
- 'db-starter-kit'
55+
- '-U'
56+
- '${DB_USER}'
57+
retries: 3
58+
timeout: 5s
4859

4960
redis:
5061
container_name: "starter-kit-redis"

migrations/1696849524727-createUsersTable.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ export class CreateUsersTable1696849524727 implements MigrationInterface {
4444
},
4545
{
4646
name: "email_verified_at",
47-
type: "datetime",
47+
type: "timestamp",
4848
isNullable: true,
4949
},
5050
{
5151
name: "phone_verified_at",
52-
type: "datetime",
52+
type: "timestamp",
5353
isNullable: true,
5454
},
5555
{
5656
name: "created_at",
57-
type: "datetime",
57+
type: "timestamp",
5858
default: "now()",
5959
},
6060
{
6161
name: "updated_at",
62-
type: "datetime",
62+
type: "timestamp",
6363
default: "now()",
6464
}
6565
],

migrations/datasource.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { DataSource, DataSourceOptions } from "typeorm"
2+
import { config as dotenvConfig } from 'dotenv';
3+
4+
dotenvConfig({ path: '.env' });
5+
6+
const typeOrmDataSource = new DataSource({
7+
name: 'default',
8+
type: "postgres",
9+
database: process.env.DB_NAME,
10+
username: process.env.DB_USER,
11+
password: process.env.DB_PASS,
12+
host: process.env.DB_HOST,
13+
port: parseInt(process.env.DB_PORT ?? "5432"),
14+
migrations: [
15+
"migrations/*-*.{ts,js}"
16+
],
17+
synchronize: false,
18+
migrationsTableName: "migrations",
19+
})
20+
21+
22+
export default typeOrmDataSource;

nest-cli.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"include": "i18n/**/*",
1515
"watchAssets": true,
16-
"outDir": "dist/src"
16+
"outDir": "dist"
1717
}
1818
]
1919
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
2020
"test:e2e": "jest --config ./test/jest-e2e.json",
2121
"nest": "nest",
22-
"typeorm": "typeorm"
22+
"typeorm": "ts-node ./node_modules/typeorm/cli",
23+
"migration:run": "npm run typeorm migration:run -- -d ./migrations/datasource.ts",
24+
"migration:create": "npm run typeorm -- migration:create ./src/migrations/$npm_config_name",
25+
"migration:revert": "npm run typeorm -- -d ./migrations/datasource.ts migration:revert"
2326
},
2427
"dependencies": {
2528
"@google-cloud/storage": "^6.12.0",

src/config/typeorm.config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { TypeOrmModuleAsyncOptions, TypeOrmModuleOptions } from "@nestjs/typeorm";
22
import { join } from "path";
3+
import { DataSource, DataSourceOptions } from "typeorm";
4+
35
export const defaultTypeOrmConfig: TypeOrmModuleAsyncOptions = {
46
useFactory: () => {
57
return {
@@ -14,10 +16,11 @@ export const defaultTypeOrmConfig: TypeOrmModuleAsyncOptions = {
1416
join(__dirname, '..', '**', '*.entity{.ts,.js}')
1517
],
1618
synchronize: false,
19+
migrationsTableName: "migrations",
1720
poolSize: 10,
1821
poolErrorHandler: (err) => {
1922
console.log('db pooling error:', err)
2023
}
21-
}
24+
};
2225
}
23-
}
26+
}

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async function bootstrap() {
3030
.addTag('Auth', "All about authentication")
3131
.build();
3232
const document = SwaggerModule.createDocument(app, config);
33-
SwaggerModule.setup('api', app, document);
33+
SwaggerModule.setup('docs', app, document);
3434

3535
await app.listen(APP_PORT);
3636

@@ -42,6 +42,7 @@ async function bootstrap() {
4242

4343
if(process.env.APP_EXPOSED_PORT != undefined) {
4444
console.log(`App Exposed on http://0.0.0.0:${process.env.APP_EXPOSED_PORT}`);
45+
console.log(`Swagger Exposed on http://0.0.0.0:${process.env.APP_EXPOSED_PORT}/docs`);
4546
}
4647

4748
console.log("--------------------------------------------------------");

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"strictBindCallApply": false,
1818
"forceConsistentCasingInFileNames": false,
1919
"noFallthroughCasesInSwitch": false
20-
}
20+
},
21+
"include": ["src/**/*", "typings/*"]
2122
}

0 commit comments

Comments
 (0)