 Hello guys, this is we and today we are going to talk about go postgres and how to connect them Wow, this is one of my favorite topics, you know when it comes to building backends So this is one of the ways you can actually build back end in 2018 and it is way way more exciting. Okay, so Just before I start the tutorial and all The kind of exercises I just want you guys let you know that this is not a beginner level course for go or postgres This is sort of like an intermediate Course or series Where I won't be talking about the beginning aspects of go or postgres But I'm going to talk about how you guys can communicate using go to postgres Means how you guys can build back end where you're writing all of your model logic in go But actually you are pushing data to postgres. So So just mind that if you don't have Enough knowledge about go or postgres, you guys can just research it out You know see other tutorial series and everything and if you think or if you want me to go through that, you know Just let me know and I'll build videos for you about go or postgres. No worry at all. Okay. So what is go? now If you guys reach here, right? Just you know, then it means that you are familiar with the go and all the advantages of the course So I actually don't want to tell you because you already know that but I just want to say that it's amazing It's modern and it's here to stay and it's it's a long long time based, you know Prepared programming language Which is planned really good for the long term staying in the industry If you're learning go, I think you're at the right place and on the right career choice you made And other thing is postgres. Okay. What is postgres now? It's a database, right? It's it's been here like almost forever. I guess and Well, it's again one of the most used open source Database when it comes to SQL in the world It's competing with oracle and MS SQL and all those kind of things but one of the important thing is postgres Provides way way many features, you know that any of the database in the whole world Postgres is SQL database, but it has support for, you know, key value stores It has support for JSON related things, Bison related things I mean, it's amazing to have such text based searching in in built inside. So I think it should be really good But in this tutorial, we are not going to go that advance We are just going to see how we can leverage go and postgres to build simple, you know, models And based on that back end So any language any programming language you use, right? You actually need some way to communicate with the database now You can actually not Write your wire protocols, you know by hand. So for that you have libraries, you know Some people have come, you know contributed Well, fortunately in go, there are a lot of libraries when it comes to postgres Go even has official support for all the SQL right SQL databases Still, there are a few ORMs and everything which are really popular So one of them is go rm which I haven't I'm not going to talk about because it's generalized orm for postgres mysql and mssql and all other kind of Databases, right? So they are actually opting out other features, you know So that I actually don't like if you want to build orm, right? It should perfect and it should be concentrated toward one thing and gopages one of the orm that I like to use on my day-to-day work When I use go and postgres Because it's it is concentrated toward postgres, you know, it doesn't give a Damn about mysql or mssql. Why because well postgres is being used Way more than those databases. I mean mysql is again one of the popular choice of people, you know, when they're building Where they build database backends So this is a pg which is sort of like orm for go lang and postgres, you know, it provides some added performance Uh, so it's really good Features, well, they have all the basic support for when it comes to dot time I mean serializing packets or serializing uh types from go to postgres So, uh You don't need to worry about anything else. They also provide, you know table to struct mapping So if you you can just write stroke and it'll get converted to table automatically So you don't need to like, I don't know manage all those kind of things And uh, yeah, they have other support for extra role. So they have a good support for transaction prepared statements They have a support for notification, which is kind of like unique because uh, let's see how it pans out Actually, uh, we'll see in the future. You will love it That's what I think And there's support for our timeouts and uh, connection pooling is also there And they have on conflict, which is like new feature that came out in postgres for like net port to net port 4 Which is sort of like upset operation, you know, if insert fails You can actually write another query which will run when in the case of insert fails. Yeah So they have a bulk insert update deletes, which is really good And they have a support for migration and sharding which will also be looking into uh In the future. So this is like sort of like a driver and uh, this is how it works and uh, I think we should get started then, you know, without I think wasting your time Hello guys, this is Ouija and uh, in this video, we are going to talk about Getting started with the project, you know, we'll do setup and everything before we do I just want you guys to install a little bit utilitical glide Glide is nothing but a package management for go if you're using go for a long time you made You might be knowing about that. So uh, go has by default go get Stuff, you know, but it's not that much useful when you want to do a good level of project management So I use glide I already had install if you didn't install it's very easy You just have to copy this and uh Paste it on your terminal and it should be good good to go and then you can just do glide in it on your non glide Uh projects and it's it will just scan it out and initialize everything And uh, so with the configuration file is in glide.yaml. I don't think you will have to Maybe ever touch that file again. I think we'll be done command line. So don't worry So it's that easy, you know, and if you whenever you want to install any dependency you just do glide get And the path that's it Okay, so let's get started. So I have glide installed already As you can see I'm just going to clear it out and I'm going to create a new uh order All PG Putes I'll go to PG tuits. Okay. I'm just gonna do Glide in it Okay Now as you can see we have a glide.yaml It just took glide.yaml. Just see nothing right because it tried to scan our directory But couldn't find any, you know go file to scan So that's why he did not scan anything and it didn't put anything in the glide.yaml But just initialization. Okay. Now. Let's just uh Go to here and just instead of go get will do glide uh get with uh this much. Okay So we need this as a dependency In our project, I'll just clear it out. Okay. I'll just do glide get this path And it's downloading now. So what this thing will going to do a glide will going to do It's just going to do go get and it's going to actually store it in glide.yaml So whenever you for example, two guys are working right and you actually do some change and push it Another guy will pull it right and in in that time if you did any dependency, right? It actually you know can download from there On top of that, this is like render dependency. Okay. So these are not like global dependency These are like project specific dependencies. So for example, if you see the render folder, right? You will will see github.com and then go pg and see This will locally uh Install your dependency So it won't affect for example, if you're using one version of go pg in here And if you want to update in some other project, right? Then you can actually do that So that's why it manages separate separate local versions of your dependency of your libraries Okay, so uh, this was uh, so we actually got uh, let's just do win glide and check it out. Oh, sorry Imglide.yaml Now as you can see it's importing, you know, this and version is this so it's very easy Close it. So this is it. We are actually downloaded our go pg dependency and let's now Start the server. Okay. So I have a postgres locally installed. Okay, and uh The other thing is this is pg admin, you know for the simplicity. I'm using pg admin and As you can see I already connected To pg admin. Okay, so I have a true database right now postgres and toots. So I'm just going to okay. So this is like toots Already created a new database. Okay, so we are going to trying to connect with the toots So before we go that I just want to say just uh, you know finish the with the done So I just want to be done with the project setup Uh, so I'm just going to open that folder now. Uh Good learning and then yeah, I do so many learnings. Yeah, so this is our project We opened it Okay, welcome. So I'm using the vs code. Uh, because it's well, it's really good. I guess One of the thing is it's really good and they say it's uh, they have inbuilt terminal and all those kind of things. So I like those Okay, so I'm in here and uh, here is our glided yaml, you know I'll just make the font a little bit smaller. So yeah, that's it. I think it's I am pretty sure you guys can See this. Okay Now, uh, we have a vendor and as you know, we need to create a main dot go file. Okay This is are gonna be our main file. Okay. I'm using vim bindings. Okay. I have in the uh vs code. So it's gonna be like something like that Package And I'm just doing, you know Hello a little bit. Hello world thing Okay Oh, it's funk. Sorry. Sorry for that. That was like some weird mistake I think install some dependency, I guess It's a piece of it. I don't worry about that. Don't focus on that and uh Just close this Yeah, so this is our hello world. Let's just uh compile it and take it out to hard work. So just go here and open the terminal No It's a good terminal and it's here. I'm just going to go build It build successfully. Now we have a pgtude binary. I'm just going to run pgtude Hello world. Well, as you can see So this was our project setup. Okay. So uh after that uh from next tutorial what we are going to do is we are going to create a separate package just for the database related things and we are going to start putting stuff Inside that database Thank you very much guys. Uh, I'll see you next tutorial Hello guys So in this tutorial, we are going to talk about how to connect with the database using a driver call gopg Okay, pretty simple. So the last tutorial what we did we created a structure we created, you know, just a framework we uh You know created this whole glide related. Uh, uh, you can say project So glide is a package manager, which will we are going to use to, you know, install dependencies and keep track of Which version of the dependency we are using and everything, you know, glide will take care of it So if you see here, right? See you can say package inversion, right? So we are just using gopg Nothing else you're using so it just shows that okay So let's get started. So what we are going to do now we are going to create a new package called db. Why that? Well, because we want everything else that we are doing, you know into one package So all the data is related functionality, right? You will be putting in one package. It's that simple You know, you don't need to worry about anything else. Just So what why like this? Well, if you are software developer, you will know that, you know, packaging your Uh, separating your code modules, you know into packages is good If you're multiple developers are working, right? You can work on your db Part and you know, other guy can work on the api part and you will never be Be colliding your code or anything like that So it'll be separated. Yeah, that said, there is no actual reason, you know You can actually write it down the whole db related things in the main.go And it'll work just fine, but the code won't be maintainable. So I would not suggest you to do that Yeah, so, okay, let's create a package. We created a package. We'll just call it We'll create a new file inside that db package will current main.go Now you can call whatever you want if you are You're doing a goal that you might know package tb let's Import our driver for eithog.com slash gopg That's it. We can alias it using here like this Now we are going to create a function called connect She's going to connect to database and we are going to test it out It's that easy nothing fancy or anything now I have a local version running Okay, so let's just Connect it Password, okay, as you can see I was able to connect to the local and if you see list of tables and you know list of table spaces So this is a tool called pgcl I'm using for macOS because I couldn't find psql for macOS. I'm sorry guys This is pretty familiar. If you are familiar with psql, this should work fine for you And this is I think a little bit. I like it because uh You know, uh It gives a really nice autocomplete, you know, so you can just check it out. These are like your table spaces and just by doing D plus you'll be like, okay, give me the database, but I don't have any database. So, okay We got it. Okay. Go back to the code now What we are going to do is we are going to try to connect to that database, okay Forex, sorry for before that we need to create a database So what we are going to do is we are going to, uh, write Vertical create database Hmm Okay, we already have one database called postgres, but uh, no, sorry. We already have a database called tute So we are going to like describe Okay I did not connect through postgres. Yes. I did connect through postgres Hmm. Okay. I think we have a database called, uh, tutes. So we are going to try and to connect to the database. Okay Uh, let's get started then I'm kind of new with that pgcli tool. So forgive me if you know I seem a little bit confused. Don't worry about that. Let's just try and you know, just connect through the go Right the witches are main purpose You know, instead of going through all the oh shit Tools and stuff, yeah One of the thing about go if you are not using the, uh, dependency that you're you embedded it's it'll go away So be careful with that. Okay. Now. I'm just going to create db Is equal to uh, pg. So they're method called pg.connect Okay, so this is it is that easy as you can see all you have to do is pg.connect it'll give of course You'll be like, hey, where do I need to, you know, give host and User import and everything. Okay. So for that, we have something called options So we'll create options pointed to option is going to make db.options Options, okay And inside that they have a user which will be in here pj That's me And then then Password Is going to put regions in because it's a local and you guys cannot do anything about it Another one is address And address is a column separated a column separated, uh, you know, are you are an import so Local hosts five four three two Sounds good So these are like three fields determined that it should be okay Yeah Okay, so it's showing that undefined db. Oh, sorry Should be pg Nice. Now what we are going to do. I'm going to pass on this In here perfect Db declared in node use now what will happen if it'll connect it'll give us a pointer So db is nothing. It's a pointer. Okay If you were to write something like this you can write something like where db of type Pg dot sorry of type pointer pg dot db Is equal to this so you can say this is the type that we are talking about Okay, db declared in node use of course, so this is a pointer It's what will happen in the case of it will fail and throw an empty pointer Which is a nil pointer safe db Is equal to nil means We'll just do log dot print f Okay, it's gonna be something like Fail to connect to Database that's it And you can just do after that os dot Exit With reason. I don't know some number 100 Like a fail to connect. Okay Else. Okay. Else. We don't need to write because it's gonna crash anyway In case if it doesn't, you know Print f I'm gonna be like, okay Connection to database Successful Sounds good. Now you have a db object. What do we want to do? You can do and after doing that What you have to do Is Close it. Okay. So once you are done with the db whole because if you're creating an api ad then It'll take a long time, you know, otherwise you can just close it out. So how you will close so it'll be like Close error is equal to db dot Close This should close it check for the closer if his closer is not equal to nil Hence Output print f There are one close thing the Connection You know printed out that are how you do the percentage three Said reasons. So this is a good way to write the logs Okay, so when you print it will show you Close it You figure it out. What happened wrong and you can just do it or exit you need to Press your program. So after that, oh, you can just say log dot print f and just Wait a minute As you can see, this is our basic. Let's compile and check it out You go in here. Okay Go build It compiled as you can see we have pg2 new binary. Let's run it out Okay, now what went wrong because we created the function, but we never used it right. So I'll just go to main dot go Okay, this is gonna get tricky and I need to get the package. So I'll be like, uh, db Okay On a prefix with db And I lower it after that. I'll be like just calling call db dot. Let's check if it's the right Define db dot. Yeah, why we calls small c. So it's a private function Okay, let's see there is gone. Let's see. Oh, sorry Go build compile successful Get it out Boom as you can see hello world connection to database successful and connection close successfully, which means Yes, we were able to successfully connect to database and Connection after using we actually close the connection Now you will be like, okay, this seems really good But how do I do queries and how do I insert data and you know do all this kind of things, right? Which you need to do when you are building really highly scalable server applications So no worry guys, you know in the next tutorial, we are going to talk about but before that, you know Doing all the query and everything you need to understand the model How you will be able to you know convert the go types into SQL types and all those SQL related PG types to go type, right? Because serialization desilization library already provides, you know, you don't need to do that So next tutorial we are going to I know Space you create some strokes and you know, we'll actually create Table according to those strokes and then we'll see How it works out and everything. Okay guys. See you in next tutorial Hello guys, my name is pj as you know and In this tutorial we are going to talk about model definitions, okay But what is model definition? So for example, if you're using some sort of Language with some sort of database, right? So what you want is you want language specific data types to convert into Postgres or some other database specific data types, right? And you don't want to do that, you know, so for that you have these libraries, you know So and one of the library that we are using right now is gopg that we are discussing and Let's see how gopg this, you know converts all of your go Data types, I'm sorry to postgres data types, okay So when it comes to database, right? So database is like the root inside that you have tables, uh, you know Concept of tables. So those tables are equal to Structs in go or gopg. Okay So you can actually define all of your table as a struct and all of your field as a column of the Table, it's that easy Okay, so this is a simple, uh, you know concept of a product Uh table that I just created, you know in five minutes. So this is just to show you guys, you know, how it, uh Feels like so this is a Model of product that we are going to create. Okay, so let's create a wasting of say Product don't go in the db Oh db Now the greatest, uh struct, uh Uh product type product I was just checking the autocomplete is working. Okay. So now this is is equal to okay So now when you're going to use uh Pg, right? I'm just going to write down some comment how it's going to behave. So When you actually run this, uh gopg migration or gopg create table, right? That we'll be seeing in just maybe next tutorial what it's going to do. It's going to create a table name products So now the name of the table is going to be products Why because one thing you'll do it and actually do uh, you know All the camera, uh, see this kind of capitalized cases said it's going trying to make it in camel cases separated by underscores So if you if you write something like product item, right? So it's going to create a table called product items product items, so So what is happening is it's separating by underscore all the capitalized? Um Uh words that you're writing and it's pluralizing it. Why because the stock represent one item, right? But table will have collection of items, you know, so it's tight. It tries to do that So whenever you create table, just skip in mind that but of course If you don't want that and you want to manually write your name of the table But there is a uh field called table Name, okay, this is a private field. So remember all the exported fields or public fields, you know, who's aware Uh, the name of the field start with the capital, uh, letter are gonna be actually working all the private fields Like these they're just gonna get ignored. So you don't worry about that Struct we can just uh product item How is happening is uh, whenever uh, you know, go busy and try to uh I'll create the table and look for this field if it contains this then now our table name is gonna not gonna be like this This is like the before we're doing but we are already with uh product items collection Okay, now let's create something called id integer. Okay, so this is whenever you are typing id Or id so it's going to track it. It's uh trying to make a primary key out of it So this is gonna be our id and let me just take other fields. Uh name unique You can also like you know change your uh column details also using uh Sql tag Sql I'm gonna change the name of the id. It's gonna be like id. Okay, and you can also do this is primary key manually It's gonna get Unrated primary but you can do manually like this. You can use primary key. Okay. Another thing is This is a good practice in a I'm actually trying to make this thing unique so you can see Works right. So this is how you use the constraints and everything Just try to remember that you need to use sql at column level. Okay, and it is good practice to just give your name here Just Now one another thing I would like to What if you want to change the type In this case type by you know real because if you do floor 64 by default in titan numeric or something like that But I want really not small Number small floating my number Like that. So this is how you can change your post to stripe So this is our whole determined model got just created So whenever we turn the command Call create table right in actually get all the properties and all the details and it'll try to create table out of it Okay, so I think nothing Was this yeah, see we just created a table And I'm just And it is a good practice, you know all the models you create in a separate go file. So as you can see it's gonna be like Expert type Okay, so as you can see this is uh, almost done Uh, so this is how the modeling works. Okay. Now you might want to know what type you're supporting, right? Also Here's the thing Hmm So these are all the types that we are are supporting Uh, when it comes to go pages, okay So these are the go tabs and these are the postgres types So if you see integer 8 you add gonna be small in 16 32 integer and these and that float that it is gonna be real Float 64 is going to be double precision that we actually talked about pool is going to be Boolean since it's redacted Why is it right today? If you are actually embedding another struct or map Or array that's going to be jsonb. Remember, this is really good. You want to try it. Okay Uh, we can actually try different features actually We can just embed another uh, okay We can just give feature name Hmm There a feature uh description is gonna be And I don't know importance, you know how important the feature is gonna be sort of like writing or private uh Our priority is gonna make it into just two, you know, so they should get going to jsonb Or you can what you can do is to give type And be right how hard it is as you can see it's really easy Okay, so this is how we do even if you create a map right map it also If you create it it's gonna be just gonna be nice And the time that you are doing is going to be time spent with time zone and IP addresses we don't have So I think that's it. Okay What if you want to ignore something okay for example, I don't know you you want to use it for example Or for the stock purpose, but you don't want it in the table. So I don't know. Let me just get a name. Okay, something like uh That's point. Okay. Just a point is just a reference somebody. No, nothing else with the database. What we can do is we can actually do like SQL underscore Sorry not underscore Gonna be as good as so what this is going to do is this going to ignore this feed Okay, so whenever the conversion is a conversion is happening from go pg to uh pg or Go to pg, you know, on the side, this will get ignored, you know So you can use it as a destruct normal struck data type in the Whenever you're using the go but when you're using it for for the reference of the database this this won't even create A column in the database so it won't be present This is how you can ignore the field that you Ignore Oh, I think this was it, you know when it comes to modeling So this is like the very basic modeling if you actually want to get into the detail Right, you can actually go to the go pg model definition Wikipedia, you know And you can take it out all the details. I think this is uh, all the hell I guess And this is all it requires, you know, all the all the other things are in the database specific that you will have to Write it in the database, you know, so So, okay, uh, so the next detail guys, we are going to see how to create a table Using go pg, you know, directly without writing any queries like create table these and these columns and everything else Okay, I'll see you then Hello guys so Well in last video we have seen that okay, how to create model definitions and everything Let's create a table out of this model definitions now, right? Okay So i'm just going to uh Write a function to create a table out of it. So i'm just going to be like function uh Items table, okay, this is like my function, so don't worry My treatment error nothing else, okay Now, uh It's very easy to write today create table. Okay, you need a little bit of options. So Although create table related options are stored. I'll just copy product code Okay What I mean another sub module in pg, uh, which contains some options. So first of all, we will create options Required to create a table. They're not required But I think one of the options specifically you should write Just put to be on the same site or m dot create table options One is if Not exist so So this option will tell uh Go pg that only create table if it's not if it doesn't exist, you know, just don't create it if it's already there, right? So what happens is this suppress is one kind error So for example, if you're running your script, right? And it's all automated and you know, you just you don't check before creating and everything So you'll throw an error while creating the table. So this is good if it's already that then we'll just ignore it It won't create it and it won't even throw an error Okay, now just create uh create error with uh Okay, I also need a dv I guess because reference db db of type pg dot db pg important pg How do we create so we will have a database reference. It'll just be like db dot Create table. It's that easy give the model definition, which is uh reference to empty product item object and options that we had Okay But create it okay now. Let's check if Create error is to needle then just log it out printf uh Error while creating table terms These are And our function is ready Like it and okay, so when we connect to the day connect to the db, right? Here will be just like That I don't actually want to check I'll just create products table with the db reference that we had and that's Before closing the connection, okay, let's compile it for that because I already Get there that so I just need to delete it. I guess Just check it out. It's already there delete it first Okay, so nothing is there So Program it's compiled Okay table product items created successfully as you can see let's just go here and check it out Yes, we have product items collection. Let's if you want to see in detail you can just There you go So as you can see this gives all the structure, right? So see id is a big it It's a next value. It's a serial, right as we you know discussed by default And uh primary key id already took it, right and name is a text description is text This see feature is json b as you can see, right? And created this times and the times boolean and everything and this is the unique key that we created, you know So this is how it works, you know, we just create a table out of calling without even writing a single query This is really good What the options right if for example, if you'll run it again, right? It's just saying table product item created Why because it's ignoring the fact but it's not actually creating again and again If you see, right, it's just the same table But if you'll remove this option, okay this This option Let's see how this pans out now date Well, I don't like creating table. Why because relation this already exists as you can see, right? Now you don't want to check this again and again, you know, you just want to ignore it. So Why you just put this So healthy way of doing things, I guess Okay, as you can see, it's working. So guys, this was this is how you create the table. Okay next today We are going to talk about how to insert some data in the table. Boom. Thank you guys Hey guys Let's talk about uh inserting some data now, okay Oh, so you have an object you created an object You have this api that get called and you know you create this object And you want to insert some data into or you want to insert some product items in Do that Okay, so let's just you know, it's it's pretty easy actually. So let's create a function I will just receive product item product items Siver I'm just going to call this save I'll take db your pointer Argument it'll just return So inserting is pretty easy. Okay, so let's just see Well, uh, it's gonna be like insert error db.insert. I mean, it's that easy I'll just pass on reference of my item, which is pi That's it. And if I'll be like if Set error is not equal to me. Oh error while Bring me um to db Also to print some reason Percentage is useful printing out the interfaces by the way might be thinking And Okay, otherwise This is we created the same function. It's going to insert it. Now we need to call this function We just gonna go to main Okay, main will have db. So it's easy. Okay So I might make some changes some changes. So when we connect, right, we should give a reference to db Uh, we should be like pointer to pg dot db return this db. Okay So we're gonna remove a couple of things like uh closing up the connection. We're gonna remove from here But now we won't even spot that about that uh using anymore I'm just going to get the connection try to create the table if it's not exist Okay, now there's a written db So this is a video cannot write your code here because this is stated Are you going to main.co? Okay You created an awesome product Take uh I'll ask for db pointer here because To I don't think we will need uh, we will need this Pinter Now what I'm doing is I'm just going to create a new product here. Okay How do you do that? It's simple new product item Is equal to db dot product Just create this item ID we don't need to give uh, we'll give something like uh Name is gonna be no. Yes. Okay guys, so let's create an object in here. Okay, uh One second, okay, so I'm just going to give a name of the product one to copy all the Name description Description this is a test data This is Description and I had something else I guess Something to do with uh importance Three right Okay Yeah, so that's why you might want to you know if you want to if you don't want to add like a non-struct Create uh, some other sub type or something These are the techniques that you might want to learn, you know, it's gonna be Free That it is time dot how get it Time dot How Do Sure boy, what's it? You created a whole product is see Let's save it now. So we already have db options. So what I'm going to do is I'm going to be like Oh, okay, I mean give this a db db ref because we already have one db even Yeah Now what we're going to do is uh, oh Okay To db ref What's bg db and we have another uh, okay, so what I'm going to do I'm just go product Item dot you created save function instead if you if you remember But for that we need a pointer s you are and it'll ask for uh db ref That's it. It'll be written to me I'm just going to call this here product page db As you can see I think works Let's compile and check it out Well it compiled wow product product when inserted successfully, let's see And this is Select star from What was the name of the table? I always forget Product I'm just going to copy this from Well, as you can see we have our first product Okay, so name is product one description is product description. This is image path price is 4.5 As you can see feature is adjacent with values. I added right right into any encoding decoding It's all done by the pg go pg It's amazing created at a time it took care of you updated took care and This is how you insert Okay, now, what if you actually want like uh You want look to return that whole product, you know, whenever you insert right you want that whole product is written So that also we can do so I just go to here Is the insert save right so You can create some other function something like Sometimes, you know, you might want to have that whole struck return to you, you know updated or something like that So we'll be save and Return okay, let's create a function called save and return little again for db It's kind of digital instead of just return it return it up on with pointed to uh product your product item and in case Okay So how do you do that? It's very simple Okay, so first we need we already have a product that we want to insert here, but we need to create a new Okay, so let's try it out Set error by dot Well, sorry, uh, it's gonna be like db dot by dot returning Okay, and you give give me everything in return then Insert okay So I'll give you result as well as errors. So the Third is search if insert error and log dot printer error Thing and I also might want to know what you got male Because you need to return to write this log dot print have item letter fully I'll do another look. I just want to know what is actually we got right so they saved new value result is percent zero no types That will print it out and then we'll return pi and Let's see. I'm just written by just for the sake of it, you know here But actual main and we'll just Update the product named it too because that's a unique fear. Okay, everything just keep up Everything else should be same doesn't matter. I should have said I'll save should give New product item, sorry updated product The Like this should give Okay, what if we just don't because we don't need it. Let's just remove it. Otherwise, I might have to print it out here again The same thing will happen. Let's come back. Check it out. Oh, yes it's my error Okay, incident successful news. It is this Okay, so we have a reference and we have this so we need to actually get into the result and see what is actually written Okay, so we'll go here Let's just see result dot We'll get model rose affected and rose written right so rose affected is uh Uh, we need to uh, okay, let's check it out Uh, so what was that in that? Here insert results model Or a model dot I think it's gonna throw an error because of primary, right? Yeah Because you see this is how you realize, you know that The thing is get Got inserted or something like that Hmm. Okay, it's a freaking pointer But you got the idea, right? So you can actually get uh, okay, let's see. Uh, the thing Hmm It rose affected Yeah, because there are Yeah, this gets Yes So that might also be integers all the things that they're providing right their pointers, you know So you might have to convert and you know to uh, decasting and stuff like that But you got the idea, right? This is how insert works and other things as a kind of insert is bulk insert Okay, that I thought I should talk about it bulk insert is uh, it's like, you know Inserting multiple items. Okay, product item Say multiple And ask for like, you know, um Items dot dot dot if you know go then this is called. Okay, product items References of product item error See how multiple works Very easy. All you have to do is, you know, wait Insert error dpo we need db also, sorry Like the kind of like pointer to p And this should be pointer not reference Yeah How are you going to do that? Okay, what is going on here with the final parameter? Okay Let's not just use something like this in here Superd here because it's periodic function parameters. Yeah, won't be able to find it out. Okay Looks good. Now I'm going to do db dot Model small is actually when you want to insert. So I'll be like items Insert that's it Well, I think I'll yeah I'll give you some results That will you write number of rows affected and stuff like that. So We actually don't need that thing. Let's see Let's call it if Set error is not equal to nil printf error island starting bulb item Reason being See what is the reason Um, as you can see We did both Oh, what are we gonna do? It's called create a couple of these items the same function. I don't wanna, you know, so much cold I'm just going to write two product. It's gonna be product number five Function name that we give First was deliberate. Uh reference another was comes very early Okay To UPR Got it Yeah, we have db do a db ref Deep all okay. Okay. Okay. Um, no new pi one Function written the way it's written is a little bit Wrong, but that's okay Not uh error while inserting bold items reasons model not fighter I need to give pointers and I'm giving the pointers Okay, let's see what is going on pointer as a pointers I can do sort of this And just now where you are the right so much for where did it functions? Uh, then I love to create, uh total Tom to arrive Uh What do you think what to me and what is going on in here Okay, I mean something like this Yes Got it. Sorry for my misunderstanding with that thing. I think they're asking to give non pointers See You go here. Okay. I think I got it man You need to give you something like you cannot just give array. You need to give something like You know, this is supposed to be Yes, see this is supposed to be you give erratic functions. You cannot give array and I give array my bad I'll just take it out if everything is there the next Hello guys So let's talk about uh Parameter placeholders. Okay You have written any kind of uh Queries before right you might know that you know when you write the query you actually instead of embedding the value We actually embed a placeholder like questions or dollar or something like that and they just give the Uh query after that, right that value after that, you know, so that uh mostly in other drivers It um, you know helps again sql injections and those kind of things it also does in gopg Uh, so let's get started. Okay. So I'm just going to just give you a simple demo If you don't know already what it is So I'm just going to create a small function. Go, you know This older demo Okay, we'll wait for Or We will have some value that we would like to uh get scanned because what what passes it is happening We'll do some query and whatever that query will return, right? We'll actually put it inside this Uh, okay So let's just run the query dv dot query Okay, let's see the signature of this So this query is actually a function that takes model, okay and a particular query And uh, whatever uh input parameters that you're giving and it'll give you result and error result. We don't care They'll give you a number of affected and this error. Yes, uh Select error, okay dv dot query First one is the model model means what model means the data that you actually are expecting to put so dv dot scan So not dv dot It'll actually Uh, so this is where you're actually going to get uh yours when you scan So for example, if you're into your query is written in five columns, right? So actually you want to scan all of the five columns in here So this is where it all comes into the play. So that is for second one is query Uh, so I'll just say query and third one is params. Okay, params. I'll just put like some sort of value 42 for example a query I love to write very Type string is equal to Select As you can see, sorry As you can see I've added question marks. So the question mark here is a place Placeholder is something That you put uh inside the query, okay? And whenever you are actually running in some function, right? You expect that is to be replaced by some particular Value So in this case, what will happen is this query is actually this query, right? I'm just, you know, I just I added it as a variable Now whenever this query will compile, right? So the gop is you will expect this Sorry gop is you will actually try to put this 42 in place of question Okay, so what the query says it written whatever value that you are giving is input, okay? So let's just see, okay first for leaf Like error is not needed to do error management While Very you tell me by printing Okay, and then I'll just return After or Things done, uh, we should just scan This full all square schools can't value It's integer. So I can represent ht and then you write The query is successful. Then we should get 42 as a result and That's it works. Then you to call this function Uh Sorry There you go scan successful scan values for to do To be honest to write how queries works in here, but this is not specific to query. Okay This is just to know that what place owners are Now instead of this for example, you can actually give a positional place owners. Okay, you can just give Zero Also, you actually expect to zero. For example, if you're giving you like 42 32 32 something like that. Oh What happened? You crashed. I guess somehow that doesn't happen In the setting What are you supposed to do? Let's see if it works or not Okay 42 and what if I just replace it one Damn 41 see as you can see so this are called a positional parameter. So, you know, what happens is see Uh, when we uh, see currently the thing is it's very small, right input and output That's why you'll be able to do that But trust me, I've written the queries where you know, I was giving like 20 input as a Place owners and what happens is I used to give like on you know the order So now after that, you know, if you want to remove something right then you have to push everything one step ahead And you know that it gets kind of messy So instead of that what you can do is actually kill, you know, put on this positional parameter So even if you like whatever that you know value that you need to change or something, right? It doesn't affect it. Why because it's don't depend on the order It depends on the position. You can actually give the position. You know, it's that easy Okay, one more better thing the third thing that I would like to talk about is named parameters Okay, so it's better than even positional parameters. It's called. It's okay. I'll just create a rocked, okay Type uh rams Rocked uh, they're um one Ring Chew Ring Sounds good Interesting Now what am I doing is sort of this I'll be like they're on one you see how it's happening, right? Oh, this is like name parameters, but I need to create object of that that I'm so I'll be like they're arms Because I need to supply the rights Rams They're one is gonna be like uh, this is Chew is like This is around Chew But when we return we should be expecting And our value will be straight Okay, now I'm written in this instead of All these things and we just say per arms See this is pretty easy, right? So it should written what this is better one. Yes. Let's see As you can see scan successful scan values. This is better one. Well, I Stake in here to the mean percentage Yes Well scan successful scan value is this is better one. Okay. Now if you think that oh, I don't like this We like better. I'm too boom boom Oh where the crap that thing went sorry guys for the language That's it. See it is that easy, right? You are not actually literally doing anything see so these are like three ways you can do place ordering when you are using go PG library, you know This I find it really interesting. So one of the things I find it so interesting is the name parameters Uh, you all you have to do is just you know create some struck for example If you're writing some sort of big query you just create a struck and put on all the conditions with k value, right? And remember one thing you must uh, sorry I'll just finish that thing off and you can just put that and you know, you can just call it in here I mean, it's that easy. So one of the other thing is again, it's see though. It's not it's not uh, it's again Passing with the camera, you know passing all the capitalized into the camel case, you know Separated matters course and everything. So you have to be like that You don't understand that thing also So this was all about parameter holders. Sorry. Sorry placeholders guys, uh Why I actually had to push this because all the other queries right update or uh, you know, uh Delete or select and all the other kind of queries This is must required because you will have all the bare conditions and everything right and all the conditions mainly we use name parameters So you have some better idea what name parameters that right? So I just thought I should just you know Give a tutorial on this before I move ahead with all the delete and in sorry delete and update and Other kind of queries. So yeah, this was it, you know, try to learn a little bit about it You know, if you have any question, you can just Shoot me a message, you know, I would be love I would love to or more than happy to you know reply you regarding this Thank you very much guys Hey guys Well in this video, we are going to talk about how to delete It's gonna be a pretty tiny video because there are just a couple of phase you can delete stuff, you know So no big deal Okay, so I'm just going to okay now we have product item and everything but you might be thinking that I don't want this product item anymore Or you think that oh, this is I think Put, you know, by mistake or something like that. You want to delete it, right? It's pretty easy create a function Product item and just to just create this, you know delete item method and expect pointed to dot Of course Now delete is again pretty easy. Okay. What do you do? You do db dot Again, first of all, you select the model. The model is what it'll actually put your uh Whatever table item is right row into the It'll load as a civilization thing Then you can do whatever you do. So what we are going to do So if you for example, if you on simple delete right delete by primary key, which is like ID You can just do db dot Delete and you know, you can just give PI but in this case you might need this ID because what this does it will create a query call, you know delete from Able name where ID is equal to this, okay But mostly I don't think that'll be the case if that'll be the case that you are deleting by ID Then you can just do db dot delete and be done with it But yeah, of course, whenever you in this PI object, you need to have your ID set. Okay. Yes But mostly that won't happen. Mostly you will be deleting based on some sort of condition, right? So How do you do that? So simple db dot first of all, you load the model which is uh by dot You'd add the condition where uh Let's say name is equal to placeholder name Okay, and what should be the name? Uh, come on. Give me that. Give me that. Give me that. Okay Now that will get from here only, right? So we don't need to and give and then delete that's it So I told you right last week, uh, It's going to be placeholders, you know name. So what happening? What is happening in here is when you do db dot model, right? So now this model PI is in context, okay? So whatever name placeholders are referring here, right? There we are that we are referring from this PI Uh parameter object, you know, so that's why we don't need to separately here give any structure because internal It'll take data from here and then we just have to run delete query. Okay I'll add again the written result and error. So it's a result. We don't care if there is some Delete error, then we can just check it out. Okay delete error Is not equal to nil, then I'll be just like log dot printf. What I'm gonna do is I'm gonna break Error while deleting Some Reason could be percentage error To return Delete error There's full Person PI dot Got it. Okay. Let's delete it. I'll go to main. I will remove this place and we'll demo a No, create a function delete I'll come Delete item Preq DB ref as pointer We need to create object remember because we are running we are doing everything over that particular object The new PI is equal to Item I just need to give name because we are referring just name only some name. I'm gonna give what Let's delete product five Is that This is what matters and I'm just going to run that thing with DB dot No, no, sorry. You can run on new PI because we already have Item So I think it should be just delete with the DB ref Okay Let's really just go to here and just say delete area. So that was the name Looks good. Delete successful for product five now just Next time Items collection Is sort of complete So if you see that product one product two product three product four and product six, so Product five is gone So this is how really it works. You know, I mean, there is no big deal about it You know, you can just write it down easily and it should work Another thing is just an experimentation Yeah, so You got it, right? It's very easy. You just give the condition and you know after that you just delete you load your model in here So this should work fine So But usually if you see right people don't delete that much, you know, they'll just set some flag is active to false or something like that I mean, I don't know they might want the data But yeah, remember this delete will literally wipe out your data. Okay, whatever data you're deleting It'll just it they're gone forever. So whenever you are using the delete just be careful Rest it out on your staging or development and environment before you actually put it in production Otherwise you might mess up a lot of things That you might not get recovered unless you have backup or something. So this was a delete guys Maybe an extra tutorial. Let's just see how to do updates and stuff Hey guys, look who's back Yeah, okay. Yeah, and let's talk about updates, you know So last video I talked about uh Delete so I'm just really showing you some some of the promising, uh, you know, uh way of doing things Okay, there are a few if you actually go to the documentation, right? There are like way Different different ways of doing this