<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-set-up-typeorm-datasource-in-your-nestjs-project-qqnjgksm2" -->

---
title: How To Set Up TypeORM DataSource in Your NestJS Project
description: Learn how to set up TypeORM DataSource in your NestJS project. TypeORM is an ORM tool for Node.js and TypeScript applications, while NestJS is a Node.js...
canonical: https://daily.dev/posts/how-to-set-up-typeorm-datasource-in-your-nestjs-project-qqnjgksm2
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How To Set Up TypeORM DataSource in Your NestJS Project | daily.dev
og:description: Learn how to set up TypeORM DataSource in your NestJS project. TypeORM is an ORM tool for Node.js and TypeScript applications, while NestJS is a Node.js...
og:url: https://daily.dev/posts/how-to-set-up-typeorm-datasource-in-your-nestjs-project-qqnjgksm2
og:image: https://api.daily.dev/og/posts/qqnJGkSm2.png
og:image:alt: How To Set Up TypeORM DataSource in Your NestJS Project
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How To Set Up TypeORM DataSource in Your NestJS Project

**[freeCodeCamp](https://daily.dev/sources/freecodecamp)** · 10 min read · 87 upvotes · 2 comments

## Summary

Learn how to set up TypeORM DataSource in your NestJS project. TypeORM is an ORM tool for Node.js and TypeScript applications, while NestJS is a Node.js framework. The tutorial covers the prerequisites, project setup, and setting up the TypeORM DataSource for data persistency. It also includes extending the DataSource repository for custom methods.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.freecodecamp.org/news/how-to-setup-typeorm-datasource-nestjs-app/>

## Community discussion

Top comments from developers on daily.dev.

**@virumabharathi** · 1 upvotes

> Hi , First of all its  Great but the thing I cant able to create a DB migration in the cli, ```npm run migration:generate --d migrations/newMigrations``` when i run this its asking for the dataSource.
>
> P.S - I'm new to Nest...

**@markojr** · 0 upvotes

> You could do so much more with so much less code
>
> why do you create your own `TypeOrmModule` which exports your Datasource .. you already have installed `@nestjs/typeorm`
>
> you can use the official Module provided by the package
>
>
> use also the `@nestJs/config`  module to make the config of your DB-Connection configureable
>
> instead of chaining multiple `nest g` commands you could just run `nest g resource`
>
>
> in your user-service you can directly inject a repository
>
> ```ts
> // BAD
> constructor(private dataSource: DataSource) {
>     // get users table repository to interact with the database
>    ...

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#database](https://daily.dev/tags/database), [#nodejs](https://daily.dev/tags/nodejs), [#typescript](https://daily.dev/tags/typescript), [#nestjs](https://daily.dev/tags/nestjs)

[View this post on daily.dev](https://daily.dev/posts/how-to-set-up-typeorm-datasource-in-your-nestjs-project-qqnjgksm2)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How To Set Up TypeORM DataSource in Your NestJS Project","url":"https://daily.dev/posts/how-to-set-up-typeorm-datasource-in-your-nestjs-project-qqnjgksm2","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-set-up-typeorm-datasource-in-your-nestjs-project-qqnjgksm2"},"datePublished":"2024-04-25T13:32:32.412Z","dateModified":"2024-05-09T09:23:16.455Z","description":"Learn how to set up TypeORM DataSource in your NestJS project. TypeORM is an ORM tool for Node.js and TypeScript applications, while NestJS is a Node.js...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/4ce0c34c057e74b422bc2fe894547eb5?_a=AQAEufR","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/4ce0c34c057e74b422bc2fe894547eb5?_a=AQAEufR","isAccessibleForFree":true,"articleSection":"freeCodeCamp","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"freeCodeCamp","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1628412854/logos/freecodecamp","url":"https://daily.dev/sources/freecodecamp"},"commentCount":2,"discussionUrl":"https://daily.dev/posts/how-to-set-up-typeorm-datasource-in-your-nestjs-project-qqnjgksm2","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":87},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":2}],"keywords":"javascript,database,nodejs,typescript,nestjs","timeRequired":"PT10M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"freeCodeCamp","item":"https://daily.dev/sources/freecodecamp"},{"@type":"ListItem","position":3,"name":"How To Set Up TypeORM DataSource in Your NestJS Project"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/how-to-set-up-typeorm-datasource-in-your-nestjs-project-qqnjgksm2","comment":[{"@type":"Comment","text":"Hi , First of all its  Great but the thing I cant able to create a DB migration in the cli, npm run migration:generate --d migrations/newMigrations when i run this its asking for the dataSource.\nP.S - I’m new to Nest…","datePublished":"2024-08-14T10:42:30.637Z","url":"https://daily.dev/posts/qqnJGkSm2#c-bUKvrVDfP","author":{"@type":"Person","name":"Viruma Bharathi","url":"https://daily.dev/virumabharathi","image":"https://lh3.googleusercontent.com/a/ACg8ocI-hF4JXTjermGLsOmh1YqBoIsXFJP1QC7EOTfiVsIWB6cPqnkZ=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"You could do so much more with so much less code\nwhy do you create your own TypeOrmModule which exports your Datasource … you already have installed @nestjs/typeorm\nyou can use the official Module provided by the package\nuse also the @nestJs/config  module to make the config of your DB-Connection configureable\ninstead of chaining multiple nest g commands you could just run nest g resource\nin your user-service you can directly inject a repository\n// BAD\nconstructor(private dataSource: DataSource) {\n    // get users table repository to interact with the database\n    this.userRepository = this.dataSource.getRepository(UserEntity);\n}\n\n// Better\nconstructor(\n@InjectRepository(UserEntity) private readonly repo : Repository&lt;UserEntity&gt;\n) {}\n\nalso instead of doing a try catch in all your services you could use an\nExceptionFilter … which can catch generic exception like this centralized","datePublished":"2026-03-18T08:23:50.420Z","url":"https://daily.dev/posts/qqnJGkSm2#c-zt5gIHiIQ","author":{"@type":"Person","name":"Marko Jozic-Reyes","url":"https://daily.dev/markojr","image":"https://media.daily.dev/image/upload/s--TvIfWcng--/f_auto/v1769558803/avatars/avatar_x9cfuU5ai02W3BPEbroGK?_a=BAMAMiiu0"}}]}
```

