Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course

The "Building a CLI App" Lesson is part of the full, Hardcore Functional Architecture Patterns in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian explores how to build a CLI blog for creating and viewing blogposts by focusing on the architectural decisions to build the app in a functional way.

Preview
Close

Transcript from the "Building a CLI App" Lesson

[00:00:00]
>> Okay, so we're at the CLI Blog, our first app. It's a blog with a twist. It's a CLI Blog. [LAUGH] So, it's your favorite, it's a graph. [LAUGH] So we have a, these are what we're gonna do, it's kinda like the map of interactions. We have a starting point and we're gonna see if we have any authors we can go straight to menu.

[00:00:22]
If we don't have any authors, we go to Create Author down at the bottom here. And after we create the author, we go back to menu. From menu we can see all the posts, go see the latest posts. Go create more authors. We can go Write, but Write doesn't go back to menu.

[00:00:38]
Write goes up to the latest after its done cuz you just wrote it, you wanna see it here. So this is kind of the map, world map of what we're doing. We'll see how far we get. I really just care about working on the architectural decisions and making this app in a functional way.

[00:00:57]
More so than I do like completing all the little nuts and bolts to make this work. So let's see what we can do in the time allotted. Get back to our VS code. We're gonna start with this little setup here. We have a question and answer interface. And it works like so, we can call getInput.

[00:01:20]
So, and then, I can map over that and be like, whatever x x to UpperCase it's. And so we're just dealing inside a task now but this is my answer to that question, right. So, this will pose the question in the console and then I'll get my answer back.

[00:01:44]
Let's just run that. Just make sure it works console.error console.log. We run it. Still try to get rid of those pop ups, cannot do it. Thanks VS code, all right. It's probably my fault. It's not their fault I'm sure. So go to blog index and so set like now, there you go.

[00:02:07]
Cool, there's the app. We're done. Feel that luck. So this is neat. This kinda just comes with the interface realign we just grabbed it in the task. So what I want to start by doing is looking back at that kinda world map we have, I'd like to just sketch out some of these functions and kinda see what the type of these functions are.

[00:02:31]
And to do so, I just have to write some rough like wishful thinking code. So I'm not gonna go implement everything. I'm just gonna kinda loosely write stuff and then see what we might need as a function model. So start with Start. It's a good place to start [LAUGH] to function.

[00:02:49]
I guess we'll, first, we're gonna check to see if there's any authors, right? So what we can do is, I believe we're gonna bring in a little array based database that I think I've previously prepared. See if this is here, it's in lib, okay. So we have this little database.

[00:03:16]
Actually, let me go take a look at Dbx it exports, save find all and the store. Okay, cool and this just we're working with arrays wrapped in a task. I was, if we had time I would have gone into the strategy pattern or something in here but we are, it's really neither here nor there.

[00:03:38]
So we have save all and that's all we really need. We could use find maybe, but focus on that. So the way this works is I guess we'll say all we have to give it a table name. So let's give it like author table, just authors. So give it the table name.

[00:03:58]
And then that will, I can change over that and get, so this is a task. Actually just map over it right now and get the authors. And we'll say if we have authors, we're gonna go into the menu otherwise, we're gonna go to Create Author. All right now, again, none of this is implemented.

[00:04:25]
It looks like this is another function, some mapping over this function and returning another function, that's a little weird. We'll have to just keep an eye on that. So what does CreateAuthor look like? And let's just get through these maybe three functions and then we'll kinda see what we're dealing with.

[00:04:48]
And as we make more functions, we might have to come back and refactor, see what happens. But CreateAuthor, sketch that out. All right, what would that look like? Well, we'll ask, we'll get the input On, let's say, name. And then when we get the input back, we get, I keep going straight to chain, but I'm not really sure if I need the chain yet.

[00:05:09]
So we get back the author name. Let's go ahead and make a little author type that takes a name. And the interesting thing about that is it's really nothing. It's just going to pick a name, slap it in a type. And the reason I like to do this, in a more typed formal language ,or if we had a class or whatever else we would, it keeps your data types defined so that you're not just willy nilly creating data types all throughout your app as you go.

[00:05:42]
And so like this passenger constructor, even though it's not really doing anything besides putting in an object. Then we'll go ahead and map over that. So we have an author and what do we do with that? I guess now that we've created an author, we probably have to save it in the DB.

[00:05:58]
Okay, so that'll be a chain. Or no, let's just, that's a map there. And this is chain, yeah. So save this in the DB, again in the author table. So save here, we're gonna save the author and then finally we have to map that into, we don't really care that we saved the author as long as it, I imagine this will reject the task if it doesn't save.

[00:06:25]
We could remodel the database in a number of ways, we could have a task either right where the DB connection might be why the task fails, and then it still has a successful task with an error inside it if it didn't save the author. Do some validations, perhaps we will use our validation library to work with the author.

[00:06:43]
But right now I'm just going to say whatever. Let's just sketch these out. I'm gonna say menu is where we'll go from there, right. We have an author and now we're going to menu. And if we look back at our little map, every time we create an author, I go back to menu.

[00:06:59]
There's nowhere else to go, so I'm just going to go right there. Last one, let's go ahead and write menu, it's a fun one. Menu, Let's see, where do you want to go today? MSN? 1991? And we'll, maybe private provide a hint, do we want to do like create author, write, all her latest.

[00:07:33]
All right, cool, because there's some some hints at what we can actually do and once we get that input, we get the route, I suppose. We'll need a little router, so we'll do a router, we'll pass in the route. And that will be, look at this, we're just passing these continuations along, now that I see it, right?

[00:07:57]
So, once I'm passing the router out, let's go ahead and write a little router, and then we'll finish our little sketch, let's put this down here. And one of the things about this is we're defining these functions and JavaScript will not like it if I mention this functions before they're defined.

[00:08:16]
But since they're inside functions here, they don't care of that. I can call router within there but I have to write the router down here, Start, Menu. Well, we don't want to let people go back to the start, so that's not even a possibility. In Menu, createAuthor and we can add more.

[00:08:33]
All right, good times.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now