Export of forest-express-mongoose QueryBuilder

Feature(s) impacted

I’m developing a custom Smart Action where it is required to parse the query into mongo aggregation pipeline. Then I can use that pipeline to query my model.

Actually I was trying to use RecordsGetter class to get all the ids of the query by using RecordsGetter.getIdsFromRequest function, but this class requires a user object in its constructor, which I don’t have.

For the reason I don’t have this user object, we are setting up an custom authentication mechanism and this unfortunately couldn’t be changed from my side.

Observed behavior

Currently QueryBuilder class is not exported in forest-express-mongoose.

Expected behavior

forest-express-mongoose should export QueryBuilder class for a more flexible use.

Context

  • Agent (forest package) name & version: “forest-express-mongoose”: “^9.3.2”
  • Database type: mongodb

Or event better, is there a way to bypass the user params of RecordsGetter class constructor?

class RecordsGetter {
  constructor(model, opts, params, user) {

because we really don’t have the user param here, missing of which will cause the RecordsGetter.getIdsFromRequest to throw “Missing required user” error

Hello @Thong_Dang,

If I’m not mistaken, it should be:

const ids = await new RecordsGetter([yourMongooseModel], request.user, request.query)
          .getIdsFromRequest(request);

https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions/use-a-smart-action-form#get-selected-records-with-bulk-action
Another example

Let me know if it helps. :pray:

Kind regards,
Morgan

Hi, thanks for the reply.

In my case the request.user is undefined.

But I found a workaround. We can create a record getter using this:

const recordGetter = new RecordsGetter(NewPaymentModel, { renderingId: forestEnvId } as User, ctx.query);

It was not obvious what renderingId was, but then I digged deep into to the source code & found out that it is the ID of the project environment.

We can let this case close :slight_smile:

1 Like

Hello @Thong_Dang,

Great that it works but it’s pretty weird that the user (request.user) is undefined. You are probably missing the ensureAuthenticated middleware on this route which is also weird.

Example of globally mounted ensureAuthenticatedmiddleware

const {
  ensureAuthenticated,
  PUBLIC_ROUTES,
} = require('forest-express-mongoose');

...


app.use('/forest', (request, response, next) => {
  if (PUBLIC_ROUTES.includes(request.url)) {
    return next();
  }
  return ensureAuthenticated(request, response, next);
});

So you probably don’t have set the permissionMiddlewareCreator.smartAction() neither which is not great. :eyes:

https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions

Kind regards,
Morgan