Module basic module

Module db

Database access module

Basic module. Can be used to create and manipulate database resources, reference method:

1 2
var db = require('db'); var conn = db.open('rng://user:pass@host:port/dbname');

By specifying the database engine, different database links can be established. fibjs has two built-in sql engines: sqlite and mysql. It also supports connecting more databases through ODBC/unixODBC. Based on ODBC/unixODBC, fibjs builds a driver for mssql and PostgreSQL. In order to use ODBC/unixODBC, you need to install the corresponding driver. To use mssql under posix, you need to install freetds, and to use PostgreSQL, you need to install psqlodbc. Under normal circumstances, the driver can be used directly after successful installation without further configuration.

Static function

open

Open a database, this method is a general entry, call different engines according to the provided connString

1
static object db.open(String connString) async;

Call parameters:

  • connString: String, database description, such as: mysql://user:pass@host/db

Return result:

  • object, Returns the database connection object

openMySQL

Open a mysql database

1
static MySQL db.openMySQL(String connString) async;

Call parameters:

  • connString: String, database description, such as: mysql://user:pass@host/db

Return result:

  • MySQL, Returns the database connection object

openSQLite

Open a sqlite database

1
static SQLite db.openSQLite(String connString) async;

Call parameters:

  • connString: String, database description, such as: sqlite:test.db or test.db

Return result:

  • SQLite, Returns the database connection object

openOdbc

Open a sqlite database

1
static DbConnection db.openOdbc(String connString) async;

Call parameters:

  • connString: String, database description, such as: odbc://user:pass@host/db?driver=PostgreSQL%20ANSI

Return result:


openMSSQL

Open a mssql database

1
static DbConnection db.openMSSQL(String connString) async;

Call parameters:

  • connString: String, database description, such as: mssql://user:pass@host/db

Return result:

In order to establish a connection with mssql, the odbc driver of freetds must be installed under posix. You can also use the mssql driver of Microsoft by specifying the driver. The method of specifying the driver is in url After adding ?driver=msodbcsql17[.so/.dylib] option.


openPSQL

Open a PostgresSQL database

1
static DbConnection db.openPSQL(String connString) async;

Call parameters:

  • connString: String, database description, such as: psql://user:pass@host/db

Return result:

In order to establish a connection with PostgresSQL, the odbc driver of PostgresSQL must be installed.


openMongoDB

Open a mongodb database

1
static MongoDB db.openMongoDB(String connString) async;

Call parameters:

  • connString: String, database description

Return result:

  • MongoDB, Returns the database connection object

openLevelDB

Open a leveldb database

1
static LevelDB db.openLevelDB(String connString) async;

Call parameters:

  • connString: String, database description, such as: level: test.db or test.db

Return result:

  • LevelDB, Returns the database object

openRedis

Open one Redis database

1
static Redis db.openRedis(String connString) async;

Call parameters:

  • connString: String, database description, such as: redis://server:port or "server"

Return result:

  • Redis, Returns the database connection object