functional
This commit is contained in:
parent
508258bf0f
commit
a01b6bdbc2
@ -1,7 +1,7 @@
|
|||||||
'use server'
|
'use server'
|
||||||
|
|
||||||
import { APIError } from "@/api/error";
|
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 { cookies } from "next/headers";
|
||||||
import { where } from "sequelize";
|
import { where } from "sequelize";
|
||||||
|
|
||||||
|
|||||||
37
src/model/Auth.ts
Normal file
37
src/model/Auth.ts
Normal file
@ -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<InferAttributes<Auth>, InferCreationAttributes<Auth>> {
|
||||||
|
declare id: CreationOptional<number>;
|
||||||
|
declare token: CreationOptional<string>;
|
||||||
|
declare user_id:ForeignKey<User['id']>;
|
||||||
|
declare getUser: BelongsToGetAssociationMixin<User>;
|
||||||
|
declare static associations: {
|
||||||
|
user: Association<User, Auth>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
});
|
||||||
20
src/model/Models.ts
Normal file
20
src/model/Models.ts
Normal file
@ -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 }
|
||||||
56
src/model/Post.ts
Normal file
56
src/model/Post.ts
Normal file
@ -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<User['id']>;
|
||||||
|
}
|
||||||
|
export type PostCreationAttributes = {
|
||||||
|
title: string;
|
||||||
|
content: string;
|
||||||
|
date: number;
|
||||||
|
user_id:number;
|
||||||
|
}
|
||||||
|
export class Post extends Model<PostAttributes, PostCreationAttributes> {
|
||||||
|
declare id: number;
|
||||||
|
declare user:NonAttribute<User>;
|
||||||
|
declare user_id:ForeignKey<User['id']>;
|
||||||
|
declare getUser: BelongsToGetAssociationMixin<User>;
|
||||||
|
declare static associations: {
|
||||||
|
user: Association<User, Post>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
);
|
||||||
91
src/model/User.ts
Normal file
91
src/model/User.ts
Normal file
@ -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<InferAttributes<User>, InferCreationAttributes<User>>{
|
||||||
|
declare id: number|null;
|
||||||
|
declare username: string;
|
||||||
|
declare password: string;
|
||||||
|
declare getAuthTokens:HasManyGetAssociationsMixin<Auth>;
|
||||||
|
declare getPosts:HasManyGetAssociationsMixin<Post>;
|
||||||
|
declare getPerms:HasOneGetAssociationMixin<UserPerms>;
|
||||||
|
declare createPerms:HasOneCreateAssociationMixin<UserPerms>;
|
||||||
|
declare perms?:NonAttribute<UserPerms>
|
||||||
|
declare static associations: {
|
||||||
|
perms: Association<UserPerms,User>;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
32
src/model/UserPerms.ts
Normal file
32
src/model/UserPerms.ts
Normal file
@ -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<InferAttributes<UserPerms>, InferCreationAttributes<UserPerms>> {
|
||||||
|
declare id: CreationOptional<number>;
|
||||||
|
declare user:NonAttribute<User>;
|
||||||
|
declare user_id:ForeignKey<User['id']>;
|
||||||
|
declare isAdmin:CreationOptional<boolean>;
|
||||||
|
}
|
||||||
|
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
|
||||||
|
});
|
||||||
@ -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<InferAttributes<UserPerms>, InferCreationAttributes<UserPerms>> {
|
|
||||||
declare id: CreationOptional<number>;
|
|
||||||
declare user:NonAttribute<User>;
|
|
||||||
declare user_id:ForeignKey<User['id']>;
|
|
||||||
declare isAdmin:CreationOptional<boolean>;
|
|
||||||
}
|
|
||||||
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<InferAttributes<Auth>, InferCreationAttributes<Auth>> {
|
|
||||||
declare id: CreationOptional<number>;
|
|
||||||
declare token: CreationOptional<string>;
|
|
||||||
declare user_id:ForeignKey<User['id']>;
|
|
||||||
declare getUser: BelongsToGetAssociationMixin<User>;
|
|
||||||
declare static associations: {
|
|
||||||
user: Association<User, Auth>;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
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<User['id']>;
|
|
||||||
}
|
|
||||||
export type PostCreationAttributes = {
|
|
||||||
title: string;
|
|
||||||
content: string;
|
|
||||||
date: number;
|
|
||||||
user_id:number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Post extends Model<PostAttributes, PostCreationAttributes> {
|
|
||||||
declare id: number;
|
|
||||||
declare user:NonAttribute<User>;
|
|
||||||
declare user_id:ForeignKey<User['id']>;
|
|
||||||
declare getUser: BelongsToGetAssociationMixin<User>;
|
|
||||||
declare static associations: {
|
|
||||||
user: Association<User, Post>;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
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<InferAttributes<User>, InferCreationAttributes<User>>{
|
|
||||||
declare id: number|null;
|
|
||||||
declare username: string;
|
|
||||||
declare password: string;
|
|
||||||
declare getAuthTokens:HasManyGetAssociationsMixin<Auth>;
|
|
||||||
declare getPosts:HasManyGetAssociationsMixin<Post>;
|
|
||||||
declare getPerms:HasOneGetAssociationMixin<UserPerms>;
|
|
||||||
declare createPerms:HasOneCreateAssociationMixin<UserPerms>;
|
|
||||||
declare perms?:NonAttribute<UserPerms>
|
|
||||||
declare static associations: {
|
|
||||||
perms: Association<UserPerms,User>;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
Loading…
x
Reference in New Issue
Block a user