Skip to main content

Database

In order to create the database in indexeddb, you need to perform two steps -

  1. Create database schema
  2. Call initDb

1. Create database schema

Database schema is an object which contains name of database and list of tables.

Syntax

var database = {
name: "database name",
tables: [table1, table2, table3]
}

Example

var tblProduct = {
name: 'Product',
columns: {
// Here "Id" is name of column
Id:{ primaryKey: true, autoIncrement: true },
ItemName: { notNull: true, dataType: "string" },
Price: { notNull: true, dataType: "number" },
Quantity : { notNull: true, dataType: "number" }
}
};

var tblOrder = {
name: 'Order',
columns: {
// Here "OrderId" is name of column
OrderId:{ primaryKey: true, autoIncrement: true }
}
};

var db = {
name: dbName,
tables: [tblProduct, tblOrder]
}

2. Call initDb

// initiate jsstore connection
var connection = new JsStore.Connection();

// step1 - create database schema
var tblProduct = {
name: 'Product',
columns: {
// Here "Id" is name of column
Id:{ primaryKey: true, autoIncrement: true },
ItemName: { notNull: true, dataType: "string" },
Price: { notNull: true, dataType: "number" },
Quantity : { notNull: true, dataType: "number" }
}
};

var tblOrder = {
name: 'Order',
columns: {
// Here "OrderId" is name of column
OrderId:{ primaryKey: true, autoIncrement: true }
}
};

var db = {
name: dbName,
tables: [tblProduct, tblOrder]
}

// step 2
var isDbCreated = await connection.initDb(db);
// isDbCreated will be true when database will be initiated for first time

if(isDbCreated){
alert('Db Created & connection is opened');
}
else{
alert('Connection is opened');
}

// since connection is opened now, you can call apis.

Example