Here is the basic structure I created to set up my current project with NestJS and PostgreSQL just in case this helps.
I added the following packages:
$ npm install --save knex objection objection-graphql pg
Created the nest database module and added this module to the "imports" and "exports" within the app.module.ts:
./src/database/database.module.ts
- Then added the following files and folders:
./src/database/migrations
./src/database/models
./src/database/base.model.ts
./src/database/user.model.ts
./src/database/seeds
./src/database/migration.stub
./src/database/seed.stub
- At the root level, I placed the knexfile.ts
import 'dotenv/config';
import Knex from 'knex';
import { knexSnakeCaseMappers } from 'objection';
module.exports = {
development: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations: {
directory: './src/database/migrations',
stub: './src/database/migration.stub',
},
seeds: {
directory: './src/database/seeds',
stub: './src/database/seed.stub'
},
...knexSnakeCaseMappers()
},
production: {
client: 'pg',
connectio: process.env.DATABASE_URL,
migrations: {
directory: __dirname + '/database/migrations',
},
seeds: {
directory: __dirname + '/database/seeds/production',
},
},
} as Knex.Config;
- Then for each new Module, I called the model from the database folder
import { UserModel } from '../database/models/user.model'
That's it. Here you have connected PostgreSQL with NestJS.
As others have mentioned the NestJS manual is a well written resource for a detailed reference.
RON