Loopback Mixin for generating random data on model properties
npm install loopback-faker-mixinA mixin for loopback using the faker library.
In your ``model.json``js
{
"name": "Customer",
"base": "User"
"properties": {
"name": {
"type": "string",
faker: 'faker.method' || { method: 'faker.method', args: ['arg1']} see https://github.com/marak/Faker.js/
}
},
"mixins": {
"Faker": {
"email": "internet.email",
"password": "internet.password"
}
}
}
`
After the app has initialised you are able to access the static faker method via
`js
let { Customer } = app.models
Customer.faker({
name: 'New Name' // this will overwrite the faker fakeness if you want
})
.then(customer => {
// customer with faked data
})
Customer.bulkFaker({
global: {
name: 'Every One Gets this Name'
},
individuals: [
{
name: 'except this guy... this is his name'
}
]
}, 10).then(customers => {
let individual = customers[0]
customers.length === 10
individual.name === 'except this guy... this is his name'
})
`Motivation
A simple way to create random data for testing and seeding
`npm install faker loopback-faker-mixin`
=============
Add the mixins property to your server/model-config.json:
`json``
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"
"../common/mixins"
]
}
}
|
:---: |:---: |:---: |:---: |:---: |:---: |
Henry Stevens |