// iBoxDB Node.js Embedded NoSQL Database example // ver 1.2// Download source code to compile // if running on win-x64 or linux-x64, download the compiled file.// https://sourceforge.net/projects/benchmarksql3/files/// import classesconst{JsBox,JsBoxConfig,db_root,db_debug_deletefile,print,pause}=require("./_load_iboxdb.js")// set the database's files directorydb_root("../DBRoot")// set the database's address, a Number.constaddress=10// if in Debug, delete the old files.db_debug_deletefile(address)// new databaseletjsbox=newJsBox(address)// a prototype object to define what types of the fieldsconstproto={"id":BigInt(0),"value":String("_"),"ver":BigInt(0),"other_fields":"..."}// setup databaseletcfg=jsbox.get_config()// setup tablecfg.ensure_table(proto,"table","id")// setup index, [optional].letisUnique=false;cfg.ensure_index(proto,"table",isUnique,"value(32)")// setup a version field, [optional].cfg.ensure_increment(proto,"table","ver")// setup cache, [optional].cfg.cache_length(1024*1024*512)// open the database to usejsbox.open()// copy field from prototypeletobj={...proto}// modify fieldletid=jsbox.new_id()obj.id=idobj.value='v_'+String(id)// insert object to tablejsbox.insert("table",obj)obj=null// get object from tableobj=jsbox.get("table",id)// print the objectprint(obj)//modify the objectobj.value+="++";jsbox.update("table",obj)obj=null// use sql-like to query objectsletobjs=jsbox.select("from table where id==?",id)obj=objs[0]print(obj)// delete the objectjsbox.delete("table",obj.id)//count the tableletsize=jsbox.count("from table where id>?",0n)print(`${size}`)obj={...proto}// modify fieldid=jsbox.new_id()obj.id=idobj.value='v_'+String(id)// a Array fieldobj.items=[{"name":"ABC"}]//replace = insert or updatejsbox.replace("table",obj)obj=nullletlastId=0nfor(xofjsbox.select("from table")){print(x)lastId=x.id}// add one more itemobj=jsbox.get("table",lastId)obj.items.push({"name":"DEF"})jsbox.replace("table",obj)for(xofjsbox.select("from table")){print(x)}//close databasejsbox.close()print("end.")
Last edit: iBoxDB 6 days ago