Open eikaramba opened 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`
});
}
};
Ah, I see what you mean, index
is related to count
. I'll add that as well.
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.
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.
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.
It's because you passed the result of a function call.
Use counter
or an arrow function per the example.
i'm silly, sry you're right. Just strange that it worked in the above example.
So, can this issue be closed?
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.
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: