Maggregate
Bun

method

sqlite.DatabaseSync.aggregate

name: string,
): void;

Registers a new aggregate function with the SQLite database. This method is a wrapper around sqlite3_create_window_function().

When used as a window function, the result function will be called multiple times.

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 }
@param name

The name of the SQLite function to create.

@param options

Function configuration settings.

name: string,
options: AggregateOptions<T>
): void;

Registers a new aggregate function with the SQLite database. This method is a wrapper around sqlite3_create_window_function().

When used as a window function, the result function will be called multiple times.

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 }
@param name

The name of the SQLite function to create.

@param options

Function configuration settings.

Referenced types

interface AggregateOptions<T extends SQLInputValue = SQLInputValue>

  • deterministic?: boolean
  • directOnly?: boolean
  • inverse?: (accumulator: T, ...args: SQLOutputValue[]) => T

    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?: (accumulator: T) => SQLInputValue

    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.

  • start: T | () => T

    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: (accumulator: T, ...args: SQLOutputValue[]) => T

    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.

  • useBigIntArguments?: boolean

    If true, integer arguments to function are converted to BigInts. If false, integer arguments are passed as JavaScript numbers.

  • varargs?: boolean

    If true, function may be invoked with any number of arguments (between zero and SQLITE_MAX_FUNCTION_ARG). If false, function must be invoked with exactly function.length arguments.