Skip to content

Instantly share code, notes, and snippets.

@Rowadz
Last active April 4, 2019 22:45
Show Gist options
  • Save Rowadz/5873d383eaa06f774e8fe85809883526 to your computer and use it in GitHub Desktop.
Save Rowadz/5873d383eaa06f774e8fe85809883526 to your computer and use it in GitHub Desktop.
/* eslint-disable no-console */
const faker = require('faker');
const fs = require('fs');
/**
*
* I want to add 30 records for each table we
* have with a recpect to the relations.
* and store the users in a json file to
* be able to login with them
*/
const seed = async app => {
try {
const users = [];
for (let i = 0; i < 30; i++) {
const u = {
email: faker.internet.email(),
password: faker.internet.password()
};
users.push(u);
await app.service('users').create(u);
}
fs.writeFile('src/users.json', JSON.stringify(users), 'utf8', () => {});
const arrayOfUsersIds = (await app.service('users').find({
query: {
$limit: 30,
$select: ['id']
}
})).data.map(idObj => idObj.id);
for (let i = 0; i < 30; i++) {
await app.service('posts').create({
text: faker.lorem.words(),
userId: faker.random.arrayElement(arrayOfUsersIds)
});
}
const arrayOfPostsIds = (await app.service('posts').find({
query: {
$limit: 30,
$select: ['id']
}
})).data.map(idObj => idObj.id);
for (let i = 0; i < 30; i++) {
await app.service('comments').create({
text: faker.lorem.words(),
userId: faker.random.arrayElement(arrayOfUsersIds),
postId: faker.random.arrayElement(arrayOfPostsIds)
});
}
} catch (error) {
console.error(error);
}
};
module.exports = seed;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment