Menu

Usage

SKS

Get the database API handler and initialize database:

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...");
    });

Create table object:

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}]}});

Create database schema:

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...");
});

Alter 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.

  • If some table needs to be dropped then use "dropTablesObj" property in the alter object else do not specify this property.
  • Drop table object looks like var dropTablesObj = []; dropTablesObj.push({table_name: "CUSTOMERS"});
  • If some table need to be altered then use "createTablesObj" property in the alter object else do not specify this property. New table structure object should be created and passed.
  • Create table object is same as explained in above section.
    alterSchemaObj = {"db_name": 'Testing_Database', "dbVersionNum": 2, "createTablesObj":createTablesObj, "dropTablesObj":dropTablesObj};
    adapter_handler.alterSchema(alterSchemaObj, function(){
        console.log("Successfully altered database schema...");
    });
    

Insert data:

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" should be the valid name of table where insert has to happen. Table name specified should be present in the database.
  • "data" should be the data which needs to be inserted. While preparing data object care should be taken on whether those columns exists in the table or not and not violating unique constraint.
    {"table_name" : "CUSTOMERS", "data" :{"ssn": "333-44-4444", "name": "Bill", "age": 35, "email": "bill@company.com" }}
    

Select data:

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" should be the valid name of table where select has to happen. Table name specified should be present in the database.
  • "select_column_array" is an array of column names whose data is required. If this property is not specified then data from all column is returned. This is like select column names in SQL SELECT statement.
  • "where_column_object.where_column_name_array" is the array of columns from whom data is required to be fetched. This is like where clause in SQL SELECT statement.
    • Present adapter supports only one column name to be specified here.
    • Presently where supports only basic search. Does not support IN, NOT IN, != etc. type of searches.
      {"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
      

Delete data:

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" should be the valid name of table where delete has to happen. Table name specified should be present in the database.
  • "where_column_object.where_column_name_array" is the array of columns from whom data is required to be deleted. This is like where clause in SQL DELETE statement.
    • Present adapter supports only one column name to be specified here.
    • Presently where supports only basic search. Does not support IN, NOT IN, != etc. type of searches.
      {"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 data:

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" should be the valid name of table where update has to happen. Table name specified should be present in the database.
  • "where_column_object.where_column_name_array" is the array of columns from whom data is required to be updated. This is like where clause in SQL UPDATE statement.
    • Present adapter supports only one column name to be specified here.
    • Presently where supports only basic search. Does not support IN, NOT IN, != etc. type of searches.
  • "updateData" is the new updated data object.
        {"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.
    

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.