method

sqlite.Database.aggregate

aggregate<T = any>(
name: string,
options: AggregateOptions<T>
): this;

Define a SQL aggregate function, such as sum() or group_concat(), that calls JavaScript functions. With inverse it is also a window function.

What is said about arguments, results, thrown values and the use of the Database in Database.function holds for each callback.

@param name

Name of the SQL function

@returns

The Database

db.aggregate("add_all", {
  start: 0,
  step: (total, value) => total + value,
});
db.query("SELECT add_all(dollars) AS total FROM expenses").get();
// => { total: 92 }

Referenced types

interface AggregateOptions<T = any>

Options for Database.aggregate

  • deterministic?: boolean

    Promise SQLite that the function returns the same result for the same arguments. SQLite then allows it in an index and in a generated column, and can call it fewer times.

    Equivalent to SQLITE_DETERMINISTIC

  • directOnly?: boolean

    Only SQL that is passed to the Database can call the function. A view, a trigger or another part of the schema cannot. Use it for a function with side effects when the database file comes from somebody else.

    Equivalent to SQLITE_DIRECTONLY

  • inverse?: null | (accumulator: T, ...args: any[]) => void | T

    Called for each row that leaves the window, with the arguments that step got for that row. It undoes step. Without it, the function cannot be used as a window function.

  • result?: null | (accumulator: T) => SQLFunctionResult

    Makes the result of the SQL function from the accumulator. Without it, the accumulator is the result. For a window function it is called once for each row, so it must leave the accumulator as it is.

  • safeIntegers?: boolean

    An INTEGER argument is a bigint instead of a number, and a bigint result that does not fit in a signed 64-bit integer throws a RangeError.

  • start?: T | () => T

    The accumulator that each group of rows starts with. A function is called for each group and returns it: use one for an object or an array, so that groups do not share it.

  • step: (accumulator: T, ...args: any[]) => void | T

    Called for each row of a group, with the accumulator and the arguments of the SQL function. What it returns is the new accumulator. When it returns undefined, the accumulator stays.

    The number of its parameters after the first one is the number of arguments the SQL function takes, unless varargs is set.

  • varargs?: boolean

    The SQL function takes any number of arguments, whatever the number of parameters of the JavaScript function.