Object built-in object

ObjectDbConnection

DBConnection is the base class of database connection and is used to establish and maintain a database connection session. It implements the basic operations of connection and serves as the basis for derived classes. It also supports operations such as starting a transaction, committing a transaction, and rolling back a transaction.

Subclasses of DBConnection include:Odbc,MySQL,SQLite, by instantiating each subclass, we can easily access different kinds of databases.

DBConnection cannot be created directly, only throughdb.openCreated by other methods, for example:

1 2
var db = require("db"); var conn = db.open("mysql://root:123456@localhost:3306/test");

inheritance relationship

member properties

type

String, query the current connection database type

1
readonly String DbConnection.type;

member function

close

Close current database connection

1
DbConnection.close() async;

use

Select the default database for the current database connection

1
DbConnection.use(String dbName) async;

Call parameters:

  • dbName: String, specify the database name

begin

Start a transaction on the current database connection

1
DbConnection.begin(String point = "") async;

Call parameters:

  • point: String, specifies the name of the transaction, not specified by default

commit

Commit the transaction on the current database connection

1
DbConnection.commit(String point = "") async;

Call parameters:

  • point: String, specifies the name of the transaction, not specified by default

rollback

Rollback a transaction on the current database connection

1
DbConnection.rollback(String point = "") async;

Call parameters:

  • point: String, specifies the name of the transaction, not specified by default

trans

Enter a transaction to execute a function, and commit or rollback based on the execution of the function.

1
Boolean DbConnection.trans(Function func);

Call parameters:

  • func: Function, a function executed in a transactional manner

Return results:

  • Boolean, returns whether the transaction is committed, returns true for normal commit, returns false for rollback, and throws an error if the transaction goes wrong.

There are three results of func execution:

  • The function returns normally, including end of operation and active return, at which time the transaction will be automatically committed.
  • The function returns false and the transaction will be rolled back
  • Function operation error, transaction automatically rolled back

Enter a transaction to execute a function, and commit or rollback based on the execution of the function.

1 2
Boolean DbConnection.trans(String point, Function func);

Call parameters:

  • point: String, specifies the name of the transaction
  • func: Function, a function executed in a transactional manner

Return results:

  • Boolean, returns whether the transaction is committed, returns true for normal commit, returns false for rollback, and throws an error if the transaction goes wrong.

There are three results of func execution:

  • The function returns normally, including end of operation and active return, at which time the transaction will be automatically committed.
  • The function returns false and the transaction will be rolled back
  • Function operation error, transaction automatically rolled back

execute

Execute a sql command and return the execution result

1
NArray DbConnection.execute(String sql) async;

Call parameters:

  • sql: String, string

Return results:

  • NArray, returns an array containing result records. If the request is UPDATE or INSERT, the returned result will also include affected and insertId. mssql does not support insertId.

Execute a sql command and return the execution result. The string can be formatted according to the parameters.

1 2
NArray DbConnection.execute(String sql, ...args) async;

Call parameters:

  • sql: String, format string, optional parameters are specified with ?. For example: 'SELECT FROM TEST WHERE [id]=?'
  • args: ..., optional parameter list

Return results:

  • NArray, returns an array containing result records. If the request is UPDATE or INSERT, the returned result will also include affected and insertId. mssql does not support insertId.

createTable

Create data table

1
DbConnection.createTable(Object opts) async;

Call parameters:

  • opts: Object, parameter list

dropTable

Delete data table

1
DbConnection.dropTable(Object opts) async;

Call parameters:

  • opts: Object, parameter list

createIndex

Create data table index

1
DbConnection.createIndex(Object opts) async;

Call parameters:

  • opts: Object, parameter list

dropIndex

Delete data table index

1
DbConnection.dropIndex(Object opts) async;

Call parameters:

  • opts: Object, parameter list

insert

Insert new record

1
Number DbConnection.insert(Object opts) async;

Call parameters:

  • opts: Object, parameter list

Return results:

  • Number, returns the id containing the insertion, or 0 if the engine does not support it.

find

Query data based on specified conditions

1
NArray DbConnection.find(Object opts) async;

Call parameters:

  • opts: Object, parameter list

Return results:

  • NArray, returns records containing results

count

Count the number of data records based on specified conditions

1
Integer DbConnection.count(Object opts) async;

Call parameters:

  • opts: Object, parameter list

Return results:

  • Integer, returns the number of records containing the result

update

Update data based on specified conditions

1
Integer DbConnection.update(Object opts) async;

Call parameters:

  • opts: Object, parameter list

Return results:

  • Integer, returns the number of records containing updates

remove

Delete data based on specified conditions

1
Integer DbConnection.remove(Object opts) async;

Call parameters:

  • opts: Object, optional parameter list

Return results:

  • Integer, returns the number of records containing updates

format

Format a sql command and return the formatted result

1 2
String DbConnection.format(String method, Object opts);

Call parameters:

  • method: String, specifies the requested method
  • opts: Object, optional parameter list

Return results:

  • String, returns the formatted sql command

Format a sql command and return the formatted result

1 2
String DbConnection.format(String sql, ...args);

Call parameters:

  • sql: String, format string, optional parameters are specified with ?. For example: 'SELECT FROM TEST WHERE [id]=?'
  • args: ..., optional parameter list

Return results:

  • String, returns the formatted sql command

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String DbConnection.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value DbConnection.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable