Many Changes

This commit is contained in:
2024-05-14 12:49:50 +02:00
parent a01b6bdbc2
commit f6eef1ded3
53 changed files with 636 additions and 579 deletions
+44 -47
View File
@@ -1,56 +1,53 @@
import { Association, BelongsToGetAssociationMixin, DataTypes, ForeignKey, Model, NonAttribute, Sequelize } from "sequelize";
import { User } from "./User";
import { Association, BelongsToGetAssociationMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User";
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'db.sqlite'
});
import { SqliteDialect } from '@sequelize/sqlite3';
import { Attribute, AutoIncrement, BelongsTo, BelongsToMany, CreatedAt, HasMany, NotNull, PrimaryKey, Unique, UpdatedAt } from "@sequelize/core/decorators-legacy";
import { Tag } from "./Tag";
import { PostTag } from "./PostTag";
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>;
export class Post extends Model<InferAttributes<Post>, InferCreationAttributes<Post>> {
// Attributes
@Attribute(DataTypes.INTEGER)
@PrimaryKey
@AutoIncrement
@Unique
declare id: CreationOptional<number>;
@Attribute(DataTypes.INTEGER)
@NotNull
declare user_id:ForeignKey<User['id']>;
@Attribute(DataTypes.STRING)
declare title:string
@Attribute(DataTypes.STRING)
declare content:string
// Date thingies
@CreatedAt
declare createdAt: CreationOptional<Date>;
@UpdatedAt
declare updatedAt: CreationOptional<Date>;
// Associatoins
@BelongsTo(()=>User, { foreignKey: 'user_id', inverse: { type: 'hasMany', as: 'posts' } })
declare user:NonAttribute<User>;
@BelongsToMany(()=>Tag, { through: { model: ()=>PostTag, unique: false}, inverse: {as: 'taggedPosts'} })
declare postTags?:NonAttribute<Tag[]>;
declare getUser: BelongsToGetAssociationMixin<User>;
declare static associations: {
user: Association<User, Post>;
postTags: Association<Tag, 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
}
);
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [Post]
})