How can I increment a counter variable in LoopBack 4 with a MongoDB datasource?

天大地大妈咪最大 提交于 2021-01-29 09:27:22

问题


I'm trying to convert my Nodejs Express app to Loopback 4 and I can't figure out how to increment a counter. In my Angular 9 app when a user clicks an icon a counter is incremented. This works perfectly in Express

In Express

const updateIconCount = async function (dataset, collection = 'icons') {
    let query = { _id: new ObjectId(dataset.id), userId: dataset.userId };
    return await mongoController.update(
        collection,
        query,
        { $inc: { counter: 1 } },
        function (err, res) {
            logAccess(res, 'debug', true, 'function update updateIconLink');
            if (err) {
                return false;
            } else {
                return true;
            }
        }
    );
};

I tried to first get the value of counter and then increment but every time I save VS Code reformats the code in an an unusual way. In this snippet I commented out the line of code that causes this reformatting. I can set the counter value, e.g. 100.

In Loopback 4

@patch('/icons/count/{id}', {
    responses: {
      '204': {
        description: 'Icons PATCH success',
      },
    },
  })
  async incrementCountById(
    @param.path.string('id') id: string,
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Icons, {partial: true}),
        },
      },
    })
    icons: Icons,
  ): Promise<void> {
    // let targetIcon = this.findById(id).then(icon => {return icon});
    icons.counter = 100;
    console.log(icons.counter);
    await this.iconsRepository.updateById(id, icons);
  }

How do I implement { $inc: { counter: 1 } } in Loopback 4?

Added to aid solution My mongo.datasource.ts

import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
import {juggler} from '@loopback/repository';

const config = {
  name: 'mongo',
  connector: 'mongodb',
  url: '',
  host: '192.168.253.53',
  port: 32813,
  user: '',
  password: '',
  database: 'firstgame',
  useNewUrlParser: true,
  allowExtendedOperators: true,
};

// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
@lifeCycleObserver('datasource')
export class MongoDataSource extends juggler.DataSource
  implements LifeCycleObserver {
  static dataSourceName = 'mongo';
  static readonly defaultConfig = config;

  constructor(
    @inject('datasources.config.mongo', {optional: true})
    dsConfig: object = config,
  ) {
    super(dsConfig);
  }
}

Amended endpoint

@patch('/icons/count/{id}', {
    responses: {
      '204': {
        description: 'Icons PATCH success',
      },
    },
  })
  async incrementCountById(
    @param.path.string('id') id: string,
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Icons, {partial: true}),
        },
      },
    })
    icons: Icons,
  ): Promise<void> {
    console.log(id);
    // @ts-ignore
    await this.iconsRepository.updateById(id, {$inc: {counter: 1}});//this line fails
    // icons.counter = 101; //these lines will set the icon counter to 101 so I know it is connecting to the mongodb
    // await this.iconsRepository.updateById(id, icons);
  }

回答1:


You can use the mongo update-operators.

Basically, you just have to set allowExtendedOperators=true at your MongoDB datasource definition (guide). After that, you can directly use these operators.

Usage example:

// increment icon.counter by 3
await this.iconsRepository.updateById(id, {$inc: {counter: 3}} as Partial<Counter>);

Currently, these operators are missing from the lb4 types so you must cheat typescript to accept them. It's ugly but that's the only solution I could find right now.

You can follow this issue to see what's going on with these operators.



来源:https://stackoverflow.com/questions/63006243/how-can-i-increment-a-counter-variable-in-loopback-4-with-a-mongodb-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!