feathers-seeder icon indicating copy to clipboard operation
feathers-seeder copied to clipboard

Provide placeholder for Iterator number

Open eikaramba opened this issue 7 years ago • 9 comments

Let's say i want to generate [email protected] to [email protected]. It would be nice to be able to specify the following template:

template: {
        email: 'user_{{index}}@myservice.com',
        password: 'test123'
      }

eikaramba avatar Dec 01 '16 20:12 eikaramba

Sounds good, will add this to next build.

Edit: Actually not sure this would be possible. You'll have to use a callback. In the next build, I'll add an app and service argument to be passed to callbacks, so you will be able to do this:

const cfg = {
  path: 'users':

  callback(user, seed, app, service) {
    return service.patch(user._id, {
      email: `user_${user._id}@myservice.com`
    });
  }
};

thosakwe avatar Dec 01 '16 21:12 thosakwe

Ah, I see what you mean, index is related to count. I'll add that as well.

thosakwe avatar Dec 01 '16 22:12 thosakwe

Wow what a quick response, respect :) I thought about implementing it myself, but wanted to first create a ticket.

Anyway, thx and yes {{index}} is just the iterator number of the count loop.

eikaramba avatar Dec 01 '16 22:12 eikaramba

I think you could do this with template functions [#11] now:

let index = 0;
const counter = () => ++index;

const template = {
  email: () => `user_${counter()}@myservice.com`
}

Give it a try and let us know.

jamesholcomb avatar Dec 04 '16 03:12 jamesholcomb

Interestingly it works in this case.

path: 'users',
delete: false,
randomize: false,
templates: [{
   id: counter(),
   group: 'user',
   email: '[email protected]',
   birthday: 1990,
   password: 'test123'
  }, {
   id: counter(),
   group: 'user',
   email: '{{internet.email}}',
   birthday: 1990,
   password: 'test123'
}],

but not in this case:

path: 'users',
count: 3,
delete: false,
template: {
id: counter(),
group: 'user',
email: '{{internet.email}}',
birthday: () => Math.floor(Math.random() * 100) + 1900,
gender: '{{random.boolean}}',
password: 'test123'

Here it is always the same value.

eikaramba avatar Dec 06 '16 20:12 eikaramba

It's because you passed the result of a function call.

Use counter or an arrow function per the example.

jamesholcomb avatar Dec 07 '16 00:12 jamesholcomb

i'm silly, sry you're right. Just strange that it worked in the above example.

eikaramba avatar Dec 07 '16 13:12 eikaramba

So, can this issue be closed?

thosakwe avatar Dec 07 '16 21:12 thosakwe

Well to be honest it can still be improved, just using {{index}} would be really great, but as a workaround this is legitimate. Maybe referecing this in the documentation at least.

eikaramba avatar Dec 07 '16 23:12 eikaramba