method
sqlite.DatabaseSync.aggregate
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 }The name of the SQLite function to create.
Function configuration settings.
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 }The name of the SQLite function to create.
Function configuration settings.
Referenced types
interface AggregateOptions<T extends SQLInputValue = SQLInputValue>
- inverse?: (accumulator: T, ...args: SQLOutputValue[]) => T
When this function is provided, the
aggregatemethod 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
Functionis 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 tofunctionare converted toBigInts. Iffalse, integer arguments are passed as JavaScript numbers. - varargs?: boolean
If
true,functionmay be invoked with any number of arguments (between zero andSQLITE_MAX_FUNCTION_ARG). Iffalse,functionmust be invoked with exactlyfunction.lengtharguments.