WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.46k stars 396 forks source link

Docs question: how to implement multiple arguments with aggregate function #1038

Closed nchilla closed 1 year ago

nchilla commented 1 year ago

Thanks so much for this amazing lib! For .aggregate, the docs mention that:

Just like regular user-defined functions, user-defined aggregates can accept multiple arguments

But it doesn't seem to specify/show an example of how additional arguments can be included. My assumption is that they go after the first two parameters in the step function, e.g.:

db.aggregate('addAllCustom', {
  start: 0,
  step: (total, nextValue, customArgument) => total + nextValue + customArgument,
});

But I'd appreciation confirmation on that (or clarification if I'm confused and it actually works differently)

JoshuaWise commented 1 year ago

Your assumption is correct. The first parameter of step() is always the current aggregate value, and all of the remaining parameters of step() are the arguments passed to your user-defined aggregate function.