fix: relations between tables

This commit is contained in:
Sonny
2024-04-28 23:25:01 +02:00
committed by Sonny
parent 97044907ee
commit 31f22d382e
6 changed files with 59 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
import PATHS from '#constants/paths';
import type { Authenticators } from '@adonisjs/auth/types';
import type { HttpContext } from '@adonisjs/core/http'; import type { HttpContext } from '@adonisjs/core/http';
import type { NextFn } from '@adonisjs/core/types/http'; import type { NextFn } from '@adonisjs/core/types/http';
import type { Authenticators } from '@adonisjs/auth/types';
/** /**
* Auth middleware is used authenticate HTTP requests and deny * Auth middleware is used authenticate HTTP requests and deny
@@ -10,7 +11,7 @@ export default class AuthMiddleware {
/** /**
* The URL to redirect to, when authentication fails * The URL to redirect to, when authentication fails
*/ */
redirectTo = '/login'; redirectTo = PATHS.AUTH.LOGIN;
async handle( async handle(
ctx: HttpContext, ctx: HttpContext,
@@ -19,7 +20,9 @@ export default class AuthMiddleware {
guards?: (keyof Authenticators)[]; guards?: (keyof Authenticators)[];
} = {} } = {}
) { ) {
await ctx.auth.authenticateUsing(options.guards, { loginRoute: this.redirectTo }); await ctx.auth.authenticateUsing(options.guards, {
loginRoute: this.redirectTo,
});
return next(); return next();
} }
} }

View File

@@ -1,4 +1,5 @@
import Collection from '#models/collection'; import Collection from '#models/collection';
import Link from '#models/link';
import type { GoogleToken } from '@adonisjs/ally/types'; import type { GoogleToken } from '@adonisjs/ally/types';
import { column, manyToMany } from '@adonisjs/lucid/orm'; import { column, manyToMany } from '@adonisjs/lucid/orm';
import type { ManyToMany } from '@adonisjs/lucid/types/relations'; import type { ManyToMany } from '@adonisjs/lucid/types/relations';
@@ -30,4 +31,9 @@ export default class User extends AppBaseModel {
relatedKey: 'authorId', relatedKey: 'authorId',
}) })
declare collections: ManyToMany<typeof Collection>; declare collections: ManyToMany<typeof Collection>;
@manyToMany(() => Link, {
relatedKey: 'authorId',
})
declare links: ManyToMany<typeof Link>;
} }

View File

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

View File

@@ -8,12 +8,20 @@ export default class extends BaseSchema {
async up() { async up() {
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`); this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
this.schema.createTable(this.tableName, (table) => { this.schema.createTable(this.tableName, (table) => {
table.uuid('id').notNullable(); table.uuid('id').unique().notNullable();
table.string('name', 254).notNullable(); table.string('name', 254).notNullable();
table.string('description', 254); table.string('description', 254);
table.uuid('next_id').defaultTo(null); table
table.uuid('author_id').notNullable(); .uuid('next_id')
.references('id')
.inTable('collections')
.defaultTo(null);
table
.uuid('author_id')
.references('id')
.inTable('users')
.onDelete('CASCADE');
table.enum('visibility', Object.values(Visibility), { table.enum('visibility', Object.values(Visibility), {
useNative: true, useNative: true,
enumName: this.visibilityEnumName, enumName: this.visibilityEnumName,

View File

@@ -5,14 +5,22 @@ export default class extends BaseSchema {
async up() { async up() {
this.schema.createTable(this.tableName, (table) => { this.schema.createTable(this.tableName, (table) => {
table.uuid('id').notNullable(); table.uuid('id').unique().notNullable();
table.string('name', 254).notNullable(); table.string('name', 254).notNullable();
table.string('description', 254); table.string('description', 254);
table.text('url').notNullable(); table.text('url').notNullable();
table.boolean('favorite').notNullable().defaultTo(0); table.boolean('favorite').notNullable().defaultTo(0);
table.uuid('collection_id').notNullable(); table
table.uuid('author_id').notNullable(); .uuid('collection_id')
.references('id')
.inTable('collections')
.onDelete('CASCADE');
table
.uuid('author_id')
.references('id')
.inTable('users')
.onDelete('CASCADE');
table.timestamp('created_at'); table.timestamp('created_at');
table.timestamp('updated_at'); table.timestamp('updated_at');

View File

@@ -0,0 +1,24 @@
import { BaseSchema } from '@adonisjs/lucid/schema';
export default class extends BaseSchema {
protected tableName = 'collection_link';
async up() {
this.schema.createTable(this.tableName, (table) => {
table
.uuid('collection_id')
.references('id')
.inTable('collections')
.onDelete('CASCADE');
table
.uuid('link_id')
.references('id')
.inTable('links')
.onDelete('CASCADE');
});
}
async down() {
this.schema.dropTable(this.tableName);
}
}