Skip to content

Commit f76724a

Browse files
committed
Added migration script + ormconf file + fixed the issue with typeorm module
1 parent 6fe1987 commit f76724a

File tree

5 files changed

+244
-23
lines changed

5 files changed

+244
-23
lines changed

.env.production

Whitespace-only changes.

.ormconf.ts

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
import * as dotenv from "dotenv";
21
import * as path from "path";
32
import { DataSource } from "typeorm";
43

54
const __nodeEnv__ = String(process.env.NODE_ENV);
65

7-
// !! Don't touch ANYTHING
8-
const __env = dotenv.config({
9-
path: path.join(__dirname, `.env.${__nodeEnv__}`),
10-
debug: true,
11-
}).parsed;
12-
136
// ** =============================================================== ** //
147
// ** Environment ** //
158
// ** =============================================================== ** //
169
// * Note: Environment variables are not read from `.env` files inside pods. They are 'set'
17-
const POSTGRES_HOST = __env?.POSTGRES_HOST || process.env.POSTGRES_HOST;
10+
const POSTGRES_HOST = process.env.POSTGRES_HOST;
1811

1912
// ** ONLY USED IN PRODUCTION
20-
const POSTGRES_SERVICE_PORT_EXPOSE =
21-
__env?.POSTGRES_SERVICE_PORT_EXPOSE || process.env.POSTGRES_SERVICE_PORT_EXPOSE;
13+
const POSTGRES_SERVICE_PORT_EXPOSE = process.env.POSTGRES_SERVICE_PORT_EXPOSE;
14+
15+
const POSTGRES_SERVICE_PORT_MAP = process.env.POSTGRES_SERVICE_PORT_MAP;
16+
const POSTGRES_SUPERUSER_USERNAME = process.env.POSTGRES_SUPERUSER_USERNAME;
2217

23-
const POSTGRES_SERVICE_PORT_MAP =
24-
__env?.POSTGRES_SERVICE_PORT_MAP || process.env.POSTGRES_SERVICE_PORT_MAP;
25-
const POSTGRES_SUPERUSER_USERNAME =
26-
__env?.POSTGRES_SUPERUSER_USERNAME || process.env.POSTGRES_SUPERUSER_USERNAME;
18+
const POSTGRES_SUPERUSER_PASSWORD = process.env.POSTGRES_SUPERUSER_PASSWORD;
2719

28-
const POSTGRES_SUPERUSER_PASSWORD =
29-
__env?.POSTGRES_SUPERUSER_PASSWORD || process.env.POSTGRES_SUPERUSER_PASSWORD;
20+
const POSTGRES_DEFAULT_DATABASE = process.env.POSTGRES_DEFAULT_DATABASE;
3021

31-
const POSTGRES_DEFAULT_DATABASE =
32-
__env?.POSTGRES_DEFAULT_DATABASE || process.env.POSTGRES_DEFAULT_DATABASE;
22+
const POSTGRES_SCHEMA = process.env.POSTGRES_SCHEMA;
3323

34-
const POSTGRES_SCHEMA = __env?.POSTGRES_SCHEMA || process.env.POSTGRES_SCHEMA;
24+
if (
25+
!POSTGRES_HOST ||
26+
!POSTGRES_SERVICE_PORT_EXPOSE ||
27+
!POSTGRES_SERVICE_PORT_MAP ||
28+
!POSTGRES_SUPERUSER_USERNAME ||
29+
!POSTGRES_SUPERUSER_PASSWORD ||
30+
!POSTGRES_DEFAULT_DATABASE ||
31+
!POSTGRES_SCHEMA
32+
) {
33+
console.error("Some env variables are not set. Check.");
34+
process.exit(1);
35+
}
3536

3637
// ** =============================================================== ** //
3738
// ** The Actual Environment ** //
@@ -42,7 +43,7 @@ export const config: DataSource = new DataSource({
4243
port: parseInt(
4344
__nodeEnv__ === "production"
4445
? (POSTGRES_SERVICE_PORT_EXPOSE as string)
45-
: POSTGRES_SERVICE_PORT_MAP!.split(":")[0],
46+
: POSTGRES_SERVICE_PORT_MAP.split(":")[0],
4647
),
4748
username: POSTGRES_SUPERUSER_USERNAME,
4849
password: POSTGRES_SUPERUSER_PASSWORD,

.vscode/settings.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
{
2-
"cSpell.words": ["INITDB", "nestjs", "ormconf", "swcrc", "typeorm"]
2+
"cSpell.words": [
3+
"ENDCOLOR",
4+
"INITDB",
5+
"nestjs",
6+
"ormconf",
7+
"STARTCOLOR",
8+
"swcrc",
9+
"typeorm",
10+
"urandom"
11+
]
312
}

run

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#!/usr/bin/env bash
2+
3+
# ======================================================================= #
4+
# Shell Params #
5+
# ======================================================================= #
6+
set -o errexit
7+
set -e
8+
shopt -s expand_aliases
9+
10+
11+
# ======================================================================= #
12+
# Setup #
13+
# ======================================================================= #
14+
DC="${DC:-exec}"
15+
16+
# If we're running in CI we need to disable TTY allocation for docker compose
17+
# commands that enable it by default, such as exec and run.
18+
TTY=""
19+
if [ ! -t 1 ]; then
20+
TTY="-T"
21+
fi
22+
23+
# ======================================================================= #
24+
# Utilities #
25+
# ======================================================================= #
26+
check_executable () {
27+
executable_name=$1;
28+
29+
print_style "Checking $executable_name... " "info"
30+
31+
if ! command -v "$executable_name" &> /dev/null; then
32+
print_style "Missing" "danger";
33+
exit 1;
34+
fi
35+
36+
print_style "$(command -v "$executable_name"); \n" "success";
37+
}
38+
39+
40+
generate_secret() {
41+
length=$1;
42+
43+
LC_ALL=C tr -dc \
44+
'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' </dev/urandom \
45+
| head -c "$length" ; echo
46+
}
47+
48+
generate_safer_secret() {
49+
length=$1;
50+
51+
LC_ALL=C tr -dc \
52+
'A-Za-z0-9' </dev/urandom \
53+
| head -c "$length" ; echo
54+
}
55+
56+
print_style() {
57+
if [ "$2" = "info" ] ; then
58+
COLOR="96m";
59+
elif [ "$2" = "success" ] ; then
60+
COLOR="92m";
61+
elif [ "$2" = "warning" ] ; then
62+
COLOR="93m";
63+
elif [ "$2" = "danger" ] ; then
64+
COLOR="91m";
65+
else #default color
66+
COLOR="0m";
67+
fi
68+
69+
STARTCOLOR="\e[$COLOR";
70+
ENDCOLOR="\e[0m";
71+
72+
printf "$STARTCOLOR%b$ENDCOLOR" "$1";
73+
}
74+
75+
# ======================================================================= #
76+
# Migration #
77+
# ======================================================================= #
78+
_build_migration_command() {
79+
# ? I know I know. Try using 'npx' yourself and see what happens.
80+
81+
echo ./node_modules/.bin/typeorm-ts-node-commonjs \
82+
-d "$MIGRATION_CONF_FILE" \
83+
"migration:$1" \
84+
"$MIGRATION_ROOT_DIR/""$2""_""$3"
85+
}
86+
87+
_get_last_migration_code() {
88+
source "./.env.development"
89+
90+
find "$MIGRATION_ROOT_DIR" -type f -name "[0-9][0-9][0-9][0-9]*.ts" |
91+
awk -F_ '{print $NF}' |
92+
cut -d '.' -f 1 |
93+
sort -r |
94+
head -n 1
95+
}
96+
97+
_generate_next_migration_code() {
98+
# shellcheck source=./.env.development
99+
source "./.env.development"
100+
101+
last_migration_code=$(_get_last_migration_code)
102+
103+
printf "%04d\n" $((10#$last_migration_code + 1))
104+
105+
}
106+
107+
_escape()
108+
{
109+
case $# in 0) return 0; esac
110+
(
111+
while :
112+
do
113+
printf "'"
114+
unescaped=$1
115+
while :
116+
do
117+
case $unescaped in
118+
119+
*\'*)
120+
printf %s "${unescaped%%\'*}""'\''"
121+
unescaped=${unescaped#*\'}
122+
;;
123+
*)
124+
printf %s "$unescaped"
125+
break
126+
esac
127+
done
128+
shift
129+
case $# in 0) break; esac
130+
printf "' "
131+
done
132+
printf "'\n"
133+
)
134+
}
135+
136+
_sanitize_migration_name() {
137+
migration_name="$1"
138+
139+
if [ -z "$1" ]; then
140+
echo "Missing migration name."
141+
exit 1
142+
fi
143+
144+
_escape "$migration_name" | \
145+
tr -d '[:punct:]' | \
146+
tr '[:upper:]' '[:lower:]' | \
147+
tr ' ' '_'
148+
}
149+
150+
migration_create_empty() {
151+
migration_name=$(_sanitize_migration_name "$1")
152+
153+
. "./.env.development"
154+
155+
printf "Creating Empty Migration %s inside %s\n" "$migration_name" "$MIGRATION_ROOT_DIR"
156+
157+
npx typeorm-ts-node-commonjs migration:create "$MIGRATION_ROOT_DIR/$1""_""$(_generate_next_migration_code)"
158+
}
159+
160+
migration_generate() {
161+
migration_name=$(_sanitize_migration_name "$1")
162+
163+
source "./.env.development"
164+
165+
next_migration_code=$(_generate_next_migration_code)
166+
migration_command=$(_build_migration_command "generate" "$migration_name" "$next_migration_code")
167+
168+
printf "Generating Migration Number %s\n" "$next_migration_code"
169+
170+
exec $migration_command
171+
}
172+
173+
# migration_revert() {
174+
# . "./.env.development"
175+
176+
# last_migration_code=$(_get_last_migration_code)
177+
# migration_command=$(_build_migration_command "revert" "$last_migration_code")
178+
179+
# printf "Reverting Migration Number %s\n" "$last_migration_code"
180+
181+
# exec "$migration_command"
182+
# }
183+
184+
migration_run() {
185+
# shellcheck source=.env.production
186+
# shellcheck source=.env.development
187+
source "./.env.$NODE_ENV"
188+
189+
migration_command=$(_build_migration_command "run" "")
190+
191+
printf "Running Migrations..."
192+
193+
npx typeorm-ts-node-commonjs migration:run \
194+
-d "$MIGRATION_CONF_FILE"
195+
196+
}
197+
198+
199+
# ======================================================================= #
200+
# Help!!! #
201+
# ======================================================================= #
202+
help() {
203+
printf "%s <task> [args]\n\nTasks:\n" "${0}"
204+
205+
compgen -A function | grep -v "^_" | cat -n
206+
207+
printf "\nExtended help:\n Each task has comments for general usage\n"
208+
}
209+
210+
TIMEFORMAT=$'\nTask completed in %3lR'
211+
"${@:-help}"

src/config/postgres/postgres.config.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { TypeOrmModule } from "@nestjs/typeorm";
44
import { PostgresConfigService } from "./postgres.config.service";
55

66
@Module({
7-
providers: [PostgresConfigService],
8-
exports: [PostgresConfigService],
97
imports: [
108
TypeOrmModule.forRootAsync({
119
imports: [PostgresConfigModule],
@@ -34,5 +32,7 @@ import { PostgresConfigService } from "./postgres.config.service";
3432
}),
3533
}),
3634
],
35+
providers: [PostgresConfigService],
36+
exports: [PostgresConfigService],
3737
})
3838
export class PostgresConfigModule {}

0 commit comments

Comments
 (0)