mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
chore: create user, collection and link models, migrations and seeders
This commit is contained in:
3
Makefile
3
Makefile
@@ -7,3 +7,6 @@ dev:
|
|||||||
prod:
|
prod:
|
||||||
docker compose -f dev.docker-compose.yml down
|
docker compose -f dev.docker-compose.yml down
|
||||||
docker compose up -d --build --wait
|
docker compose up -d --build --wait
|
||||||
|
|
||||||
|
seed:
|
||||||
|
node ace db:seed
|
||||||
|
|||||||
3
app/controllers/collections_controller.ts
Normal file
3
app/controllers/collections_controller.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
// import type { HttpContext } from '@adonisjs/core/http';
|
||||||
|
|
||||||
|
export default class CollectionsController {}
|
||||||
@@ -1,5 +1,29 @@
|
|||||||
import { BaseModel, CamelCaseNamingStrategy } from '@adonisjs/lucid/orm';
|
import { BaseModel, CamelCaseNamingStrategy, beforeCreate, column } from '@adonisjs/lucid/orm';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
export default class AppBaseModel extends BaseModel {
|
export default class AppBaseModel extends BaseModel {
|
||||||
static namingStrategy = new CamelCaseNamingStrategy();
|
static namingStrategy = new CamelCaseNamingStrategy();
|
||||||
|
static selfAssignPrimaryKey = true;
|
||||||
|
|
||||||
|
@column({ isPrimary: true })
|
||||||
|
declare id: string; // UUID
|
||||||
|
|
||||||
|
@column.dateTime({
|
||||||
|
autoCreate: true,
|
||||||
|
serializeAs: 'createdAt',
|
||||||
|
})
|
||||||
|
declare createdAt: DateTime;
|
||||||
|
|
||||||
|
@column.dateTime({
|
||||||
|
autoCreate: true,
|
||||||
|
autoUpdate: true,
|
||||||
|
serializeAs: 'updatedAt',
|
||||||
|
})
|
||||||
|
declare updatedAt: DateTime;
|
||||||
|
|
||||||
|
@beforeCreate()
|
||||||
|
static assignUuid(item: any) {
|
||||||
|
item.id = uuidv4();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
33
app/models/collection.ts
Normal file
33
app/models/collection.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import AppBaseModel from '#models/app_base_model';
|
||||||
|
import Link from '#models/link';
|
||||||
|
import User from '#models/user';
|
||||||
|
import { belongsTo, column, manyToMany } from '@adonisjs/lucid/orm';
|
||||||
|
import type { BelongsTo, ManyToMany } from '@adonisjs/lucid/types/relations';
|
||||||
|
|
||||||
|
export default class Collection extends AppBaseModel {
|
||||||
|
@column()
|
||||||
|
declare name: string;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare description: string | null;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare visibility: Visibility;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare nextId: string;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare authorId: string;
|
||||||
|
|
||||||
|
@belongsTo(() => User, { foreignKey: 'authorId' })
|
||||||
|
declare author: BelongsTo<typeof User>;
|
||||||
|
|
||||||
|
@manyToMany(() => Link)
|
||||||
|
declare links: ManyToMany<typeof Link>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum Visibility {
|
||||||
|
PUBLIC = 'PUBLIC',
|
||||||
|
PRIVATE = 'PRIVATE',
|
||||||
|
}
|
||||||
31
app/models/link.ts
Normal file
31
app/models/link.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import AppBaseModel from '#models/app_base_model';
|
||||||
|
import Collection from '#models/collection';
|
||||||
|
import User from '#models/user';
|
||||||
|
import { belongsTo, column } from '@adonisjs/lucid/orm';
|
||||||
|
import type { BelongsTo } from '@adonisjs/lucid/types/relations';
|
||||||
|
|
||||||
|
export default class Link extends AppBaseModel {
|
||||||
|
@column()
|
||||||
|
declare name: string;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare description: string;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare url: string;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare favorite: boolean;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare collectionId: string;
|
||||||
|
|
||||||
|
@belongsTo(() => Collection, { foreignKey: 'collectionId' })
|
||||||
|
declare collection: BelongsTo<typeof Collection>;
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare authorId: string;
|
||||||
|
|
||||||
|
@belongsTo(() => User, { foreignKey: 'authorId' })
|
||||||
|
declare author: BelongsTo<typeof User>;
|
||||||
|
}
|
||||||
@@ -1,15 +1,10 @@
|
|||||||
|
import Collection from '#models/collection';
|
||||||
import type { GoogleToken } from '@adonisjs/ally/types';
|
import type { GoogleToken } from '@adonisjs/ally/types';
|
||||||
import { beforeCreate, column } from '@adonisjs/lucid/orm';
|
import { column, manyToMany } from '@adonisjs/lucid/orm';
|
||||||
import { DateTime } from 'luxon';
|
import type { ManyToMany } from '@adonisjs/lucid/types/relations';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
|
||||||
import AppBaseModel from './app_base_model.js';
|
import AppBaseModel from './app_base_model.js';
|
||||||
|
|
||||||
export default class User extends AppBaseModel {
|
export default class User extends AppBaseModel {
|
||||||
static selfAssignPrimaryKey = true;
|
|
||||||
|
|
||||||
@column({ isPrimary: true })
|
|
||||||
declare id: string; // UUID
|
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
declare email: string;
|
declare email: string;
|
||||||
|
|
||||||
@@ -22,6 +17,7 @@ export default class User extends AppBaseModel {
|
|||||||
@column({ serializeAs: 'avatarUrl' })
|
@column({ serializeAs: 'avatarUrl' })
|
||||||
declare avatarUrl: string;
|
declare avatarUrl: string;
|
||||||
|
|
||||||
|
@column()
|
||||||
declare isAdmin: boolean;
|
declare isAdmin: boolean;
|
||||||
|
|
||||||
@column({ serializeAs: null })
|
@column({ serializeAs: null })
|
||||||
@@ -30,24 +26,8 @@ export default class User extends AppBaseModel {
|
|||||||
@column({ serializeAs: null })
|
@column({ serializeAs: null })
|
||||||
declare providerId: string;
|
declare providerId: string;
|
||||||
|
|
||||||
@column.dateTime({
|
@manyToMany(() => Collection, {
|
||||||
autoCreate: true,
|
relatedKey: 'authorId',
|
||||||
serialize: (value: DateTime) => value.toISODate(),
|
|
||||||
serializeAs: 'createdAt',
|
|
||||||
})
|
})
|
||||||
declare createdAt: DateTime;
|
declare collections: ManyToMany<typeof Collection>;
|
||||||
|
|
||||||
@column.dateTime({
|
|
||||||
autoCreate: true,
|
|
||||||
autoUpdate: true,
|
|
||||||
serialize: (value: DateTime) => value.toISODate(),
|
|
||||||
serializeAs: 'updatedAt',
|
|
||||||
})
|
|
||||||
declare updatedAt: DateTime;
|
|
||||||
|
|
||||||
@beforeCreate()
|
|
||||||
static assignUuid(user: User) {
|
|
||||||
user.id = uuidv4();
|
|
||||||
user.isAdmin = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ const dbConfig = defineConfig({
|
|||||||
naturalSort: true,
|
naturalSort: true,
|
||||||
paths: ['database/migrations'],
|
paths: ['database/migrations'],
|
||||||
},
|
},
|
||||||
|
seeders: {
|
||||||
|
paths: ['./database/seeders/main'],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ export default class extends BaseSchema {
|
|||||||
|
|
||||||
async up() {
|
async up() {
|
||||||
this.schema.createTable(this.tableName, (table) => {
|
this.schema.createTable(this.tableName, (table) => {
|
||||||
table.string('id').notNullable();
|
table.uuid('id').notNullable();
|
||||||
|
|
||||||
table.string('email', 254).notNullable().unique();
|
table.string('email', 254).notNullable().unique();
|
||||||
table.string('name', 254).notNullable();
|
table.string('name', 254).notNullable();
|
||||||
table.string('nick_name', 254).notNullable();
|
table.string('nick_name', 254).notNullable();
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
database/migrations/1714254076754_create_links_table.ts
Normal file
25
database/migrations/1714254076754_create_links_table.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
34
database/seeders/collection_seeder.ts
Normal file
34
database/seeders/collection_seeder.ts
Normal 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
47
database/seeders/link_seeder.ts
Normal file
47
database/seeders/link_seeder.ts
Normal 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
33
database/seeders/main/index_seeder.ts
Normal file
33
database/seeders/main/index_seeder.ts
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
28
database/seeders/user_seeder.ts
Normal file
28
database/seeders/user_seeder.ts
Normal 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
"Servers": {
|
"Servers": {
|
||||||
"1": {
|
"1": {
|
||||||
"Name": "project",
|
"Name": "project",
|
||||||
"Group": "Server Group 1",
|
"Group": "Servers",
|
||||||
"Port": 5432,
|
"Port": 5432,
|
||||||
"Username": "postgres",
|
"Username": "postgres",
|
||||||
"Host": "postgres",
|
"Host": "postgres",
|
||||||
|
|||||||
1
package-lock.json
generated
1
package-lock.json
generated
@@ -36,6 +36,7 @@
|
|||||||
"@adonisjs/eslint-config": "^1.3.0",
|
"@adonisjs/eslint-config": "^1.3.0",
|
||||||
"@adonisjs/tsconfig": "^1.3.0",
|
"@adonisjs/tsconfig": "^1.3.0",
|
||||||
"@emotion/babel-plugin": "^11.11.0",
|
"@emotion/babel-plugin": "^11.11.0",
|
||||||
|
"@faker-js/faker": "^8.4.1",
|
||||||
"@japa/assert": "^3.0.0",
|
"@japa/assert": "^3.0.0",
|
||||||
"@japa/plugin-adonisjs": "^3.0.1",
|
"@japa/plugin-adonisjs": "^3.0.1",
|
||||||
"@japa/runner": "^3.1.4",
|
"@japa/runner": "^3.1.4",
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
"@adonisjs/eslint-config": "^1.3.0",
|
"@adonisjs/eslint-config": "^1.3.0",
|
||||||
"@adonisjs/tsconfig": "^1.3.0",
|
"@adonisjs/tsconfig": "^1.3.0",
|
||||||
"@emotion/babel-plugin": "^11.11.0",
|
"@emotion/babel-plugin": "^11.11.0",
|
||||||
|
"@faker-js/faker": "^8.4.1",
|
||||||
"@japa/assert": "^3.0.0",
|
"@japa/assert": "^3.0.0",
|
||||||
"@japa/plugin-adonisjs": "^3.0.1",
|
"@japa/plugin-adonisjs": "^3.0.1",
|
||||||
"@japa/runner": "^3.1.4",
|
"@japa/runner": "^3.1.4",
|
||||||
|
|||||||
Reference in New Issue
Block a user