2

WatermelonDB with ReactNative 0.64

I am trying to add a generate action inside the model like this:

class Events extends Model {
  static table = 'events';

  @field('event_at') event_at;
  @field('trigger') trigger;
  // other fields

  @action async generateRandomData() {
    console.log('ASYNC EVENT ACTION');
    const randomData = [];
    for (let i = 0; i < 10; i++) {
      randomData.push(
          // code to generate data
        ),
      );
    }
    return await this.batch(randomData);
  }
}

export default Events;

But how will be able to call the from inside a component function?

Things i have tried so far:

database.action(async () => {
      await events.generateRandomData();
 });
database.collections.get('events').generateRandomData()

and some other combinations... but in all places i was getting generateRandomData not defined.

I tried to find some help in the documentation and examples but couldn't find anything.

diganta das
  • 123
  • 1
  • 5

1 Answers1

0

Actions are deprecated use writer instead and it should work as expected https://nozbe.github.io/WatermelonDB/Writers.html

Ryker
  • 446
  • 3
  • 14