Object 內建對象

物件SQLite

SQLite 物件是內建模組db的一個成員,主要負責SQLite 資料庫的連接和操作,可用於SQLite 資料庫的建立、查詢、插入、更新等操作。同時,SQLite 物件也提供了一些類似備份、格式化SQL 等進階操作。SQLite 連線物件也支援交易操作

在實際應用中,我們通常會根據業務需求建立SQLite 類型的資料表,然後進行資料的增刪改查等操作,例如:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
var db = require('db') // open a SQLite database var sqlite = db.openSQLite('test.db') // use execute method to create a table sqlite.execute('CREATE TABLE test (id INT PRIMARY KEY NOT NULL, name TEXT NOT NULL, age INT NOT NULL)') // use execute method to insert data sqlite.execute('INSERT INTO test (id, name, age) VALUES (?, ?, ?)', 1, 'Alice', 18) sqlite.execute('INSERT INTO test (id, name, age) VALUES (?, ?, ?)', 2, 'Bob', 20) sqlite.execute('INSERT INTO test (id, name, age) VALUES (?, ?, ?)', 3, 'Charlie', 22) // use execute method to query data var rs = sqlite.execute('SELECT * FROM test') console.log(rs) // use execute method to update data sqlite.execute('UPDATE test SET name=?, age=? WHERE id=?', 'Marry', 19, 1) // use execute method to delete data sqlite.execute('DELETE FROM test WHERE id=?', 2)

SQLite 也內建了vec_index 模組,我們可以在SQLite 資料庫上建立對向量欄位的索引,基於向量欄位進行檢索,得到與目標向量最近似的向量集合。支援使用數值類型的陣列表示向量,如:[1, 2, 3],同時支援向量維度。此外,vec_index 支援在交易內批量操作。

下面是一個簡單的範例:

1 2 3 4 5 6 7 8
var db = require('db'); var path = require('path'); var conn = db.openSQLite(path.join(__dirname, 'vec_test.db')); conn.execute('create virtual table vindex using vec_index(title(3), description(3))'); conn.execute(`insert into vindex(title, description, rowid) values("[1,2,3]", "[3,4,5]", 3)`);

可以使用vec_search 函數執行向量檢索,例如:

1 2 3 4
var key = [1, 2, 5.1234]; var limit = 1; var res = conn.execute(`select rowid, distance from vindex where vec_search(title, "${JSON.stringify(key)}")`);

vec_search 傳回一個最接近的向量集合和距離數組,其中距離按照從小到大順序排列。如果需要傳回多個最接近的向量集合,可以使用:limit 參數,例如:

1 2 3 4
var key = [1, 2, 5.1234]; var limit = 1; var res = conn.execute(`select rowid, distance from vindex where vec_search(title, "${JSON.stringify(key)}:10")`);

繼承關係

成員屬性

fileName

String, 目前資料庫檔名

1
readonly String SQLite.fileName;

timeout

Integer, 查詢並設定資料庫逾時時間,以毫秒為單位

1
Integer SQLite.timeout;

type

String, 查詢目前連線資料庫類型

1
readonly String SQLite.type;

成員函數

backup

備份目前資料庫到新文件

1
SQLite.backup(String fileName) async;

呼叫參數:

  • fileName: String, 指定備份的資料庫檔名

close

關閉目前資料庫連接

1
SQLite.close() async;

use

選擇目前資料庫連線的預設資料庫

1
SQLite.use(String dbName) async;

呼叫參數:

  • dbName: String, 指定資料庫名

begin

在目前資料庫連線上啟動一個事務

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

呼叫參數:

  • point: String, 指定事務的名稱,預設不指定

commit

提交目前資料庫連線上的事務

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

呼叫參數:

  • point: String, 指定事務的名稱,預設不指定

rollback

回滾目前資料庫連線上的事務

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

呼叫參數:

  • point: String, 指定事務的名稱,預設不指定

trans

進入事務執行一個函數,並根據函數執行情況提交或回滾

1
Boolean SQLite.trans(Function func);

呼叫參數:

  • func: Function, 以交易方式執行的函數

回傳結果:

  • Boolean, 傳回交易是否提交,正常commit 時傳回true, rollback 時傳回false,如果交易出錯則拋出錯誤

func 執行有三種結果:

  • 函數正常返回,包括運行結束和主動return,此時事務將自動提交
  • 函數傳回false,此時交易將回滾
  • 函數運行錯誤,事務自動回滾

進入事務執行一個函數,並根據函數執行情況提交或回滾

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

呼叫參數:

  • point: String, 指定事務的名稱
  • func: Function, 以交易方式執行的函數

回傳結果:

  • Boolean, 傳回交易是否提交,正常commit 時傳回true, rollback 時傳回false,如果交易出錯則拋出錯誤

func 執行有三種結果:

  • 函數正常返回,包括運行結束和主動return,此時事務將自動提交
  • 函數傳回false,此時交易將回滾
  • 函數運行錯誤,事務自動回滾

execute

執行一個sql 指令,並回傳執行結果

1
NArray SQLite.execute(String sql) async;

呼叫參數:

  • sql: String, 字串

回傳結果:

  • NArray, 傳回包含結果記錄的數組,如果請求是UPDATE 或INSERT,回傳結果也會包含affected 和insertId,mssql 不支援insertId。

執行一個sql 指令,並傳回執行結果,可根據參數格式化字串

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

呼叫參數:

  • sql: String, 格式化字串,可選參數用? 指定。例如:'SELECT FROM TEST WHERE [id]=?'
  • args: ..., 可選參數列表

回傳結果:

  • NArray, 傳回包含結果記錄的數組,如果請求是UPDATE 或INSERT,回傳結果也會包含affected 和insertId,mssql 不支援insertId。

createTable

建立資料表

1
SQLite.createTable(Object opts) async;

呼叫參數:

  • opts: Object, 參數列表

dropTable

刪除資料表

1
SQLite.dropTable(Object opts) async;

呼叫參數:

  • opts: Object, 參數列表

createIndex

建立資料表索引

1
SQLite.createIndex(Object opts) async;

呼叫參數:

  • opts: Object, 參數列表

dropIndex

刪除資料表索引

1
SQLite.dropIndex(Object opts) async;

呼叫參數:

  • opts: Object, 參數列表

insert

插入新記錄

1
Number SQLite.insert(Object opts) async;

呼叫參數:

  • opts: Object, 參數列表

回傳結果:

  • Number, 傳回包含插入的id,如果引擎不支援則傳回 ​​0

find

根據指定的條件查詢數據

1
NArray SQLite.find(Object opts) async;

呼叫參數:

  • opts: Object, 參數列表

回傳結果:

  • NArray, 傳回包含結果記錄

count

根據指定的條件統計資料記錄數

1
Integer SQLite.count(Object opts) async;

呼叫參數:

  • opts: Object, 參數列表

回傳結果:

  • Integer, 傳回包含結果記錄數

update

根據指定的條件更新數據

1
Integer SQLite.update(Object opts) async;

呼叫參數:

  • opts: Object, 參數列表

回傳結果:

  • Integer, 傳回包含更新的記錄數

remove

根據指定的條件刪除數據

1
Integer SQLite.remove(Object opts) async;

呼叫參數:

  • opts: Object, 可選參數列表

回傳結果:

  • Integer, 傳回包含更新的記錄數

format

格式化一個sql 指令,並回傳格式化結果

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

呼叫參數:

  • method: String, 指定請求的方法
  • opts: Object, 可選參數列表

回傳結果:

  • String, 傳回格式化之後的sql 指令

格式化一個sql 指令,並回傳格式化結果

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

呼叫參數:

  • sql: String, 格式化字串,可選參數用? 指定。例如:'SELECT FROM TEST WHERE [id]=?'
  • args: ..., 可選參數列表

回傳結果:

  • String, 傳回格式化之後的sql 指令

toString

傳回物件的字串表示,一般回傳"[Native Object]",物件可以根據自己的特性重新實現

1
String SQLite.toString();

回傳結果:

  • String, 傳回物件的字串表示

toJSON

傳回物件的JSON 格式表示,一般傳回物件定義的可讀屬性集合

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

呼叫參數:

  • key: String, 未使用

回傳結果:

  • Value, 傳回包含可JSON 序列化的值