0

I am from .net background and few days back started using nestjs for one of my project.

I love the way nestjs built but few question here.

When using .net I can easily connect .net application with database by using some ORM like Ado.net, EF etc.

How do I connect nestjs with Postgres or SQL Server database?

Whatever will be the answer I would also like to know will that be suitable for enterprise applications?

Is there any reference site?

Thanks in advance

2
  • There are many libraries out there, and the choice would depend a lot on your experience with ProstgreSQL, and personal preferences. For starters, how well do you know PostgreSQL, do you prefer writing SQL directly or code around it via data models. Performance requirements, and real use cases. Commented Sep 30, 2019 at 23:47
  • From my point of view, TypeORM would be a good tool to build solid backend both with NestJS, and it is also backed by quite a good amount of sponsors (opencollective.com/typeorm), and final points it is well documented and overall it has a good adoption by the community. Commented Oct 1, 2019 at 23:08

2 Answers 2

1

The docs show examples of how to conenct to a database using TypeORM and Sequilize, or you can roll your own dynamic module with custom providers if you want to use something else. There are a few packages around for knex, pg-promise, and massiveORM. These should all be more than suitable for enterprise applications, but if anything shows up as an issue, make sure to notify the owners of the repository.

Sign up to request clarification or add additional context in comments.

13 Comments

Just so, worth noting, pg-promise is the only one on the list that's not an ORM, it is for executing SQL directly, good choice for those who knows SQL well.
@vitaly-t you are absolutely right. I just started listing different NestJS database packages I knew of. I ended up rolling my own pg module and using it in my personal project. All the others though are ORMs and would work for the case given above.
@JayMcDoniel Thanks for the answer . I tried installing TypeORM with nestjs and it's not working. Do you have any reference ? I use documentation docs.nestjs.com but still it gives lots of warning.
What kind of problems are you running into? I have a repository set up that is to show how to test a TypeORM Nest project, you can take a look at it here.
@Glennsingh it looks like you aren't providing a username and password for logging into the SQL server. Can you log into the server locally? Do you know what credentials you use if so?
|
0

Here is the basic structure I created to set up my current project with NestJS and PostgreSQL just in case this helps.

  1. I added the following packages:

    $ npm install --save knex objection objection-graphql pg 
    
  2. Created the nest database module and added this module to the "imports" and "exports" within the app.module.ts:

./src/database/database.module.ts
  1. 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
  1. 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;
  1. 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.