I’m creating a Knex database module to use it with nestJS, but i faced some problems.
I can create migrations and seeds without creating knexfile. But run migrations and seeds – no. So there’s a question: “Can I create a Database Knex module, that don’t need a knexfile to run migrations? I don’t want to create knexfile only to run migrations and seed, this is like a crutch.”
Here’s my code
import 'dotenv/config';
import { Module, NotFoundException, OnModuleInit } from '@nestjs/common';
import { KnexModule } from 'nest-knexjs';
const { PG_HOST, PG_USER, PG_PASSWORD, PG_DATABASE } = process.env;
@Module({
imports: [
KnexModule.forRootAsync({
useFactory: () => ({
config: {
client: 'pg',
useNullAsDefault: true,
version: '5.7',
connection: {
host: PG_HOST || 'localhost',
user: PG_USER,
password: PG_PASSWORD,
database: PG_DATABASE,
},
pool: {
min: 0,
max: 100,
},
searchPath: ['delivery'],
acquireConnectionTimeout: 10000,
migrations: {
extension: 'ts',
directory: './migrations',
tableName: 'migrations',
},
seeds: {
extension: 'ts',
directory: './seeds',
tableName: 'seeds',
},
},
}),
}),
],
})
export class DatabaseModule implements OnModuleInit {
async onModuleInit() {
if (!PG_USER || !PG_PASSWORD || !PG_DATABASE) {
throw new NotFoundException('Неверно указаны переменные среды');
}
}
}
Raydenshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.