@@ -119,6 +119,85 @@ added: v22.5.0
119
119
120
120
Constructs a new ` DatabaseSync ` instance.
121
121
122
+ ### ` database.aggregate(name, options) `
123
+
124
+ <!-- YAML
125
+ added: REPLACEME
126
+ -->
127
+
128
+ Registers a new aggregate function with the SQLite database. This method is a wrapper around
129
+ [ ` sqlite3_create_window_function() ` ] [ ] .
130
+
131
+ * ` name ` {string} The name of the SQLite function to create.
132
+ * ` options ` {Object} Function configuration settings.
133
+ * ` deterministic ` {boolean} If ` true ` , the [ ` SQLITE_DETERMINISTIC ` ] [ ] flag is
134
+ set on the created function. ** Default:** ` false ` .
135
+ * ` directOnly ` {boolean} If ` true ` , the [ ` SQLITE_DIRECTONLY ` ] [ ] flag is set on
136
+ the created function. ** Default:** ` false ` .
137
+ * ` useBigIntArguments ` {boolean} If ` true ` , integer arguments to ` options.step ` and ` options.inverse `
138
+ are converted to ` BigInt ` s. If ` false ` , integer arguments are passed as
139
+ JavaScript numbers. ** Default:** ` false ` .
140
+ * ` varargs ` {boolean} If ` true ` , ` options.step ` and ` options.inverse ` may be invoked with any number of
141
+ arguments (between zero and [ ` SQLITE_MAX_FUNCTION_ARG ` ] [ ] ). If ` false ` ,
142
+ ` inverse ` and ` step ` must be invoked with exactly ` length ` arguments.
143
+ ** Default:** ` false ` .
144
+ * ` start ` {number | string | null | Array | Object | Function} The identity
145
+ value for the aggregation function. This value is used when the aggregation
146
+ function is initialized. When a {Function} is passed the identity will be its return value.
147
+ * ` step ` {Function} The function to call for each row in the aggregation. The
148
+ function receives the current state and the row value. The return value of
149
+ this function should be the new state.
150
+ * ` result ` {Function} The function to call to get the result of the
151
+ aggregation. The function receives the final state and should return the
152
+ result of the aggregation.
153
+ * ` inverse ` {Function} When this function is provided, the ` aggregate ` method will work as a window function.
154
+ The function receives the current state and the dropped row value. The return value of this function should be the
155
+ new state.
156
+
157
+ When used as a window function, the ` result ` function will be called multiple times.
158
+
159
+ ``` cjs
160
+ const { DatabaseSync } = require (' node:sqlite' );
161
+
162
+ const db = new DatabaseSync (' :memory:' );
163
+ db .exec (`
164
+ CREATE TABLE t3(x, y);
165
+ INSERT INTO t3 VALUES ('a', 4),
166
+ ('b', 5),
167
+ ('c', 3),
168
+ ('d', 8),
169
+ ('e', 1);
170
+ ` );
171
+
172
+ db .aggregate (' sumint' , {
173
+ start: 0 ,
174
+ step : (acc , value ) => acc + value,
175
+ });
176
+
177
+ db .prepare (' SELECT sumint(y) as total FROM t3' ).get (); // { total: 21 }
178
+ ```
179
+
180
+ ``` mjs
181
+ import { DatabaseSync } from ' node:sqlite' ;
182
+
183
+ const db = new DatabaseSync (' :memory:' );
184
+ db .exec (`
185
+ CREATE TABLE t3(x, y);
186
+ INSERT INTO t3 VALUES ('a', 4),
187
+ ('b', 5),
188
+ ('c', 3),
189
+ ('d', 8),
190
+ ('e', 1);
191
+ ` );
192
+
193
+ db .aggregate (' sumint' , {
194
+ start: 0 ,
195
+ step : (acc , value ) => acc + value,
196
+ });
197
+
198
+ db .prepare (' SELECT sumint(y) as total FROM t3' ).get (); // { total: 21 }
199
+ ```
200
+
122
201
### ` database.close() `
123
202
124
203
<!-- YAML
@@ -728,6 +807,7 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
728
807
[ `sqlite3_column_origin_name()` ] : https://www.sqlite.org/c3ref/column_database_name.html
729
808
[ `sqlite3_column_table_name()` ] : https://www.sqlite.org/c3ref/column_database_name.html
730
809
[ `sqlite3_create_function_v2()` ] : https://www.sqlite.org/c3ref/create_function.html
810
+ [ `sqlite3_create_window_function()` ] : https://www.sqlite.org/c3ref/create_function.html
731
811
[ `sqlite3_exec()` ] : https://www.sqlite.org/c3ref/exec.html
732
812
[ `sqlite3_expanded_sql()` ] : https://www.sqlite.org/c3ref/expanded_sql.html
733
813
[ `sqlite3_get_autocommit()` ] : https://sqlite.org/c3ref/get_autocommit.html
0 commit comments