feathers-plus / generator-feathers-plus

A Yeoman generator to (re)generate a FeathersJS application supporting both REST and GraphQL architectural concepts and their query languages.
https://generator.feathers-plus.com/
Other
44 stars 30 forks source link

Feature Request: index of fake #254

Closed florianbepunkt closed 5 years ago

florianbepunkt commented 5 years ago

This is a feature request: Would it be possible to add an index of the current fake record to the ctx property?

Use case example:

I would like to create sets of currency rates for the last three months. Right now I have these two properties that determine the time range for which a currency set is valid:

validFrom: {
      $id: '#/properties/validFrom',
      faker: {
        exp: 'ctx.moment().subtract(ctx.ix, "month").startOf("month").format()'
      },
      format: 'date-time',
      type: 'string'
    },
    validTo: {
      $id: '#/properties/validTo',
      faker: {
        exp: 'ctx.moment().subtract(ctx.ix, "month").endOf("month").format()'
      },
      format: 'date-time',
      type: 'string'
    }

the ctx.ix property does currently not exist, but it would help a lot in similar scenarios where you would need to access previously created fakes or have sequential changes to your fake data.

So for example you set fakeRecords to 3. then ctx.ix would return the current index of the to be created fake record.

eddyystop commented 5 years ago

You need to be more clear. What ctx where are you referring to?

florianbepunkt commented 5 years ago

From the docs at: https://generator.feathers-plus.com/get-started/#expressions

Other conveniences are also passed to the expression in the ctx object.

- ctx.data (Array< Object >) - All the generated records for all the services. Values for foreign keys and expressions have been calculated up to the current record.
- ctx.hashPassword (Function(password)) - Calculates a password hash compatible with Feathers' hashPassword hook. This password will work correctly with Feathers local authentication.
- fakeData.expContext from module config/default.js is also merged into ctx. You can refer to any of those merged properties in the expression.
- ctx.tablesInfo {Object) - A structure you can use to determine the current shuffled key for each service. Its structure is:

It would be helpful if the ctx object conatined an index of the current record.

eddyystop commented 5 years ago

Expressions are calculated in https://github.com/feathers-plus/seeder-foreign-keys/blob/master/lib/index.js#L117-L119 . You probably have to change https://github.com/feathers-plus/seeder-foreign-keys/blob/master/lib/index.js#L88 to something like

tableNames.forEach((tableName, i) => {
  ctx.currIndex = i;

A PR would be welcome. That's the way Open Source is supposed to work.

florianbepunkt commented 5 years ago

@eddyystop Thank you. Sure, that's how it's supposed to work. Sorry, if this came across the wrong way. I didn't know whether this was already implemented in some way or not. Thanks for pointing out where to start. I'll have a go with it and see if I can come up with a PR.

eddyystop commented 5 years ago

Thanks much.

dataCurrIndex is probably a better name than currIndex.

Many Open Source maintainers think the OS model is broken. Some of the milder articles: https://motherboard.vice.com/en_us/article/43zak3/the-internet-was-built-on-the-free-labor-of-open-source-developers-is-that-sustainable https://blog.tidelift.com/open-source-has-a-working-for-free-problem?utm_source=hs_email&utm_medium=email&utm_content=70566283

We're tired of people asking for changes who say THEY are too busy to submit a PR. So Marshall and I are encouraging/coercing people to submit PRs rather than have us just go away.

Please add a test like https://github.com/feathers-plus/seeder-foreign-keys/blob/master/tests/expressions.test.js#L51-L74 . Maybe by using a ctx.dataCurrIndex expression to set a field equal to the record key. Afterwards you can check the records to see they are correct.

Please also update the docs https://github.com/feathers-plus/generator-feathers-plus/blob/master/docs/get-started/README.md#expressions