A few weeks ago, I released version 1.3 of Vatican.js, my microframework design to ease the process of writing APIs in Node.js.
My goal with Vatican has always been speed of development. And the main focus on version 1.3 is getting up and running as fast as you can.
With that in mind, what are the basics things that an API would need to have to be usable (we’re talking MVP level of usable):
- Web server: We need something to hit, so the web server is kind of important.
- Resource handlers: Vatican’s focus are REST APIs, so resources are a basic need here.
- A database connection: Because we want to save persist information from the get go
- Resource representation for db: We’re working with an ORM so we need an object representation of the data
That’s about it, if we want to test a concept, having all of that out of the box, would be pretty helpful, don’t you agree?
Back with version 1.2 of Vatican, you’d have the first two out of the box, but the resource handlers wouldn’t do anything. So that wasn’t good enough.
Now with verson 1.3, Vatican will auto-generate as much code as it can for you, so the basic CRUD actions will be there already. Currently the storage engine used is MongoDB, with the Mongoose module but I’m planning on allowing for other options in the future.
How do we do that?
To get there, we’ll need to use the CLI tools that come with Vatican. First we’ll create a new project by doing:
$vatican new api-project
With that command, Vatican just created the project’s folder, a handlers folder inside it, where all the resource handlers will be saved, a package.json file with the basic dependencies needed, and the index.js file, also, with the minimun code needed for the project to start up.
Once this is done, and from within the new folder, we’ll need to create the resources. It helps if you know beforehand how that resource should look like (which attributes it’ll have, and what you’ll want to do with it).
So, lets go with two simple resources for the example:
Book
- Title: string
- ISBN: string
- Author: Reference to Author resource
- Stars: integer
And Author
- First name: string
- Last name: string
- Address: string
The actions will be the following:
- For books: Create a book, list all books and remove a book by it’s id and update it’s author.
- For authors: We’ll want to add new authors and list them up
So with this in mind, the commands we’ll need are the following:
$ vatican g book -a title:string isbn:string author:Author stars:integer -m newBook:post listBooks:get deleteBook:delete addAuthor:put
$ vatican g author -a first_name:string last_name:string address:string -m createAuthor:post listAuthors:get
The lines from above have just created 4 files, the resource handlers inside the handlers folder, and the mongoose schemas inside the schemas folder.
Now just by running npm install
and then node index.js
the API will be up and running on port 8753.
Is that it?
Well, almost, the code for the newBook
,listBooks
,deleteBook
,createAuthor
and listAuthors
method as generated by Vatican, based on the names of the methods, it tried to guess what they did.
The only coding you’ll have to do, is the one for the method addAuthor inside the books resource handler. That will be all up to you.
For more information, please refer to the full documentation at:
Final thoughts
So, what version 1.3 of Vatican is trying to do, is shaving off developing time by auto generating the simple things, it’s not going to guess the full extent of what you’re trying to achieve (not yet anyways!) but it’ll do it’s best to help you get there.
If you liked what you read, please give Vatican a try at http://vaticanjs.info