Prepare the database initialization object, in which "db_name" and "db_version" are mandatory parameters.
If no database size is specified then 25 MB is the default size.
var adapter_handler = new coreAdapater({db_name:'Testing_Database', db_version:1, db_desc: "This is a testing database", db_size: 15 * 1024 * 1024}, function(dbAccessApi_Hanlder) {
console.log("Adapter handler successfully recived...");
});
var createTablesObj = [];
Option 1. Table with primary key and no indexed tables: Creates a table named CUSTOMERS with columns as ssn, name, age and email, with "ssn" as primary key column. This means that only "ssn" column can be used for searching.
createTablesObj.push({table_name: "CUSTOMERS", columnData: {column_array: ["ssn", "name", "age", "email"], primaryKeyCol: "ssn"}});
Option 2. Table with primary key and few indexed tables: Creates a table named CUSTOMERS with columns as ssn, name, age and email, with "ssn" as primary key column and indexed column as "name". This means that "ssn" and "name" columns can be used for searching. Since "name" column has non-unique indexed so duplicate values can be inserted in this column.
createTablesObj.push({table_name: "CUSTOMERS", columnData: {column_array: ["ssn", "name", "age", "email"], primaryKeyCol: "ssn", indexColArr:[{colName: "name", isUnique: false}]}});
Option 3. Table with no primary key but few indexed tables: This is the preferred way because in case of IndexedDB, it results in faster insertion. Creates a table named CUSTOMERS with columns as ssn, name, age and email, with no primary key column but indexed column as "ssn" and "name". This means that "ssn" and "name" columns can be used for searching. No duplicated allowed in "ssn" but "name" column can have duplicate values.
createTablesObj.push({table_name: "CUSTOMERS", columnData: {column_array: ["ssn", "name", "age", "email"], indexColArr:[{colName: "ssn", isUnique: true}, {colName: "name", isUnique: false}]}});
createSchema() method creates the database schema. It accepts 2 parameters - first the the tables creation object (in this example "createTablesObj") and second is success callback method.
adapter_handler.createSchema(createTablesObj, function(){
console.log("Successfully created database schema...");
});
alterSchema() method lets alter the database schema. It accepts 2 parameters - first the table alter object and second is the success callback method.
alterSchemaObj = {"db_name": 'Testing_Database', "dbVersionNum": 2, "createTablesObj":createTablesObj, "dropTablesObj":dropTablesObj};
adapter_handler.alterSchema(alterSchemaObj, function(){
console.log("Successfully altered database schema...");
});
insert() method takes 3 paramters. First is the insert object, second is success callback and third is error callback.
Insert object: 2 important properties of select object are "table_name" and "data".
{"table_name" : "CUSTOMERS", "data" :{"ssn": "333-44-4444", "name": "Bill", "age": 35, "email": "bill@company.com" }}
select() method takes 3 paramters. First is the select object, second is success callback and third is error callback.
Insert object: 3 important properties of insert object are "table_name" and "select_column_array" and "where_column_object".
{"table_name": "CUSTOMERS", "select_column_array": ["ssn", "name"], "where_column_object" : {"where_column_name_array": ["name"], "where_column_value_array": [["Bill"]]}} //selects values from "ssn" and "name" columns where "name" = "Bill".
{"table_name": "CUSTOMERS", "where_column_object" : {"where_column_name_array": ["name"], "where_column_value_array": [["Bill"]]}} //selects values from all columns where "name" = "Bill".
{"table_name": "CUSTOMERS"} //selects values from all columns
remove() method takes 3 paramters. First is the delete object, second is success callback and third is error callback.
Delete object: 2 important properties of delete object are "table_name" and "where_column_object".
{"table_name": "CUSTOMERS_1", "where_column_object" : {"where_column_name_array": ["name"], "where_column_value_array": [["Bill"]]}} //delete all rows where "name" = "Bill".
{"table_name": "CUSTOMERS_1"} //delete all rows from database.
update() method takes 3 paramters. First is the delete object, second is success callback and third is error callback.
Delete object: 3 important properties of delete object are "table_name", "where_column_object" and "updateData".
{"table_name": "CUSTOMERS_1", "where_column_object" : {"where_column_name_array": ["name"], "where_column_value_array": [["Bill"]]}, "updateData":{"name": "Bill2", "age": 2}} // update specified where "name" = "Bill".
{"table_name": "CUSTOMERS_1", "updateData":{"name": "Bill_Bill", "age": 100}} //update all rows for specified data.