TypeORM v0.3.x Migrations, queries, with NestJS!

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • In this quick video we go over the basics of setting up and using TypeORM specifically with NestJS. This video covers the newer typeORM 0.3 and above versions which has had some breaking changes from 0.2. We'll cover how to create a CRUD API, how to write SQL queries, and finally how to do migrations.
    00:00 - Intro
    00:21 - New Nest JS application and setup TypeORM
    04:31 - Create a new entity
    09:52 - Implement CRUD database queries
    16:29 - Testing our API
    20:36 - Database Migrations

Komentáře • 196

  • @biserivanov3433
    @biserivanov3433 Před 11 měsíci +1

    I have watched many similar tutorials how to do migrations and your is by far the best one. Thanks for the great content.

  • @lukramingo1496
    @lukramingo1496 Před rokem +4

    I have been stuck for two days typeorm migration generate, now you save my life...........Please create more videos about nestjs for new versions upto date👍

  • @tkchannel5856
    @tkchannel5856 Před rokem +2

    Thank you for this vid, after your previous one on TypeORM i had problems with migrations on CLI v0.3, finally there is a somewhat complete guide on new version

  • @loiskouninef5014
    @loiskouninef5014 Před rokem

    Just want to say a huge THANK YOU.
    Navigating through your NestJS videos (and, of course, official documentations) really helped me understand the framework and build basic APIs with Postgresql.
    Can't wait to see what's up next!
    Again, thank you and keep up the good work :)

    • @mariusespejo
      @mariusespejo  Před rokem

      You’re welcome! Thanks for stopping by to comment 🙂

  • @user-st8tc4np2i
    @user-st8tc4np2i Před rokem

    I have watched your nestjs crash course in netninja's channel and from there, headed straight to your channel to learn typeorm. You have explained both of these concepts really really well. I have been a fan of yours, I would say. Thank you.

    • @mariusespejo
      @mariusespejo  Před rokem

      Thanks! Glad you’re finding the channel useful

  • @syedumairnaeem6724
    @syedumairnaeem6724 Před rokem +3

    The way you explain complex stuff is just amazing. Learned a lot about nest and testing from your videos. I was looking for a tutorial on migration and here it is. Much appreciated.

    • @mariusespejo
      @mariusespejo  Před rokem +1

      Thanks for the feedback 🙏

    • @mohammadumar443
      @mohammadumar443 Před rokem

      @@mariusespejo how can we do DB first using typeorm and nestjs ? I just want to connect to DB and write APIs with the data in the DB.

    • @mariusespejo
      @mariusespejo  Před rokem

      Not sure what you mean by DB first. With typeorm you would typically create entities representing your database tables, then you can connect and write APIs. I have several videos on nest and typeorm if you check the channel

  • @tonyeneh8194
    @tonyeneh8194 Před rokem +1

    you're soo good with explaining stuff. You have a way of figuring out questions that would crop up in viewers minds and then answering them just as they come up. Keep it up bro 👏👏👏

  • @mikoajrenke2985
    @mikoajrenke2985 Před rokem +3

    Thanks for sharing this!
    The documentation regarding this Topic is currently so insufficient that I was really struggling...
    With this Tutorial, it all went smoothly!

    • @mariusespejo
      @mariusespejo  Před rokem

      Yeah the typeorm docs are really bad at the moment.. glad the video helps!

  • @mutaqinyusuf2190
    @mutaqinyusuf2190 Před rokem +1

    You are just amazing, straightforward and I barely can understand the points easily!!! Thanksss

  • @eugenematsetska8406
    @eugenematsetska8406 Před měsícem

    after this video things with nestjs and typeorm going much better. THANK YOU MARIUS.

  • @ron-almog
    @ron-almog Před rokem +1

    Yes, it works, finally! thank you for clarifying the tricks of the new version!

  • @Pedro21Torres
    @Pedro21Torres Před rokem

    Glad to see you back, thanks for that video!

  • @Yoshgunn
    @Yoshgunn Před rokem +1

    Definitely would love a Query Builder crash course one day!!

  • @Torres158
    @Torres158 Před rokem +1

    Great video! It helped me understand TypeOrm better.

  • @AliAliOxenFree
    @AliAliOxenFree Před rokem

    this is a very valuable video. all the other migration guides out there are for the outdated typeorm. even chatgpt is stuck on the ormconfig.json method which has been deprecated.
    thanks for the lesson!

  • @vibekdutta6539
    @vibekdutta6539 Před rokem

    I needed that omgggggg. Been watching ur vdos recently and my migrations were failing. Thanks a ton

  • @zackOverflow
    @zackOverflow Před rokem

    WOW!!! you killed it in just 28 minutes, I am a big fan. I love the accent, I love the swag. Everything is FANTASTIC!!! 👍💥

  • @permanar_
    @permanar_ Před rokem

    Finally my favs CZcamsr!! I've been waiting for your new damn videos like every day, week, month 😭😂
    Whatever it is, it's just glad I've seen your channel seems like not getting hacked! Haha.
    Cheers mate!

  • @mateusz6260
    @mateusz6260 Před rokem

    Thank You for come back.🥳🙂

  • @muawallaceewi8410
    @muawallaceewi8410 Před 8 měsíci

    Super amazing clear tutorial, thanks very much for the great job done

  • @emadzaheer
    @emadzaheer Před 11 měsíci

    Flawlessly explained!

  • @gptwisdomtalks
    @gptwisdomtalks Před rokem

    Welcome back , waiting for your microservices tutorials

  • @bayalesko7956
    @bayalesko7956 Před rokem

    Great content and quality is up level. I knew you with this video @Marius Espejo and now wait new videos :) Great job!

  • @shakapaker
    @shakapaker Před rokem +11

    Please don't stop making videos, your way of explaining it's amazing 😻

    • @mariusespejo
      @mariusespejo  Před rokem +1

      Thanks 🙂 I’ve been a little busy lately but plan to get back into it soon!

  • @mouadelhilali2082
    @mouadelhilali2082 Před rokem +1

    Finally !!! LOVE YOU

  • @pierreaillet9249
    @pierreaillet9249 Před 2 měsíci

    Thank you so much man, very helpful

  • @Unifono2012
    @Unifono2012 Před rokem

    Great tutorial, thank you!

  • @vsevolod_zorin
    @vsevolod_zorin Před rokem

    Oh man, thank you so much! Your work was not in vain. I wish you peace and goodness!

  • @davidferreira3926
    @davidferreira3926 Před rokem

    finally i undertand. Thanks bro

  • @user-zr5tj5gu5v
    @user-zr5tj5gu5v Před rokem

    Красавчик!) Хоть где-то нашелся рабочий пример! Перед этим я перерыл уйму статей и форумов.

  • @emmanuelawolusi9343
    @emmanuelawolusi9343 Před rokem

    this helped a lot... thank you

  • @eugenematsetska8406
    @eugenematsetska8406 Před měsícem

    best tutorial about migration for nestjs
    ** in my case was issue that i did use yarn+morepo - better use npm for run migrations

  • @NicolaMazzocato
    @NicolaMazzocato Před rokem

    Thank you for your help! :)

  • @FlavienBesseau
    @FlavienBesseau Před 17 dny

    Thanks man. Even chatgpt was bad to help me with postgres and typeorm

  • @RafaelPascual
    @RafaelPascual Před rokem +3

    Hi Marius, I'm wondering why I can't find you in udemy. Your tutorial is more robust, easier to understand and more useful in real-life situations than any tutorials I found there. Thank you so much for giving us free tutorials.

    • @mariusespejo
      @mariusespejo  Před rokem

      Yes all my stuff is free at the moment on youtube. Thank you for your comment!

  • @veritatas678
    @veritatas678 Před rokem +1

    Cool explanation of typorm.
    could you maybe do a video on platformatic ?
    It recently came out and is basically an ORM that also creates CRUD endpoints + graphql.

  • @phimngantiengthai
    @phimngantiengthai Před rokem

    Worked, thx

  • @beatrizpatricio9892
    @beatrizpatricio9892 Před rokem

    working fine thank you

  • @BRP-Moto-Tips
    @BRP-Moto-Tips Před rokem

    nice. thanks!

  • @user-rv1bx8hx4v
    @user-rv1bx8hx4v Před 4 měsíci

    Thank you!

  • @user-tf9dl4iv7r
    @user-tf9dl4iv7r Před 10 měsíci

    Nice!

  • @artuturu
    @artuturu Před rokem

    Brillant :)

  • @vincenttosinmicheal4640
    @vincenttosinmicheal4640 Před 8 měsíci

    Thanks a lot.

  • @kakelayes1298
    @kakelayes1298 Před 7 měsíci

    good video

  • @sithumdilshan5878
    @sithumdilshan5878 Před rokem

    thank you

  • @user-oi1tt8hc9l
    @user-oi1tt8hc9l Před rokem

    Hi. I prefer the theme you are using! Which theme of editor and icon are you using?

  • @a.anvarbekov
    @a.anvarbekov Před rokem +1

    This was a desired update for the older video! Could you brother make a deployment tutorial as a next step for this, please?

    • @mariusespejo
      @mariusespejo  Před rokem +1

      There’s a lot of ways to deploy a node app but definitely something I’d like to cover at some point

  • @aleksazivkovic2792
    @aleksazivkovic2792 Před rokem

    I would like a deeper dive honestly

  • @heisenbergyt49
    @heisenbergyt49 Před rokem +1

    +1 for QueryBuilder..

  • @asmaakhalidy3775
    @asmaakhalidy3775 Před rokem

    Yes, could you please make us a video about query builder 🙏!

  • @Medivelias
    @Medivelias Před rokem

    Hi Marius, thanks for your tutorial, it helped a lot! I was wondering which masOS terminal you were using?

    • @mariusespejo
      @mariusespejo  Před rokem

      That’s just iterm, but I use things like oh-my-zsh to add basic plugins like syntax highlighting and autocomplete. I’ve also been trying out Warp recently, check it out!

  • @TheBrownBox
    @TheBrownBox Před rokem

    Hi @marius, Thank you so much for the tutorial, but may I ask what font that you're using? :D
    Thank you very much!

    • @mariusespejo
      @mariusespejo  Před rokem +1

      My default font at the moment is cascadia mono, it’s the same as cascadia code but without ligatures. It’s a free font check it out!

    • @TheBrownBox
      @TheBrownBox Před rokem

      @@mariusespejo THank you for your response!

  • @fareedezzedeen8017
    @fareedezzedeen8017 Před rokem

    Thank for your unique contents and your excellent way to transfer knowledge.
    My concern about typeORM is how you trust a library when it's code repository has 1.7K open issues?
    Thank you again

    • @mariusespejo
      @mariusespejo  Před rokem +4

      Any reasonably popular open source project will have a lot of issues unless it’s backed by a good size dedicated team. For example a more modern alternative that I’d recommend also checking out is prisma. And even that has double the issues. Personally I’ve used typeorm in production enough that I think it’s a reasonable solution that works great with Nest. But nothing is perfect, you have to decide what tradeoffs you’re comfortable with.

  • @alexandrecastro9528
    @alexandrecastro9528 Před rokem

    You saved a Brazilian intern!

    • @mariusespejo
      @mariusespejo  Před rokem +1

      Haha great! Hope your internship goes well!

    • @alexandrecastro9528
      @alexandrecastro9528 Před rokem

      @@mariusespejo Man, the migrations part isn't clear on docs. The NestJS docs basically says: Go to see the typeorm docs. And the typeorm docs don't consider the Nest Pattern ( typeorm module). So, it was impossible to instantiate a datasource that both recognized and it was possible to specify to cli the typeorm where it was. I'm really grateful 👏💪🏻

  • @purushothamag3898
    @purushothamag3898 Před rokem

    Very very informative please made video on dataseeder

    • @mariusespejo
      @mariusespejo  Před rokem

      Typeorm doesn’t directly support seeding, you can use migrations to pull it off or a 3rd party tool

  • @muratasarslan2359
    @muratasarslan2359 Před rokem

    Thank you, it's very helpful. github repo for this app?

  • @mdtowhidulislam5260
    @mdtowhidulislam5260 Před rokem

    hey Marius Espejo, Can you please share the way for postgres? It would be really helpful for me.

  • @soroushmadani
    @soroushmadani Před rokem +1

    That was amazing!
    I have a question though, I am using configService in datasourceOptions. I couldn't do the same for configService (new ConfigService(configOptions)).
    What am I supposed to do?

    • @mariusespejo
      @mariusespejo  Před rokem

      ConfigService (or pretty much most things in Nest) is not meant to be instantiated with “new” like that. It’s meant to be injected by nest. I suggest taking a look at the config module docs

  • @okeyshourovroy2769
    @okeyshourovroy2769 Před rokem

    How to do distance based search or geoquery in typeorm?

  • @didierngabouwayo8424
    @didierngabouwayo8424 Před rokem

    nice and simple explanation thanks, can you cover how to generate a custom auto-incrementing primary keys in typeorm such as CUST_0001?

    • @mariusespejo
      @mariusespejo  Před rokem

      I think to pull that off you’d have to use entity listeners which allows you to add @BeforeInsert logic where you can do things like generate your own keys, add custom timestamps etc

    • @luisonio11
      @luisonio11 Před rokem

      You could create a sequence into a migration (remember the revert command into drop method) and request the sequence's next value throw typeorm query API.

  • @masadamsahid
    @masadamsahid Před rokem

    Great tutorial, Marius. Big thanks
    But, how to enable .env variables in the db/data-source.ts?
    Of course we dont want to push our DB configuration to Github repo right?

    • @mariusespejo
      @mariusespejo  Před rokem +1

      Just like how you would in any node app, use process.env to access environment variables. You can also initialize a connection using TypeOrmModule.forRootAsync() which allows you to pass in either a class or a factory, both of which can inject the ConfigService (which parses values from .env, it’s basically dotenv)

    • @masadamsahid
      @masadamsahid Před rokem

      ​@@mariusespejo Oh, yeah. You're right. I think it's because of my own mistake. 😅
      I didn't import the 'dotenv' module properly using the asterisk symbol and 'as' keyword.
      The way I imported 'dotenv' before:
      import dotenv from 'dotenv'; // Not working. Bad ❌
      The right way I should did:
      import * as dotenv from 'dotenv'; // Worked successfully. Good ✅
      I dont know why both of them have different results. The first way is usually works fine in other Node projects, but does not work successfully in a Nestjs project.
      Anyway, big thanks for your tutorials and advices, Marius. 👍

    • @mariusespejo
      @mariusespejo  Před rokem

      Already gave notes above, take a moment to try and figure it out 🙂. If you rely too much on tutorials it’ll just limit your own growth. Take the time to trial & error

  • @kheangchea5689
    @kheangchea5689 Před rokem

    Thank you for your great video ! I have got questions . How can I config DataSourceOption that get value using .env ?

    • @mariusespejo
      @mariusespejo  Před rokem

      For nest specifically you would use the ConfigService for that, which needs to be injected so you need to do .forRootAsync for the typeorm module

  • @radosawhasiuk3478
    @radosawhasiuk3478 Před rokem +1

    Hi, just a question. Why don't you use typeorm-ts-node-commonjs to run migrations on .ts files but instead you build the app and run it on .js files? Is there any reason for that or just preference?

    • @mariusespejo
      @mariusespejo  Před rokem +1

      I did talk about that in the video actually, basically the nest app works off of dist when you’re running it, so if you reference ts files there it will error. The approach you’re suggesting is possible if you don’t share the same data source options between the app and the typeorm cli

    • @radosawhasiuk3478
      @radosawhasiuk3478 Před rokem

      Thanks its clear to me now

  • @jennifferorjuela947
    @jennifferorjuela947 Před rokem

    I'm working with providers for that conexion data getting from configService, but I can make connect to the database. Can anyone help me, pls?

  • @porbaixodospanos8989
    @porbaixodospanos8989 Před rokem

    please don't stop

  • @abolfazljalildoost766

    please also make a *Microservices with Rabbitmq* tutorial , thank you

  • @ICOReviewtoken
    @ICOReviewtoken Před rokem

    Hi bro. I want to setup multi tenancy 1 database and multiple schemas. each schema is generated from a subdomain and is unique. What I have to do ?

  • @moezfazzi8337
    @moezfazzi8337 Před rokem

    thank you for the video please what is the extension of vs code to see the db

    • @mariusespejo
      @mariusespejo  Před rokem

      Sorry, I don’t remember the name of it, I’m sure if you search for database or sqlite viewer it will show up

  • @sarunmrzn
    @sarunmrzn Před rokem +1

    Hey Marius, great vid. I'm configuring migration for my own project and I'm stuck in this one thing.
    I've configured database object with config service, which works great in application, but for the migration cli, i cant seem to find a way to get env vars with config service, do you have idea how?

    • @sarunmrzn
      @sarunmrzn Před rokem +1

      the above doesn't work and the variable are undefined

    • @sarunmrzn
      @sarunmrzn Před rokem +1

      const configService = new ConfigService();
      const dataSource = new DataSource(databaseConfig(configService));
      export default dataSource;

    • @mariusespejo
      @mariusespejo  Před rokem +1

      hey Sarun, I don’t think the ConfigService is designed to run outside of Nest, which migrations are kind of a standalone thing if you think about it. You can install dotenv directly (which is what configservice likely uses under the hood) then just access your env variables via process.env

    • @sarunmrzn
      @sarunmrzn Před rokem +1

      @@mariusespejo Yep, apparently so. Was just wondering if there was a workaround without installing new package. Thank you so much for this!

  • @mehdimahmud5191
    @mehdimahmud5191 Před rokem

    Thank you for your great videos. on nestjs. Any tool available if I want to import all the stored procedures and entities from sql server database?

    • @mariusespejo
      @mariusespejo  Před rokem +1

      There are 3rd party tools like this one:
      www.npmjs.com/package/typeorm-model-generator
      Although other tools have much better first-class support for introspection, e.g. prisma, drizzle-orm, etc

    • @mehdimahmud5191
      @mehdimahmud5191 Před rokem

      @@mariusespejo thank you for the reply. I apretiate it.

  • @carlosemmanuelarcosnovelo7711

    Thanks so much... I just have a question, if we have other databa different than sqlite and go to producction, how can set my password and user in the datasourceOptions. I tried to set my env variables but I got a error, it seems that the env is undefined

    • @mariusespejo
      @mariusespejo  Před 8 měsíci

      I suggest using the ConfigModule with nestjs, they have docs on it. It’s basically like dotenv

  • @aleksanderradziszewski1242

    Hey, thanks a lot. Would you be so nice and make a video on how to set up the configuration for Postgres? Because it's not easy as seems to be.

    • @mariusespejo
      @mariusespejo  Před rokem +2

      The only thing that would be different is you need to install pg
      npm install pg --save
      Then in your datasource config make sure to provide the required connection config
      If you need a reference you can create a sample project like this
      npx typeorm init --name MyProject --database postgres
      Compare you config with what that generates

    • @luisonio11
      @luisonio11 Před rokem

      @@mariusespejo I guess he is talking about use a .env to set the secrets and use it in datasource, that is right?

    • @mdtowhidulislam5260
      @mdtowhidulislam5260 Před rokem

      @@luisonio11 yes i am also facing the same issue with postgres.can you show an example for me?I will help me a lot.

    • @luisonio11
      @luisonio11 Před rokem

      Please let me know if I could help you to solve any doubt :) I also suffered to configure it
      PACKAGE JSON COMMANDS
      "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -d ./ormconfig.ts",
      "windows:migration:create": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli migration:create ./src/databases/migrations/%npm_config_name%",
      "migration:create": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli migration:create ./src/modules/databases/migrations/$npm_config_name",
      "windows:migration:generate": "npm run build && npm run typeorm -- migration:generate ./src/modules/databases/migrations/%npm_config_name%",
      "migration:generate": "npm run build && npm run typeorm -- migration:generate ./src/modules/databases/migrations/$npm_config_name",
      "migration:run": "npm run build && npm run typeorm -- migration:run",
      "migration:revert": "npm run typeorm -- migration:revert",
      "migration:show": "npm run typeorm -- migration:show",
      "schema:sync": "npm run typeorm -- schema:sync",
      "schema:log": "npm run typeorm -- schema:log",
      "schema:drop": "npm run typeorm -- schema:drop"
      ORMCONFIG.TS IN /
      import { ConfigModule } from '@nestjs/config';
      import { DataSource, DataSourceOptions } from 'typeorm';
      import dbConfiguration from './src/common/config/database.config';
      ConfigModule.forRoot({
      isGlobal: true,
      load: [dbConfiguration],
      });
      console.log(dbConfiguration());
      export default new DataSource(dbConfiguration() as DataSourceOptions);
      DATABASE CONFIG JSON
      {
      type: process.env.DB_TYPE,
      host: process.env.DB_HOST,
      port: parseInt(process.env.DB_PORT, 10) || 1433,
      username: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_NAME,
      synchronize: false,
      logging: false /*process.env.NODE_ENV !== 'production'*/,
      entities: ['dist/src/**/*.entity{.ts,.js}'],
      migrations: ['dist/src/modules/databases/migrations/**/*{.ts,.js}'],
      extra: {
      trustServerCertificate: true,
      },
      }

    • @mdtowhidulislam5260
      @mdtowhidulislam5260 Před rokem

      @@luisonio11 in package.json you use windows.migration:...
      is this solution only work for windows?
      I am using ubuntu

  • @weekndninja
    @weekndninja Před rokem

    After execution of script no data is getting inserted to the migration table. Also how is the data getting inserted without any insert uery

  • @EstebanHelgueroCardiff
    @EstebanHelgueroCardiff Před 3 měsíci

    Hi Marius! Question, how do you manage migrations in production? Do you run a GitHub action to apply the migrations to DB or do you have any other way to do It? Thanks

    • @mariusespejo
      @mariusespejo  Před 2 měsíci +1

      I’d suggest having a test or staging DB to test things out first. But then to apply it for production, pretty much yeah you can trigger in whatever pipeline tech you are using, assuming it has access to update your Database. How that is done honestly depends per company though

  • @inanesilence
    @inanesilence Před 10 měsíci

    Migrations register automatically schema of the database or is it necessary to specify it in the query runner?

  • @KhoiNguyen-pm2jg
    @KhoiNguyen-pm2jg Před rokem

    Hello bro, thank you for your tutorial.
    I have a question, how can i create a migration using migration:create?
    I tried add "migration:create": "npm run typeorm -- migration:create", to package.json, but it not work. It throw the message "Unknown argument: d"

    • @mariusespejo
      @mariusespejo  Před rokem

      That means you didn’t provide the path to the datasource file. Take a look at how I have the typeorm script in the video

  • @AbhinavKulshreshtha
    @AbhinavKulshreshtha Před rokem

    What would a good way to make this migration use .sql file for first import, instead of writing commands directly.

    • @mariusespejo
      @mariusespejo  Před rokem

      Don’t think it’s designed to work with sql files directly, just copy your sql into an initial migration file

  • @reymarkandog1441
    @reymarkandog1441 Před rokem

    Which file icon and theme you are using?

    • @mariusespejo
      @mariusespejo  Před rokem +1

      During actual work I usually use Monokai Pro, the icons come from that. However for tutorials I tend to use darker themes, in this one github dark

  • @samrat131
    @samrat131 Před rokem

    hi, how to use process.env.DB_HOST ... others env value in data-source.ts file? because when I use it shows undefined ! please help. thanks

    • @mariusespejo
      @mariusespejo  Před rokem

      You need to use the ConfigModule or you can use dotenv directly to populate those values for you. Basically have that environment variable actually filled and available by the time your app runs

  • @afrosso
    @afrosso Před rokem

    Hi, why you don't use async/await in userService?

    • @mariusespejo
      @mariusespejo  Před rokem

      Unless you actually need to await the response and further process it before returning, there’s no need to. Nest will automatically “unwrap” your promises for you

  • @simpsomk
    @simpsomk Před 9 měsíci

    10:45 why do we need to use create method of repository before saving?

    • @mariusespejo
      @mariusespejo  Před 9 měsíci

      Save only works on actual instances of an entity

  • @TylerJusCodes
    @TylerJusCodes Před rokem

    HI, i'm having issues .. when i want to create a migration , it can't find my entites. until i had to import them into the entites array.
    now i want to run migrations, and it can't find the migrations folder in the dist. but it's there

    • @mariusespejo
      @mariusespejo  Před rokem +1

      Assuming you followed the video exactly, then I’m not sure. Might want to ask in stackoverflow

  • @Cognitoman
    @Cognitoman Před rokem

    Hey quick question. I notice that in nest.js people do validation in the controller with Rules such as checking username to make sure it’s between 3-20 characters long, and then throw errors like in the pipe/controller to the front end with message like “username isn’t long enough”. My question should stuff like be conducted in the entity for validation ? Because that would be considered business rules. Wouldn’t make more sense in the controller/pipe just to check if data coming in looks correct and is of the right type ?

    • @mariusespejo
      @mariusespejo  Před rokem

      The recommended way to do validation is definitely in pipes which technically is “before” your controller, using class-validator annotations in your DTOs (which can be different from your entities)
      I covered that in this video czcams.com/video/2n3xS89TJMI/video.html

    • @Cognitoman
      @Cognitoman Před rokem

      @@mariusespejo yeah I’m super surprised it’s done this way , because it’s business rules which should be in the domain layer . Not in the controller. Not your fault or saying your wrong it just seems like it should be done else where

    • @mariusespejo
      @mariusespejo  Před rokem

      It’s not in the controller.. the rules are defined on the DTO and the validation pipe does the check BEFORE it gets to the controller. You can register pipes on controllers or even globally, but the validation itself does not happen in the controller if you follow the recommendation. I think you’re maybe misunderstanding the flow.

    • @mariusespejo
      @mariusespejo  Před rokem

      Also important to understand that nest is really just a layer above Express, and pipes are really just middleware with a specific purpose. So if you took the same setup and did it in express, it’s like validating at the middleware that runs before your route handler / “controller”. Not sure if that comparison helps

    • @Cognitoman
      @Cognitoman Před rokem

      @@mariusespejo I watched it, yeah the DTO is the data transfer object, what I’m saying is I’m seeing people doing validation on the DTO, like checking a value for very specific things like “the date must be between this day and this day” using class validator, then they throw that value in the database, but what I was saying is that validation check on the DTO should belong on the entity not the DTO because it’s a business rule. Idk it’s just different the way I was taught. Not saying it’s wrong just different it has pros and cons

  • @onrightside
    @onrightside Před rokem

    Migration:generate cmd generated a migration file which contains all the entities. it's a mess. Is there any way to generate separate migration file for each entity ?

    • @mariusespejo
      @mariusespejo  Před rokem +1

      That means you’re generating off of an empty database so that’s kind of expected. If you want it one at a time then make a new entity, generate, migrate…rinse repeat. You also can just split up that giant file yourself

  • @abrutii
    @abrutii Před rokem

    Seems like we can't pass in the config service to the DataSourceOptions object, that's really a pain in a situation where u have many Nest applications...
    e.g. I'm on a project that has libraries and many apps (not following the multirepository architecture of NestJs doc). I have a library that encapsulates TypeOrm logic and the config depends on the APP. I used to pass in configservice, but with data sources this changes everything

    • @mariusespejo
      @mariusespejo  Před rokem

      you can still do TypeOrmModule.forRootAsync(), which allows you to pass in a useClass (provider) or useFactory (function) both of which allow injection. That API is fairly standard to a lot of 3rd party modules, for cases when you need to initialize something async

    • @abrutii
      @abrutii Před rokem

      @@mariusespejo but you can’t pass it to the data source object in the data source file right ?

  • @bombrman1994
    @bombrman1994 Před rokem

    do you have anything related to mongodb/mongoose etc how to use it with nestjs do we use it with typeorm. I am looking for some details and guidance at least minimal

    • @mariusespejo
      @mariusespejo  Před rokem

      Not at the moment. Nest does have docs on using mongo and mongoose, have you already looked at that? Although typeorm does also support mongo, I’m not sure why you’d use that over mongoose.. unless you have a need to connect to both mongo and a relational DB and want to use the same tool for both

    • @bombrman1994
      @bombrman1994 Před rokem

      @@mariusespejo thank you for clarifying it, I was not sure if they are a must to be used together or just some special cases. I just have a regular case where I need one of them. TypeORM was easy I followed you and now I’m digging MongoDB separately trying to get it work.

  • @kietne9251
    @kietne9251 Před rokem +1

    I dont understand why my migration ignores my entity. It does not generate newMigration. Only table "migraions" is generated in my database

  • @rahman9418
    @rahman9418 Před rokem +3

    hi marius do you have udemy online classes for nest js ? are you interested in making it? because it's a good thing for developers, I want to learn it from beginner to intermediate or advance

    • @mariusespejo
      @mariusespejo  Před rokem +1

      I don’t at the moment, you’ll find lots of free nest content in my channel though

    • @rahman9418
      @rahman9418 Před rokem

      @@mariusespejo okay, but if you make it me and other developers might be interested in using nestjs for a real project, and it will be very good for its development, I'm looking forward to it on udemy, nestjs with a real project tutorial example, thank you

  • @dendytbm3113
    @dendytbm3113 Před rokem

    how to do refresh migration? start all over again at once, not revert one by one

    • @mariusespejo
      @mariusespejo  Před rokem

      You can delete the records in the migration table and drop all your tables, or add a drop if exists on your migrations

  • @mustafakendiguzel468
    @mustafakendiguzel468 Před rokem

    I am getting No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command error when try generate,do you know why is that happen?

    • @mariusespejo
      @mariusespejo  Před rokem

      Guessing your cli isn’t finding the entities, so there is no schema to actually compare with the database, which leads to not being able to generate anything

    • @denizsultanoglu6538
      @denizsultanoglu6538 Před rokem

      Kanka çözebildin mi sorunu?

  • @sumitmahajan5730
    @sumitmahajan5730 Před rokem

    Hey we are not able to create migration using nest 9.2.0 and node 16.13.0 can you tell us what is there reason

    • @mariusespejo
      @mariusespejo  Před rokem

      Not sure, did you follow the video? Otherwise please check the release notes of your dependencies

  • @biximi2625
    @biximi2625 Před 7 měsíci

    how do i activate the sqlite viewer in vscode, kindly reqesting for your help

    • @mariusespejo
      @mariusespejo  Před 7 měsíci

      It is just an extension, you can search for it in the extensions list and install the one you like

  • @amerhamed1729
    @amerhamed1729 Před měsícem

    Please Help after update the entities add colum or update name and try to run generate it give me create not alter and when i use migration:run i get sql error this table is already exist

    • @mariusespejo
      @mariusespejo  Před měsícem +1

      Make sure your datasource config is setup properly. Generally how generate command works is it connects to your DB to figure out current schema, and compares it to your local entities to understand the diff. If your generate results in CREATE commands, that likely means you are connecting to a database where the tables were not yet created. Also you are still in full control of the SQL, if generate results in incorrect SQL you should correct it

    • @amerhamed1729
      @amerhamed1729 Před měsícem

      @@mariusespejo i use "paths": {
      "@/*": ["./src/*"]
      } as config in ts config file can be this problem

  • @eclipse-cmd
    @eclipse-cmd Před 11 měsíci

    It would be really kind of you if you can give us a link to the code repository.

    • @mariusespejo
      @mariusespejo  Před 10 měsíci

      Sorry no repo, this was really meant more as a quick lesson rather than a project to clone. If you need a written resource I recommend the docs

    • @eclipse-cmd
      @eclipse-cmd Před 10 měsíci

      @@mariusespejo thank you🙂

  • @anhuc8411
    @anhuc8411 Před rokem

    When I followed and posted I got an error "No metadata for "User" was found."

    • @mariusespejo
      @mariusespejo  Před rokem

      Double check that what you provided for entities in the datasource config matches what I have in the video. You’re likely providing the wrong path or file type, hence it can’t find the metadata for your entity

    • @anhuc8411
      @anhuc8411 Před rokem

      @@mariusespejo I succeeded, thank you very much 😍😍😍

    • @mariusespejo
      @mariusespejo  Před rokem

      Great!

  • @Cognitoman
    @Cognitoman Před rokem

    Dude you need start a discord.

  • @jonathanndambuki6216
    @jonathanndambuki6216 Před rokem +1

    Hello Take us on Query Builder Please.

  • @Raihan004
    @Raihan004 Před rokem

    make one a Query Builder crash course please

  • @lord4791
    @lord4791 Před rokem

    Can you make some nextjs 13 video please? :)

  • @syedkausaralinaqvi2311

    I gave path to my .ts entities instead of "dist"and it did not give any error.