@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

driver and dataSource options support sqlite, mysql, mssql, postgresql and machbase without pre-defined bridge.

OptionTypeDefaultDescription
driverStringdriver name
dataSourceStringdatabase connection string
  • Options for Bridge

It is also possible to create Client with predefined bridge.

OptionTypeDefaultDescription
bridgeStringbridge name
Properties
PropertyTypeDescription
supportAppendBooleantrue if the client supports “Append” mode.

connect()

connect to the database.

Return value

Conn

close()

disconnect to the database and release

Syntax
close()

query()

Syntax
query(String *sqlText*, any ...*args*)
Return value

queryRow()

Syntax
queryRow(String *sqlText*, any ...*args*)
Return value

exec()

Syntax
exec(sqlText, ...args)
Parameters
  • sqlText String Sql text string
  • args any A variable-length list of arguments.
Return value

appender()

Create new “appender”.

Syntax
appender(table_name, ...columns)
Parameters
  • table_name String The table name of to append.
  • columns String A variable-length list of column names. If columns is omitted, all columns of the table will be appended in order.
Return value

Rows

Rows encapsulates the result set obtained from executing a query.

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);
}

close()

Release database statement

Syntax
close()
Parameters

None.

Return value

None.

next()

fetch a record, returns null if no more records

Syntax
next()
Parameters

None.

Return value
  • any[]

columns()

Syntax
columns()
Parameters

None.

Return value

columnNames()

Syntax
columnNames()
Parameters

None.

Return value
  • String[]

columnTypes()

Syntax
columnTypes()
Parameters

None.

Return value
  • String[]

Row

Row encapsulates the result of queryRow which retrieve a single record.

columns()

Syntax
columns()
Parameters

None.

Return value

columnNames()

names of the result

Syntax
columnNames()
Parameters

None.

Return value
  • String[]

columnTypes()

types of the result

Syntax
columnTypes()
Parameters

None.

Return value
  • String[]

values()

result columns

Syntax
values()
Parameters

None.

Return value
  • any[]

Result

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

Properties
PropertyTypeDescription
messageStringresult message
rowsAffectedNumber

Columns

Properties
PropertyTypeDescription
columnsString[]names of the result
typesString[]types of the result

Appender

append()

Invoke the append method with the specified values in the order of the columns.

Syntax
append(...values)
Parameters
  • values any The values to be appended to the table, provided in the order of the specified columns.
Return value

None.

close()

Close the appender.

Syntax
close()
Parameters

None.

Return value

None.

result()

Returns the result of the append operation after the appender is closed.

Syntax
result()
Parameters

None.

Return value
  • Object
PropertyTypeDescription
successNumber
faileNumber
Last updated on