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👍
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!
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
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 👏👏👏
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.
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
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.
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.
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 :)
Finally my favs UA-camr!! 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!
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!
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.
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?
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
@@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 👏💪🏻
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!
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 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
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
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.
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
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
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?
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
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?
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
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?
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
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
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.
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
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
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?
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)
@@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. 👍
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
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
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
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
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
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
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 ?
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
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"
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
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
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
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
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
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
@@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.
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 ?
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 ua-cam.com/video/2n3xS89TJMI/v-deo.html
@@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
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.
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
@@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
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👍
same happened to me, thanks
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!
Yeah the typeorm docs are really bad at the moment.. glad the video helps!
Please don't stop making videos, your way of explaining it's amazing 😻
Thanks 🙂 I’ve been a little busy lately but plan to get back into it soon!
I have watched many similar tutorials how to do migrations and your is by far the best one. Thanks for the great content.
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
Definitely would love a Query Builder crash course one day!!
after this video things with nestjs and typeorm going much better. THANK YOU MARIUS.
Yes, it works, finally! thank you for clarifying the tricks of the new version!
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 👏👏👏
Thanks man!
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.
Thanks for the feedback 🙏
@@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.
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
You are just amazing, straightforward and I barely can understand the points easily!!! Thanksss
best tutorial about migration for nestjs
** in my case was issue that i did use yarn+morepo - better use npm for run migrations
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.
Thanks! Glad you’re finding the channel useful
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.
Yes all my stuff is free at the moment on youtube. Thank you for your comment!
WOW!!! you killed it in just 28 minutes, I am a big fan. I love the accent, I love the swag. Everything is FANTASTIC!!! 👍💥
I needed that omgggggg. Been watching ur vdos recently and my migrations were failing. Thanks a ton
🙌🙌
Oh man, thank you so much! Your work was not in vain. I wish you peace and goodness!
Thank you Vsevolod! 🙏
Great video! It helped me understand TypeOrm better.
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 :)
You’re welcome! Thanks for stopping by to comment 🙂
Finally my favs UA-camr!! 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!
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!
Glad to see you back, thanks for that video!
Красавчик!) Хоть где-то нашелся рабочий пример! Перед этим я перерыл уйму статей и форумов.
Thanks man. Even chatgpt was bad to help me with postgres and typeorm
Finally !!! LOVE YOU
finally i undertand. Thanks bro
Super amazing clear tutorial, thanks very much for the great job done
Welcome back , waiting for your microservices tutorials
Great content and quality is up level. I knew you with this video @Marius Espejo and now wait new videos :) Great job!
Thank you! 🙏
Thank You for come back.🥳🙂
Thank you so much man, very helpful
Flawlessly explained!
This was a desired update for the older video! Could you brother make a deployment tutorial as a next step for this, please?
There’s a lot of ways to deploy a node app but definitely something I’d like to cover at some point
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.
+1 for QueryBuilder..
Great tutorial, thank you!
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?
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
nice. thanks!
Thank you for your help! :)
Please do a detailed video of the Query Builder
Very very informative please made video on dataseeder
Typeorm doesn’t directly support seeding, you can use migrations to pull it off or a 3rd party tool
Thank you!
I would like a deeper dive honestly
You saved a Brazilian intern!
Haha great! Hope your internship goes well!
@@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 👏💪🏻
Thanks a lot.
Nice!
this helped a lot... thank you
Yes, could you please make us a video about query builder 🙏!
I wanna thank you for your very helpful videos 😊.
Hi. I prefer the theme you are using! Which theme of editor and icon are you using?
Hi Marius, thanks for your tutorial, it helped a lot! I was wondering which masOS terminal you were using?
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!
working fine thank you
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
I don’t at the moment, you’ll find lots of free nest content in my channel though
@@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
U d best
good video
thank you
I dont understand why my migration ignores my entity. It does not generate newMigration. Only table "migraions" is generated in my database
could be an issue with the glob pattern?
please don't stop
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
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.
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
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
Brillant :)
Hi @marius, Thank you so much for the tutorial, but may I ask what font that you're using? :D
Thank you very much!
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!
@@mariusespejo THank you for your response!
After execution of script no data is getting inserted to the migration table. Also how is the data getting inserted without any insert uery
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 ?
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?
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
Kanka çözebildin mi sorunu?
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?
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
Thanks its clear to me now
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?
the above doesn't work and the variable are undefined
const configService = new ConfigService();
const dataSource = new DataSource(databaseConfig(configService));
export default dataSource;
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
@@mariusespejo Yep, apparently so. Was just wondering if there was a workaround without installing new package. Thank you so much for this!
nice and simple explanation thanks, can you cover how to generate a custom auto-incrementing primary keys in typeorm such as CUST_0001?
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
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.
Thank you, it's very helpful. github repo for this app?
Worked, thx
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?
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
@@mariusespejo thank you for the reply. I apretiate it.
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.
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
@@mariusespejo I guess he is talking about use a .env to set the secrets and use it in datasource, that is right?
@@lugadev97 yes i am also facing the same issue with postgres.can you show an example for me?I will help me a lot.
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,
},
}
@@lugadev97 in package.json you use windows.migration:...
is this solution only work for windows?
I am using ubuntu
hey Marius Espejo, Can you please share the way for postgres? It would be really helpful for me.
please also make a *Microservices with Rabbitmq* tutorial , thank you
10:45 why do we need to use create method of repository before saving?
Save only works on actual instances of an entity
What about inmemory sqlite ?
How to do distance based search or geoquery in typeorm?
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?
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)
@@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. 👍
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
Thank you for your great video ! I have got questions . How can I config DataSourceOption that get value using .env ?
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
thank you for the video please what is the extension of vs code to see the db
Sorry, I don’t remember the name of it, I’m sure if you search for database or sqlite viewer it will show up
Migrations register automatically schema of the database or is it necessary to specify it in the query runner?
Not sure I understand your question
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
I suggest using the ConfigModule with nestjs, they have docs on it. It’s basically like dotenv
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?
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
Assuming you followed the video exactly, then I’m not sure. Might want to ask in stackoverflow
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
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
@@mariusespejo but you can’t pass it to the data source object in the data source file right ?
It would be really kind of you if you can give us a link to the code repository.
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
@@mariusespejo thank you🙂
Hi, why you don't use async/await in userService?
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
What would a good way to make this migration use .sql file for first import, instead of writing commands directly.
Don’t think it’s designed to work with sql files directly, just copy your sql into an initial migration file
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 ?
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
Hello Take us on Query Builder Please.
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
Not sure, did you follow the video? Otherwise please check the release notes of your dependencies
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"
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
how do i activate the sqlite viewer in vscode, kindly reqesting for your help
It is just an extension, you can search for it in the extensions list and install the one you like
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
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
how to do refresh migration? start all over again at once, not revert one by one
You can delete the records in the migration table and drop all your tables, or add a drop if exists on your migrations
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
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
@@mariusespejo i use "paths": {
"@/*": ["./src/*"]
} as config in ts config file can be this problem
Which file icon and theme you are using?
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
When I followed and posted I got an error "No metadata for "User" was found."
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
@@mariusespejo I succeeded, thank you very much 😍😍😍
Great!
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
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
@@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.
make one a Query Builder crash course please
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 ?
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 ua-cam.com/video/2n3xS89TJMI/v-deo.html
@@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
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.
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
@@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