mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 14:43:24 +00:00
chore: fix seeders
This commit is contained in:
3
Makefile
3
Makefile
@@ -13,6 +13,9 @@ prod:
|
||||
seed:
|
||||
@node ace db:seed
|
||||
|
||||
fresh:
|
||||
@node ace migration:fresh
|
||||
|
||||
down:
|
||||
@-docker compose down
|
||||
@-docker compose -f dev.compose.yml down
|
||||
|
||||
@@ -4,6 +4,8 @@ import User from '#user/models/user';
|
||||
import { BaseSeeder } from '@adonisjs/lucid/seeders';
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
const ID_OFFSET = 100;
|
||||
|
||||
export default class extends BaseSeeder {
|
||||
static environment = ['development', 'testing'];
|
||||
|
||||
@@ -11,7 +13,7 @@ export default class extends BaseSeeder {
|
||||
const users = await getUserIds();
|
||||
|
||||
const collections = faker.helpers.multiple(
|
||||
() => createRandomCollection(users),
|
||||
(_, index) => createRandomCollection(users, ID_OFFSET + index),
|
||||
{
|
||||
count: 50,
|
||||
}
|
||||
@@ -25,12 +27,10 @@ export async function getUserIds() {
|
||||
return users.map(({ id }) => id);
|
||||
}
|
||||
|
||||
let collectionId = 0;
|
||||
function createRandomCollection(userIds: User['id'][]) {
|
||||
function createRandomCollection(userIds: User['id'][], id: number) {
|
||||
const authorId = faker.helpers.arrayElements(userIds, 1).at(0);
|
||||
collectionId++;
|
||||
return {
|
||||
id: collectionId,
|
||||
id,
|
||||
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
|
||||
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
|
||||
visibility: Visibility.PRIVATE,
|
||||
|
||||
@@ -5,6 +5,8 @@ import User from '#user/models/user';
|
||||
import { BaseSeeder } from '@adonisjs/lucid/seeders';
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
const ID_OFFSET = 100;
|
||||
|
||||
export default class extends BaseSeeder {
|
||||
static environment = ['development', 'testing'];
|
||||
|
||||
@@ -12,9 +14,12 @@ export default class extends BaseSeeder {
|
||||
const users = await getUserIds();
|
||||
|
||||
const links = await Promise.all(
|
||||
faker.helpers.multiple(async () => createRandomLink(users), {
|
||||
count: 500,
|
||||
})
|
||||
faker.helpers.multiple(
|
||||
async (_, index) => createRandomLink(users, ID_OFFSET + index),
|
||||
{
|
||||
count: 500,
|
||||
}
|
||||
)
|
||||
);
|
||||
await Link.createMany(links.filter((a) => typeof a !== 'undefined') as any);
|
||||
}
|
||||
@@ -25,7 +30,7 @@ async function getCollectionIds(authorId: User['id']) {
|
||||
return collection.map(({ id }) => id);
|
||||
}
|
||||
|
||||
async function createRandomLink(userIds: User['id'][]) {
|
||||
async function createRandomLink(userIds: User['id'][], id: number) {
|
||||
const authorId = faker.helpers.arrayElements(userIds, 1).at(0)!;
|
||||
const collections = await getCollectionIds(authorId);
|
||||
|
||||
@@ -35,7 +40,7 @@ async function createRandomLink(userIds: User['id'][]) {
|
||||
}
|
||||
|
||||
return {
|
||||
id: faker.string.uuid(),
|
||||
id,
|
||||
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
|
||||
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
|
||||
url: faker.internet.url(),
|
||||
|
||||
@@ -3,22 +3,27 @@ import { GoogleToken } from '@adonisjs/ally/types';
|
||||
import { BaseSeeder } from '@adonisjs/lucid/seeders';
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
const ID_OFFSET = 100;
|
||||
|
||||
export default class extends BaseSeeder {
|
||||
static environment = ['development', 'testing'];
|
||||
|
||||
async run() {
|
||||
const users = faker.helpers.multiple(createRandomUser, {
|
||||
count: 25,
|
||||
});
|
||||
const users = faker.helpers.multiple(
|
||||
(_, index) => createRandomUser(ID_OFFSET + index),
|
||||
{
|
||||
count: 25,
|
||||
}
|
||||
);
|
||||
await User.createMany(users);
|
||||
}
|
||||
}
|
||||
|
||||
export function createRandomUser() {
|
||||
export function createRandomUser(index: number) {
|
||||
return {
|
||||
id: faker.number.int(),
|
||||
id: index,
|
||||
email: faker.internet.email(),
|
||||
name: faker.internet.userName(),
|
||||
name: faker.internet.username(),
|
||||
nickName: faker.internet.displayName(),
|
||||
avatarUrl: faker.image.avatar(),
|
||||
isAdmin: false,
|
||||
|
||||
Reference in New Issue
Block a user