PAI DISTRIBUTED DATABASE

You are here:
< All Topics
Table of Contents

PAI Distributed Database (pai-ddb) is a distributed in-memory real-time database.

PAI-DDB works with distributed network of nodes that each one is a micro-instance of the distributed database. Unlike Blockchain where all the nodes contains the same data in pai-ddb every node can hold its own information in addition to the just create synchronized clones like in Blockchain system.

With pai-ddb you can distributed data to different nodes and by so securing the data, real-time data analysis, super privacy and increase performance.

The pai-ddb system is based on PAI-BOT O/S including essential elements for pai-ddb like pai-code, pai-bot, pai-net and more. PAI-DDB Node can be installed as a pai-code module that can be installed on any pai-bot.

PAI-DDB can work on both client & server and can store objects & tables.

PAI-DDB JS

npm repository:

"@pai-tech/pai-ddb"

Node.JS import

const pai_ddb = require("@pai-tech/pai-ddb").get_instance;

PAI-DDB is a Singleton class (static instance) and getting the single instance is done by calling the get_instance() method.

Methods:

get_instance

static get get_instance()

Retrieves the active instance of the pai-ddb class.

init

async init()

Initialize the database.

* this method is being invoked automatically with get_instance() call.

load_data

async load_data()

Loads the data to the memory.

* this method is being invoked automatically with get_instance() call.

add_entity

async add_entity(pai_entity , auto_commit = true)

Adding PAIEntity to database.

auto_commit (true is by default) performs commit() after adding the entity to the pai-ddb; for bulk insertion of entities it is recommended to loop the add_entity method with auto_commit = false and call commit() method after the loop

* PAIEntity includes JSON schema to define the entity

find

Search entity by filter

find(entity_name , filter , include_schema = false)

enitity_name – The name of the entity to search in.
filter – filter object with the search criteria
include_schema – include PAIEntity JSON schema within the results

filter object

The filter object contains the search criteria.

let filter = {"field-name":"field-value"};
let filter = {"field-name":{"is-empty":true}};

filter search object can contains the following commands:
"is-empty" : true/false
"starts-with" : "text"
"ends-with" : "text"
"contains" : "text"

"in" : ["array","of","data";]

filter commands can be inverted by adding the sign “!” before the command:
filter = {"first-name" : "!starts-with" : "a")
this will return all the records with the first-name the NOT starting with the letter “a”.

update

This method uses yo update entities.


update(entity_name, updates , filter , auto_commit = true)

enitity_name – The name of the entity to search in.
updates – object with the updated fields
filterfilter object with the search criteria
auto_commit (true is by default) performs commit() after update the entity recordset to the pai-ddb; for bulk updates of entities it is recommended to loop the add_entity method with auto_commit = false and call commit() method after the loop

update object example:
let updates = {"<field-to-update-1>":"value-1" , "<field-to-update-2>":"value-2"};

delete

This method uses to delete entity from the database

delete(entity_name , filter , auto_commit = true)

enitity_name – The name of the entity to search in.
filterfilter object with the search criteria
auto_commit (true is by default) performs commit() after update the entity recordset to the pai-ddb; for bulk updates of entities it is recommended to loop the add_entity method with auto_commit = false and call commit() method after the loop