Working state for attachments and buckets
This commit is contained in:
+12
-8
@@ -1,26 +1,30 @@
|
||||
import { Association, BelongsToGetAssociationMixin, BelongsToManyGetAssociationsMixin, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";
|
||||
import { Association, BelongsToGetAssociationMixin, BelongsToManyGetAssociationsMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";
|
||||
import { Post } from "./Post";
|
||||
|
||||
import { SqliteDialect } from '@sequelize/sqlite3';
|
||||
import { Attribute, AutoIncrement, BelongsTo, BelongsToMany, NotNull, PrimaryKey, Unique } from "@sequelize/core/decorators-legacy";
|
||||
import { Bucket } from "./Bucket";
|
||||
import { UUID } from "crypto";
|
||||
|
||||
export class Attachment extends Model<InferAttributes<Attachment>, InferCreationAttributes<Attachment>> {
|
||||
@PrimaryKey
|
||||
@AutoIncrement
|
||||
@Attribute(DataTypes.INTEGER)
|
||||
@Unique
|
||||
declare id: number;
|
||||
declare id: CreationOptional<number>;
|
||||
@Attribute(DataTypes.STRING)
|
||||
declare path: string
|
||||
declare filename: string
|
||||
|
||||
// Associations
|
||||
@Attribute(DataTypes.INTEGER)
|
||||
@Attribute(DataTypes.UUIDV4)
|
||||
@NotNull
|
||||
declare postid: number;
|
||||
@BelongsTo(()=>Post,{foreignKey: 'postid', inverse: {type: "hasMany", as: 'attachments'}})
|
||||
declare post?:NonAttribute<Post>;
|
||||
declare bucket_id: ForeignKey<Bucket['id']>;
|
||||
|
||||
@BelongsTo(()=>Bucket,{foreignKey: 'bucket_id', inverse: {type: "hasMany", as: 'attachments'}})
|
||||
declare bucket?:NonAttribute<Bucket>;
|
||||
|
||||
declare static associations: {
|
||||
posts: Association<Post, Attachment>;
|
||||
bucket: Association<Bucket, Attachment>;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { Association, BelongsToGetAssociationMixin, BelongsToManyGetAssociationsMixin, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize, sql } from "@sequelize/core";
|
||||
import { Post } from "./Post";
|
||||
|
||||
import { SqliteDialect } from '@sequelize/sqlite3';
|
||||
import { Attribute, AutoIncrement, BelongsTo, BelongsToMany, Default, NotNull, PrimaryKey, Unique } from "@sequelize/core/decorators-legacy";
|
||||
import { Attachment } from "./Attachment";
|
||||
import { UUID } from "crypto";
|
||||
|
||||
export class Bucket extends Model<InferAttributes<Bucket>, InferCreationAttributes<Bucket>> {
|
||||
@PrimaryKey
|
||||
@Unique
|
||||
@Attribute(DataTypes.UUIDV4)
|
||||
@Default(sql.uuidV4)
|
||||
declare id: UUID
|
||||
|
||||
/** Defined by {@link Post.buckets} */
|
||||
declare bucketPosts?:NonAttribute<Post>[];
|
||||
|
||||
/** Defined by {@link Attachment.bucket} */
|
||||
declare attachments?:NonAttribute<Attachment>[];
|
||||
|
||||
declare static associations: {
|
||||
bucketPosts: Association<Post, Bucket>;
|
||||
attachments: Association<Attachment, Bucket>;
|
||||
};
|
||||
}
|
||||
|
||||
const sequelize = new Sequelize({
|
||||
dialect: SqliteDialect,
|
||||
storage: 'db.sqlite',
|
||||
models: [Bucket]
|
||||
})
|
||||
+15
-5
@@ -1,4 +1,4 @@
|
||||
import { Association, BelongsToGetAssociationMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User";
|
||||
import { Association, BelongsToGetAssociationMixin, BelongsToManyAssociation, BelongsToManyCreateAssociationMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User";
|
||||
|
||||
import { SqliteDialect } from '@sequelize/sqlite3';
|
||||
import { Attribute, AutoIncrement, BelongsTo, BelongsToMany, CreatedAt, Default, HasMany, NotNull, PrimaryKey, Unique, UpdatedAt } from "@sequelize/core/decorators-legacy";
|
||||
@@ -6,6 +6,8 @@ import { Tag } from "./Tag";
|
||||
import { PostTag } from "./PostTag";
|
||||
import { Project } from "./Project";
|
||||
import { Attachment } from "./Attachment";
|
||||
import { Bucket } from "./Bucket";
|
||||
import { UUID } from "crypto";
|
||||
|
||||
export class Post extends Model<InferAttributes<Post>, InferCreationAttributes<Post>> {
|
||||
|
||||
@@ -47,21 +49,29 @@ export class Post extends Model<InferAttributes<Post>, InferCreationAttributes<P
|
||||
@BelongsToMany(()=>Tag, { through: { model: ()=>PostTag, unique: false}, inverse: {as: 'taggedPosts'} })
|
||||
declare postTags?:NonAttribute<Tag[]>;
|
||||
|
||||
/** Defined by {@link Attachment.post} */
|
||||
declare Attachments:NonAttribute<Attachment>[];
|
||||
@BelongsToMany(()=>Bucket,{through: { model: ()=> PostBucket, unique: false}, inverse:{as: 'bucketPosts'}, foreignKey: 'postId', otherKey: 'bucketId'})
|
||||
declare postBuckets:NonAttribute<Bucket[]>;
|
||||
|
||||
|
||||
declare getUser: BelongsToGetAssociationMixin<User>;
|
||||
|
||||
|
||||
declare static associations: {
|
||||
user: Association<User, Post>;
|
||||
project: Association<Project, Post>;
|
||||
postBuckets: Association<Bucket, Post>
|
||||
postTags: Association<Tag, Post>;
|
||||
};
|
||||
}
|
||||
export class PostBucket extends Model<InferAttributes<PostBucket>, InferCreationAttributes<PostBucket>>{
|
||||
declare postId: number;
|
||||
declare bucketId: UUID;
|
||||
}
|
||||
|
||||
const sequelize = new Sequelize({
|
||||
dialect: SqliteDialect,
|
||||
storage: 'db.sqlite',
|
||||
models: [Post]
|
||||
})
|
||||
models: [Post, PostBucket]
|
||||
})
|
||||
|
||||
PostBucket.sync();
|
||||
Reference in New Issue
Block a user