|
NextDB.net | |||||
| PREV NEXT | FRAMES NO FRAMES | |||||
<script src="http://www.nextdb.net/api.js"></script>The JavaScript API is lightweight, tightly namespaced, extremely flexible and it plays well with all of the popular JavaScript libraries, such as prototype, jquery and dojo to name a few.
rows
variable to the callback function, it is an array of JavaScript Objects representing
the rows returned from the database. A typical return structure might be:
rows[0].TABLE_NAME.column_name.
var conn = new net.nextdb.Connection("YourAccountName","DatabaseWithinYourAccount");
var query = new net.nextdb.Query("QUERY1");
conn.executeQuery(query,
function(rows,error){
if(error){
// handle error, for example:
net.nextdb.Util.print(error.toString());
} else {
// handle success, for example:
for(var i=0;i<rows.length;i++)
net.nextdb.Util.print(rows[i].USERS.first_name);
}
}
);
Queries can be designed to take runtime arguments (i.e. parameters). These 'bind
variables' are added to the query using the setParameters method in the Query class. The
setParameters method takes an object and parses out the name value pairs as the
parameters and their values. These parameters only represent values, there is
no query logic exposed on the client, it is all defined and compiled on the server
through the query builder interface on the admin pages, which ensures security.
var conn = new net.nextdb.Connection("YourAccountName","DatabaseWithinYourAccount");
var query = new net.nextdb.Query("QUERY1");
query.setParameters( { email : "info@nextdb.net", date : "now" } );
conn.executeQuery(query,
function(rows,error){
if(error){
// handle error, for example:
net.nextdb.Util.print(error.toString());
} else {
// handle success, for example:
for(var i=0;i<rows.length;i++)
net.nextdb.Util.print(rows[i].USERS.first_name);
}
}
);
var conn = new net.nextdb.Connection("YourAccountName","DatabaseWithinYourAccount");
var insert = new net.nextdb.Insert("TABLE1");
insert.setParameters( { email : "info@nextdb.net" , date : "now" } );
conn.executeInsert(insert,
function(rowId,error){
if(error){
// handle error, for example:
net.nextdb.Util.print(error.toString());
} else {
// handle success, for example:
net.nextdb.Util.print(rowId);
}
}
);
The insert above is simple but there is no security. NextDB offers two ways of securing
inserts against hackers. The first is by allowing tables to be configured with
captcha protection, which means that in order to perform an insert, the user needs to
supply the text on a human readable image. Captcha protection is a very common
method for disabling hackers and bots from flooding a user registeration page. In order to
add captcha protection you first captcha protect the table using the admin pages,
then add a captcha image to you page with the following utility:
net.nextdb.Util.sourceCaptchaImg(referenceToImgInDOM);
which will source a captcha image to the page.
Inserts take a plain old JavaScript object where the name is the column and the
value is what you inserting into the row for that column. The executeInsert
callback is passed the encrypted rowId for the row inserted, and any errors.
var conn = new net.nextdb.Connection("YourAccountName","DatabaseWithinYourAccount");
var insert = new net.nextdb.Insert("TABLE1");
insert.setParameters( { email : "info@nextdb.net" ,
date : "now",
captcha : USER_SUPPLIED_VALUE} );
conn.executeInsert(insert,
function(rowId,error){
if(error){
// handle error, for example:
net.nextdb.Util.print(error.toString());
} else {
// handle success, for example:
net.nextdb.Util.print(rowId);
}
}
);
The second way of securing inserts is with relationships to other data (e.g. key constraints).
When data is inserted into a table it can be bound to data in other tables by
providing a key to the other row and naming the relationship that exists between the
tables. Relationships are defined using the admin pages. For example,
a relationship is between a USER and the CONTACTS associated with that USER. This
relationship has a 'one to many' cardinality, as a single (one) user can have multiple
(many) contacts. For public sites, it is a good idea to have one central table
(such as a USER table) which is captcha protected, then have all of the other tables
secured by reference to the captcha protected table using relationships. Inserts
without a valid key to the parent table will fail, and the key to the parent table
will only be granted by logging into the system.
This is a common design principle in databases to 'constrain' the data using
primary keys and foreign keys to protect the integrity of the database. Here is
an example of a query, then an insert and relate all in one shot:
var conn = new net.nextdb.Connection("YourAccountName","DatabaseWithinYourAccount");
// get a certain user to whom we will add a new contact
var query = new net.nextdb.Query("USER");
query.setParameters( { email : "info@nextdb.net" } );
conn.executeQuery(query,
function(rows,error){
if(error){
// handle error
} else {
// in the callback from the query we execute the insert
var insert = new net.nextdb.Insert("CONTACTS");
// set insert values
insert.setParameters( { cell_phone_number : "888 888 8888" ,
home_phone_number : "888 888 8888" } );
// get a specific encrypted key
var key=rows[0].USER.PK;
// set relationship between the key of the query results and the insert
insert.setRelationship(key, "USER_CONTACTS");
conn.executeInsert(insert,
function(rowId,error){
if(error){
// handle error
} else {
// success
}
}
);
}
}
);
var conn = new net.nextdb.Connection("YourAccountName","DatabaseWithinYourAccount");
var query = new net.nextdb.Query("QUERY2");
var args={};
args["email"]="info@nextdb.net";
query.setParameters(args);
conn.executeQuery(query,
function(rows,error){
if(error){
// handle error
} else {
//get a specific encrypted key
var key=rows[0].TABLE1.PK;
var update = new net.nextdb.Update("TABLE1");
update.setParameters( { first_name : "edgar" ,
last_name : "codd" } );
update.setRowId(key);
conn.executeUpdate(update,
function(error){
if(error){
// handle error
} else {
// success
}
}
);
}
}
);
var conn = new net.nextdb.Connection("YourAccountName","DatabaseWithinYourAccount");
var query = new net.nextdb.Query("QUERY3");
conn.executeQuery(query,
function(rows,error){
if(error){
// handle error
} else {
//get a specific encrypted key
var key=rows[0].TABLE1.PK;
var del = new net.nextdb.Delete("TABLE1");
del.setRowId(key);
conn.executeDelete(del,
function(error){
if(error){
// handle error
} else {
// success
}
}
);
}
}
);
function start(){
var date = Date.today().next().friday().add(3).hour();
var colors={"border":"rgb(200, 200, 200)",
"font":"rgb(8, 8, 8)",
"background":"rgb(255, 255, 255)",
"selectedBackground":"rgb(0, 88, 0)",
"selectedFont":"rgb(255, 255, 255)",
"mouseoverBackground":"rgb(200, 200, 200)",
"mouseoverFont":"rgb(0, 0, 0)"};
// construct Calendar with optional date instance and color config
// the defaults are a new Date(), and the color scheme above
var calendar = new net.nextdb.html.Calendar(date, colors);
var input=calendar.getElement();
var button=document.createElement("input");
button.type="button";
button.value="add date";
button.onclick=function(){
if(input.value==""){
alert("click on text field");
return;
}
insert(input.value);
}
document.getElementById("somewhereinpage").appendChild(input);
document.getElementById("somewhereinpage").appendChild(button);
}
function insert(d){
var conn = new net.nextdb.Connection("YourAccountName","DatabaseWithinYourAccount");
var insert = new net.nextdb.Insert("DATE_TABLE"); // table to take insert
insert.setParameters( { date : d } );
conn.executeInsert(insert,
function(rowId,error){
if(error){
// handle error
} else {
// success
}
}
);
}
var uploader = new net.nextdb.html.FileUpload(conn,"USER_PIC",
function(){
// start loading callback - initialize progress indicator
},
function(url){
// end loading callback - remove progress indicator
// create insert for the uploaded image
var insert = new net.nextdb.Insert("PIC");
insert.map["pic"]=url;
conn.executeInsert(insert, function(key,error){
if(error){
// handle error
}else{
// handle success
}
}
);
},
function(progress){
// if supplied this function is passed
// a progress indicator every 100 milliseconds
var uploadedBytes = progress.rx;
var totalBytes = progress.totalRX;
var perct = (Math.round((uploadedBytes/totalBytes)*100))+"%";
}
);
// now append to the page
document.getElementById("somewhereinpage").appendChild(uploader.getElement());
|
NextDB.net | |||||
| PREV NEXT | FRAMES NO FRAMES | |||||