Database API

The basic table structure of CUTOS database is in Key-Value format, and Value can be any json object. The advantage of this design is that the database does not need to be upgraded to modify the Value field, but the disadvantage is that it cannot be accessed as flexibly as traditional databases. If you need more flexible database access functions, you can use the SQL fully compatible interface provided by CUTOS.

CUTOS basic table structure (default primary key name is id, data type is INTEGER):

Field Data type Description
id INTEGER Primary key
value TEXT Data
tid TEXT Foreign key, transaction id
sync INTEGER Synchronization flag: 0-unsynchronized, 1-synchronized
timestamp INTEGER Last modified timestamp

Database

Constructor

let database = new Database(db);
  • db: database name, default is lwa.db if name is not passed

Database.connect

Connect to database

database.connect(callback)
  • callback: callback function
Example:
let database = database.connect((result, error) => {
  if (!error) {
    console.log(result)
  }
})
  • Return result example:
{
  "dbPath": "/home/linaro/.config/dios/data/lwa.db"
}

Database.run

Run sql

database.run(sql, callback)
  • sql: execute sql statement
  • callback: callback function
Example:
database.run('select * from device', (result, error) => {
  if (!error) {
    console.log(result)
  }
})
  • Return result example:
[
  {
    "id": 1,
    "value": "{\"type\":\"printer\",\"name\":\"HP-1\"}",
    "tid": "tra-001",
    "sync": 0,
    "timestamp": 1691401911678
  },
  {
    "id": 2,
    "value": "{\"type\":\"printer\",\"name\":\"HP-2\"}",
    "tid": "tra-002",
    "sync": 0,
    "timestamp": 1691401911681
  }
]

Database.Table

Constructor

let table = new Database.Table(name, db);
  • name: table name
  • db: database instance
Example:
let table = new Database.Table('device', database);

Table.create

Create a database table

table.create([opts], callback)
  • opts: optional parameters, including 3 attributes:
    • retentionTime: retention time, in hours; if not passed, it will be retained forever by default
    • keyName: name, if not passed, it will be 'id' by default
    • keyType: type, including 2 types 'TEXT' and 'INTEGER', if not passed, it will be 'INTEGER' by default
  • callback: callback function
Example:
table.create((result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
[]

Table.insert

Insert data

table.insert(value, [opts], callback);
  • value: Insert data
  • opts: optional parameters
    • tid: foreign key id, used to establish relationships between multiple records
  • callback: callback function
Example:
let tid = "tra-001";
table.insert({type: "printer", name: "NEC"}, {tid: tid}, (result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
{
  "status": true,
  "row": 1
}

table.insertById

Insert data by id

table.insertById(id, value, [opts], callback)
  • id: data id
  • value: insert data
  • opts: optional parameters
    • tid: Foreign key id, used to establish relationships between multiple records
  • callback: callback function
Example:
let tid = "tra-001";
table.insertById(1, {type: "printer", name: "NEC"}, {tid: tid}, (result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
{
  "status": true,
  "row": 1
}

Table.update

Update data

table.update(id, value, callback);
  • id: data id
  • value: update data
  • callback: callback function
Example:
table.update(row, {type: "printer", name: "HP-2"}, (result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
{
  "status": true,
  "msg": 0
}

table.delete

Delete data

table.delete(id, callback)
  • id: data id
  • callback: callback function

Table.query

Query data

table.query(id, callback);
  • id: data id
  • callback: callback function
Example:
deviceTable.query(row, (result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
[
  {
    "id": 2,
    "value": "{\"type\":\"printer\",\"name\":\"HP-2\"}",
    "tid": "tra-001",
    "sync": 0,
    "timestamp": 1691401911681
  }
]

Table.sync

Synchronize data

table.sync(id, callback);
  • id: data id
  • callback: callback function
Example:
deviceTable.sync(row, (result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
{
  "status": true,
  "msg": 0
}

Table.queryUnsynced

Query unsynchronized data

table.queryUnsynced([opts], callback);
  • opts: optional parameters
  • callback: callback function
Example:
deviceTable.queryUnsynced((result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
[
  {
    "id": 1,
    "value": "{\"type\":\"printer\",\"name\":\"NEC\"}",
    "tid": "tra-001",
    "sync": 0,
    "timestamp": 1691402230172
  }
]

Table.queryByTid

Query data by foreign key

table.queryByTid(tid, callback);
  • tid: foreign key id
  • callback: callback function
Example:
deviceTable.queryByTid("tra-001", (result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
[
  {
    "id": 2,
    "value": "{\"type\":\"printer\",\"name\":\"HP-2\"}",
    "tid": "tra-001",
    "sync": 0,
    "timestamp": 1691401911681
  }
]

Table.queryAll

Query all data

table.queryAll(callback);
  • callback: callback function
Example:
deviceTable.queryAll((result, error) => {
  if (!error) {
    console.log(result)
  }
});
  • Return result example:
[
  {
    "id": 1,
    "value": "{\"type\":\"printer\",\"name\":\"HP-1\"}",
    "tid": "tra-001",
    "sync": 0,
    "timestamp": 1691401911678
  },
  {
    "id": 2,
    "value": "{\"type\":\"printer\",\"name\":\"HP-2\"}",
    "tid": "tra-002",
    "sync": 0,
    "timestamp": 1691401911681
  }
]

results matching ""

    No results matching ""