Objection.js - A SQL ORM - London Node User Group - May 2018

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my school I'm just actually going to swap timer as well I just forgot about that 20:46 okay cool so I'm gonna give a talk today about a RM called objections yes and for those of you we're here last month hello it's me again so no jess has plenty of orm s sequel eyes is one that comes to mind up so using MongoDB using Mongoose and so you know why consider another sometimes a fresh take on an existing challenge can offer something better and so for your consideration I would like to suggest objections yes you can find it at Vince it's /a objection Jeff so github so some of the things that it has as an ORM unless you create models for tables with es6 classes and defined relationships between them you can make queries with nodes async/await style which is really nice you can add validation to your models using json schema you can do eager loading transactions with your models in what with nested documents in rows if you're using Postgres it's got h store feature you can do graph inserts and updates I'll show you a graph inserts later on and you can do some more stuff but let's get cracking okay so the way you install it is you've got to install two dependencies you could install next and objection so next J is I think it's called next it could be called connects I don't know basically is a really old-school battle-hardened sequel builder that's used in a whole bunch of different our rooms so rather than reinvent the wheel object said we're going to use next you can also use it to do database connections pooling connections and managing database schemas through database migrations you'll also need to install a database driver as well for whatever sequel database you want to use and those are some various options so kick off with look at a little bit what next does in terms of managing database schemas so migrations are really good pan for being able to manage a databases schema over time as you introduce new features and as you sort of realize that the first initial attempt at a database schema had some changes that needed to be made later on you've got to evolve the database schema so this is able to do that straight away and so if you just kick off a little script create database what it has is two ideas of applying migrations up rolling them back you climbed down and so the good thing of using this approach is that actually you can keep the version of your database schema and alongside your source code and get so you've actually got an idea of exactly what the database schema was like a particular git commit in your project's history and objects uses next is built-in support so I'll just quickly show you what that looks like so you can just run MPX next migrate make create tasks table what it will go off and do is it will actually go and create a migration in the migrations folder you'll then have a file in there with that name that you supplied and then from there you can see a timestamp in there so you can organize migrations in chronological time order rather than just using a integer count if you've got loads of developers working on a project at same time you don't want to use that patent you want to use time stamps and that's what it does what it shows you in the file is a little bit of something like this and what you can do is you can just plop in some here that just creates a table straightaway so we're going to create tasks a table so we're creating a task so you can say let's have an incrementing ID field that's our primary key let's have a string field for the name the dates the Dubai we're gonna have a boolean flag default it to false straightaway add some timestamps and keeping unique index on the name and then the down one is just literally let's drop the table unless it exists already or if it doesn't and so you've got those two existing and you've got one that creates one that drops down so it's a reverse of your up and then if you want to run them you run it you get your migration run you'll start you'll see at a stable straight away you want to roll it back and then you'll see it's gone and it's actually really good practice to just do this locally often so that you can just double check that everything works exactly as you expect so that when you do in production it's less daunting it's just when you've done many times before some models some models or wrappers around database tables they help you turn capsule Adak that you have within those tables and you can create them with ESS classes so good example here really nice and simple we have you basically get the model from objection you've got a database connection you pass it in detail model to link back through next and then you've got this task class extends model all you've got to do from the start is just tell it what table in the database that model links to and that's it and then exported jets from there you can start to do some math queries so if you just want to select everything the task table that's what you got to do it's just a nice clean simple again same for if you want to do a more typical select query you just start to use a little bit of chaining logic and then that's really nice simple again if you want to do an insert or that's what you've got to do and if you want to delete or do an update in this case you just call patch instead of swinging like update and then if you want to delete it you can do it just like that in terms of relationships objection dress provides a way to define relationships between models that's really powerful and flexible and then relationships allow you to do other things like do eager loading and support objects and Jessa's graph QL plug-in so here's an example so say you've got a toss table and your tasks joins table and a task can have many dependencies and a task can have many dependents and actually it's just a sort of self referential join table what we could have here is for example we've got a separate model for the tasks join there is to find it defines its table name and then what we can supply in this bit and I wonder if I'd there we go yeah that little bit you define a function bit that you just go relational mappings and then you say return an object and your objects you can define dependence and dependencies that point to the model class here so we're just linking it to that model we're defining the nature of the relation so has many relation comes from objection and then on the join we just say go from the tasks ID and then go to this other field inside the table so you just to find the two table points there and then you've got that available straight away and you can also specify relationships through join tables so this is a little bit more of a using that table to then link back to the tasks so you thinkin though okay I want to go from the tasks ID I want to find all of its dependent tasks so we go from this and then we go through a another table and we'd find in the fruit table toss joins a dependency ID to a dependent ID and then back to the tasks ID and then we just do the inverse for the other type of relationship right and my example just totally free this is doing the dishes so say for example I'm using this to manage my life and right now I've got four tasks I've got to do dishes I've got to buy some dishwashing soap by dishcloth and by a good you look little yellow sponge with the rough green stuff on the top and there's my tasks and I realize that I it's not just a you know simple list I've actually got a link some of the relationships between these things so I realize that what I want to do is I want to basically link the do the dishes to buying a dish washing soap by the dishcloth buy a good yellow sponge and then I can then do the dishes anyway so I have this table join I've got the relationship I can then just quickly find out what tasks on that doing the dishes do I need to do or get out before I can do that and so in this career I can literally just go I've got one of the task elements returned just do related query dependent tasks it will walk for the chain it will give me back the tasks and I do have a simple example if I just swap out so look here we go now do I go which code I go to crockery here we go so just quickly punch this way and there we go boom so as you can see in this I'll just make it a little bit bigger because imagine looks tiny there we go so I just ran that query and it's given me back the free things I need to do my dishwashing soap by the dishcloth by your life plunge so I've got I've bent the straightaway back to the presentation and then here we go so then eager loading eager loading you all you've got to do is this you just go give me my dependent tasks and give my dependency tasks if I want to just see the whole graph relationship between my tasks I can again go to here we are this a little bit chop it in quickly through there we go so what you can see here you've got three tasks in the very top tasks that it are its dependent tasks and then the dependency tasks are just the ones in the other three so you can see those two relationships to find their straightaway and then if I now go on to so look so that's eager loading lifecycle functions so lifecycle functions there are a way to trigger functions when a record ends and so updated deleted fetched or validated and they follow a pattern of before the action after the action so these are the ones that exist and you can use them and just to show you how they're used so in this case I actually get it to set some time stamps with JavaScript dates before insert and before update just apply those values to those fields and then I can set that the really cool example I wanted to do that in get time was actually to have a little web app showing some tasks and in the model will do something which would like say update tasks in here you know send a message on to some queue like in Redis that then gets read by something that's actually connected WebSockets that sends a WebSocket back up to the web app that then updates the UI you have all this potential to like use these and really interesting different ways so that's another option you've got there with using this sort of lifecycle functions and the validations so objection allows you to add validation logic to your those models using JSON schema and the validation logic will raise errors which can then be incepted which I catch so here's a good example from one of our internal projects at Stockholm so basically here you say the schema is it's an object for a particular model it requires a particular field called batch ID you then define what properties exist what their types are what formats they need if they read only or not and then that is used when you're trying to create any kind of records or update records to then perform validation logic for you and it's JSON schema so this is actually standard that's out there and if you add JSON schema to your models you get to do some really cool plugins later on so one example I'm gonna show you that isn't to do a JSON schema but it's cool it's graph inserts Graaf inserts are cool just to show you what I mean my example is again managing my life making a nice meal so I will quickly actually I don't I can code with 100 that's fine so here I've got example make a nice meal I've got two dependent tasks on that making that nice meal decide what to eat get recipe and buy the ingredients aren't technically that's free ok we're just gonna link it to one and then let's make the recipe with the ingredients so we've got these two dependent tasks that I need to do before I can have my master no but anyway so quick demo I'm just gonna switch out of this and I'm gonna go back to where are you graph insert here we go so I've got this code nice and ready do I need to make a bigger ok grab that I'm gonna slide it right in here I'll just get that clear boom that's what you run and if I now go back to this database here what you will see is it has credit those three tasks and it's done the tasks drawings for me these are new there these are all here so I didn't have to try and worry about the insertion order of all those things I could let just go here's a graph work how dependencies just insert everything and then it just goes and populates it and that's quite nice that just saves a little bit of time and thinking and then if we go to okay plugins so there are two really cool plugins that are worth checking out one of them is objection graph QL and the other one is objection password so objection password quickly adds local authentication to your application using bcrypt to apply the salting and the hashing setting up is really simple just to make sure you've got a password field in your database table specified number of hashing rounds when encrypting password and then make your user model an extended class and password class so to give an example here so you've got this class user extends password model so rather than just extending model straight away what you want to do is pass model into password password is required from objection password though at the top just specify a number of rounds that you want to put in the more the more round to put in the more longer it will take to sort and hash button theory the more securities so here's an example of just doing like an insert you've got a user name email a password when it then gets ya inserted the passwords just sorted in hash hash there and then when you want to do any querying back to sort of doing your authentication you can just let you do it like this and its really nice and simple and that's just like authentication just handled at least that bit there is many aspects of indication but this is like at least that bit taken care of and then the second plug-in that I'm really really enjoying is objection graph QL so objection graph QL allows you to put a graph QL API on top of your objection J's models with a few lines of code and you will need to have your models using JSON schema in order to get the benefits of this so to show you what I mean I'm gonna go to a quick little demo again another project at star count that we did was actually I'll show the project quickly because it's cool make a page here we are was this no not that this so this is a customer dashboard we built this for CEOs of companies to basically work out what's going on in their business from a very top down sales custom customer segmentations key performance indicators that are used to evaluate they're mainly retail businesses and we basically have this like storing loads data and objections used in this case and as you click on update it will do stuff or not they'll get boom so it's a nice way for them to filter in terms of the code repo there's all these models here there's about 21 models I think off the top of my head there's lots of models but the really interesting thing is I was running a little API for it and then I thought well if I can use graph QL it turns out you can so to use graph QL schema all you've got to do say this is all your models you've got a load graph cure builder here then you require all your models there's nice long list and then you pass them in here now the reason I do the field name and list field name here is because there's actually a pluralization bug in the plugin at the moment it just adds an extra s rather than doing proper pluralization but you can basically specify this and tell it okay these are the different models that I have and then you go click build and we just export that schema here and then to load it in I lift you just had graphic you LS there Express Express graphic you are passing schemas pass and graph QL build up the app load up the options here just specify the endpoints API is up there and straight away I have basically a graph QL API we're just the models so it becomes really simple to then just start doing queries and like getting data and even just seeing what actual fields exist on all of the different schemas and there's lots of schemas and that's really powerful like I literally all I could do is write the models write my business logic pokrovsky on top and I've got an API so that's really cool so that's why I really really was sort of like this is a really good library anyway back to the presentation in terms of performance because they when asked about ORM Segoe what Aram's usually put on a sort of payload legacy to being able to do the fetching and then instantiating all the objects in memory and all that do benchmark it and see what the benchmark impact is like because that is usually a good way to evaluate what the cost of using an ORM is for your Paris projects so to wrap up we basically have objections yes we have necks I'll just quickly go back you from kiss on one takes notes we have the next Jess we have objection password and we also have objection graph cure and one more thing happy to announce now that's official so after two and a half glorious years at star count helping them to become a consumer insights consultancy from the Phoenix origins of a social media startup I'm going to launch my own consultancy called an Phoenix and I'm available for contracting from mid-june and that is it thank you very much [Applause]
Info
Channel: Pusher
Views: 10,018
Rating: undefined out of 5
Keywords: developer language, language code, libraries, help, tips, forum, meet-up, meet up, learn code, coding education, coding hints and tips, front end development, .net, dot net, java script, JS, reactivate, ruby, ember, Node, php, react, react native, functionals, functional, graph ql, sql, python, haskell, lecture, coding lecture, learn about code, learn a developer language
Id: RyQ1MTVjYK8
Channel Id: undefined
Length: 19min 0sec (1140 seconds)
Published: Mon Jun 04 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.