thosakwe / feathers-seeder

Straightforward data seeder for Feathers.js services. Promise-based.
https://docs.feathersjs.com/guides/seeding-services.html
MIT License
38 stars 6 forks source link

Provide placeholder for Iterator number #8

Open eikaramba opened 7 years ago

eikaramba commented 7 years ago

Let's say i want to generate user_1@myservice.com to user_10@myservice.com. It would be nice to be able to specify the following template:

template: {
        email: 'user_{{index}}@myservice.com',
        password: 'test123'
      }
thosakwe commented 7 years ago

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 commented 7 years ago

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

eikaramba commented 7 years ago

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.

jamesholcomb commented 7 years ago

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.

eikaramba commented 7 years ago

Interestingly it works in this case.

path: 'users',
delete: false,
randomize: false,
templates: [{
   id: counter(),
   group: 'user',
   email: 'user@test.xxx',
   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.

jamesholcomb commented 7 years ago

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

Use counter or an arrow function per the example.

eikaramba commented 7 years ago

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

thosakwe commented 7 years ago

So, can this issue be closed?

eikaramba commented 7 years ago

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.