 My name is Abhimanyu. The topic that I want to pick up today is building a bookshelf using bookshelf.js. And Ember.js is what I am using as the UI front end kind of the technology. So it is going to be a simple application. It is from an end to end perspective it is going to be you can see a list of books. You click on a book you can see the details of the book and all. You can add a book if you want etc. So let's get started. Cool. So what is the problem with handling data using Node.js today? So if you were using Node.js how would you be typically interacting with database say Mongo or MySQL? So you write the queries right? So it is a massive select blah blah blah blah blah. What do you think is the problem that comes out of writing it in a stringy fight form? Spelling mistakes? Unhappiness. The errors of coding and your business logic gets complicated and your queries get complicated and you require something and you have to mess up the other queries. What else? I mean that is obviously a very big problem. Anything else? It is also very unstructured. Yes. What do you mean? I mean very normally it will be higher unstructured. Yes. Yes. What about problem and list? Yes that is also a problem. Arbitrization. Arbitrization is a problem. So obviously there are a lot of problems that we face and you know you end up feeling something like this like crap loads of work to do and no real way out. Right. So this is what the problem is and in comes what is called as an ORM. So what is an ORM? Yes. Yes. Absolutely. So can you guys give me an example of an ORM? Simple ORMs that we have used in our day to day lives. So we have Hibernate. And just we have liquid SQL for .NET. We have K2HP, core data. Moves. Moves. Yes. But what is there for JavaScript? I mean when I am using Node.js, Node.js is like amazing right? You can write things really fast, dynamically type and all that jazz. Why does not have an ORM for JavaScript? It really makes you feel sad right? This JavaScript just you know it is such a nice thing but we do not have an ORM. No it is not. And hence I introduce you to Bookshell.js. So Bookshell.js is an ORM for JavaScript. Essentially you write it with Node. And it allows you to do anything and everything that a traditional ORM will allow you to do only with JavaScript. Sounds pretty neat. So let's see what ORM really helps us with. So why Bookshell? Apart from Bookshell we also have things like SQLize which have already been there. But there is a reason why I picked Bookshell. The reason was I was looking for a Bookshell application and we found Bookshell.js. The good part about it is it is structured very much like Backboard. How many of you have worked with Backboard.js? How about Ember? Anybody on Ember? So just like in Backboard you have you extend from a model and you put in the properties. You have a similar kind of structure that you use with Bookshell as well. So we will get into the details later. And just like you can just do a model.method name equal to some function and just invoke the method. You can do the same with Bookshell.js. It's promises. So I know Pradeep just came and mentioned that we all hate promises but promises are not bad. And if you don't like promises it also does support the old callback mechanism. So it's up to you whether you want to go down the promises route or the callback route both of them are supported. Dynamic relationships. So you have a table and you have basically two tables and you want them to map to each other by say you know I have one laptop. So this laptop belongs to me. So I need to create this relationship. Ideally if I was using my SQL what would my query look like? It would be a joint. And let's say I want to pick up a post which I want to pull out the comments from a table. I want to pull out the author from a table. I want to pick out tags from another table. That's going to turn out to be a massive massive joint. And writing this massive massive joint in strings is going to be a big headache for me. So if I have something like relationships where I can very very easily define that this guy has many comments. This post belongs to an author. Things like that make my life much easier. Right. And it has custom queries and schemas. So bookshelf is essentially built on top of a library called as next.js. So next.js is a query builder for JavaScript where how many of you from C sharp this background? Yes. So people who work with C sharp would ideally know the I think it's there for JavaScript also you can do a dot something dot something dot something. So instead of actually writing select star from some table where something ordered by something. Next allows you to translate it into something dot select dot where dot this thing and you just put in your parameters and your quality quality inside the where plus right making less mistakes done by them. So if you find yourself a situation where bookshelf is not quite providing the right query that you want. Go ahead and write it yourself. Then we look at custom schemas. So what do you mean by custom schemas. Very simply put I have a node just application. I want everyone to have it. Can I make sure that everyone goes and creates the same tables on their machines and then they can start using my schema. That's stupid. Right. So what can I do. I do a very simple thing. I create my schema using bookshelf. So when somebody runs the application bookshelf will create those tables for you and deploy them on say my SQL or SQL light or Maria SQL depending upon whatever you're using depending on the configuration that you provide. Right. So gradually reducing headaches. I am not taking the headache of creating the tables. I don't worry about what the times my columns are going to be. Is it a varcat 10? Is it a varcat 11? It really doesn't matter because bookshelf will handle that as well for you making your life easier. But it's not. I think it's not supported the schema is already in this time. Yes. Absolutely. So I'm going to demonstrate for existing schema only. So if it gets you thinking right it's kind of solving all of my problems. It seems really neat. So you know let's get started about it. So let's see. So we have the various queries that you can have. Right. So I'm just going to go over the queries before I deep dive into the examples. So you have you can do a fetch you can do a fetch all which is like a get forget all a save you can do an update every simple thing that you can do. And the relationship that you spoke about where you say it can be it can be and has many relationship belongs to relationship or even a belongs to many relationship. So as many would be a one to one belongs to will be a one to one or more like a has one and a belongs to many is an end to end relationship. Right. So sounds quite promising. So let's quickly have a look at the examples. I'll just increase the fun. So over here what I'm doing is I have already created an application. It uses ever jazz. And so since people are not familiar with images I'm going to quickly get into images and get out of it so that everyone is on the same page. Right. So images is an ABC framework like angular jazz or backbone jazz or knock out or whatever. There are certain things which are good about the jazz which is why I really like it. So emerges works on a concept of templates. So you structure your entire application in terms of small small small small templates. So you say this is my template which is going to render a book. This is a template which is an author. This is a template which renders a list of books. Right. So typically when I just try when I don't give a template a name it is the root template. It means the thing that I see when I land on the application. So here if I look at my code it you know basically has a navigation here et cetera et cetera and has something called as an outlet. So in in traditional ASP dot net or master page concept. So this is like a master page template. So the outlet is the container is a placeholder container. So for every page everything will rendered as is and the outlet will be replaced by whatever current template I am on. How do I decide the current template by the route by the URL that I see. Ember works very very closely with you. So if you are on a slash book URL Ember has enough intelligence to know that the template I want to render is going to be called book. I am going to use a book view. I am going to use a book controller. My model is going to be something called as a book and my route is a book route. So it works in a very Ruby like fashion. It can mention over configuration right. You don't think about okay now that I have defined this let me point this guy to that route and this is my controller hence that is my model. No need to do all of that. Ember will figure all of it out by itself. Hence reducing the lines of what you're writing for creating an application. So beyond that we see we just have a lot of the index template and everything and it follows the handlebars syntax. So has anybody used moustache or handlebars. So moustache is a templating engine framework something like that. So basically what moustache allows you to do is if my server side has certain values that I want to put on the UI. One way is that I use jQuery and I said and then I open it and you know stringify the entire to put some values inside it. Hectic task alternatively I use moustache which basically takes a syntax like this. This one say for example and it says you know for each book in the model it very readable kind of a syntax. I want you to iterate over my model and pick out every book and this entire deal will be repeated for every book in the model. If I have 10 books it is just repeat itself for 10 books. I don't need to go and write 10 different deals for me to be doing this right. And then I just bind to every property and I give it certain feeds and values and whatever right. So this is moustache am I going to fast. It's fine. So yeah whatever this is this is the HTML. Now we look at the JavaScript bit of it. So this is the ember bit so we just say ember application create. Now the moment that I write that ember knows that this is an ember application. Right then I go ahead and define my routes. So I said I have a route called as author. Which is basically the path for that will be slash author slash some author ID. Right it can be author slash one two three four. Why do we have a syntax like this. I'll come to that in a minute. Similarly we have books and add books and whatever. So whenever we say my index route is my route. So we saw a template called as index. I have not defined the route as index though. Now I have just I'm defining a index route for it where I want the model to give me a book object. Right so ember is has now wired up everything by itself. So it has it knows that when I'm at the root location and my you are on this route. I will render the index template. Right. I will use the index route and the model for the index route I have defined to be there's nothing called as an index model. I don't have one so I'm saying I just want you to find. When I say find a book since I'm not specified what book I want you to find it will just go ahead and find all books. This particular syntax is what is handled by the ember data. Normally what could you do you would write a Ajax request right. We'll create a massive Ajax and say this is the URL this is something this is something you don't really need to do that because I can just say. Find it I have never defined what store is or what is my URL. So what is what is my URL at this point of time. So ember says if I'm going to find book my URL is slash books. So if I have a service which is which has a method at slash books ember is happy about doesn't really matter. So then I say what is the books route and now I say I want you to find a book by a particular ID. So you see this particular sentence under ID. This is why I had touch and touch in place. So when I want to look at up one particular book I will just find my book. I want all books I just find all books right. And this is the beauty of ember data now works very very seamlessly and very beautifully with you know making your life very easy. Right. So similarly I want to look at an off the same code again. Right. You just get a list of office and similarly for everything you know how you create a book or whatever. Correct me if I'm wrong I haven't played like this year app.js. So you are defining the so in the router not maps. Yeah. And this big note project so I know we also do it in app.js. This is the client side. This is not not yet I've not stepped into load right now. This is a client side. This is the client side. Just one file. And again the client side essentially has two files right now. One is the index dot html the second is app.js. That's all. That's the only part we have covered till now. I'll get to the load bit now. You can see I have two app.js. This is right. That one is the load. Like when you enter URL so that you are just responding the node app.js. And so what is happening here is that I'm not letting node control my routes. So node is basically a server layer for just attending to myself. So I'm not using Jade or I'm using node responses to render the view. No basically acts as a so we see kind of a layer for me. The entire UI routing logic is handled by Ember. Because frankly Ember does more elegantly. This route will be used by the hash. Yes. That way all the requirements that we have actually make the requirements to the same page. We are not going to use the server. So this is the actual single page application. Yes. Similar yes absolutely similar. They solve purposes. I think it's called fake routing or something fake URLs or something like that. There is an actual library that is there which allows you to create the hash slash URL kind of a mapping. You can do you can just pull it up and create your own routes if you want. I'm getting the name. I think it's fake routing or pseudo routing or something like that. This has become more popular when you start a single page application. Yeah. It's only one HTML file. One HTML file basically has all of the templates. And you really don't need to worry about creating 15 files and your stuff loading everywhere. No need to worry about that. This also can do tracking in stream. Yes. Exactly. I mean it's a single page. If you're going back in areas of normalization, you will get back the previous page. But there is only one page. Yes. Yes. So that's the good part of it. So the philosophy being that you can literally take the URL and give it to somebody and the state of the page is maintained. So the state doesn't change when you are you know out of context of a browser put it into a different browser. The state really doesn't change. Yeah. So this was the UI bit of it. The UI is handling the views and everything. And what does my model look like? So this is my model. My model basically has it as the book has a title and author and image summary price. So again, we see a belongs to here. Right. And this is ember data's way of handling relationships. So when I have belongs to an ember. What ember does is it will call book not recall the book service get the books from the author field to take the author ID and call the author service to get the author. Right. So it will not make an embedded call. It will make two distinct calls. The advantage is that it will now catch the authors. But if I have five books written by the same author, I will never get the payload worth for the five authors. I will just get the author once and for every subsequent call, the author is now cashed for response. So on a large scale, it helps you reduce a lot of calls. Right. And over here I have the author which has a first name, last name and a list of books. So obviously author has many books. And this thing is called as a computer property. Where I say the full name of the author is a dependent of the first name and the last name. And how do I calculate the full name? I say the full name is first name space last name. That's it. So I don't bind the first name and last name. I just bind the full name. Obviously there are better examples to demonstrate this. But again, for the sake of simplicity, this is what I'm thinking. So this is the model that I've used on my client side application. If I was using backbone, it would be similar. If I was using Angular, I think it would still be similar. Or any MVC framework, my model would still look extremely similar to what it is now. Right. So let's hop on to the app.js node. So over here, this is the part where I have defined next. So I say next is basically my SQL client. The connection is this. The user is this. The database I'm using is bookshelf. And I define bookshelf as bookshelf, which takes next as a barometer, since bookshelf requires next to be built. So I define my configuration with next, allowing me now query using next. And bookshelf just wraps up next and gives me the values that I expected to give me. Right. And then I have definitely the routes that, you know, we have defined. So I will basically be covering these many. So it's a fetch fetch all and save on books and authors. But we'll just go around the pool stuff. So let's look at book.js. Right. It has the list. It has the get and it has the add method. They will eventually return us whatever we want. Similarly, the author will also have the same thing. Right. You have the list of the get and the add methods. Right. Now let's get cracking. So I define author as and with this line, essentially I have created an author object, which is type of a bookshelf. Obviously now I need to point it to a table name because I don't know what table name author is going to point to. So what I can do is very simply say, that's it. So now I have something called as an author, which now points to a table name called as authors. Right. If I look at the get the list call that I might have. I'll basically have a new author dot fetch all. And then this is a promise to fit. I just return authors as is. Right. So what does my database look like? Well, So this is my author's table where it just has first name and last name. Now there is obviously a very deleted problem here where I have first name as first underscore name. And my last is last underscore name, which means the response that I now take is going to give me something which looks like a first underscore name under last underscore name. Whereas when we saw emojis, it had camel casing, right? Not snake casing. So there are two ways you handle things like this either you say my UI will handle it and you put it in the UI logic. But since you're not talking about bookshelf, we'll try and handle it in bookshelf. So one good thing that comes from using surface bookshelf was bundled with underscore as well. And where you have underscore, you can use underscore dot reduce to basically take your case and converted to camel case and vice versa. Right. So there are two methods that bookshelf defines. They are called the parse and the format. The responsibilities of these two methods are essentially to convert the database response to your service response or your model and the other way around. Right. So things I've already written around. So this is a model that you can find. Right. So you say that you're going to have a format method where the format is just going to underscore your values and the parse is going to snake case your values. Right. So I'm using inflection here. It's very commonly used nowadays for all of this manipulation that we use to have a case saying this code and etc. They have very similar function signatures. Now the reason why I'm binding it to the bookshelf model or extend is that I wanted to apply to all of my models. So I say by default, this will apply to all of my models. And now instead of my author inheriting from bookshelf model, it will just inherit from that model. And with that one change now my first underscore name and last underscore name will automatically start coming to me as first name and last name. But there is still one thing that we are missing. Right. We have not bound the books attribute because every author has a list of books and hence I will introduce you to the relationships. So I will say books is basically a function and now we can actually say that a book has an author has many books. So what exactly is this book that we are talking about is the same thing that we will create here. So we'll actually say book is format model extend and the table name is books. Right. So now we say we have somebody called as a book which points to a table called as books. We have author pointing to a table called as authors and books essentially has the author has something called as books, which is basically a collection of books. But when we look at these equal database, you notice that the table really does not have anything called as books. Right. Which is a problem. It's supposed to have something called as books. I really don't need to because my books table has the author idea. So now I come to a point where I need to somehow find a way to join these two to get a response where I want all books. All books written by this particular author. Right. How would you normally do it? What would be your SQL query? Select books from that particular table where author ID could be. I want the author as well. Right. So this is a classic join. Yes, that is true. Yes, there are so many ways of doing it. So let's see how bookshelf prescribes we do this. So I'm looking at the get request for author. The reason I'm not writing it right now is because it will take time. So I say that obviously the request that I get, I extract the author ID from it. And then I say dot fetch. What it's going to do is it's going to fetch. What do I fetch? I want you to fetch all related books. Let's say fetch with related books. So if I have four relationships, I will just say fetch with related books, tags, comments, blah, blah, blah, whatever. It does not matter to me. Right. And it just go ahead and give me everything. Right. So how do I get the relation bit? So I just say the author, which is written by the promise dot related books. And it will give me the related books as an array. Right. And over here what I'm doing is just mapping and putting the IDs into one feed so that I can send the IDs to emerges. So this is just a mapping bit. Essentially what I wanted to show here is how the relationships are handled. And when we spend some, there are like three ways of querying the database for get me this where this and this or something like that. I don't really have to care about that. Whether it's a has many, whether it's yes. Sorry. Yes. If you wanted to, depending upon how you write the fetch, if you don't want to just remove the ventilated, but it will just fetch the author for you. But yeah. So is there is no way to say eager and is this something like that? This is eager. He can do it. Yes. But there's a problem with this. I mean, I think is that it's a, if I have 40 hours, so it will be 40 and then what is it? For the books. Yes. No, it's just one, still one query, right? It's not querying the books separately. But actually how that it is going? That. Yes. Yes, it is. So, so it will. It will. It will. Yes. I do not know. I'm not sure. I can tell you how many it will make. So by default, it's supposed to be the loading. Right. It will make 41. It's the context of this particular. It depends where I am here. So that is what it's doing, right? It is picking up an author ID and calling with respect to an author ID. I'm not doing a fetch all. So if I do a fetch all, that's the fetch all call. So then it will not pull up all the. So fetch all for authors will give you one query, which is a fetch all of books. So let's say I need to fetch all. Right. When I get an author's list. Right. As it also has the books. No. Right. So. Over here what I've. Yeah, sorry. You can do it with related again, but then it will make those many number of cards. But then you would anyways make them right when you do a joint. If you were doing it in. What I would do, you know, in one, and I say seven star from other, it will simply fire a star from others, you know, and then they'll do it to join in everything. Yes. So that is one thing over here. It will get just one author and get another call to get one. So essentially it probably was done. I'm not that good with databases. But what do you think does it does it work out with the same or join? Is it too fast? This is going to give me a hard time. Let's say if I am pulling up a thousand records or something. Right. So. It would depend upon what you want. I might be able to click on one order and I would like to see. Yes. So when you click on that author and then you make another get for only for that. You can. So in this case, let's assume that you are seeing the details of a book. The book required. No need for you to have the details of the books. The authors have has published. So the model is there. No way to say, you know, use another. As in. The definition. So here. So here. So here. So here. So here. So here. So here. So here. So here. So here. So here. So here. Okay, I think it should be working. When you say and. This is the summary page. Is it a high post of book down or something. So, in this case it is one to me, let us take an example of employee and department. So, I would like to display employee and department name. So, in that case it has to be there, there it is a one to one. One to one would not suffice. One to me. Yeah. So, this is also. It is a starting point. You want to start the same way all with the department. Yeah. No, but let us say if I would like to display just the name and the department name. Employee name and the department name. Yeah, then you use the employee model for displaying all the employees. Correct. And if you get for the employee, you get the department name. So, when you see the details of one particular author, then only will you be expected to know about this. So, when I look at this, it is just the author names, right? It just has this first name is the last name of the author with a list of books. If I go to the details of the books, I will still see the book list, nothing related to the author. Only when I go into the authors will it now want to get the books written by the author. Right. So, it's not that it's now querying the book's database at this point of time. Right. Yeah. Yeah. This is not embedded by the way. This is Bookshed. So, Ember handles the UI side of it. So, the reason why Ember is also useful at that point of time is because Ember usually allows you to not get an embedded data. It would always, always want you to get a side loaded relationships. Right. So, you will have a response that gives you IDs. It will query with those IDs and get other responses. So, in most cases, you really don't need to worry about, okay, how is this going to happen? So, the reason why I am actually casting it into the ID type of format is because that is what Ember data will support straight out of the box. If we go on to go into the details that you basically will have to override certain stuff in Ember and this really doesn't have to change. Right. That's again Ember data's advantages. We are looking at this. The other thing is that when we look at the with related with, right. So, over here, this is going to fire a very, very simple with related query where it's going to say, okay, when I want you to find all books with this particular author. So, I have not specified the field name or anything as such. So, what this guy is going to do is it is going to singularize the motel table name. So, when I say with related books, it is going to look for something called as book underscore ID. Which means, sorry, for author, it will look for author underscore ID in the table called as books. So, my default will look for books dot author underscore ID. Now, that is a problem for me because what if I don't have something called as author underscore. Right. So, I need a way to specify that instead of going to author underscore ID to say something called as author. Right. And that is something you can just define over here. So, the relationship allows you to specify a table and a foreign key. So, I want to say that I have many books and I wanted to refer this particular column. So, looking at this thing, I have the field called as author. It's not called author ID. Right. So, I will. So, what I will add to this and now it will start looking for this dot author. Right. As in that, you know, one is written by three authors. Okay. So, we have a, in the database, we maintain this kind of structure. You know, we have author one, author two, author three. Okay. Most of the books are authored by one. It's the one of a book. Okay. But there is a scenario. Right. Now, I have to mention that this can only be done all those columns. That author one, author two, author three. Okay. Yes. Right. So, in that case, what you do is since it's a. So, one thing is that you can use a belongs to many instead of doing a has many the second alternative is since it's a very specific case, what you can do is with related can actually take a query. So, with related can is also something which will work with next. You can actually put up a function here, which can then say, if this is my code and I want you to carry where author one is this or this or this or this. So, you can specify the kind of constraint you want to write with a for a key. So, that I have just to say, match author ID. Do I also want to match a type? Do I also want to match the country? Then you specify those particular context inside a custom query that you write with the withdrawal. But again, you'll be using not a string based format, but more of a what what next gives. So, you will have a function which gives you a query object and the query object dot where will give you the. And next and the bookshelf both give you this a very nice way of where they say where in and where unless and all that stuff. So, they wrap where with the not equal to greater than kind of a functionality. We just specify the column name and the value and they just handle the rest by themselves. Okay. Here in this scenario, we have not mentioned that, you know, what are the columns we require from book table? For example, in book, you know, we do have a PDF format of that book or maybe the image, cover image of that book. Yes. Yes. And why query author? I just want to need books, not the PDF version of blog or blog. Okay. So, select it. Selective columns probably. Not try that so I do not know. So, basically here in this scenario, we will go and get all the columns where are equal to this. There is a way and be sure, but I don't know. Sorry. So, the one thing that I'm. Bookshelf.js also gives you is the JSON methods that you can use. So, you have the dot to JSON, which will basically take your, take the response that you get from the database, convert it into JSON. Now, while it is doing a JSON, what it is essentially doing is that it is going to call a set on your model. Right. It is going to take a value set from the model. So, things like author dot related books, they, you don't get anything called as author dot books because that is not a part of the database response. That is something you have custom loaded as a relationship. So, that comes in the form of a function where the function can resolve dot related books and return something to you. So, the dot to JSON actually does flattening of these relationship models as well. Plus, what it will do in addition to this is that since I am inheriting from a format model, it will also put my camel case in place. So, the database although has returned to me in snake casing. This two JSON will essentially just convert it to camel casing. If even if I don't write all of it, just this bit, RAS or JSON will also do the same thing because it will actually try and flatten the entire string out and while it does try to do that, it will flatten all of these functions and relationships into a flat model side by side. So, although this is unnecessary, but you can see here that I have not really done a two JSON or a stringy fire or anything. So, it really gives you this like you can do a JSON or stringy fire or two JSON if you want to be simple. So, basically a lot of helper methods which you know make. So, I mean if you talk about camel case and snake case where you know it is helping us mapping both of them. Assuming that you know a developer created a module, printed module like FNN1LA which is definitely not at all the same. So, it is helping somewhere or can define it. Yes, you can pretty well define it where we put in the this thing. So, when you have the pass or the format, let's just say you have put down everything is first letter underscore something. So, it has to be either a particular syntax. Now, let's just say there is no particular syntax. This is very specific, but you want to say okay fine my guy has put author to read something like this. You have FNN1LA. So, what you can do here is over here when you are defining author. What you do is you define the pass and the format in author itself, right? Take the specific values and then just say F name is actually first name and L name is actually L name. Model specific. Yes. Model specific of like camel is man. Absolutely. Absolutely. Yeah. So, it the customization can happen either here or you can have it customized at the front end. Depending upon whatever you want to have. Right. And similarly, what we do is we will have a book basically having one author which was there has a field and he was but I want to actually get the author response as a part of the book as well. So, again, there are two ways either I just get or I say that this book has an author which belongs to an author table. So, it has a very similar syntax. So, I say return this dot belongs to author. And this means that I have something called as an author which belongs to the table author. This is different from getting the ID. It's not about the same thing. So, it's about getting the actual author response as well. So, if you have the UI which understands embedded service responses where I have books, author has an object inside it which has another object maybe inside it. You can easily map it using something like this. If you don't have it, not a problem. Just use existing model. Right. So, let's quickly have a look at. So, let's quickly have a look at the lists. Absolutely similar implementation as that for nothing different at all. What I can get get does nothing different. So, it's not getting the author now. Right. So, there is no with related author call that is happening at this point of time. I'm essentially just getting whatever is there in there. Right. And similarly, when I say new book, I pass it an ID. Whatever request parameters I have. Right. So, let's put this in action. Look at the app. Let's hope this works. So, the books are what was stored there. The values over here that means 150 only is something which Ember is parsing for you. So, the value stored in the database if you have a look is essentially 100 to 150, 150, etc. Right. So, Ember is basically converting everything that it thinks is called a price. And it's putting a RS in front of it and only after it. Right. This is not the first name space last name. This is the full name of the author. So, you just have a look at it. Right. So, let's just say I click on this particular book and this opens this particular page where I have. So, till now I have not really gone and asked for a lot of authors, but now let's just say I want the books that Agatha Christie has written. So, I tap on more from Agatha Christie. And now I have made two calls to the server which gives me the two books. Right. So, right now I was just dealing with the book IDs. I'm not really looking at what books she has written. But when I go into it, now I can say, okay, these are the books that she has written. Right. So, this all of this is a very salient feature of Ember. The fact that I really don't have to specify a model and essentially the only place that I've specified the first name and last name is at the database level and then at my UI level and the entire service layer doesn't really care about first name, last name, title, anything of that sort. It's because both of them are not talking at a very similar model logic. Right. They understand similar variables. They understand similar names. And it is now, now I would have to go ahead and write queries like your select star from where first name is this. It really doesn't have to be that way. Can you elaborate on the sentence that you said? It just shows the value and the currency rupees and only that Ember takes care of it. Yes. So, Ember works with something called as handlebars. Okay. So, handlebars is, as I mentioned, it's a templating engine. So what you can do in handlebars is you can specify a helper. So what the helper does is the helper takes a value and it can modify that value. It's essentially like a computed property that we saw the full name property was. So what it does is that it will take a value and you can say, okay, fine, whatever value I have, I want to say rupees space value space only and return this value. So for every price object it gets, if it is translated into p space, price space only. So if instead of 100, I got nan, it should be nan only. Right? So it is like custom handlebars. Yes. It is just doing a string format. So the partners. Yes. So Angular also uses handlebars only. So we define this 150th price and we send it to the handlebar and get these return with a hundred rupees and only as the return value. Yes. So we don't say if it is a price. So we say that I want you to take the book dot price and pass it through a helper. Yes. Right? So you just say is, so we have a lot of helpers that we saw it. There was a link of each helper. So you can just define a hash price and pass it book dot price and it will take the price from the book and it has converted into rupees something only. And we can test mine. Yes. Definitely. These are dollars. Yeah, dollars also. That's not a problem. Right? Yeah. Can you show the network request and the JSON response? So this is the list of book that was loaded. And every book has an offer and depending upon the author, since I need to evaluate the first name in the last name of the author, I get a first name in the last name. The books is just returning me an address. There is no value for the books. Right? Just the service course. Yeah. So. So first you get the book. Then again for each author, for each author for every book. Yes. But notice that I have. So the book's response has seven, seven, eight. Yeah. And the author's call are one, two, four, five, three and six. That's lesser than eight. So it's not that I'm calling the author for every book. If a book has multiple authors, sorry, if multiple books have one author, I will not make the call again because embedded data is smart enough to know that with author IDX, this is the author. Let me cash and keep. Now keep an eye on this now. Right? So we have got the authors. We have got books. Sorry. Yeah, yeah. Sorry. Yeah, no problem. So that means by default, it's by IDX somewhere which is specified in the model. No, we are not specified by embedded handles by itself. More reason why I love it. So I clicked on the book details of a book. No calls have been made. Not a single call has been made. So I got everything not making a single extra call. Now let's look at the details call. Since just one book has been. So the summary page has handled all of my college. Now I am not making any more calls. So Emma data is handling all of this data for me. And what's even more neat is that if you look at the, so this is a plugin for Chrome which gives you, it shows you the ember data. So I tap on ember. It says that I have eight books and six authors. There's cashed everything for me. Right. And I look at the books that are there and I can actually see what are the values that ember has gotten them for me. How does it catch? It is the Chrome's cash. Yes. So what is even better about ember is and I'm going more and more into it is that it will also catch the templates that you were using. So once you have pretty much rendered a template, all it needs to do is recompile the same template. So it will not always get the template and then compile it again and again. It will just put in those values again. So if you are familiar with j template or handlebars, there is a pre compile option and there is a compile option. The compile option will compile the entire template and the pre compile will basically compile stuff that it can without putting in the values. So it will essentially just store everything it can. So does ember data. So no calls. What's going on? So is it true that if I render the index page and my index page is around 100 books, so it will catch the data for all 100 books and no further network also being made. So if you load them in the same view, if it's a scrolling view, then as and when you scroll the books come, they get cashed. They get stored. Easy to do. Yes. And then what is the issue? How will that solve all this kind of the data we should catch in the browser? As soon as we go to the board, it will probably get a lot of life frames. So, Well, it depends when you want to invalidate the session. Ideally, if you have a session concept, you're going to invalidate everything that is a part of the session. But for something like this, if you have a hundred objects, then you probably have to disable the cash at certain part of time or have a lazy loading mechanism which brings certain elements from the cache and puts in certain elements based on some amount of logic. So is there a way to invalidate the cache by an element? Because on my server? Possible. Yes. Yes. Ember, I think it allows you to invalidate parts of the cache as well. So it can allow you to discard the cache for a certain route. No, no, no. I mean, let's say that my book price has been changed or something, that added has been changed. Not for this kind of static. Oh, okay. So let's say I'm listing out employees and at the same time, somebody Wow. from somewhere to change the name or something then, you know, when I'm So for that, you use something of a real time database. Let's take, for example, Firebase. So Ember has an integration Firebase called as Ember Fire, which basically taps into the Firebase APIs and gives you promises. Right. So when as soon as Firebase updates itself, it updates the Ember data model. And since Ember data got updated, it updates Ember model and updates the UI. So this is what is called as a three-way data binding. It was first introduced by Angular Fire as an integration with Angular. JS and Firebase. And then it came to Ember as well. It's called Ember Fire. So, yes, but the good part is that if you don't Yes. Yes. Yes, it will still allow you to at least navigate. Yeah. Since it has since also a single page application, all your JavaScripts and everything CSS files have already been loaded once. So no subsequent calls for them will be made. So also can I encrypt the Ember data? Possibly, yes. So how this is going to do as Ember data I see. So how Ember data works is it has multiple layers. So you obviously have the view controller, etc. So the controller from the controller to the service, it goes through two people. One is the adapter. One is the serializer. Yes. So you can configure the serializer to do whatever you want with the data. So like for example, when we over here in the responses, we saw that. So over in the responses, we saw that there was a books call followed by author, author, author, author. So what you can ideally do is if your service returns an important response, you can actually take it in the serializer, convert it into a side loaded response. So that Ember doesn't by itself make the second call. So you can configure all of these things in the serializer. And the good part or better part about the serializer is that the serializer can be configured per router. So you can say my posts want to be serialized like this. My books want to be serialized like this. My authors want to be serialized like this. So while it gives you a very plain and simple way of handling things, it also gives you the power of customizing whatever you want. Right. So while we're going low on time, I guess. So just one last bit that is left is to look at the add method. So I'm sorry. So what happens is the book shall gives you a force method where you can basically create an object on the fly. So whatever response you get from it, we can actually say forge title is this image is this image, whatever, and just save it. And with that one commit, you can save whatever is that you have gotten from Ember. Now, the reason why I'm actually passing via a forge and not assigning it directly because I want to demonstrate the forge method. Alternatively, I can just take this which pretty much has the same model as Ember and the service, and I can pass it to the safe. I don't really need to go through the forge method, but the forge is a very important method. It allows you to create things on the fly without really having to worry about what is the thing that you are creating. A couple of more good things about Bookshelf is that it allows you to have constructors. So just like I think Pradeep mentioned with regards to TypeScript, which you have a constructor. What you can do with this thing is that you have a constructor which specifies default values. So you can say why default, my first name will be empty and my last name will be empty. And then you can just pass it. The second good thing, which I really admire is that suppose when I'm calling the safe method, I don't want to do an insert, but I want to do an update. So the safe method ideally is an insert or update. So it will ideally always look for whether this is something new or this is something new. How do I define that? If I call the safe method, if I pass it an ID parameter, look for that ID and update everything that I have passed to it. So if I just want to pass one value, it will just go ahead and take that one volley. And no, it will not really require the rest of the values to be present. So I really don't need to think about two other methods that I have to write. Okay, fine. This is an update. Let me go ahead and update it. I really don't need to worry about that. All I do is a safe method with just a different parameter and it turns into an update method. Right. Cool. I think I have covered it. Right. Yes. Power sync format. Yes, I have supported it for Bookshelf. There are a lot of things that Bookshelf covers. The intention of this talk was not to cover all of them, but to basically give everyone an insight so that you guys can take a stab at it. And try it out in your own ways because from what my experience has been with this particular thing is that there are multiple ways of doing it. You don't really have to stick to say just need to do a fetch like this and that is the only right way of doing it. You can do it in multiple ways and every way has certain benefit or every way has certain drawbacks. The biggest advantage it provides out of the box is that it makes your code much more readable, probably even much more maintainable and you make less errors. The moment I remove strings from my code and I start dealing with the real methods, the chances that I'm going to make an error reduce trust. Because then node will start throwing exceptions at me. So yeah, that's it. So I think it supports only three added elements because the remaining all are not even written. Yes. So it currently supports Maria, SQLite and MySQL. Postgres. Postgres, yes. So SQLite databases. I know a lot of people don't use SQL but still it's a good thing. So there are relationships. Yes. So it has dynamic relationships. It has transactions. It has eGrooting. Anything that you can think of every ORM provides, it does provide all of that. So it's pretty cool. Awesome. Thank you.