Skip to content

Instantly share code, notes, and snippets.

View kerren's full-sized avatar

Kerren Ortlepp kerren

View GitHub Profile
@kerren
kerren / database.ts
Created March 21, 2021 21:48
[An example of a feature config] How a database config would look #config #feature
export const database = {
type: 'postgres',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT || 5432),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
name: 'default',
};
@kerren
kerren / use-config.ts
Created March 21, 2021 21:44
[Importing the config] A few ways to do it #config #import #nestjs
// 1. Import the full config object
import { Config } from '@config';
console.log(Config.database);
// 2. Import a single "feature"
import { database } from '@config';
// 3. Import the full config but as a namespace
import * as config from '@config';
@kerren
kerren / tsconfig.json
Created March 21, 2021 21:41
[Tsconfig paths for setting up config] An easier way to import the config #tsconfig #config #nestjs
{
"compilerOptions": {
"paths": {
"@config": ["src/config"]
}
}
}
@kerren
kerren / index.ts
Created March 21, 2021 21:37
[Config barrel file] The barrel file in the config folder #config #barrel
import * as path from 'path';
/**
* I use app-root-path to determine where the root of the project is. By doing this, I am able
* to place this index.ts file anywhere in the project structure.
*/
import * as appRootPath from 'app-root-path';
/**
* This is an awesome way to read in .env files if you don't always want to load the env in
@kerren
kerren / configuration.ts
Created March 21, 2021 21:17
[The standard form of the config] How the docs say you should import the config #config #import #standard #nestjs
export default () => ({
port: parseInt(process.env.PORT, 10) || 3000,
database: {
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT, 10) || 5432
}
});
@kerren
kerren / database-config.ts
Created March 21, 2021 21:02
[Current NestJS Config] The suggested process in the NestJS docs #current #suggested #config #nestjs
interface DatabaseConfig {
host: string;
port: number;
}
const dbConfig = this.configService.get<DatabaseConfig>('database');
// you can now use `dbConfig.port` and `dbConfig.host`
const port = dbConfig.port;
@kerren
kerren / Dockerfile.migration
Created August 7, 2019 11:42
The migration service Dockerfile
FROM node:lts
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install
COPY entrypoint.sh /entrypoint.sh
COPY . .
@kerren
kerren / docker-compose.yaml
Last active August 7, 2019 11:44
The Docker Compose file that waits for migration
version: '3.6'
services:
database:
image: postgres
environment:
- POSTGRES_PASSWORD=testing
- POSTGRES_USER=testing
- POSTGRES_DB=testing
ports:
- 5432
@kerren
kerren / entrypoint.sh
Created August 7, 2019 11:35
An entrypoint script for a migration service
#!/bin/bash
echo "Running database migrations!"
# First we run the migration of the database
npm run migrate
# Now start the service so that the port opens and the wait command can continue
npm run start
@kerren
kerren / docker-compose.yaml
Created August 7, 2019 11:26
A Docker Compose file that uses wait
version: '3.6'
services:
database:
image: postgres
environment:
- POSTGRES_PASSWORD=testing
- POSTGRES_USER=testing
- POSTGRES_DB=testing
ports:
- 5432