Moved DB config to ENV (#4)

Co-authored-by: Joe Arndt <jmarndt@users.noreply.github.com>
Reviewed-on: #4
This commit is contained in:
Joe 2026-02-09 22:31:15 +00:00
parent 6600745072
commit 164e51bf03
10 changed files with 431 additions and 80 deletions

View file

@ -3,31 +3,29 @@ REST API for expense tracking and budgeting.
## Project setup ## Project setup
```bash ```bash
$ npm install npm install
``` ```
## Compile and run the project ## Run Locally
By default, the API looks for a locally running Postgres instance, which can be satisfied by running the following container:
```bash ```bash
# development docker run --name common-cents-db -e POSTGRES_USER=common-cents -e POSTGRES_PASSWORD=CommonCents_123! -p 5432:5432 -d postgres
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
``` ```
## Run tests Alternatively, a DB configuration can be provided through an environment file placed in the root directory (`common-cents-api/.env`):
```text
# .env
DB_SYNC=true # should be false for production environment
DB_HOST=localhost
DB_PORT=5432
DB_USER=common-cents
DB_PASS=CommonCents_123!
DB_NAME=common-cents
```
Once the DB is configured/running, the API can be started:
```bash ```bash
# unit tests npm run start:dev
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
``` ```
## Deployment ## Deployment

View file

@ -12,6 +12,6 @@ post {
body:json { body:json {
{ {
"name": "Category Three" "name": "First Cat"
} }
} }

408
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,7 @@
"build": "nest build", "build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start", "start": "nest start",
"start:dev": "nest start --watch", "start:dev": "nest start --env-file .env --watch",
"start:debug": "nest start --debug --watch", "start:debug": "nest start --debug --watch",
"start:prod": "node dist/main", "start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
@ -21,13 +21,14 @@
}, },
"dependencies": { "dependencies": {
"@nestjs/common": "^11.0.1", "@nestjs/common": "^11.0.1",
"@nestjs/config": "^4.0.3",
"@nestjs/core": "^11.0.1", "@nestjs/core": "^11.0.1",
"@nestjs/mapped-types": "*", "@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^11.0.1", "@nestjs/platform-express": "^11.0.1",
"@nestjs/typeorm": "^11.0.0", "@nestjs/typeorm": "^11.0.0",
"pg": "^8.18.0",
"reflect-metadata": "^0.2.2", "reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1", "rxjs": "^7.8.1",
"sqlite3": "^5.1.7",
"typeorm": "^0.3.28" "typeorm": "^0.3.28"
}, },
"devDependencies": { "devDependencies": {

View file

@ -1,29 +1,29 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { MerchantsModule } from './merchants/merchants.module'; import { MerchantsModule } from './merchants/merchants.module';
import { Merchant } from './merchants/entities/merchant.entity';
import { TagsModule } from './tags/tags.module'; import { TagsModule } from './tags/tags.module';
import { Tag } from './tags/entities/tag.entity';
import { CategoriesModule } from './categories/categories.module'; import { CategoriesModule } from './categories/categories.module';
import { Category } from './categories/entities/category.entity';
import { SubCategoriesModule } from './sub-categories/sub-categories.module'; import { SubCategoriesModule } from './sub-categories/sub-categories.module';
import { SubCategory } from './sub-categories/entities/sub-category.entity';
import { ExpensesModule } from './expenses/expenses.module'; import { ExpensesModule } from './expenses/expenses.module';
import { Expense } from './expenses/entities/expense.entity'; import { ConfigModule, ConfigService } from '@nestjs/config';
const entities = [Merchant, Tag, Category, SubCategory, Expense];
const sqliteConfig: TypeOrmModuleOptions = {
synchronize: true,
type: 'sqlite',
database: 'common-cents.db',
entities
}
@Module({ @Module({
imports: [ imports: [
TypeOrmModule.forRoot(sqliteConfig), TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
synchronize: configService.get<boolean>('DB_SYNC') ?? true,
type: 'postgres',
host: configService.get<string>('DB_HOST') ?? 'localhost',
port: configService.get<number>('DB_PORT') ?? 5432,
username: configService.get<string>('DB_USER') ?? 'common-cents',
password: configService.get<string>('DB_PASS') ?? 'CommonCents_123!',
database: configService.get<string>('DB_NAME') ?? 'common-cents',
autoLoadEntities: true
})
}),
MerchantsModule, MerchantsModule,
TagsModule, TagsModule,
CategoriesModule, CategoriesModule,

View file

@ -2,9 +2,13 @@ import { Module } from '@nestjs/common';
import { CategoriesService } from './categories.service'; import { CategoriesService } from './categories.service';
import { CategoriesController } from './categories.controller'; import { CategoriesController } from './categories.controller';
import { CategoryDataService } from './category-data.service'; import { CategoryDataService } from './category-data.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Category } from './entities/category.entity';
@Module({ @Module({
controllers: [CategoriesController], controllers: [CategoriesController],
providers: [CategoriesService, CategoryDataService] providers: [CategoriesService, CategoryDataService],
imports: [TypeOrmModule.forFeature([Category])],
exports: [TypeOrmModule]
}) })
export class CategoriesModule { } export class CategoriesModule { }

View file

@ -2,9 +2,13 @@ import { Module } from '@nestjs/common';
import { ExpensesService } from './expenses.service'; import { ExpensesService } from './expenses.service';
import { ExpensesController } from './expenses.controller'; import { ExpensesController } from './expenses.controller';
import { ExpenseDataService } from './expense-data.service'; import { ExpenseDataService } from './expense-data.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Expense } from './entities/expense.entity';
@Module({ @Module({
controllers: [ExpensesController], controllers: [ExpensesController],
providers: [ExpensesService, ExpenseDataService], providers: [ExpensesService, ExpenseDataService],
imports: [TypeOrmModule.forFeature([Expense])],
exports: [TypeOrmModule]
}) })
export class ExpensesModule { } export class ExpensesModule { }

View file

@ -2,9 +2,13 @@ import { Module } from '@nestjs/common';
import { MerchantsService } from './merchants.service'; import { MerchantsService } from './merchants.service';
import { MerchantsController } from './merchants.controller'; import { MerchantsController } from './merchants.controller';
import { MerchantDataService } from './merchant-data.service'; import { MerchantDataService } from './merchant-data.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Merchant } from './entities/merchant.entity';
@Module({ @Module({
controllers: [MerchantsController], controllers: [MerchantsController],
providers: [MerchantsService, MerchantDataService] providers: [MerchantsService, MerchantDataService],
imports: [TypeOrmModule.forFeature([Merchant])],
exports: [TypeOrmModule]
}) })
export class MerchantsModule { } export class MerchantsModule { }

View file

@ -2,9 +2,13 @@ import { Module } from '@nestjs/common';
import { SubCategoriesService } from './sub-categories.service'; import { SubCategoriesService } from './sub-categories.service';
import { SubCategoriesController } from './sub-categories.controller'; import { SubCategoriesController } from './sub-categories.controller';
import { SubCategoryDataService } from './sub-category-data.service'; import { SubCategoryDataService } from './sub-category-data.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SubCategory } from './entities/sub-category.entity';
@Module({ @Module({
controllers: [SubCategoriesController], controllers: [SubCategoriesController],
providers: [SubCategoriesService, SubCategoryDataService] providers: [SubCategoriesService, SubCategoryDataService],
imports: [TypeOrmModule.forFeature([SubCategory])],
exports: [TypeOrmModule]
}) })
export class SubCategoriesModule { } export class SubCategoriesModule { }

View file

@ -2,9 +2,13 @@ import { Module } from '@nestjs/common';
import { TagsService } from './tags.service'; import { TagsService } from './tags.service';
import { TagsController } from './tags.controller'; import { TagsController } from './tags.controller';
import { TagDataService } from './tag-data.service'; import { TagDataService } from './tag-data.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Tag } from './entities/tag.entity';
@Module({ @Module({
controllers: [TagsController], controllers: [TagsController],
providers: [TagsService, TagDataService] providers: [TagsService, TagDataService],
imports: [TypeOrmModule.forFeature([Tag])],
exports: [TypeOrmModule]
}) })
export class TagsModule { } export class TagsModule { }