Quick note about CouchDB and cURL

CouchDB is a non-relational database (NRDBMS as opposed to RDBMS) that comes from the NoSQL philosophy and written in Erlang. We say that CouchDB is a document oriented database which respects the MapReduce concept. It also provides a RESTFul JSON API (yes, CouchDB uses JavaScript).

CouchDB picture

Here are some notes about how to use CouchDB with cURL.

Installation

$ apt-get install couchdb

CouchDB is running on port 5984.

Now, you can test it:

$ curl http://10.0.0.1:5984/
{"couchdb":"Welcome","version":"0.11.0"}

Create a new database

$ curl -X PUT http://10.0.0.1:5984/newdb 
{"ok":true}

Create a document

Imagine I want to create a blog with some posts. A Post is composed of a title, a body and that's all for the moment.

$ curl -X POST --data '{"title": "My first document", "content": "Hello, World !"}' --header "Content-Type: application/json" http://10.0.0.1:5984/newdb
{
"ok":true,
"id":"496cfdc0ac6b2dfe3f8bf9c10c0001ac",
"rev":"1-e7fe3b4ed3bad8a8a37b6e8594cd64ec"
}

Each document receives an id and a revision, automatically. Now let's retrieve it by using the GET method:

$ curl -X GET http://10.0.0.1:5984/newdb/496cfdc0ac6b2dfe3f8bf9c10c0001ac
{
"_id":"496cfdc0ac6b2dfe3f8bf9c10c0001ac",
"_rev":"1-e7fe3b4ed3bad8a8a37b6e8594cd64ec",
"title":"My first document","content":"Hello, World !"
}

Nice, isn't it ? But imagine you want to set your own identifier, you can't use the POST method but the PUT method works fine.

$ curl -X PUT --data '{"title": "My first document", "content": "Hello, World !"}' --header "Content-Type: application/json" http://10.0.0.1:5984/newdb/my_first_document
{
"ok":true,
"id":"my_first_document",
"rev":"1-e7fe3b4ed3bad8a8a37b6e8594cd64ec"
}

Update a document

To update a document, you have to use the PUT method and to specify the last revision number:

$ curl -X PUT --data '{"title": "My first blog post", "content": "Hello, world !", "_rev": "1-e7fe3b4ed3bad8a8a37b6e8594cd64ec"}' --header "Content-Type: application/json" http://10.0.0.1:5984/newdb/my_first_document
{
"ok":true,
"id":"my_first_document",
"rev":"2-8e34a1f6c8eece9256d51d2359377ee0"
}

The revision number has changed. If you forget it or if it's not the last revision number, you will get the following message:

{"error":"conflict","reason":"Document update conflict."}

Delete a document

To delete a document, you have to use the DELETE method and to specify the last revision number to avoid conflict errors:

$ curl -X DELETE http://10.0.0.1:5984/newdb/my_first_document\?rev\=2-8e34a1f6c8eece9256d51d2359377ee0
{"ok":true,"id":"my_first_document","rev":"3-686bced1bf73cd50a48aefe0956fcaa6"}

If you try to get it, you'll see the following message:

$ curl -X GET http://10.0.0.1:5984/newdb/my_first_document
{"error":"not_found","reason":"deleted"}

If you get this message, you will probably understand that the document isn't really deleted. In fact, you need to use the compact feature that will delete physically all documents deleted by the DELETE method and also all old revisions (it just keeps the last one):

$ curl -X POST http://10.0.0.1:5984/newdb/_compact
{"ok":true}

That's all :-)

Read more »

Yeah ! Github and Jekyll to run our dev blog

We are proud to announce that this is our new official technical web blog ! This blog is running on Github and Jekyll. This is a simple static site generator.

Wow ! Static ? What's the Hell ?!

Yep ! Basically, a blog engine doesn't need to be "dynamic", most of the time, it runs behind a proxy which serves static content from a cache. So we decided to use this way to manage our new blog. A technical blog is a blog designed for our developers, to let them talk about what they want. And this kind of tools is a good toy for them.

How does it work ?

Jekyll is written in ruby language. You don't need to know that but it's for your information. So, you need to create some files and directories :

bazinga_dev_blog
\
 `-- _config.yml
 |
 `-- _layouts
 |
 `-- _posts
 |
 `-- index.html
  • _config.yml should contain something like that:
safe:        false
auto:        false

pygments:    true
markdown:    rdiscount
permalink:   pretty
paginate:    5
  • _layouts contains HTML templates.

  • _posts contains posts in markdown syntax.

  • index.html is the main page of your project.

So if you want to write a post, you just have to write a basic file named year-month-day-my-awesome-post.markdown (can also be textile). To improve it, you can use Pygments which is a Python syntax highlighter. It requires a custom css file that fit your needs and that's all.

What about Github ?

Github is hosting our dev blog. In fact, we use the gh-pages feature. That means our blog is a simple Git repository with few files. When someone want to write a post, he clones the repository, add a new post file and push it. Github will automatically update our pages with jekyll.

But WHY ?

Markdown syntax is probably one of the most readable syntax : it's just text ! No HTML tags, no inline CSS, ... So each file can be read without any style and you separate the content and the style. You know, like the Model View Controller architecture in development.

Behind the scene !

This is our "slogan". I write for the whole dev team, this is our blog, we do what we want and we can talk about very very technical/geek stuffs :-D

See ya !

Read more »

[Web] How do we work ?

Hi,

Today, I would like to talk to you about our common tools at Bazinga. This is pretty simple, we are loving Unix systems and we work a lot with them. I'm using Debian, Ubuntu and a OSX to develop for the iPhone platform.

VIM

Yep, we are using Vim which is a powerful editor. Well, it's a bit hard to use at the beginning but once you started it, you cannot do anything without it. But, Vim couldn't be Vim without its plugins. I'm using Project to organize my files, Snipmate to use snippets (that's a lovely plugin) and a lot of others plugins to get autocompletion and syntax hightlighting for example. Yes, I'm using Vim but I'm not crazy.

If you want to try, I have a Github repository for my vim-config:

  • Autocompletion
  • Syntax hightlighting
  • Snippets (comes from vim-symfony-snipmate)
  • xdebug debugger integration
  • GIT integration
  • reStructuredText syntax + preview in browser
  • Markdown syntax + preview in browser
  • Ready for symfony/Symfony2
  • etc

At Bazinga, we are symfony specialists so this config is made to develop in PHP, symfony 1.x and Symfony2 and Ruby as well because we are also RoR developers. Moreover, you can use Vmail, a Vim interface for Gmail : "Because some people are 1000 times more productive and happy in Vim than in any web browser or GUI program".

Note: We are not sectarians, each developer can use its favorite editor ;-)

GIT

GIT is a Distributed Version Control System and it's our default VCS. We have our own private repository (you can read How to set up Git over HTTP) but we are also Github fans.

ZSH

ZSH is a powerful shell that I started to use few months ago. I love how it present you information:

➜ PropelBundle ±(master) ls                                                   ~/projects/PropelBundle
Command/  DependencyInjection/  Logger/  PropelBundle.php  README  README.markdown  Resources/  Tests/

Here I'm in the ~/projects/PropelBundle directory, this is a Git project and the current branch is master. There is a native autocompletion for command parameters and a lot of others stuffs but the big improvement comes from the oh-my-zsh project, a community-driven framework for managing your zsh configuration. This is awesome and I recommand you to use it. To help you, I also have a Github repository for my zsh-config.

And...

It's over for the common tools but I marked this post as [Web] so I can tell you that we used to use Selenium to test our web apps, Hudson as our Continuous Integration server (not Jenkins for now, stability above all) and Lime (which comes with symfony 1.x) and PHPUnit to write our unit tests. We use Mantis as a bug tracker for our customers but mail alerts allow us to forget it. Finally, when we have to deploy an application we use Capistrano.

Another tool that we love is called : Synergy. It allows you to share a single mouse and keyboard between multiple computers with different operating systems, each with its own display, without special hardware. It's pretty interesting to test under OSX, Windows and Linux from a single mouse and it's also more efficient to use just one mouse and one keyboard in front of you.

That's all folks. These are our common tools in a common web developer day. We don't waste our time with a lot of tools, don't forget : Keep It Simple and Smart !

Read more »