default db sqlite

This commit is contained in:
Joe Arndt 2026-02-09 23:11:53 -06:00
parent 2ca91b97fb
commit 104b9067c1
4 changed files with 1405 additions and 34 deletions

View file

@ -10,28 +10,44 @@ npm install
``` ```
## Run Locally ## Run Locally
By default, the API looks for a locally running Postgres instance, which can be satisfied by running the following `docker` container: Start the API on [localhost:3000](http://localhost:3000)
```bash
npm run start:dev
```
Default port (`3000`) can be changed by setting the `PORT=xxxx` environment variable.
### Database
There are two databases that are supported:
- Postgres (recommended)
- SQLite (default for simplicity)
For simplicity SQLite is used as the database in the `default`/`unconfigured` state.
Using Postgres is pretty simple, by default the API is looking for a locally running instance, which can be satisfied by running the following `docker` container:
```bash ```bash
docker run --name common-cents-db -e POSTGRES_USER=common-cents -e POSTGRES_PASSWORD=CommonCents_123! -p 5432:5432 -d postgres docker run --name common-cents-db -e POSTGRES_USER=common-cents -e POSTGRES_PASSWORD=CommonCents_123! -p 5432:5432 -d postgres
``` ```
Alternatively, an external Postgres can be configured through an environment file placed in the root directory (`common-cents-api/.env`): To specify Postgres as the database, set the `DB_TYPE=postgres` environment variable.
Alternatively, an external Postgres can be configured through additional environment variables. To simplify all of this, use an `env`file placed in the root directory (`common-cents-api/.env`):
```text ```text
# .env # .env
DB_SYNC=true # should be false for production environment
DB_HOST=localhost # Type 'sqlite' or 'postgres' (if not set sqlite will be used)
DB_PORT=5432 DB_TYPE=sqlite
DB_USER=common-cents
DB_PASS=CommonCents_123! # Sync should be false for production
DB_NAME=common-cents DB_SYNC=true
# Postgres overrides
PG_HOST=localhost
PG_PORT=5432
PG_USER=common-cents
PG_PASS=CommonCents_123!
``` ```
Once the DB is configured/running, the API can be started:
```bash
npm run start:dev
```
Default port (`3000`) can also be changed in the `.env` file by setting `PORT=xxxx`.
## Bruno ## Bruno
The included `bruno` folder contains a collection of calls to make it more convenient to interact with the API. Local calls assume the default NestJS port (`3000`) is being used. The included `bruno` folder contains a collection of calls to make it more convenient to interact with the API. Local calls assume the default NestJS port (`3000`) is being used.

1366
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -29,6 +29,7 @@
"pg": "^8.18.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

@ -13,16 +13,26 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
TypeOrmModule.forRootAsync({ TypeOrmModule.forRootAsync({
imports: [ConfigModule], imports: [ConfigModule],
inject: [ConfigService], inject: [ConfigService],
useFactory: (configService: ConfigService) => ({ useFactory: (configService: ConfigService) => {
synchronize: configService.get<boolean>('DB_SYNC') ?? true, const type = configService.get<'sqlite' | 'postgres'>('DB_TYPE') ?? 'sqlite';
type: 'postgres', const synchronize = configService.get<boolean>('DB_SYNC') ?? true;
host: configService.get<string>('DB_HOST') ?? 'localhost', const autoLoadEntities = true;
port: configService.get<number>('DB_PORT') ?? 5432, const database = type === 'sqlite' ? 'common-cents.db' : 'common-cents';
username: configService.get<string>('DB_USER') ?? 'common-cents',
password: configService.get<string>('DB_PASS') ?? 'CommonCents_123!', const defaults = { type, database, synchronize, autoLoadEntities };
database: configService.get<string>('DB_NAME') ?? 'common-cents',
autoLoadEntities: true if (type === 'postgres') {
}) return {
...defaults,
host: configService.get<string>('PG_HOST') ?? 'localhost',
port: configService.get<number>('PG_PORT') ?? 5432,
username: configService.get<string>('PG_USER') ?? 'common-cents',
password: configService.get<string>('PG_PASS') ?? 'CommonCents_123!'
};
}
return defaults;
}
}), }),
MerchantsModule, MerchantsModule,
TagsModule, TagsModule,