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,13 +1,30 @@
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<
export class Project extends Model<InferAttributes<Project>, InferCreationAttributes<Project>> { InferAttributes<Project>,
@Attribute(DataTypes.INTEGER) @PrimaryKey @Unique @AutoIncrement InferCreationAttributes<Project>
> {
@Attribute(DataTypes.INTEGER)
@PrimaryKey
@Unique
@AutoIncrement
declare id: CreationOptional<number>; declare id: CreationOptional<number>;
@Attribute(DataTypes.STRING) @Unique @Attribute(DataTypes.STRING)
@Unique
declare readableIdentifier: string; declare readableIdentifier: string;
@Attribute(DataTypes.STRING) @Attribute(DataTypes.STRING)
declare name: string; declare name: string;
@ -17,5 +34,4 @@ export class Project extends Model<InferAttributes<Project>, InferCreationAttrib
declare static associations: { declare static associations: {
posts: Association<Post, Project>; posts: Association<Post, Project>;
}; };
} }

View File

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

View File

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