chore: fix seeders

This commit is contained in:
Sonny
2025-08-04 19:56:37 +02:00
parent 7e7a010d5e
commit b45d06bbe0
4 changed files with 29 additions and 16 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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), {
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(),

View File

@@ -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, {
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,