Skip to content

API Cheat Sheet [store5]

bapquad edited this page Feb 26, 2022 · 3 revisions

Working with data storage on web technology

For using store5 module. Let's import this module into your application.

import {store5} from '@bapquad/element5'

Functions

Open

Open the database simulator.

Parameter

  • type:string - The types of storage engine. Required

Return

Returns the storage engine.

Storage engine constants

  • cookie_ds - Data stored base on cookie. It returns the ExpireData Simulator object.
  • local_ds - Data stored base on local storage interfaces. It returns the LocalData Simulator object
  • session_ds - Data stored base on session storage interfaces. It returns the SessionData Simulator object
  • indexed_db - Data stored base on IndexedDB interfaces. It returns the IndexedDB Simulator object, It must be declared with a database name.

Example

import {store5} from '@bapquad/element5'

let storage = store5.Open("cookie_ds");

// Print the storage engine
console.log(storage);

ExpireData Simulator

Manages the ExpireData Simulator storage engine.

public

Create the public variable. This can access everywhere in the application.

Parameter

  • name:string - The data name. Required
  • value:any - The data value. Required
  • period:number - The period time in the second unit. Optional

Return

Returns the ExpireDataRecordItem object.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("cookie_ds");
ds.public("username", "John Paul', 24*60*60);

private

Create the private variable. This can access in the current branch only.

Parameter

  • name:string - The data name. Required
  • value:any - The data value. Required
  • period:number - The period time in the second unit. Optional

Return

Returns the ExpireDataRecordItem object.

Example

In the ./sub_branch/index.html file.

import {store5} from '@bapquad/element5'

let ds = store5.Open("cookie_ds");
ds.private("username", "John Paul", 24*60*60);

// Print the data value
console.log(ds.private("username"));

In the ./index.html file.

import {store5} from '@bapquad/element5'

let ds = store5.Open("cookie_ds");

// Print the data
console.log(ds.private("username"));

ExpireDataRecordItem

Manipulating the records of ExpireData Simulator storage engine.

GetValue

Get value of the cookie variable.

Parameter

  • Has no parameter.

Return

Returns the data value.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("cookie_ds");
let item = ds.private("username", "John Paul", 60));

// Print the data value
console.log(item.GetValue());

SetValue

Set value to the cookie variable.

Parameter

  • value:any - The data value. Required

Return

Returns the void value.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("cookie_ds");
let item = ds.private("username", "John Paul", 60));
item.SetValue("Magaret");

// Print the data value
console.log(item.GetValue());

Destroy

Destroy the cookie variable.

Parameter

  • Has no parameter.

Return

Returns the void value.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("cookie_ds");
let item = ds.private("username", "John Paul", 60));
item.SetValue("Magaret");
item.Destroy();

// Print the data value
console.log(item.GetValue());

LocalData Simulator

Manages the LocalData Simulator storage engine.

public

Create the public variable. This can access everywhere in the application.

Parameter

  • name:string - The data name. Required
  • value:any - The data value. Required

Return

Returns the LocalDataRecordItem object.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("local_ds");
let item = ds.public("username", "John Paul");

// Print the localdata item
console.log(item);

private

Create the private variable. This can access in the current branch only.

Parameter

  • name:string - The data name. Required
  • value:any - The data value. Required

Return

Returns the LocalDataRecordItem object.

Example

In the ./sub_branch/index.html file.

import {store5} from '@bapquad/element5'

let ds = store5.Open("local_ds");
ds.private("username", "John Paul");

// Print the data value
console.log(ds.private("username"));

In the ./index.html file.

import {store5} from '@bapquad/element5'

let ds = store5.Open("local_ds");

// Print the data value
console.log(ds.private("username"));

LocalDataRecordItem

Manipulating the records of LocalData Simulator storage engine.

GetValue

Retrieves the value of the variable.

Parameter

  • Has no parameter.

Return

Returns the data value.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("local_ds");
let item = ds.public("username", "John Paul");

// Print the data value
console.log(item.GetValue());

SetValue

Sets the value to the variable.

Parameter

  • value:any - The data value. Required

Return

Returns the LocalDataRecordItem object.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("local_ds");
let item = ds.public("username", "John Paul");
item.SetValue("Magaret");

// Print the data value
console.log(item.GetValue());

Remove

Removes the variable from LocalDB database.

Parameter

  • Has no parameter.

Return

Returns the void value.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("local_ds");
let item = ds.public("username", "John Paul");
item.Remove();

// Print the data value
console.log(item.GetValue());

SessionData Simulator

Manages the SessionData Simulator storage engine.

public

Create the public variable in `SessionStorage. This can access everywhere in the application.

Parameter

  • name:string - The data name. Required
  • value:any - The data value. Required

Return

Returns the SessionDataRecordItem object.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("session_ds");
let item = ds.public("username", "John Paul");

// Print the sessiondata item
console.log(item);

private

Create the private variable. This can access in the current branch only.

Parameter

  • name:string - The data name. Required
  • value:any - The data value. Required

Return

Returns the SessionDataRecordItem object.

Example

In the ./sub_branch/index.html file.

import {store5} from '@bapquad/element5'

let ds = store5.Open("session_ds");
ds.private("username", "John Paul");

// Print the value
console.log(ds.private("username"));

In the ./index.html file.

import {store5} from '@bapquad/element5'

let ds = store5.Open("session_ds");

// Print the value
console.log(ds.private("username"));

SessionDataRecordItem

Manipulating the records of SessionData Simulator storage engine.

GetValue

Retrieves the value of the variable.

Parameter

  • Has no parameter.

Return

Returns the data value.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("session_ds");
let item = ds.public("username", "John Paul");

// Print the data
console.log(item.GetValue());

SetValue

Sets the value to the variable.

Parameter

  • value:any - The data value. Required

Return

Returns the SessionDataRecordItem object.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("session_ds");
let item = ds.public("username", "John Paul");
item.SetValue("Magaret"));

// Print the data
console.log(item.GetValue());

Remove

Removes the variable from SessionStorage.

Parameter

  • Has no parameter.

Return

Returns the void value.

Example

import {store5} from '@bapquad/element5'

let ds = store5.Open("session_ds");
let item = ds.public("username", "John Paul");
item.Remove();

// Print the value
console.log(item.GetValue());

IndexedDB Simulator

Manages the IndexedDB simulator storage engine.

Schema

Constructs a database with the schema of tables. It updates when the database change the version.

Parameters

  • tables[,...]:list - List of schema arguments. Required

Return

Returns the IDBOpenDBRequest object with IndexedDB Simulator extensions.

Example

import {store5} from '@bapquad/element5'

var db = store5.Open("indexed_db", "program", 1);
db.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

IDBDatabase

Manipulating the database of IndexedDB Simulator storage engine.

From

Retrieves the table (the ObjectStore Interface).

Parameter

  • name:string - The name of table. Required

Return

Returns the IDBObjectStore object.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.From("scores"));
}

Table

Retrieves the table (the ObjectStore Interface).

Parameter

  • name:string - The name of table. Required

Return

Returns the IDBObjectStore object.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores"));
}

IDBObjectStore

The IDBObjectStore interface of the IndexedDB API represents an object store in a database. Records within an object store are sorted according to their keys. This sorting enables fast insertion, look-up, and ordered retrieval.

indexNames

[Read Only] A list of the names of indexes on objects in this object store.

Value

A DOMStringList.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores").indexNames);
}

autoIncrement

[Read Only] The autoIncrement read-only property of the IDBObjectStore interface returns the value of the auto increment flag for this object store.

Value

The Boolean value.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores").autoIncrement);
}

keyPath

[Read Only] The key path of this object store. If this attribute is null, the application must provide a key for each modification operation.

Value

The any value.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores").keyPath);
}

name

The name property of the IDBObjectStore interface indicates the name of this object store.

Value

A DOMString containing the object store's name.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores").name);
}

transaction

[Read Only] The transaction read-only property of the IDBObjectStore interface returns the transaction object to which this object store belongs.

Value

An IDBTransaction object.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores").transaction);
}

add

An IDBRequest object on which subsequent events related to this operation are fired.

Parameter

  • value:object - The desired data object. Required

Return

Returns an IDBRequest object on which subsequent events related to this operation are fired.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores"));
  let item = {
    playerName: "Walk dog", 
    playerBirth: "02/09/1987"
  };
  store.database.Table("scores").add(item); 
}

clear

Creates and immediately returns an IDBRequest object, and clears this object store in a separate thread. This is for deleting all the current data out of an object store.

Parameter

  • Has no parameter.

Return

Returns an IDBRequest object on which subsequent events related to this operation are fired.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores"));
  let item = {
    playerName: "Walk dog", 
    playerBirth: "02/09/1987"
  };
  store.database.Table("scores").clear(); 
}

count

Returns an IDBRequest object, and, in a separate thread, returns the total number of records that match the provided key or IDBKeyRange. If no arguments are provided, it returns the total number of records in the store.

Parameter

  • Has no parameter.

Return

Returns an IDBRequest object on which subsequent events related to this operation are fired.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  let request = store.database.Table("scores").count(); 
  request.onsuccess = function() {
    console.log(request.result);
  }
}

delete

Deletes the specified record or records.

Parameter

  • key:any - The key object record. Required

Return

Returns an IDBRequest object on which subsequent events related to this operation are fired. The request.result attribute is set to undefined.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores"));
  let item = {
    playerName: "Walk dog", 
    playerBirth: "02/09/1987"
  };
  store.database.Table("scores").add(item); 
  store.database.Table("scores").delete(1);
}

get

Returns an IDBRequest object, and, in a separate thread, returns the object store selected by the specified key. This is for retrieving specific records from an object store.

Parameter

  • key:any - The key object record. Required

Return

Returns an IDBRequest object on which subsequent events related to this operation are fired.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores"));
  let item = {
    playerName: "Walk dog", 
    playerBirth: "02/09/1987"
  };
  store.database.Table("scores").add(item); 
  let request = store.database.Table("scores").get(1); 
  request.onsuccess = function() 
  {
    let record = request.result;
    console.log(item, record);
  }
}

getAll

Retrieves all records in object store.

Parameter

  • Has no parameter.

Return

Returns an IDBRequest object on which subsequent events related to this operation are fired.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores"));
  let item = {
    playerName: "Walk dog", 
    playerBirth: "02/09/1987"
  };
  store.database.Table("scores").add(item); 
  let request = store.database.Table("scores").getAll(); 
  request.onsuccess = function() 
  {
    let records = request.result;
    console.log(item, records);
  }
}

update

Updates a given record in a database, or inserts a new record if the given item does not already exist.

Parameter

  • data:object - The update data. Required
  • key:number - The key of primary key. Required
  • success:callback - The callback function called when the data had been updated. Optional.

Return

Returns an IDBObjectStore object.

Example

import {store5} from '@bapquad/element5'

var store = store5.Open("indexed_db", "program", 1);
store.Schema({
  name: "scores",
  primaryKey: "id",
  active: true, 
  autoIncrement: true, 
  fields: [
    {
      name: "score",
      data: ["playerName", "score"],
      type: {"unique" : true}
    },
  ],
});

store.success = (e) => 
{
  console.log(store.database.Table("scores"));
  let item = {
    playerName: "Walk dog", 
    playerBirth: "02/09/1987"
  };
  store.database.Table("scores").add(item); 
  store.database.Table("scores").update({playerName: "Walk dog"}, 1); 
}