database.aggregate(name, options)
向 SQLite 数据库注册一个新的聚合函数。此方法是 sqlite3_create_window_function()
的封装器。
¥Registers a new aggregate function with the SQLite database. This method is a wrapper around
sqlite3_create_window_function()
.
-
name
<string> 要创建的 SQLite 函数的名称。¥
name
<string> The name of the SQLite function to create. -
options
<Object> 函数配置设置。¥
options
<Object> Function configuration settings.-
deterministic
<boolean> 如果是true
,则在创建的函数上设置SQLITE_DETERMINISTIC
标志。默认值:false
。¥
deterministic
<boolean> Iftrue
, theSQLITE_DETERMINISTIC
flag is set on the created function. Default:false
. -
directOnly
<boolean> 如果是true
,则在创建的函数上设置SQLITE_DIRECTONLY
标志。默认值:false
。¥
directOnly
<boolean> Iftrue
, theSQLITE_DIRECTONLY
flag is set on the created function. Default:false
. -
useBigIntArguments
<boolean> 如果使用true
,则options.step
和options.inverse
的整数参数将转换为BigInt
。如果false
,则整数参数作为 JavaScript 数字传递。默认值:false
。¥
useBigIntArguments
<boolean> Iftrue
, integer arguments tooptions.step
andoptions.inverse
are converted toBigInt
s. Iffalse
, integer arguments are passed as JavaScript numbers. Default:false
. -
varargs
<boolean> 如果true
、options.step
和options.inverse
可以使用任意数量的参数(介于 0 到SQLITE_MAX_FUNCTION_ARG
之间)调用。如果false
、inverse
和step
必须使用恰好length
个参数调用。默认值:false
。¥
varargs
<boolean> Iftrue
,options.step
andoptions.inverse
may be invoked with any number of arguments (between zero andSQLITE_MAX_FUNCTION_ARG
). Iffalse
,inverse
andstep
must be invoked with exactlylength
arguments. Default:false
. -
start
<number> | <string> | <null> | <Array> | <Object> | <Function> 聚合函数的标识值。此值在聚合函数初始化时使用。当传递 <Function> 时,其标识将作为其返回值。¥
start
<number> | <string> | <null> | <Array> | <Object> | <Function> The identity value for the aggregation function. This value is used when the aggregation function is initialized. When a <Function> is passed the identity will be its return value. -
step
<Function> 聚合中每一行要调用的函数。该函数接收当前状态和行值。此函数的返回值应为新状态。¥
step
<Function> The function to call for each row in the aggregation. The function receives the current state and the row value. The return value of this function should be the new state. -
result
<Function> 用于获取聚合结果的函数。该函数接收最终状态并返回聚合结果。¥
result
<Function> The function to call to get the result of the aggregation. The function receives the final state and should return the result of the aggregation. -
inverse
<Function> 提供此函数后,aggregate
方法将用作窗口函数。该函数接收当前状态和已删除的行值。此函数的返回值应为新状态。¥
inverse
<Function> When this function is provided, theaggregate
method will work as a window function. The function receives the current state and the dropped row value. The return value of this function should be the new state.
-
用作窗口函数时,result
函数将被多次调用。
¥When used as a window function, the result
function will be called multiple times.
const { DatabaseSync } = require('node:sqlite');
const db = new DatabaseSync(':memory:');
db.exec(`
CREATE TABLE t3(x, y);
INSERT INTO t3 VALUES ('a', 4),
('b', 5),
('c', 3),
('d', 8),
('e', 1);
`);
db.aggregate('sumint', {
start: 0,
step: (acc, value) => acc + value,
});
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }
import { DatabaseSync } from 'node:sqlite';
const db = new DatabaseSync(':memory:');
db.exec(`
CREATE TABLE t3(x, y);
INSERT INTO t3 VALUES ('a', 4),
('b', 5),
('c', 3),
('d', 8),
('e', 1);
`);
db.aggregate('sumint', {
start: 0,
step: (acc, value) => acc + value,
});
db.prepare('SELECT sumint(y) as total FROM t3').get(); // { total: 21 }