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

Tuesday, March 5, 2013

Will get back

Work has been busy, getting part of the house fixed, and I worked the FIRST robotics Finger Lakes regional this past weekend.....
Need to put together the post on setting up for inputting clients and other things. Only one trick, but you should be able to figure it out.
Hope to have the pulldown info written in about two weeks

Thursday, February 21, 2013

Couchdb part 3, the script

Today we will look at the script.I will put these files on the web somewhere and link at the bottom of this page.

Start a file named recordedit.js
Here is the top of it

This is what is happening in the first 28 lines

line 1 connect to the database named courses
lines 2-28 is a function called update courses.

div #courses is emptied
and the view named byname is called, this was built in the last episode. The file will be attached below.
We then have some data, the function itterates through the rows of data, looking for the client name and title.  If there is no data, as there will be the first time this is run, nothing will happen. But with data, we will get a line with the client name and course name. This is followed by links to Edit the course or Remove the course. The remove the course will show up on line 179 of the script and the edit will start on 211, but we have a bit more to do until then.

The next step will build the dataform, it will be blank if the new course from the html page is clicked or populated if the update course button is clicked above.

the setup for this

lines 30-40 are the naming of the form, then we build the form. line 40 says the form will be a table. Then we write some repdative code for each line of data we want.
Looking at lines 44-48, we make the form with the first cell having the text Client, the second cell is the input form for the id client. Line 47 looks odd at first, but what it is asking is if we are editing, then fill in the form with the data from the document we are editing. then we close the line of the table, and so on. This is where you are building  your JSON object, name these what you want, you can change the names later, this form has many lines....
Find all of the lines attached... but

here is the bottom of the table and the submit function.

Now we will either append the JSON file or build a new file. The lines starting in 144 just take the data from the form and put them into the doc. These match up with the form lines in the previous section. This is where errors can occur, make sure the input names match in both sections!

lines 160 and 161 are the last of the building of the doc lines. 161 will be used later as things get more complex, but it shows you can throw anything in there if you wish to help identify the data.
163 and 164 we finish up with the making the document.

166 starts the fun things that happen after the page is loaded--hence the document ready function.
From before, remember that the data has an Edit button and a Remove button next to it. The add new was part of the index file, so within the div # courses, we can either edit or remove. The clicking of one of these will start the events on 170. It takes the document id on 174 and gets ready to do something. If edit is chosen, it opens the document and populates it up in line 30.
181 starts the delete function, if Remove Course is chosen. By the way this was made in lines 20-23.

When delete is called, it gives a chance to cancel the command. Always a good idea. The user is given the option of Delete or Cancel. If cancel is chosen, line 203 happens and we go back to where we were. If delete is chosen, line 190 is called and the database removes the document with the id chosen earlier.

now the end..
Here we add to the database. in 217 it is checking to see if the document has an id, if it does, then we just rewrite the document in the database, then clear the update form. Otherwise, if there is no id, that is it is a new set of data, the database will assign it an id.
then back to where it was.

When making this version, I realized if I wanted the system to be really searchable, some pulldown menus would need to be created, to keep the data uniform.

So, you have a workable form now. Next steps will be to improve the form with pulldowns. And the pulldowns will be populated from the database!!

Link to documents

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
 Apache CouchDB
 and for

 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 ( 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 

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,, _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.

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.

Monday, January 9, 2012

The moon is down

The moon is down
Originally uploaded by tsakshaug

Continuing 366 photos for 2012. Moonset in our backyard this morning.