Showing posts with label couchdb. Show all posts
Showing posts with label couchdb. Show all posts

Wednesday, March 20, 2013

The Best Part of Couchdb

When you finally get some data in your database, you can use the best feature of the couch web  app. That is the map and reduce functions. In the first post I had you write a map function for the first script. That is all well and good, but with the web interface you can write functions to your heart's content and get immediate feedback. A good source to start this can be found in the Couchdb definitive guide, Cookbook for SQL jockeys chapter. I seem to refer to this every now and then, if I have not used Couch for a few weeks.

Instant gratification while working with a database! Who could ask for more?
 A quick overview:
If you have a dataview you can call it up  in the web interface.  From here you can see the result of your query.


To view the code for the view, click the 'View Code' arrow on the left side of the windo





Here you can see the code and mess around with it

 Your results will show in the window below after you hit the run button. If you like it, use the save as or save buttons. If you use Temporary view, you pretty much have a blank slate. But do save this when you are done, temporary functions really don't work well in the programming.


Wednesday, March 13, 2013

Short post on CQ5 and Couchdb

The long and short of making the next stage of the Couchdb site involved the inital setup before putting in pull down menus--think I sorta started in the middle there.

Having to put together a batch of static websites for a client, what is even worse is they are having me use CQ5 (the pre-Adobe version). If you ever have had the pleasure of using a template based webpage builder, imagine one that does not work very well, and gives very little access to code. I often want to kick my laptop and things of this nature. But that is the way it is.

Some days it works OK, other days, ugh. Today is one of the ugh days. You can almost make it work, then.....whoops there goes a section....somewhere.

Have some Wikipedia deadlines coming up too, not as bad, except I have no access to the client's production server. Works great on the test and development servers, then a glitch on production, which then involves Emailing setting files back and fourth.  Does not happen often, Have a small issue now, but we will live.

While I wait for CQ5 to do what it supposed to do, I shall do some writing.

The next concept is to make files containing company information and region information. These are key areas for this database. Now due to the type of database we are running, which is really a pile of files, it is possible to store different patterns of data on files in the same folder.

There then is a need for input of information on files for Region and Client, and make these different from files which hold course information. Build the files based on the first file and name them what  you want, I named them Region.html and Client.html, along with their respective .js files. If you have questions, let me know.

But  how to easily differentiate the files from one another?
in the previous post, in the code line 160, I added the term "level 3" for the course database. So did the same for clinets "level1" and regions "level 2". Now it is just a matter of searching for level 1, 2 or 3 to pull up the type of file you want.

Brief but understandable?

The next problem. Pull down menus taken from the Client and Region database!

Until then I will be drop kicking my laptop

Thursday, February 14, 2013

Couchdb part 2

CYBERPOWERPC Gamer Xtreme GXi440 Gaming Computer (Google Affiliate Ad)
If you need help loading and starting Couchdb onto your server please go to one of these resources:

Couchdb the definitive guide http://guide.couchdb.org/
 Apache CouchDB http://couchdb.apache.org/
 and for
 Couchapp http://couchapp.org/page/index

 There are many more out there, but I have found these most useful.

 As stated in my earlier post, my programming skills are a bit rusty, and at times I need someone to walk me bit by bit into the processes. I will, hopefully do that walk through here. I don’t really use forums such as stackoverflow, as I feel like an idiot when some answers are given to me. If you have questions, please ask, I hope I will not talk down or up to you.

 Couchdb, with JQuery and Javascript should be easy to use, and most of what you read tells you how easy it is, and once you cross the first hurdle it is. Crossing that hurdle took me several hours of work. Many more than it should have.

 Enough whining Let’s get started.

You need to have Couchdb and Couchapp installed, and if you are a normal computer person, I would assume you messed around a bit with both. You will be using the command line and the Couchdb web interface for most of this work. You will also need a text editing program. I use either notepad ++ or Komodo.

 These examples are done on a Linux Ubuntu box, not that it really makes too much of a difference.

 If you go to your web interface (http://127.0.0.1/_utils) and make a new database, for this example call it ‘courses’ as this project is a catalog of training courses.
 Or you can use the curl command in the command line $ curl -X PUT http://127.0.0.1:5984/courses 

If you messed with the security settings of couchdb, you may need to modify this command, something along the lines of: 

After that step, it is time to start making our application.

 Using the command line, change to a directory will want to work out of, I usually use the documents, or a subdirectory in documents, but have used others, just whatever you feel best using and does not get you confused.

This will be one of commands you will use over and over again, so learn to love it!
 $ couchapp generate app courses
What this does is makes a folder named courses.
The folder is full of many wonderful confusing things, most of which we will not deal with. There should be a few documents on the top level, couchappignore, couchapprc, README.md, _id, couchapp.json, language. Not doing much with these for this tutorial. The folders in the directory should be _attachments, evently, lists, shows, updates, vendor, views.

 We will be dealing with the _attachements and views folders, the others are useful to poke around in, and will be useful for future projects.

 If you did not know already, Couchdb stores its data as JSON documents, which means that each piece of data can be searched by itself, and each set of data is its own document, no mucking about with too many key and making sure everything is set before starting. You can change you search and fields at anytime. If you need help understanding what JSON is there are many articles out there to help you.

 INDEX.html 
We will start in the _attachments folder, specifically with the index.html file. As with most web applications, the index file is opened by default by a web browser. Right now the file is fairly boring, but we will change it. In a later part of this tutorial we will replace this index file with another, rename this file something else, and make a few similar copies of this index file. I am sure smarter people could get by with one index file, but I am not one. I am more of a code basher, than a finesse person. finesse is good. Bashing works for me

 This is what the index.html file should be changed to.

Now let us break this down to its parts. The header:



 Gives us the title , the css file location and loads up some scripts. As you can see there is a loader script named loader.js which loads most things, the recordedit.js script will be written by you soon, and I threw in a link to a general jquery library, as things were not working quite right before doing that.

 Now to the body:

 From the .css file we have a div named account. The title on the page will be “Course Data” The next line will make a link with the term Add Courses, to a form to add courses to our database. The next two divs will make a list of the current data or make a form to edit or add data to the database. The last two lines are used to maneuver between forms we will make later, feel free to leave them off for the time being. Ta-da one of three documents finished .

 The second document is even simpler. Making a view Views are how we, well, view the data. Please hang with me for a bit, as this may be a tiny bit confusing, hopefully not. When we make our database, we of course will have many named fields, the view allows us to search the JSON documents for that particular field, or a value in one of the fields. This file will go into the application in the views folder, in a folder named “byname” and name the file map.js, or using the command line, $couchapp generate view courses byname you will still need to edit the map file in the application as follows




 That is it What this looks for is a JSON document with the field named title, it will then ,gather this document for use. The web interface of Couchdb is an easier way to make views, we will use these the next time.

With that cliffhanger, I will quit. Next time, making the recordedit.js file, the heart of the application.

Friday, February 8, 2013

CouchDB success

It has been a year from my last post, goodness, not a good year, that may be part of it. I have been working with a learning development company for a few months as an interactive developer. Pretty much write code for html courses and work on articulate courses. I was hired to do a MediaWiki site for a major international company. I do some of their web pages too. The other week, I was given an opportunity to develop a site for another company which lists all of their training and is searchable. Simple database, no? No. So my meager MySQL skills were of no use. The programming lingua fraca here are javascript and JQuery, the other developer wanted the output to be a JSON object, and have the database small enough to live anywhere. Soooooo... I did some research and found Couchdb, it is not new and some are moving away from it, but thought I would give it a whirl. What I wanted was a simple input, edit save interface, and the ability to get different readouts of data. Again, nothing too difficult with MySQL, and one would hope not to hard with Couchdb. It turns out to be not too difficult, but getting useful information from most couchdb sites for this project was not easy. The part that was lacking, was how to make an input form. Almost all of the sites wanted you to bulk upload JSON objects to the database. Not going to happen. Most of the examples found were using a command line interface for doing work with the data, not what I wanted. What I will do over a few blog posts is to give simple instructions on how to build this interface and make it work. There are thousands of tutorials on how to install couchdb, so I won't go into that. Before the next post, download and install couchdb and couchapps.