default db sqlite
This commit is contained in:
parent
2ca91b97fb
commit
104b9067c1
4 changed files with 1405 additions and 34 deletions
42
README.md
42
README.md
|
|
@ -10,28 +10,44 @@ npm install
|
|||
```
|
||||
|
||||
## 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
|
||||
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
|
||||
# .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
|
||||
|
||||
# Type 'sqlite' or 'postgres' (if not set sqlite will be used)
|
||||
DB_TYPE=sqlite
|
||||
|
||||
# Sync should be false for production
|
||||
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
|
||||
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
1366
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -29,6 +29,7 @@
|
|||
"pg": "^8.18.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
"sqlite3": "^5.1.7",
|
||||
"typeorm": "^0.3.28"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
|
|
@ -13,16 +13,26 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
|
|||
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
|
||||
})
|
||||
useFactory: (configService: ConfigService) => {
|
||||
const type = configService.get<'sqlite' | 'postgres'>('DB_TYPE') ?? 'sqlite';
|
||||
const synchronize = configService.get<boolean>('DB_SYNC') ?? true;
|
||||
const autoLoadEntities = true;
|
||||
const database = type === 'sqlite' ? 'common-cents.db' : 'common-cents';
|
||||
|
||||
const defaults = { type, database, synchronize, autoLoadEntities };
|
||||
|
||||
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,
|
||||
TagsModule,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue