From a01b6bdbc2aa664fc2f4bcc59ddaa8e92559a5d9 Mon Sep 17 00:00:00 2001 From: Andreas Schaafsma Date: Mon, 15 Apr 2024 12:06:01 +0200 Subject: [PATCH] functional --- src/app/api/post/route.ts | 2 +- src/model/Auth.ts | 37 ++++++ src/model/Models.ts | 20 ++++ src/model/Post.ts | 56 +++++++++ src/model/User.ts | 91 ++++++++++++++ src/model/UserPerms.ts | 32 +++++ src/model/sequelize/NewModels.ts | 199 ------------------------------- 7 files changed, 237 insertions(+), 200 deletions(-) create mode 100644 src/model/Auth.ts create mode 100644 src/model/Models.ts create mode 100644 src/model/Post.ts create mode 100644 src/model/User.ts create mode 100644 src/model/UserPerms.ts delete mode 100644 src/model/sequelize/NewModels.ts diff --git a/src/app/api/post/route.ts b/src/app/api/post/route.ts index ac27579..30d7bd3 100644 --- a/src/app/api/post/route.ts +++ b/src/app/api/post/route.ts @@ -1,7 +1,7 @@ 'use server' import { APIError } from "@/api/error"; -import { Auth, Post, User } from "@/model/sequelize/NewModels"; +import { Auth, Post, User } from "@/model/Models"; import { cookies } from "next/headers"; import { where } from "sequelize"; diff --git a/src/model/Auth.ts b/src/model/Auth.ts new file mode 100644 index 0000000..33b7fa5 --- /dev/null +++ b/src/model/Auth.ts @@ -0,0 +1,37 @@ +import { Association, BelongsToGetAssociationMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, Sequelize, UUIDV4 } from "sequelize"; +import { User } from "./User"; + + +const sequelize = new Sequelize({ + dialect: 'sqlite', + storage: 'db.sqlite' +}); + + +export class Auth extends Model, InferCreationAttributes> { + declare id: CreationOptional; + declare token: CreationOptional; + declare user_id:ForeignKey; + declare getUser: BelongsToGetAssociationMixin; + declare static associations: { + user: Association; + }; +} + +Auth.init({ + id: { + allowNull: false, + autoIncrement: true, + type: DataTypes.INTEGER, + primaryKey: true, + unique: true, + }, + token: { + type: DataTypes.UUID, + defaultValue: UUIDV4 + } +}, +{ + tableName: 'Auths', + sequelize // passing the `sequelize` instance is required +}); \ No newline at end of file diff --git a/src/model/Models.ts b/src/model/Models.ts new file mode 100644 index 0000000..c0f4672 --- /dev/null +++ b/src/model/Models.ts @@ -0,0 +1,20 @@ +import { + Association, DataTypes, HasManyAddAssociationMixin, HasManyCountAssociationsMixin, + HasManyCreateAssociationMixin, HasManyGetAssociationsMixin, HasManyHasAssociationMixin, + HasManySetAssociationsMixin, HasManyAddAssociationsMixin, HasManyHasAssociationsMixin, + HasManyRemoveAssociationMixin, HasManyRemoveAssociationsMixin, Model, ModelDefined, Optional, + Sequelize, InferAttributes, InferCreationAttributes, CreationOptional, NonAttribute, ForeignKey, BelongsTo, BelongsToGetAssociationMixin, UUIDV4, UUID, + VirtualDataType, + HasOneGetAssociationMixin, + HasOneCreateAssociationMixin + } from 'sequelize'; + +import { UserPerms } from './UserPerms'; +import { Auth } from './Auth'; +import { Post } from './Post'; +import { User } from './User'; + + + + +export { User, Auth, Post, UserPerms } \ No newline at end of file diff --git a/src/model/Post.ts b/src/model/Post.ts new file mode 100644 index 0000000..5a2848b --- /dev/null +++ b/src/model/Post.ts @@ -0,0 +1,56 @@ +import { Association, BelongsToGetAssociationMixin, DataTypes, ForeignKey, Model, NonAttribute, Sequelize } from "sequelize"; +import { User } from "./User"; + +const sequelize = new Sequelize({ + dialect: 'sqlite', + storage: 'db.sqlite' +}); + +export type PostAttributes = { + id: number; + title: string; + content: string; + date: number; + user_id:ForeignKey; +} +export type PostCreationAttributes = { + title: string; + content: string; + date: number; + user_id:number; +} +export class Post extends Model { + declare id: number; + declare user:NonAttribute; + declare user_id:ForeignKey; + declare getUser: BelongsToGetAssociationMixin; + declare static associations: { + user: Association; + }; +} + +Post.init( + { + id: { + allowNull: false, + autoIncrement: true, + type: DataTypes.INTEGER, + primaryKey: true, + unique: true, + }, + content: { + type: DataTypes.STRING + }, + title: { + type: DataTypes.STRING + }, + date: { + type: DataTypes.DATE + }, + + }, + { + tableName: 'Posts', + sequelize // passing the `sequelize` instance is required + } +); \ No newline at end of file diff --git a/src/model/User.ts b/src/model/User.ts new file mode 100644 index 0000000..8a601c1 --- /dev/null +++ b/src/model/User.ts @@ -0,0 +1,91 @@ +import { Association, DataTypes, HasManyGetAssociationsMixin, HasOneCreateAssociationMixin, HasOneGetAssociationMixin, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "sequelize"; +import { Post } from "./Post"; +import { UserPerms } from "./UserPerms"; +import { Auth } from "./Auth"; + +const sequelize = new Sequelize({ + dialect: 'sqlite', + storage: 'db.sqlite' +}); + +export class User extends Model, InferCreationAttributes>{ + declare id: number|null; + declare username: string; + declare password: string; + declare getAuthTokens:HasManyGetAssociationsMixin; + declare getPosts:HasManyGetAssociationsMixin; + declare getPerms:HasOneGetAssociationMixin; + declare createPerms:HasOneCreateAssociationMixin; + declare perms?:NonAttribute + declare static associations: { + perms: Association; + }; + +} + +User.init( + { + id: { + allowNull: false, + autoIncrement: true, + type: DataTypes.INTEGER, + primaryKey: true, + unique: true, + }, + username: { + allowNull: false, + type: DataTypes.STRING, + }, + password: { + allowNull: false, + type: DataTypes.STRING, + }, + }, + { + defaultScope: { + attributes: { + exclude: ['password'], + }, + include: [{ association: 'perms' }], + }, + tableName: 'Users', + sequelize // passing the `sequelize` instance is required + } +); + +Auth.belongsTo(User, { + foreignKey:'user_id', + targetKey:'id', + as: 'user', +}) +User.hasMany(Auth, { + sourceKey: 'id', + foreignKey: 'user_id', + as: 'authtokens' // this determines the name in `associations`! +}); +Post.belongsTo(User, { + foreignKey:'user_id', + targetKey:'id', + as: 'user' +}) +User.hasMany(Post, { + sourceKey: 'id', + foreignKey: 'user_id', + as: 'posts' // this determines the name in `associations`! +}); +UserPerms.belongsTo(User, { + foreignKey:'user_id', + targetKey:'id', + as: 'user' +}) +User.hasOne(UserPerms, { + sourceKey: 'id', + foreignKey: 'user_id', + as: 'perms' +}) + + +User.sync(); +Auth.sync(); +Post.sync(); +UserPerms.sync(); \ No newline at end of file diff --git a/src/model/UserPerms.ts b/src/model/UserPerms.ts new file mode 100644 index 0000000..aa3b9ca --- /dev/null +++ b/src/model/UserPerms.ts @@ -0,0 +1,32 @@ +import { CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "sequelize"; +import { User } from "./User"; + +const sequelize = new Sequelize({ + dialect: 'sqlite', + storage: 'db.sqlite' +}); + +export class UserPerms extends Model, InferCreationAttributes> { + declare id: CreationOptional; + declare user:NonAttribute; + declare user_id:ForeignKey; + declare isAdmin:CreationOptional; +} +UserPerms.init({ + id: { + allowNull: false, + autoIncrement: true, + type: DataTypes.INTEGER, + primaryKey: true, + unique: true, + }, + isAdmin: { + allowNull: false, + defaultValue: false, + type: DataTypes.BOOLEAN + } +}, +{ + tableName: 'Perms', + sequelize // passing the `sequelize` instance is required +}); \ No newline at end of file diff --git a/src/model/sequelize/NewModels.ts b/src/model/sequelize/NewModels.ts deleted file mode 100644 index 9e814d7..0000000 --- a/src/model/sequelize/NewModels.ts +++ /dev/null @@ -1,199 +0,0 @@ -import { - Association, DataTypes, HasManyAddAssociationMixin, HasManyCountAssociationsMixin, - HasManyCreateAssociationMixin, HasManyGetAssociationsMixin, HasManyHasAssociationMixin, - HasManySetAssociationsMixin, HasManyAddAssociationsMixin, HasManyHasAssociationsMixin, - HasManyRemoveAssociationMixin, HasManyRemoveAssociationsMixin, Model, ModelDefined, Optional, - Sequelize, InferAttributes, InferCreationAttributes, CreationOptional, NonAttribute, ForeignKey, BelongsTo, BelongsToGetAssociationMixin, UUIDV4, UUID, - VirtualDataType, - HasOneGetAssociationMixin, - HasOneCreateAssociationMixin - } from 'sequelize'; - - -const sequelize = new Sequelize({ - dialect: 'sqlite', - storage: 'db.sqlite' -}); - -export class UserPerms extends Model, InferCreationAttributes> { - declare id: CreationOptional; - declare user:NonAttribute; - declare user_id:ForeignKey; - declare isAdmin:CreationOptional; -} -UserPerms.init({ - id: { - allowNull: false, - autoIncrement: true, - type: DataTypes.INTEGER, - primaryKey: true, - unique: true, - }, - isAdmin: { - allowNull: false, - defaultValue: false, - type: DataTypes.BOOLEAN - } -}, -{ - tableName: 'Perms', - sequelize // passing the `sequelize` instance is required -}); - - -export class Auth extends Model, InferCreationAttributes> { - declare id: CreationOptional; - declare token: CreationOptional; - declare user_id:ForeignKey; - declare getUser: BelongsToGetAssociationMixin; - declare static associations: { - user: Association; - }; -} -Auth.init({ - id: { - allowNull: false, - autoIncrement: true, - type: DataTypes.INTEGER, - primaryKey: true, - unique: true, - }, - token: { - type: DataTypes.UUID, - defaultValue: UUIDV4 - } -}, -{ - tableName: 'Auths', - sequelize // passing the `sequelize` instance is required -}); - -export type PostAttributes = { - id: number; - title: string; - content: string; - date: number; - user_id:ForeignKey; -} -export type PostCreationAttributes = { - title: string; - content: string; - date: number; - user_id:number; -} - -export class Post extends Model { - declare id: number; - declare user:NonAttribute; - declare user_id:ForeignKey; - declare getUser: BelongsToGetAssociationMixin; - declare static associations: { - user: Association; - }; -} -Post.init( - { - id: { - allowNull: false, - autoIncrement: true, - type: DataTypes.INTEGER, - primaryKey: true, - unique: true, - }, - content: { - type: DataTypes.STRING - }, - title: { - type: DataTypes.STRING - }, - date: { - type: DataTypes.DATE - }, - - }, - { - tableName: 'Posts', - sequelize // passing the `sequelize` instance is required - } -); -export class User extends Model, InferCreationAttributes>{ - declare id: number|null; - declare username: string; - declare password: string; - declare getAuthTokens:HasManyGetAssociationsMixin; - declare getPosts:HasManyGetAssociationsMixin; - declare getPerms:HasOneGetAssociationMixin; - declare createPerms:HasOneCreateAssociationMixin; - declare perms?:NonAttribute - declare static associations: { - perms: Association; - }; - -} - -User.init( - { - id: { - allowNull: false, - autoIncrement: true, - type: DataTypes.INTEGER, - primaryKey: true, - unique: true, - }, - username: { - allowNull: false, - type: DataTypes.STRING, - }, - password: { - allowNull: false, - type: DataTypes.STRING, - }, - }, - { - defaultScope: { - attributes: { - exclude: ['password'], - }, - include: [{ association: 'perms' }], - }, - tableName: 'Users', - sequelize // passing the `sequelize` instance is required - } -); - -Auth.belongsTo(User, { - foreignKey:'user_id', - targetKey:'id', - as: 'user', -}) -User.hasMany(Auth, { - sourceKey: 'id', - foreignKey: 'user_id', - as: 'authtokens' // this determines the name in `associations`! -}); -Post.belongsTo(User, { - foreignKey:'user_id', - targetKey:'id', - as: 'user' -}) -User.hasMany(Post, { - sourceKey: 'id', - foreignKey: 'user_id', - as: 'posts' // this determines the name in `associations`! -}); -UserPerms.belongsTo(User, { - foreignKey:'user_id', - targetKey:'id', - as: 'user' -}) -User.hasOne(UserPerms, { - sourceKey: 'id', - foreignKey: 'user_id', - as: 'perms' -}) - - -User.sync(); -Auth.sync(); -Post.sync(); -UserPerms.sync(); \ No newline at end of file