chore: create user, collection and link models, migrations and seeders

This commit is contained in:
Sonny
2024-04-28 00:45:10 +02:00
committed by Sonny
parent 08dcd7455f
commit 231629f0dd
17 changed files with 311 additions and 32 deletions

View File

@@ -5,7 +5,8 @@ export default class extends BaseSchema {
async up() {
this.schema.createTable(this.tableName, (table) => {
table.string('id').notNullable();
table.uuid('id').notNullable();
table.string('email', 254).notNullable().unique();
table.string('name', 254).notNullable();
table.string('nick_name', 254).notNullable();

View File

@@ -0,0 +1,32 @@
import { Visibility } from '#models/collection';
import { BaseSchema } from '@adonisjs/lucid/schema';
export default class extends BaseSchema {
protected tableName = 'collections';
private visibilityEnumName = 'collection_visibility';
async up() {
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
this.schema.createTable(this.tableName, (table) => {
table.uuid('id').notNullable();
table.string('name', 254).notNullable();
table.string('description', 254);
table.uuid('next_id').notNullable();
table.uuid('author_id').notNullable();
table.enum('visibility', Object.values(Visibility), {
useNative: true,
enumName: this.visibilityEnumName,
existingType: false,
});
table.timestamp('created_at');
table.timestamp('updated_at');
});
}
async down() {
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
this.schema.dropTable(this.tableName);
}
}

View File

@@ -0,0 +1,25 @@
import { BaseSchema } from '@adonisjs/lucid/schema';
export default class extends BaseSchema {
protected tableName = 'links';
async up() {
this.schema.createTable(this.tableName, (table) => {
table.uuid('id').notNullable();
table.string('name', 254).notNullable();
table.string('description', 254);
table.text('url').notNullable();
table.boolean('favorite').notNullable().defaultTo(0);
table.uuid('collection_id').notNullable();
table.uuid('author_id').notNullable();
table.timestamp('created_at');
table.timestamp('updated_at');
});
}
async down() {
this.schema.dropTable(this.tableName);
}
}

View File

@@ -0,0 +1,34 @@
import Collection from '#models/collection';
import User from '#models/user';
import { BaseSeeder } from '@adonisjs/lucid/seeders';
import { faker } from '@faker-js/faker';
export default class extends BaseSeeder {
static environment = ['development', 'testing'];
async run() {
// eslint-disable-next-line unicorn/no-await-expression-member
const users = await getUserIds();
const collections = faker.helpers.multiple(() => createRandomCollection(users), {
count: 50,
});
await Collection.createMany(collections);
}
}
export async function getUserIds() {
const users = await User.all();
return users.map(({ id }) => id);
}
function createRandomCollection(userIds: User['id'][]) {
const authorId = faker.helpers.arrayElements(userIds, 1).at(0);
return {
id: faker.string.uuid(),
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
nextId: faker.string.uuid(),
authorId,
};
}

View File

@@ -0,0 +1,47 @@
import { getUserIds } from '#database/seeders/collection_seeder';
import Collection from '#models/collection';
import Link from '#models/link';
import User from '#models/user';
import { BaseSeeder } from '@adonisjs/lucid/seeders';
import { faker } from '@faker-js/faker';
export default class extends BaseSeeder {
static environment = ['development', 'testing'];
async run() {
// eslint-disable-next-line unicorn/no-await-expression-member
const users = await getUserIds();
const links = await Promise.all(
faker.helpers.multiple(async () => createRandomLink(users), {
count: 500,
})
);
await Link.createMany(links.filter((a) => typeof a !== 'undefined') as any);
}
}
async function getCollectionIds(authorId: User['id']) {
const collection = await Collection.findManyBy('author_id', authorId);
return collection.map(({ id }) => id);
}
async function createRandomLink(userIds: User['id'][]) {
const authorId = faker.helpers.arrayElements(userIds, 1).at(0)!;
const collections = await getCollectionIds(authorId);
const collectionId = faker.helpers.arrayElements(collections, 1).at(0);
if (!collectionId) {
return undefined;
}
return {
id: faker.string.uuid(),
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
url: faker.internet.url(),
favorite: faker.number.int({ min: 0, max: 1 }),
authorId,
collectionId,
};
}

View File

@@ -0,0 +1,33 @@
import app from '@adonisjs/core/services/app';
import logger from '@adonisjs/core/services/logger';
import { BaseSeeder } from '@adonisjs/lucid/seeders';
export default class IndexSeeder extends BaseSeeder {
private async seed(Seeder: { default: typeof BaseSeeder }) {
/**
* Do not run when not in a environment specified in Seeder
*/
if (
!Seeder.default.environment ||
(!Seeder.default.environment.includes('development') && app.inDev) ||
(!Seeder.default.environment.includes('testing') && app.inTest) ||
(!Seeder.default.environment.includes('production') && app.inProduction)
) {
return;
}
await new Seeder.default(this.client).run();
}
async run() {
logger.info('Start user seed');
await this.seed(await import('#database/seeders/user_seeder'));
logger.info('User seed done');
logger.info('Collection user seed');
await this.seed(await import('#database/seeders/collection_seeder'));
logger.info('Collection seed done');
logger.info('Link user seed');
await this.seed(await import('#database/seeders/link_seeder'));
logger.info('Link seed done');
}
}

View File

@@ -0,0 +1,28 @@
import User from '#models/user';
import { GoogleToken } from '@adonisjs/ally/types';
import { BaseSeeder } from '@adonisjs/lucid/seeders';
import { faker } from '@faker-js/faker';
export default class extends BaseSeeder {
static environment = ['development', 'testing'];
async run() {
const users = faker.helpers.multiple(createRandomUser, {
count: 25,
});
await User.createMany(users);
}
}
export function createRandomUser() {
return {
id: faker.string.uuid(),
email: faker.internet.email(),
name: faker.internet.userName(),
nickName: faker.internet.displayName(),
avatarUrl: faker.image.avatar(),
isAdmin: false,
providerId: faker.string.uuid(),
token: {} as GoogleToken,
};
}