Search code examples
postgresqltypescriptnestjstypeorm

How to connect Nestjs Application to postgreSQL without using ORM?


So I'm new to Nestjs, and I found that all tutorials are using TypeORM to connect to the database and perform CRUD operations.

So I want to know if there's any other way to connect postgreSQL to Nestjs and write my CRUD operations in SQL instead of TypeORM.

How can I do so?

What is the advantages of doing so?

Is doing so it better for performance?


Solution

  • TypeORM ist just another ORM like Sequelize or Bookshelf or Objection. They're all database connections that you can use. However, if you want to go the route of pure SQL you can use whatever database drive you please like MySQL, SQLite, or my favorite flavor, and the one that pertains to your question, PostgreSQL. There's also pg-promise if that's more your flavor.

    After choosing your driver of choice you can work with creating a service class and module for the database connection, similar to how most stuff in Nest works, or you could just create a utility that exposes the database connection and runs the queries without creating the service class. You can look here for a module example but be warned that it takes advantage of some other packages and some complex ideas for creating transient services and RxJS.

    Overall, nothing is stopping you from using raw SQL with a driver of your choice, but you'll want to look into the Dynamic Modules chapter of the docs to help with some of the implementation.