From 1043d78181f76a11441fc82ae4ef4b0a2b7003eb Mon Sep 17 00:00:00 2001 From: Andreas Date: Tue, 2 Jul 2024 21:06:39 +0200 Subject: [PATCH] More refactoring --- src/app/api/user/route.ts | 10 +-- src/models/PostTag.ts | 25 ++++--- src/models/Project.ts | 52 +++++++++----- src/models/Tag.ts | 46 +++++++----- src/models/User.ts | 143 ++++++++++++++++++++------------------ src/models/UserPerms.ts | 3 - 6 files changed, 155 insertions(+), 124 deletions(-) diff --git a/src/app/api/user/route.ts b/src/app/api/user/route.ts index 15bbc9d..1d4f72c 100644 --- a/src/app/api/user/route.ts +++ b/src/app/api/user/route.ts @@ -1,10 +1,10 @@ 'use server' -/* ================================================================================================= +/** ================================================================================================= * - * Copyright 2024 Andreas Schaafsma - * Purpose: Handle API requests that fetch or permute User data + * @author Andreas Schaafsma + * @description Handle API requests that fetch or permute User data * * ================================================================================================= */ @@ -14,9 +14,9 @@ import { Attachment, Auth, Bucket, DBState, Post, PostTag, Project, Tag, User, U import { hashpassword } from "@/util/auth"; -// Attempt to register a new User +/** Attempt to register a new User */ async function attemptRegister(request:Request){ - // Sync db + //** Sync db await dbSync; // Get request body diff --git a/src/models/PostTag.ts b/src/models/PostTag.ts index 050a036..8f3c21d 100644 --- a/src/models/PostTag.ts +++ b/src/models/PostTag.ts @@ -1,15 +1,14 @@ -import Sequelize, { Association, DataTypes, InferAttributes, InferCreationAttributes, Model, NonAttribute } from "@sequelize/core"; -import { Attribute, AutoIncrement, NotNull, PrimaryKey, Unique } from "@sequelize/core/decorators-legacy"; -import { Post } from "./Post"; -import { Tag } from "./Tag"; -import { SqliteDialect } from "@sequelize/sqlite3"; - - -class PostTag extends Model,InferCreationAttributes> -{ - declare postId:number; - declare tagId:number; +import Sequelize, { + InferAttributes, + InferCreationAttributes, + Model, +} from "@sequelize/core"; +class PostTag extends Model< + InferAttributes, + InferCreationAttributes +> { + declare postId: number; + declare tagId: number; } - -export { PostTag } \ No newline at end of file +export { PostTag }; diff --git a/src/models/Project.ts b/src/models/Project.ts index dc39e03..ac03e57 100644 --- a/src/models/Project.ts +++ b/src/models/Project.ts @@ -1,21 +1,37 @@ -import { Association, BelongsToGetAssociationMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User"; - -import { SqliteDialect } from '@sequelize/sqlite3'; -import { Attribute, AutoIncrement, PrimaryKey, Unique } from "@sequelize/core/decorators-legacy"; +import { + Association, + CreationOptional, + DataTypes, + InferAttributes, + InferCreationAttributes, + Model, + NonAttribute, +} from "@sequelize/core"; +import { + Attribute, + AutoIncrement, + PrimaryKey, + Unique, +} from "@sequelize/core/decorators-legacy"; import { Post } from "./Post"; +export class Project extends Model< + InferAttributes, + InferCreationAttributes +> { + @Attribute(DataTypes.INTEGER) + @PrimaryKey + @Unique + @AutoIncrement + declare id: CreationOptional; + @Attribute(DataTypes.STRING) + @Unique + declare readableIdentifier: string; + @Attribute(DataTypes.STRING) + declare name: string; + /** Defined by {@link Post.project} */ + declare posts?: NonAttribute[]; -export class Project extends Model, InferCreationAttributes> { - @Attribute(DataTypes.INTEGER) @PrimaryKey @Unique @AutoIncrement - declare id: CreationOptional; - @Attribute(DataTypes.STRING) @Unique - declare readableIdentifier: string; - @Attribute(DataTypes.STRING) - declare name: string; - /** Defined by {@link Post.project} */ - declare posts?: NonAttribute[]; - - declare static associations: { - posts: Association; - }; - + declare static associations: { + posts: Association; + }; } diff --git a/src/models/Tag.ts b/src/models/Tag.ts index 08b085c..fbd652d 100644 --- a/src/models/Tag.ts +++ b/src/models/Tag.ts @@ -1,20 +1,34 @@ -import { Association, BelongsToGetAssociationMixin, BelongsToManyGetAssociationsMixin, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core"; +import { + Association, + BelongsToManyGetAssociationsMixin, + DataTypes, + InferAttributes, + InferCreationAttributes, + Model, + NonAttribute, +} from "@sequelize/core"; import { Post } from "./Post"; -import { SqliteDialect } from '@sequelize/sqlite3'; -import { Attribute, AutoIncrement, BelongsToMany, PrimaryKey, Unique } from "@sequelize/core/decorators-legacy"; +import { + Attribute, + AutoIncrement, + PrimaryKey, + Unique, +} from "@sequelize/core/decorators-legacy"; +export class Tag extends Model< + InferAttributes, + InferCreationAttributes +> { + @PrimaryKey + @AutoIncrement + @Attribute(DataTypes.INTEGER) + @Unique + declare id: number; + /** Defined by {@link Post.postTags} */ + declare taggedPosts?: NonAttribute; + declare getPosts: BelongsToManyGetAssociationsMixin; -export class Tag extends Model, InferCreationAttributes> { - @PrimaryKey - @AutoIncrement - @Attribute(DataTypes.INTEGER) - @Unique - declare id: number; - /** Defined by {@link Post.postTags} */ - declare taggedPosts?:NonAttribute; - declare getPosts: BelongsToManyGetAssociationsMixin; - - declare static associations: { - posts: Association; - }; + declare static associations: { + posts: Association; + }; } diff --git a/src/models/User.ts b/src/models/User.ts index 362ac60..26dc325 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -1,77 +1,82 @@ -import { Association, Attributes, CreationAttributes, CreationOptional, DataTypes, HasManyGetAssociationsMixin, HasOneCreateAssociationMixin, HasOneGetAssociationMixin, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core"; +import { + Association, + CreationAttributes, + CreationOptional, + DataTypes, + HasManyGetAssociationsMixin, + HasOneCreateAssociationMixin, + HasOneGetAssociationMixin, + InferAttributes, + InferCreationAttributes, + Model, + NonAttribute, +} from "@sequelize/core"; import { Post } from "./Post"; import { UserPerms } from "./UserPerms"; import { Auth } from "./Auth"; import { - PrimaryKey, - Attribute, - AutoIncrement, - NotNull, - BelongsTo, - Unique, - HasMany, - HasOne, - UpdatedAt, - CreatedAt, -} from '@sequelize/core/decorators-legacy'; - -import { SqliteDialect } from '@sequelize/sqlite3'; + PrimaryKey, + Attribute, + AutoIncrement, + Unique, + UpdatedAt, + CreatedAt, +} from "@sequelize/core/decorators-legacy"; type UserCreationAttributes = { - username:string; - password:string; - perms?:Partial> + username: string; + password: string; + perms?: Partial>; +}; +export class User extends Model< + InferAttributes, + InferCreationAttributes +> { + @Attribute(DataTypes.INTEGER) + @PrimaryKey + @AutoIncrement + @Unique + declare id: CreationOptional; + @Attribute(DataTypes.STRING) + @Unique() + declare username: string; + @Attribute(DataTypes.STRING) + declare password: string; + + /** @description The time and date when the object was created */ + @CreatedAt declare createdAt: CreationOptional; + /** @description The time and date when the object was last modified */ + @UpdatedAt declare updatedAt: CreationOptional; + + // Associations + declare getAuthTokens: HasManyGetAssociationsMixin; + declare getPosts: HasManyGetAssociationsMixin; + declare getPerms: HasOneGetAssociationMixin; + declare createPerms: HasOneCreateAssociationMixin; + + /** Defined by {@link Auth.user} */ + declare authtokens?: NonAttribute; + /** Defined by {@link UserPerms.user} */ + declare perms?: CreationOptional>; + /** Defined by {@link Post.user} */ + declare posts?: CreationOptional; + + declare static associations: { + perms: Association; + posts: Association; + authtokens: Association; + }; } -export class User extends Model, InferCreationAttributes>{ - @Attribute(DataTypes.INTEGER) - @PrimaryKey - @AutoIncrement - @Unique - declare id: CreationOptional; - @Attribute(DataTypes.STRING) - @Unique() - declare username: string; - @Attribute(DataTypes.STRING) - declare password: string; - - // Date thingies - - @CreatedAt - declare createdAt: CreationOptional; - @UpdatedAt - declare updatedAt: CreationOptional; - - // Associations - declare getAuthTokens:HasManyGetAssociationsMixin; - declare getPosts:HasManyGetAssociationsMixin; - declare getPerms:HasOneGetAssociationMixin; - declare createPerms:HasOneCreateAssociationMixin; - - /** Defined by {@link Auth.user} */ - declare authtokens?:NonAttribute - /** Defined by {@link UserPerms.user} */ - declare perms?:CreationOptional> - /** Defined by {@link Post.user} */ - declare posts?:CreationOptional - - - declare static associations: { - perms: Association; - posts: Association - authtokens: Association; - }; - +export function addUserScopes() { + User.addScope("defaultScope", { + attributes: { + exclude: ["password", "createdAt", "updatedAt"], + }, + }); + User.addScope("withPerms", { + include: [{ association: "perms" }], + }); + User.addScope("withAuthtokens", { + include: [{ association: "authtokens" }], + }); } -export function addUserScopes(){ - User.addScope('defaultScope',{ - attributes: { - exclude: ['password', 'createdAt', 'updatedAt'], - } - }); - User.addScope('withPerms',{ - include: [{association: 'perms' }] - }) - User.addScope('withAuthtokens',{ - include: [{association: 'authtokens'}] - }) -} \ No newline at end of file diff --git a/src/models/UserPerms.ts b/src/models/UserPerms.ts index 5d7b5fd..a04aff3 100644 --- a/src/models/UserPerms.ts +++ b/src/models/UserPerms.ts @@ -1,9 +1,6 @@ import { CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core"; import { User } from "./User"; - -import { SqliteDialect } from '@sequelize/sqlite3'; import { Attribute, AutoIncrement, BelongsTo, CreatedAt, PrimaryKey, Table, Unique, UpdatedAt } from "@sequelize/core/decorators-legacy"; - @Table({ tableName: "Perms" })