CouchDB / JSON

Creating desktop or client-server database solutions?

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
jihem
Posts: 53
Joined: Sun Jul 28, 2013 3:21 pm

CouchDB / JSON

Post by jihem » Sun Jul 28, 2013 3:29 pm

Hi,
I'm looking for livecode resources about CouchDB or JSON. Actually I just find some broken links to a JSON lib... Any idea?
Regards,
jihem
Last edited by jihem on Sun Jul 28, 2013 5:20 pm, edited 1 time in total.

Klaus
Posts: 11319
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: CouchDB / JSON

Post by Klaus » Sun Jul 28, 2013 4:29 pm

Hi jihem,

here an uptodate link to the JSONlib stack:
http://revonline2.runrev.com/stack/82/LibJson-1-0b

Sorry, no idea about Couch DB...


Best

Klaus

jihem
Posts: 53
Joined: Sun Jul 28, 2013 3:21 pm

Re: CouchDB / JSON

Post by jihem » Sun Jul 28, 2013 5:18 pm

Hi Klaus,
Thanks. I was looking for this type of library. I will manage the request process with it and sockets.
Have a good day,
jihem

jihem
Posts: 53
Joined: Sun Jul 28, 2013 3:21 pm

Re: CouchDB / JSON

Post by jihem » Sun Oct 06, 2013 8:31 pm

Hi,
I'm working on a CouchDB library (GPL). You can find the state in the art below. The "libJson.rev" previously discussed is needed. You can put the whole thing (libJson+libCouchDB) in the same stack script if you want (without the openStack handler). Don't forget to give credits and to attach license terms (libJson : Mark Smith ; libCouchDB : Jean-Marc "jihem" QUERE). In hope this could help. The next updates, samples and documentations will be published here and/or on my website (when it will be done).
Regards,
jihem

/*
Author: Jean-Marc "jihem" QUERE
Date: September 2013
GPL v3 29th June 2007
*/

-- load libJson loader

global gtPath

on openStack
put the effective filename of this stack into gtPath
set the itemDelimiter to slash
delete last item of gtPath
if "libJson" is not among the lines of the stacksInUse then
start using stack (gtPath & "/libJson.rev")
end if
end openStack

-- libCouchDB

local ltURL
local ltCID
local loCDB

function db.libVersion
return "0.0.1"
end db.libVersion

on db.connect tUrl
put tUrl into ltURL
if last character of ltURL is not slash then
put slash after ltURL
end if
db.disconnect
db.load("")
if db.get("/couchdb") is not "Welcome" then
db.disconnect
end if
end db.connect

on db.disconnect
put empty into loCDB
end db.disconnect

function db.isConnected
return loCDB is not empty
end db.isConnected

on db.load tId
local tDoc
put tId into ltCID
put url (ltURL & tId) into tDoc
db.set tId,jsonToArray (tDoc)
end db.load

private function db.absoluteID tId
if char 1 of tId is "*" then
put ltCID & char 2 to the length of tId of tId into tId
end if
return tId
end db.absoluteID

function db.get tId
local tDoc
put loCDB into tDoc
set ItemDelimiter to slash
repeat for each item tIdx in db.absoluteID(tId)
put tDoc[tIdx] into tDoc
end repeat
return tDoc
end db.get

on db.set tId,tVal
put db._set(loCDB,db.absoluteID(tId),tVal) into loCDB
end db.set

private function db._set tDoc,tId,tVal
local tElm
set ItemDelimiter to slash
if the number of items of tId>1 then
put first item of tId into tElm
delete first item of tId

if tDoc is empty then
put empty into tDoc[tElm]
end if

put db._set(tDoc[tElm],tId,tVal) into tDoc[tElm]
else
put tVal into tDoc[tId]
end if
return tDoc
end db._set

on db.save tId
local tDoc
put arrayToJson(db.get(tId)) into tDoc
put tDoc into url (ltURL & db.absoluteID(tId))
end db.save

--------------------------

Samples :

-- Display the document count of the database n3k0 in a button

on mouseUp
set the label of me to "?"
db.connect("http://localhost:5984")
if db.isConnected() then
db.load("n3k0")
set the label of me to db.get("n3k0/doc_count")
end if
end mouseUp

-- then load a document from the database shop and update a field

on mouseUp
set the label of me to "?"
if db.isConnected() then
db.load("shop/2dcd0af593f8f670d834b10002064403")
db.set "*/name",the long time
set the label of me to "name:"& db.get("*/name")
put "_rev:"&db.get("*/_rev")&return&"age:"&db.get("*/age") &return& "name:"& db.get("*/name") into field "fldInfo"
--put db.get("*") into tInfo
--put tInfo["age"]
db.save "*"
end if
end mouseUp

/* The document is like this one :
{
"_id": "2dcd0af593f8f670d834b10002064403",
"_rev": "10-2d699e130a9c5ba0e0909c15dd5445a2",
"age": 20,
"name": "7:35:06 PM"
}
*/

icouto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Wed May 29, 2013 1:54 am
Location: Sydney, Australia

Re: CouchDB / JSON

Post by icouto » Wed Oct 09, 2013 5:06 am

Klaus wrote:Hi jihem,
here an uptodate link to the JSONlib stack:
http://revonline2.runrev.com/stack/82/LibJson-1-0b
This one is an updated version, combining code from different JSON libraries:
https://github.com/luxlogica/easyjson

jihem
Posts: 53
Joined: Sun Jul 28, 2013 3:21 pm

Re: CouchDB / JSON

Post by jihem » Wed Oct 09, 2013 12:31 pm

icouto wrote:
Klaus wrote:Hi jihem,
here an uptodate link to the JSONlib stack:
http://revonline2.runrev.com/stack/82/LibJson-1-0b
This one is an updated version, combining code from different JSON libraries:
https://github.com/luxlogica/easyjson
Thanks. Very nice job. I have updated. I prefer to use json.ToArray / json.FromArray rather than arrayFromJson /jsonFromArray. So,I have renamed these functions. I like to use the library name as prefix (like db. in my case). I'll release a stack with samples and documentation for libCouchDB.

Post Reply

Return to “Databases”