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> If true, the SQLITE_DETERMINISTIC flag is set on the created function. Default: false.

    • directOnly <boolean> 如果是 true,则在创建的函数上设置 SQLITE_DIRECTONLY 标志。默认值:false

      ¥directOnly <boolean> If true, the SQLITE_DIRECTONLY flag is set on the created function. Default: false.

    • useBigIntArguments <boolean> 如果使用 true,则 options.stepoptions.inverse 的整数参数将转换为 BigInt。如果 false,则整数参数作为 JavaScript 数字传递。默认值:false

      ¥useBigIntArguments <boolean> If true, integer arguments to options.step and options.inverse are converted to BigInts. If false, integer arguments are passed as JavaScript numbers. Default: false.

    • varargs <boolean> 如果 trueoptions.stepoptions.inverse 可以使用任意数量的参数(介于 0 到 SQLITE_MAX_FUNCTION_ARG 之间)调用。如果 falseinversestep 必须使用恰好 length 个参数调用。默认值:false

      ¥varargs <boolean> If true, options.step and options.inverse may be invoked with any number of arguments (between zero and SQLITE_MAX_FUNCTION_ARG). If false, inverse and step must be invoked with exactly length 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, the aggregate 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 }