More refactoring

This commit is contained in:
Andreas 2024-07-02 21:06:39 +02:00
parent f6bfd285e3
commit 1043d78181
6 changed files with 155 additions and 124 deletions

View File

@ -1,10 +1,10 @@
'use server' 'use server'
/* ================================================================================================= /** =================================================================================================
* *
* Copyright 2024 Andreas Schaafsma * @author Andreas Schaafsma
* Purpose: Handle API requests that fetch or permute User data * @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"; import { hashpassword } from "@/util/auth";
// Attempt to register a new User /** Attempt to register a new User */
async function attemptRegister(request:Request){ async function attemptRegister(request:Request){
// Sync db //** Sync db
await dbSync; await dbSync;
// Get request body // Get request body

View File

@ -1,15 +1,14 @@
import Sequelize, { Association, DataTypes, InferAttributes, InferCreationAttributes, Model, NonAttribute } from "@sequelize/core"; import Sequelize, {
import { Attribute, AutoIncrement, NotNull, PrimaryKey, Unique } from "@sequelize/core/decorators-legacy"; InferAttributes,
import { Post } from "./Post"; InferCreationAttributes,
import { Tag } from "./Tag"; Model,
import { SqliteDialect } from "@sequelize/sqlite3"; } from "@sequelize/core";
class PostTag extends Model<
InferAttributes<PostTag>,
class PostTag extends Model<InferAttributes<PostTag>,InferCreationAttributes<PostTag>> InferCreationAttributes<PostTag>
{ > {
declare postId:number; declare postId: number;
declare tagId:number; declare tagId: number;
} }
export { PostTag };
export { PostTag }

View File

@ -1,21 +1,37 @@
import { Association, BelongsToGetAssociationMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User"; import {
Association,
import { SqliteDialect } from '@sequelize/sqlite3'; CreationOptional,
import { Attribute, AutoIncrement, PrimaryKey, Unique } from "@sequelize/core/decorators-legacy"; DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
NonAttribute,
} from "@sequelize/core";
import {
Attribute,
AutoIncrement,
PrimaryKey,
Unique,
} from "@sequelize/core/decorators-legacy";
import { Post } from "./Post"; import { Post } from "./Post";
export class Project extends Model<
InferAttributes<Project>,
InferCreationAttributes<Project>
> {
@Attribute(DataTypes.INTEGER)
@PrimaryKey
@Unique
@AutoIncrement
declare id: CreationOptional<number>;
@Attribute(DataTypes.STRING)
@Unique
declare readableIdentifier: string;
@Attribute(DataTypes.STRING)
declare name: string;
/** Defined by {@link Post.project} */
declare posts?: NonAttribute<Post>[];
export class Project extends Model<InferAttributes<Project>, InferCreationAttributes<Project>> { declare static associations: {
@Attribute(DataTypes.INTEGER) @PrimaryKey @Unique @AutoIncrement posts: Association<Post, Project>;
declare id: CreationOptional<number>; };
@Attribute(DataTypes.STRING) @Unique
declare readableIdentifier: string;
@Attribute(DataTypes.STRING)
declare name: string;
/** Defined by {@link Post.project} */
declare posts?: NonAttribute<Post>[];
declare static associations: {
posts: Association<Post, Project>;
};
} }

View File

@ -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 { Post } from "./Post";
import { SqliteDialect } from '@sequelize/sqlite3'; import {
import { Attribute, AutoIncrement, BelongsToMany, PrimaryKey, Unique } from "@sequelize/core/decorators-legacy"; Attribute,
AutoIncrement,
PrimaryKey,
Unique,
} from "@sequelize/core/decorators-legacy";
export class Tag extends Model<
InferAttributes<Tag>,
InferCreationAttributes<Tag>
> {
@PrimaryKey
@AutoIncrement
@Attribute(DataTypes.INTEGER)
@Unique
declare id: number;
/** Defined by {@link Post.postTags} */
declare taggedPosts?: NonAttribute<Post[]>;
declare getPosts: BelongsToManyGetAssociationsMixin<Post>;
export class Tag extends Model<InferAttributes<Tag>, InferCreationAttributes<Tag>> { declare static associations: {
@PrimaryKey posts: Association<Post, Tag>;
@AutoIncrement };
@Attribute(DataTypes.INTEGER)
@Unique
declare id: number;
/** Defined by {@link Post.postTags} */
declare taggedPosts?:NonAttribute<Post[]>;
declare getPosts: BelongsToManyGetAssociationsMixin<Post>;
declare static associations: {
posts: Association<Post, Tag>;
};
} }

View File

@ -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 { Post } from "./Post";
import { UserPerms } from "./UserPerms"; import { UserPerms } from "./UserPerms";
import { Auth } from "./Auth"; import { Auth } from "./Auth";
import { import {
PrimaryKey, PrimaryKey,
Attribute, Attribute,
AutoIncrement, AutoIncrement,
NotNull, Unique,
BelongsTo, UpdatedAt,
Unique, CreatedAt,
HasMany, } from "@sequelize/core/decorators-legacy";
HasOne,
UpdatedAt,
CreatedAt,
} from '@sequelize/core/decorators-legacy';
import { SqliteDialect } from '@sequelize/sqlite3';
type UserCreationAttributes = { type UserCreationAttributes = {
username:string; username: string;
password:string; password: string;
perms?:Partial<InferAttributes<UserPerms>> perms?: Partial<InferAttributes<UserPerms>>;
};
export class User extends Model<
InferAttributes<User>,
InferCreationAttributes<User>
> {
@Attribute(DataTypes.INTEGER)
@PrimaryKey
@AutoIncrement
@Unique
declare id: CreationOptional<number>;
@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<Date>;
/** @description The time and date when the object was last modified */
@UpdatedAt declare updatedAt: CreationOptional<Date>;
// Associations
declare getAuthTokens: HasManyGetAssociationsMixin<Auth>;
declare getPosts: HasManyGetAssociationsMixin<Post>;
declare getPerms: HasOneGetAssociationMixin<UserPerms>;
declare createPerms: HasOneCreateAssociationMixin<UserPerms>;
/** Defined by {@link Auth.user} */
declare authtokens?: NonAttribute<Auth[]>;
/** Defined by {@link UserPerms.user} */
declare perms?: CreationOptional<CreationAttributes<UserPerms>>;
/** Defined by {@link Post.user} */
declare posts?: CreationOptional<Post[]>;
declare static associations: {
perms: Association<UserPerms, User>;
posts: Association<Post, User>;
authtokens: Association<Auth, User>;
};
} }
export class User extends Model<InferAttributes<User>, InferCreationAttributes<User>>{ export function addUserScopes() {
@Attribute(DataTypes.INTEGER) User.addScope("defaultScope", {
@PrimaryKey attributes: {
@AutoIncrement exclude: ["password", "createdAt", "updatedAt"],
@Unique },
declare id: CreationOptional<number>; });
@Attribute(DataTypes.STRING) User.addScope("withPerms", {
@Unique() include: [{ association: "perms" }],
declare username: string; });
@Attribute(DataTypes.STRING) User.addScope("withAuthtokens", {
declare password: string; include: [{ association: "authtokens" }],
});
// Date thingies
@CreatedAt
declare createdAt: CreationOptional<Date>;
@UpdatedAt
declare updatedAt: CreationOptional<Date>;
// Associations
declare getAuthTokens:HasManyGetAssociationsMixin<Auth>;
declare getPosts:HasManyGetAssociationsMixin<Post>;
declare getPerms:HasOneGetAssociationMixin<UserPerms>;
declare createPerms:HasOneCreateAssociationMixin<UserPerms>;
/** Defined by {@link Auth.user} */
declare authtokens?:NonAttribute<Auth[]>
/** Defined by {@link UserPerms.user} */
declare perms?:CreationOptional<CreationAttributes<UserPerms>>
/** Defined by {@link Post.user} */
declare posts?:CreationOptional<Post[]>
declare static associations: {
perms: Association<UserPerms,User>;
posts: Association<Post,User>
authtokens: Association<Auth,User>;
};
}
export function addUserScopes(){
User.addScope('defaultScope',{
attributes: {
exclude: ['password', 'createdAt', 'updatedAt'],
}
});
User.addScope('withPerms',{
include: [{association: 'perms' }]
})
User.addScope('withAuthtokens',{
include: [{association: 'authtokens'}]
})
} }

View File

@ -1,9 +1,6 @@
import { CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core"; import { CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";
import { User } from "./User"; import { User } from "./User";
import { SqliteDialect } from '@sequelize/sqlite3';
import { Attribute, AutoIncrement, BelongsTo, CreatedAt, PrimaryKey, Table, Unique, UpdatedAt } from "@sequelize/core/decorators-legacy"; import { Attribute, AutoIncrement, BelongsTo, CreatedAt, PrimaryKey, Table, Unique, UpdatedAt } from "@sequelize/core/decorators-legacy";
@Table({ @Table({
tableName: "Perms" tableName: "Perms"
}) })