You try it:
... what others posted
comment | made by | remark | at |
---|---|---|---|
1 | Quentin | IK HOU VAN DBQUERY <3 | Gisteren |
comment | made by | remark | at |
---|---|---|---|
1 | Quentin | IK HOU VAN DBQUERY <3 | Gisteren |
Download the documentation!
dbQuery is a Javascript framework that allows you access to any MySql database by using plain Javascript. You don't have to know any serverside programming language to access the data stored in your MySQL database. You can access any table by simply calling the db() function.
The db() function will return a javascript object (or collection of) with its attributes typed and initialized as defined in the database. The returned object will also have the methodes, getAttribute(), setAttribute(), save() and remove(), which will allow you to add, modify and delete any row stored in the database.
To initialize a db() object you'll have to pass 3 parameters to the db() function:
You need 6 files to use dbQuery:
In the HTML pages where dbQuery is used, jquery.js and dbquery.js have to be included in the header using the next code:
In the file dbquery.config, security details have to be declared in order for dbQuery to be allowed to access the database. There are several variables:
The function db is the main function of dbQuery. db is used to create objects from the database. db has several parameters:
Function db( tablename [, filter ] [, handler(eventObject) )
Returns an array with objects from the database that follow the selectioncriteria or an object if the selection consists of only an integer.
When only a single object has to be fetched from the database, db can be used as constructor. When using the constructor, eventObject is the single object instead of the array of objects.
In this simple example, a customer is fetched from the database and the object is logged to the console. Because it is only one customer that is fetched from the database, the constructor is used, so the eventObject consists of the customer only.
new db('customers',1, function(customer) {## console.log(customer);##});
In this simple example, a customer is fetched from the database and the object is logged to the console. Because the constructor is not used, an array is returned.
db('customers',1, function(customers) {## console.log(customers[0]);##});
In this example, all customers who are 23 years old are fetched from the database. The size of the set is logged.
db('customers', { 'age': 23 }, function(customers) {## console.log(customers.length);##});
In this example, all active customers of 20 years old are fetched from the database and their nickname is changed.
db('customers', { 'age': 20, 'status': 'active' }, function(customers) {## customers.forEach(function(customer, i) {## customer.setAttribute('nickname','20 year old customer number '+i).save();## });##});
The following functions can be used on objects fetched from the database using db.
The function getAttribute is used to fetch an attribute of an object. When this object is a foreign key, the object where this foreign key is pointed is returned.
Function .getAttribute( attribute, handler(eventObject) )
Returns the dbObject where the function is called to, so the function can be chained.
In the next example, a customer is fetched from the database. His name is logged to the console. After that, his address (which is a foreign key) is fetched, and the zipcode is logged.
new db('customers',1, function(customer) {## // normall attribute request## console.log(customer.getAttribute('name'));## // address links to another table## customer.getAttribute('address', function(address) {## console.log(address.getAttribute('zipcode'));## });##});
In the next example, a callback is defined and used in getAttribute.
var delete = function(address) {## address.remove();##}##db('customers',{'age':'23'}, function(customers) {## customers.forEach(function(customer) {## // Use the predefined callback## customer.getAttribute('address', delete(address));## });##});
The function setAttribute is used to set the value of an attribute of the object.
Function .setAttribute( attribute, newValue)
Returns the dbObject where the function is called to, so the function can be chained.
In this example, a customer is fetched from the database and his name and institute are changed.
new db('customers',1, function(customer) {## // make changes to the attributes## customer.setAttribute('name','Plak')## .setAttribute('firstname','Rutger')## .setAttribute('institude','University of Technology Delft');##});
The function save can be used to save changes made to the object into the database.
Function .save( handler(eventObject))
Returns the dbObject where the function is called to, so the function can be chained. Because a save-call often requires post-functionality, a callback can be defined and will be executed when the object is saved into the database.
In this example, a customer is fetched from the database. His name and institution are changed and the changes are saved to the database.
new db('customers',1, function(customer) {## // making changes to the attributes## customer.setAttribute('name','Plak')## .setAttribute('firstname','Rutger')## .setAttribute('institute','University of Technology Delft')## // save## .save();##});
In this example, a new customer is created. His name and institute are changed and saved into the database, so a new row is inserted in the database. After that, the identifier of the table customers is logged to the console. This value is only available after the object is saved to the database, so the logging happens in a callback.
new db('customers',0, function(customer) {## // making changes to the attributes## customer.setAttribute('name','Plak')## .setAttribute('firstname','Rutger')## .setAttribute('institute','University of Technology Delft')## // save## .save(function(customer) {## console.log(customer.getAttribute('customer'));## });##});
The remove function is used to remove objects from the database.
Function .remove(handler(eventObject))
Because a remove-call often requires post-functionality, a callback can be defined and will be executed when the object is deleted from the database. The eventObject is the old identifier of the object, as the object itself doesn't exist anymore.
In the next example, a customer is removed from the database.
new db('customers',1, function(customer) {## // delete customer with identifier 1 from the database## customer.remove();##});
In the next example, a customer is removed from the database when the delete button in the table is clicked. After it has been removed from the database, the customer is removed from the table.
$('#myTable tr td.delete').bind('click', function() {## // create dbObject with the identifier of the tr of the table## new db('customers',$(this).parent().attr('id'), function(customer) {## // Remove the customer from the database## customer.remove(function(customerId) {## // remove the table row with the id of the deleted customer## $('tr#'+customerId).remove();## });## });##});