ลองเล่น CouchDB

Overview

รับส่งเป็น HTTP POST/GET/ PUT/DELETE (CRUD ตามลำดับ) ไปและได้ค่าเป็น JSON กลับมา

ข้อดีคือไม่ต้องลง Driver เพราะทำงานกับ HTTP สินะ

Installation

On ubuntu,

$ sudo apt-get install couchdb

Start CouchDB

$ sudo -i -u couchdb couchdb

Verify Install

$ curl http://127.0.0.1:5984/
{"couchdb":"Welcome","version":"1.0.1"}

Create Database

$ curl -X PUT http://localhost:5984/dbName
{"ok":true}

Show Databases

$ curl -X GET http://localhost:5984/_all_dbs
["_users","dbName"]

Get Database

$ curl -X GET http://localhost:5984/dbName
{"db_name":"dbName","doc_count":0,"doc_del_count":0,"update_seq":0,"purge_seq":0,"compact_running":false,"disk_size":79,"instance_start_time":"1447076562317587","disk_format_version":5,"committed_update_seq":0}

Drop Database

$ curl -X DELETE http://127.0.0.1:5984/dbName
{"ok":true}

Insert Document

curl -X PUT http://127.0.0.1:5984/dbName/6e1295ed6c29495e54cc05947f18c8af -d '{"test1":"test11","test2":"test22"}'
{"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"1-3ff5b3730635ea11c6c75647ba6176cb"}

Everything in CouchDB has an address, a URI, and you use the different HTTP methods to operate on these URIs

Ask for uuid

curl -X GET http://127.0.0.1:5984/_uuids

Ask for more than one uuid

curl -X GET http://127.0.0.1:5984/_uuids?count=10

Get Document

$ curl -X GET http://127.0.0.1:5984/dbName/6e1295ed6c29495e54cc05947f18c8af
{"_id":"6e1295ed6c29495e54cc05947f18c8af","_rev":"1-3ff5b3730635ea11c6c75647ba6176cb","test1":"test11","test2":"test22"}

Update Document
Need _rev ( Revision )

$ curl -X PUT http://127.0.0.1:5984/dbName/6e1295ed6c29495e54cc05947f18c8af -d '{"test1":"test11","test2":"test22","_rev":"1-3ff5b3730635ea11c6c75647ba6176cb"}'
{"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"2-0d6cc4a67784f846f3fe6edb830bac7d"}

NOTE: Using Revision is `Multi-Version Concurrency Control (MVCC)`

Insert Attachment

curl -X PUT http://127.0.0.1:5984/dbName/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg?rev=2-2739352689 --data-binary @fileOnLocal.jpg -H "Content-Type: image/jpg"

To see attachment

Go to http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg

ref : http://guide.couchdb.org/draft/api.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s