Ff00ff/mammoth

How to accept a list of columns as a parameter?

Closed this issue · 1 comments

I have a helper class that iterates over a table, returning a page of rows at a time. I'd like the class to accept a list of columns to project, e.g.

const lister = new UserLister(db);
const users = await lister.nextPageAsync([db.users.firstName, db.users.lastName]);

for (const user of users) {
    console.log(user.firstName);
}

Is there a way to write nextPageAsync? I've tried a couple things but eventually get stuck on trying to pass the list of columns to db.select(...). (I'd be fine using a limited amount of as any to get the body of nextPageAsync working.)

Possibly a tangent, but Mammoth's variable-length methods like .select(...) are implemented by having separate overrides for 1-75 arguments. Is it possible to write that with TS 4's variadic tuple types? Would that make these kinds of things easier?

This is fixed. You can now use GetColumn<T> helper type to get the types of the columns from a table e.g. GetColumn<typeof db.users>.

You can then write your own functions to accept these columns. You must pass these to the select call explicitly so the return type can be correctly inferred.

const selectFromFoo = <T extends GetColumn<typeof db.foo>[]>(...columns: [...T]) => {
  return db.select<T>(...columns).from(db.foo);
};