@jsh/db

Since v8.0.52

Client

The database client.

Usage example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
const db = require("@jsh/db");
const client = new db.Client();
try {    
    conn = client.connect();
    rows = conn.query("select * from example limit 10")
    cols = rows.columns()
    console.log("cols.names:", JSON.stringify(cols.columns));
    console.log("cols.types:", JSON.stringify(cols.types));
    
    count = 0;
    for (const rec of rows) {
        console.log(...rec);
        count++;
    }
    console.log("rows:", count, "selected" );
} catch(e) {
    console.log("Error:", e);
} finally {
    if (rows) rows.close();
    if (conn) conn.close();
}

Creation

ConstructorDescription
new Client(options)Instantiates a database client object with an options

If neither bridge nor driver is specified, the client defaults to connecting to the internal Machbase DBMS.

Options

OptionTypeDefaultDescription
lowerCaseColumnsBooleanfalsemap the lower-cased column names to the result object
  • Options for Drivers

Using Drivers shows the examples how to connect to the databases with driver and dataSource.

OptionTypeDefaultDescription
driverStringdriver name
dataSourceStringdatabase connection string
  • Options for Bridge

It is also possible to create Client with predefined bridge.

OptionTypeDefaultDescription
bridgeStringbridge name

Methods

MethodReturnsDescription
connect()Connconnect to the database

Conn

Methods

MethodReturnsDescription
close()disconnect to the database and release
query(String sqlText, any …args)Rows
exec(String sqlText, any …args)Result

Rows

Rows encapsulates the result set obtained from executing a query.

Methods

MethodReturnsDescription
close()release database statement
next()any[]fetch a record, returns null if no more records
columns()Columns
columnNames()String[]names of the result
columnTypes()String[]types of the result

It implements Symbol.iterable, enabling support for both patterns:

for(rec := rows.next(); rec != null; rec = rows.next()) {
    console.log(...rec);
}

for (rec of rows) {
    console.log(...rec);
}

Result

Result represents the outcome of the exec() method, providing details about the execution.

Properties

PropertyTypeDescription
messageStringresult message
rowsAffectedNumber

Columns

Properties

PropertyTypeDescription
columns[]Stringnames of the result
types[]Stringtypes of the result

Using Drivers

@jsh/db module supports sqlite, mysql, mssql, postgresql and machbase without pre-defined bridge.

Machbase

This example demonstrates how to connect to another Machbase instance via port 5656 and execute a query.

Set lowerCaseColumns: true at line 5 to ensure that the query results use lower-cased property names in the record object, as demonstrated at line 17.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
db = require("@jsh/db");
host = "192.168.0.207"
port = 5656
user = "sys"
pass = "manager"
client = db.Client({
    driver: "machbase",
    dataSource: `host=${host} port=${port} user=${user} password=${pass}`,
    lowerCaseColumns: true
})

try {
    sqlText = "select * from example where name = ? limit ?,?";
    tag = "my-car";
    off = 10;
    limit = 5;

    conn = client.connect()
    rows = conn.query(sqlText, tag, off, limit)
    for( rec of rows) {
        console.log(rec.name, rec.time, rec.value)
    }
} catch(e) {
    console.error(e.message)
} finally {
    rows.close()
    conn.close()
}

SQLite

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const db = require("@jsh/db");
client = new db.Client({
    driver:"sqlite",
    dataSource:"file::memory:?cache=shared"
});

try{
    conn = client.connect()
    conn.exec(`
        CREATE TABLE IF NOT EXISTS mem_example(
            id         INTEGER NOT NULL PRIMARY KEY,
            company    TEXT,
            employee   INTEGER
        )
    `);

    conn.exec(`INSERT INTO mem_example(company, employee) values(?, ?);`, 
        'Fedel-Gaylord', 12);

    rows = conn.query(`select * from mem_example`);
    for( rec of rows ) {
        console.log(...rec)
    }
}catch(e){
    console.error(e.message);
}finally{
    rows.close();
    conn.close();
}
Last updated on