Database

Introduction

To access the database

1
const table = db.table(scope, code, indexes);

db is the global module in FIBOS, The data tables we defined in the abi file will be mounted under the db module, This way we can access the data table defined in the abi file through the db module.

We will demonstrate the addition, deletion and modification of the data table:

The operation on the data table is written in the JS contract!

Save

1
2
3
4
5
6
7
8
9
exports.emplace = param => {
var players = db.players(action.account, action.account);
players.emplace(action.account, {
title: 'ceo',
age:48,
nickname:'lion1',
id:123
});
};

Save the data to the data table via the emplace function under the table module.

View

1
2
3
4
5
6
exports.find = param => {
var players = db.players(action.account, action.account);
var record_id = 123;
var itr = players.find(record_id);
console.log('find =>', itr.data);
};

Find data from the data table via the find function under the table module, Returning a DBIterator. The data property in DBIterator shows the data we want to query. For details, please refer to DBIterator in the API.

Modify

1
2
3
4
5
6
7
8
exports.update = param => {
var players = db.players(action.account, action.account);
var record_id = 123;
var itr = players.find(record_id);
itr.data.title = 'cto';
itr.data.age = 23;
itr.update(action.account);
};

Modify the data from the data table via the update function under the DBIterator module, We can modify the data property in DBIterator by looking up the data, and finally save and modify it with ‘update’.

Delete

1
2
3
4
5
6
exports.remove = param => {
var players = db.players(action.account, action.account);
var record_id = 123;
var itr = players.find(record_id);
itr.remove();
};

The specified data is deleted from the data table via the remove function under the DBIterator module.

Set up index

1
2
3
4
5
6
7
const indexes = {
detail1:[128, o => [o.age,o.weight]],
};

exports.hi = v => {
var players = db.players(action.account, action.account, indexes);
}

The above code defines one or more indexes, Add the indexes parameter when accessing the table, so you can use the index when working on the table.

Index based query

1
2
var itr = players.indexes.age.find({age:48,weight:100});
console.log(itr.data);

Conclusion