An Intro to Ecto: Using Ecto in Non-Phoenix Elixir Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thank you chin I'll just start with this Chan is like a react wizard and I obviously am NOT him I'm plugging my HDMI cable and if you guys don't get offended easily and like strong opinions you should follow on Twitter because there's some good stuff on there about reactance from time to time so I'm Jeffery Leslie I work here at Planning Center I'm a ruby dev but I have been dabbling in a lick sir and I've really really really enjoyed it have you guys tried that elixir yet who's who's taking a stab at elixir all right yeah Jesse okay one person well today I'm going to talk a little bit about a lick sir and something called ecto have you guys heard of Phoenix it's kind of a buzzword right now Phoenix is kind of like the rails for elixir it's a framework where you can do stuff on the web with a lick sir super fast super cool so what is ecto ecto is a database basically wrapper it's something that makes using a database super easy and elixir straight out of the dot says ecto is a domain-specific language for writing queries and interacting with databases in elixir as boring as you expect from a from a document from documentation so it's kind of like if you guys are familiar with rails it's kind of active record for rails it basically ties your code and the database together I see you into white SQL it will do all that for you it doesn't it specifically tied to a specific database you can see here that it works with Postgres works with MySQL mssql sqlite3 MongoDB there's been a new release within the past couple months they just released a version two and those last three actually don't work with version two yet but they're working on that but the major ones that most likely you'll be using on most of your projects if you're kind of on the open source world is Postgres and MySQL anyway so the cool thing about this is you can switch from one database to the next without really having to rewrite any of your code you just plug in a different adapter and your code will work the same with that new adapter so if I wrote all this in Postgres which I'm going to tonight and we do a little bit live coding and we're going to write it as plugging into a Postgres database but if for some reason we needed to move over to a MySQL database they could do that with just changing basically one word literally what's really cool in fact this afternoon I basically just done a lot of updating and reinstall things on my death machine this week and as I was preparing this afternoon realized that Postgres wasn't working for me locally so I was like oh great this is gonna be horrible sounds like that's right I'll just use MySQL but I got it working some of these Postgres because that's the default all right so here's tonight's goals since a lot of you are familiar with the lick sir and especially since you're not familiar with elixir you won't be familiar with ecto where's you guys some some really we're not gonna aim very high basically so we're gonna I'm just gonna show you how fast and easy it is to get started with a lick sir I'm start with ecto and your elixir project we're gonna set up a project we're gonna get EXO in there and we're gonna do some basic database manipulation with that then demonstrate some of the power of ecto and elixir while we're at it since not a whole lot of people in here are familiar with elixir and also just give you guys a little bit of interest in maybe going out and looking at some of this information on your own to get more info alright so unlike Chan I I'm not really a slide guy he's a slide guy I'm just like hey let's get into the terminal and mix stuff up so to create a new project in a lick sir we use something called mix new or mix new so I'm gonna go ahead and do that one of the cool things about elixir is that it has a lot of really good built-in health from the the made the main source code all the way through different projects there's a good emphasis on making sure that everything is documented well and it has inline documentation on most of stuff and that has really good tools to expose that documentation to you when you need it so for me when I like running new stuff I like to learn how to find stuff out like if I don't know something and then after google it it's you know that's one thing it's sometimes usually write lose me a stack overflow or something but I like knowing how to find the information on my own so I'm gonna show you how to do that real quick so if you type mix help it'll show you everything that it can do mix is kind of like rake for Ruby and it basically just has a suite of tools that you can use to interact with an application and lick your application so you can see here we have a few different things we want to create a new application so let's just look through here see we find a new yeah mix new creates a new elixir project that's kind of what we want to do here so let's do that so mix we don't know how to do it so we can do Nick's help new and that'll give you some of the inline documentation for how to use Nick's new so here it says we gotta create we give it a path name which is going to be our project name unless we specify otherwise and one other thing I want to point out is this su P option it can generate a OTP application skeleton including a supervision tree and again since not a whole lot familiarity with elixir in here basically one of the cool things about elixir is you can spawn off all these child processes and have they're called workers and they can have a supervisor supervising those workers and if one of the workers dies it can supervisor can start another one up in its place so your project is really reliable which is really cool but anyway what we want to do is we want to create an application that uses a supervisor because we want to make sure our database connection if it goes down as we started quickly for us and it's gonna be like it never went away so we're gonna do mix new we're going to create list a to application to do and we're going to give it the sup option okay so just like if you did rails new or something like that it gives you a skeleton an outline of an application so let's go ahead and go into our to do application here it's just a good size for you guys you see it okay cool come on make it just a little bigger because I have the space alright now that we have an application let's start getting ecto in there okay so we have a lick sir let's do echo so I'm gonna go into Emacs here and the first thing we want to open is our mix exs file yes okay so in here this is kind of like a whole bunch of different things in rails put together this is like your your application file where it tells you how to how to start the application it's also like a gem file that you can tell it what dependencies your application is going to need which is really cool so we're gonna go tell it what our dependencies are because we want to be able to use echo in our application so this depp's here it will define our dependencies so that's in here create a couple dependencies the first is one called post grex and that is basically the adapter that marries ecto and Postgres on a machine and we give it a version and then we also want ecto the star of our talk tonight and we want to make sure that we've got at least version 2.0 in there all right from there in this applications here this is where we tell our application what other applications we want it to start and so I mentioned how the supervisor can make sure everything is going correctly well this is telling it hey we want to make sure these things are happening when we run our applications so in here we want to make sure we start up both ecto and post cracks now when I mentioned that if we needed to move to MySQL that's this is one of the places where we would change couple things instead of post Greg so we would use one called Maria X and that's just the MySQL adapter we would just replace post for X with Maria X and then one other place which I'll show you here in a minute the reason I like to do something like this on when I'm wearing something new is I have databases already from past projects and it's data and I like dealing with data and trying to create an application that uses that data and so if I can somehow get into that database and start coding around playing with that then that to me is a win so I love how fast and easy it is an elixir to do that so let's exit out of this actually I'm not going to leave it up I'm going to tab over to our term I don't know why that is and we can look at I mean actually the most unix depths i get this will get all our dependencies pretty quick and then we can do mix compile now elixir is a compiled language which is well the reasons why it's crazy fast though we do need to compile things from time to time and a lot of times when you do something on your own it'll compile it for you but it's that you can see it's pretty quick to compile now if we do mix help again we can see that we've been a couple more options have been included here so now we have these mix echoes so another cool thing about mixed is you can have packages or modules plug into the mix subsystem and give you more options on what you can do within this so we want to and this case create a new repo and a repo in the echo world's Crysta like a repository is basically how your database is connected and that'll make more sense here in a minute I will mention as well that I'm gonna be going a little quick as you can tell this talk was originally written for an hour and a half talked and about 20 minutes so I'm gonna try to compile it all down as fast as I can and skip some things and help you give you a taste of what we can do with with ecto so let's do ecto gin repo and this will give you an error now another cool thing about the way elixir does things is it's built on top of Erlang and Erlang has a whole bunch of crazy complex weird error messages that are sometimes hard to figure out hard to parse and unfortunately elixir has some of those too because a lot of elixir is just piping straight to Erlang and are laying bubbles up those errors and then it presents that to you and you're like I have no idea what that means but a lot of times elixir is nice and this error messages so here it tells you exactly what we need to do this warning could not find repositories for application to do they can avoid this warning by passing the are flag or by setting their prod stories managed by this application and your config slash config the access file and it even gives you the code you want so let's do that that's that's the way we want to do it let's go back to our Emacs and we're going to edit the config dot config so we'll go to config dot config yes okay so in here go back over to I turn this way it says we need to do config our to do application with our ecto repos I'm just going to paste that in actually my paste isn't working there we go and then I'm gonna change this and we can call it our repo anything we want to and this is where we name it so I'm just gonna call it to do that repo that's kind of the standard way of doing it so now that we have that in there we'll ask again mixed to create our repo and it does so you didn't expect Ojin repo it created a couple things for it clay to create our lib to do directory it also created a repo dot exe file which is where we'll define how our repo interacts with our application and then it even gives you this helpful thing don't forget to add your new repo to your supervision tree typically and live slash to du/dx and this what we need to add so let's do that let's go to Lib slash to do X live slash to do DX alright in here under children this is where we define our supervisors or what is being supervised and so I'm going to as it suggested to paste that in there and that's all we need to do in that file we also need to define how our repo of the details of our database on our system and we do that as it says well I created this config file to do repo DX and that's where we need to define our repo so let's go in there so lives left to do slash repo so live to do repo DX and this is where our repo is defined really you don't need to touch this file this file is created by ecto there are some things you could do in this file but for us we're just leave it alone instead we're going to go to back to our config file config that config data yes and you can see we open this file earlier to put in this line and this stuff above it wasn't here this config to do to do repo but mix created that for us so it gives you some helper things so in here we just need to change a couple things to make it work for on our system the first is we want to make sure we are using Postgres which is the default nice we want to use our database I'm just going to call this full stack to do user name is Postgres and hopefully us and password out maybe it's post-crisis fast rate or not let's save that we'll find out if that's the way to do it actually I think it's no password delete that ok save that and now we want to have that database created for us and of course mix has a script or something we can use to do that so we do mix ecto create it'll create that read before so now we have a database in our post local Postgres server running for us and ready for us to interact with so the first thing we need to do is create a file that will basically model what we want to to model since we're making it to do application let's make a to-do item ok so let's go back to a lick sir we're going to edit a new file in this case is going to be to do Lib to do item e^x and here we need to def module to do that item do use ecto schema this is the ecto schema is basically something we're going to use in our file and our module to define how we want our table to look in our item to interact in our application so in this case we're going to say schema and then we put in the name of the table we're going to use for this in this case we'll just use the plural items and here we're going to define the fields that we're going to use so the first field we're going to have is maybe title and that's going to be a string and let's also do completed and that's gonna be a boolean and then it ekta provides this nice thing called time stamps so you don't have to put in that you want specific time stamp columns at all I can create and update it at and insert it at column for you which is really nice so that's all we're gonna do in that file and outside of that we are going to create a migration so we've defined in our application how we want our items to interact with the database because we have that schema but we haven't actually created that table in our database yet that's what we want to do next so we're gonna again we're gonna use mix to help us do that so ecto gin migration and we're gonna name our migration let's just say create items that's what we're gonna do so again it generates view app it created a migration called create items so let's go open that up and we will get start on that so we go to prove repo migrations create items okay so if you're familiar with rails this will look very from the very familiar basically there's a change method and you can this in this case is a function but you can also define a specific up-and-down function if you'd rather but a change method will base function will tell ecto how it needs to change the database from before you ran this migration to after so in this case we're trying to create this table so we're going to say create table items that's what we decided to name it do and we're gonna add a column called title and it's gonna be a string and we're going to add a column called completed and that's a boolean and we're also going to add those time stamps again it provides that in there which is real nice so I'm gonna open up over here on this other side my item and you can see that this looks very familiar so it's almost exactly mirrored except for a couple different changes in wording instead of schema we have create table instead of field we have add but everything else is very very similar in fact it's it's exact which is real nice because you know exactly how you're gonna be interacting with your database so we have that let's migrate our database into that new version echo dot migrates and we have a missing Terminator end okay so let's go do that yes here you are and that will migrate and we have create table items so this is good news on both fronts if we are interacting with our database correctly so we have ecto set up correctly in elixir and we have created that that table in Postgres so from there let's just go straight into using it so I'm running a little low on time some schoolers dive in and show you how to create an item in here so IEX is basically the repple for elixir and we're the - s mix tells that we want to bring in our mix application into this this shell session so we can I want to get a couple things here real quick to make it easier on our typing I'm gonna a least a couple things item so we can look for we can ask our repo give us all the items and in this case it gives you a little debug message says hey you know what I've got a list of zero items to return to you that's good we haven't connected or haven't created any but it's also good because the query was okay so we are getting connectivity to our database we're talking to it it's talking back to us we're getting getting things done that's good so let's insert an item so it repo inserts let's create an item and we'll title it about talked at full stack and there we go we inserted an item into our database so now if we ask it to give us all our items we can see we have this list with our to-do item in there another thing you'll notice is in our schema and in our my raishin we didn't tell it to create a an ID field and i do call him echo does that automatically it knows that you're gonna want an ID field most the time you can override it and tell it not to create one for you you can also override it to tell it what you want it to be named because sometimes if you're gonna happen with old databases or databases from different systems the ID field is not gonna be named ID it's gonna be named something different so you can specify that if you want to but the default is I'm just gonna create an ID field for you because I know you're gonna want it and then we can go on from there but you can tell it you know it gave us a date/time stamp for our inserter that and the updated at - and it gave us an ID so that is in the database there is a whole lot more in depth so that we can go through and cluding change sets which is a really cool thing in ecto but i'm gonna skip that right now a change set is basically it you use it to update items in the database and with it you can track all the changes and so when you pass information through a change set it'll track different changes that you made to the the particular item in this case item is a double meaning because we're changing items and it also can use validation in there and you can also do some data manipulation if you need to like hashing passwords things like that so change sets are really cool one of the things that I like about a lick sir that really turned me off at the beginning was that it's very explicit so I'm used to rails and Ruby where there's a lot of things that aren't explicitly just happen a lot of times in Ruby and rails but an elixir you have to be very explicit with things and so the change set is I was like why do I have to do all this extra typing to just update something in my database but once you realize the power of it and the benefits of it it'll be worth it I guarantee so one thing I want to do before I'm done here is I'm gonna go into an old to-do application that I created and show you a couple different ways you can query the database so you've seen the repo dot all and this particular database has a whole bunch of test information in there when I gave this talk previously we just added a bunch of random information in here just so we can do some fun queries so Jessie over there the only guy who raised his hand when they asked if anyone used a lick sir I'm gonna use him a little bit as as an example I guess because that's some of the data I have in here so we can do some powerful things in EXO in terms of getting information out of it so for example we can say let me put one actually let me import ecto that query repo dot one from i in items so we're going to look for item in our item table and we're gonna alias that to the I or to I give me the select the counts for ID ID item scheme box because I didn't do a lis is's all right I mean alias that is real quick you po save my typing for later let's try it again okay so you can see we've got one hundred and three items right now in the database that we can play with so we can ask it how many of those actually belong to Jesse so we've got in this case we have users and items and items belong to users and we also have something called estimated minutes which will actually let's just show you that so if I do item you can see that we've modified the database in this particular application a little bit more so we have estimated minutes which is an integer we can use to you know figure out how long is something is going to take and we also have a user ID so that provides that association so if we want to find out how many blows belong to Jesse we say repo all from I and item we join use a joint table you and user actually I didn't alias set yet so to do dot user where again the you from username equals Jesse and where u dot ID equals I that user ID we want to select the counts of the items let's see if that works okay so 500 is belong to Jesse that doesn't sound right but let's go with that we can also do things the cool political things about ecto as well is that the queries are composable so you can build them up as you go so let's say our query echo equals in item so you can see that we have it's an ecto query objects not an object but that's what it is and from that we can pass that through to more queries later on down the road and slowly build up our long query if we have and then we need to build up dynamically or programmatically we need to change things based on user input we can have the main portion of our query set and then we're on that later so for example let's let's do one that's real quick and then we'll be done let's say whose let's look at average minutes so repo - all from item this someone be a little bit more explicit item an item join user and user again I have an alias that to do it user on user ID equals item user ID group by item user ID so we're gonna group our things by the user and we're gonna select and the month cool things about select in ecto as you can tell it how you want the data back so in this case I'm giving it I said I'm gonna map of the information but you can also do like a tuple or a list just a straight number like I did on the counts for this case from doing a map so we're going to have our map B we're going to return the user ID of the item that user ID and then to do is we're gonna have a count of those item ID and then we're gonna say the average minutes another cool thing about ecto is again I mentioned that you don't need to jump into SQL you can if you need to but it provides a whole lot of things that gives it the that uses the databases power instead of elixir power so in this case we're gonna just till Postgres to average these columns for us so we just say average item estimated minutes I also see if this works there we go so in this case 123 minutes on average 53 to do is for user ID 3 and then 100 for minutes for 49 - duze for user - and we have these in maps that we defined for it so really that's all we have time for today I'm gonna point you to a couple of resources if you're interested in learning a little more my website is Jeffrey less calm I've got a few different articles on XO up there this was taken from an article I wrote called from zero to echo in ten minutes and it'll basically get you sorry with ecto and then I've got a few others in there that really go into using echo and depth also I gave this talk at a sandal a San Diego elixir meetup so if you guys are interested in learning more about elixir I recommend checking that out we meet in San Diego every month but on github I put up a repo that basically build that application that we took an hour and a half to build and every commit in there is highly commented that you go through each each step of the way that we built this application out so if you guys want to check that out to get a little more info on to what how I jumped from the beginning to actually querying that database then you can check that out so that's all thanks guys Thank You gia does anyone have any questions yes one question let's hold on for the micro quick place back in just a teensy sentence so who uses a lick sir like in the real world yeah they're um right now it's kind of a newer new ish language there are some shops that are starting to do all Phoenix instead of Rails so Dockyard for example was a major one that was highly in rails and they've switched everything over to Phoenix that that's running on elixir there are a lot of telecommunication things that run on Erlang which is what elixir is built on and elixir is really just using the power of Erlang and making it more expressive so if you saw a lot of my code if you're familiar with Ruby a lot of it looks like Ruby and in the way it's it's formatted in the way it's like you got deaf and deaf module and things like that that are very ruby ish and you got do's and blocks and things like that so the language itself lends itself to being expressive like Ruby is and easy to read which is one reason people are moving to it but anyway so like it's built on our line and Erlang is a lot of communicate Ella community telecommunication products in fact curling was built in this 70s I think 60s or 70s it's been running a long time on some of those systems but again I can't I know there's some out there and people ask me so why I shouldn't get a list yeah yeah there's a app I use my phone called football addicts it's European football so it's soccer but their whole back-end is built on elixir they're pretty big in Europe yes elixir Erlang specifically is built to be basically phone systems were built on elixir like Ericsson made Erlang that came out of Ericsson so it was built to handle telecommunication systems so if you think about it like your landline it never really goes down like you need maintenance you can't just shut your landline down there it's pretty pretty good uptime for a landline so they needed to make it highly fault tolerant and fail elegantly and quickly if there's a failure and like I talk about their supervisors so we'll start up new children as needed so that's one thing that's really cool about it and because of that it's very so litters built on that so that's also very fault tolerant it also spins up little processes so it's built basically for modern systems so like Ruby like thread safety is a huge issue in Ruby you've got to really work around making sure that your code you know there's thread safe and even when you do that it's still not very performant whereas elixir is built from the ground up to take advantage of those cores in fact there's multiple different examples out there you can find I've been spinning up like hundreds of thousands of processes and a process doesn't it's a tie to like a CPU process but each a liquor process or Erling process is like a two kilobyte everything the process that it can run and Scott's own system of managing all those and it's really kind of cool how many of those you can do so you can spin off a sink requests and wait for things and it's very incredible the speed that you can do some things that in Ruby would take a long time another cool thing if we wanted to sell you on elixir is it's got pattern matching which you didn't show any at all in this but you can in your functions function definitions match on patterns of parameters or arguments so for example if you have a function name average and you passed in a float you can pattern match you know deaf average when the argument is afloat and then do everything you need to in there but then let's say you passed in some number you made up or some or an integer even you can also make another function define another function deff average with an integer when it's an integer and you can do all those cascading different functions based on what happens when you know you pass different information and it gets even crazier the further you go into it anyway obviously a lot like it looks here the other questions yes yeah so one of the cool things I mentioned about yeah about elixir is the documentation so hex TM is kind of like I know it's worth all the modules are hosted for for elixir but you can go to ecto and then we go to the online documentation and so you can see the documentation is pretty good for just ecto and this is just like the initial page so they're like in fact there was a typo in one of these that I just you know went on github and submitted a pull request and they submitted it they merged it within like what's within the day I think it was even less than 12 hours but they're very thankful and they were like you know shut up you know that and where we come from we put the eyes after the ease or you know whatever they were very very nice there's also a slack group for a liquor that's very active that's also very friendly if you have any issues you can go in there and say hey I'm I just don't know what I'm doing here on this particular thing and they can help you figure out what's going on in fact the creative elixir and Korea Phoenix are both very active in intra forums and even in the slack group I think I've seen them down there yes Bree helpful you know questions well I recommend you guys go check out elixir and ecto cos ecto is a good way to get started on little command-line projects so thanks again
Info
Channel: Full Stack Talks
Views: 8,011
Rating: 5 out of 5
Keywords: iMovie
Id: 8j3DLfNI6Jw
Channel Id: undefined
Length: 30min 46sec (1846 seconds)
Published: Wed Aug 31 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.