finally got the associations to work through black magic

This commit is contained in:
2024-06-13 07:03:38 +02:00
parent c72ac5e67f
commit 21e31d0ee4
16 changed files with 98 additions and 144 deletions
+27 -12
View File
@@ -1,4 +1,4 @@
import { Association, BelongsToGetAssociationMixin, BelongsToManyAssociation, BelongsToManyCreateAssociationMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User";
import { Association, BelongsToGetAssociationMixin, BelongsToManyAddAssociationMixin, BelongsToManyAssociation, BelongsToManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManyRemoveAssociationMixin, BelongsToManySetAssociationsMixin, 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";
@@ -49,11 +49,17 @@ export class Post extends Model<InferAttributes<Post>, InferCreationAttributes<P
@BelongsToMany(()=>Tag, { through: { model: ()=>PostTag, unique: false}, inverse: {as: 'taggedPosts'} })
declare postTags?:NonAttribute<Tag[]>;
@BelongsToMany(()=>Bucket,{through: { model: ()=> PostBucket, unique: false}, inverse:{as: 'bucketPosts'}, foreignKey: 'postId', otherKey: 'bucketId'})
declare postBuckets:NonAttribute<Bucket[]>;
@BelongsToMany(()=>Bucket,{through: {model:'PostBucket'}, throughAssociations: {
fromSource: 'postPostBuckets',
toSource: 'post',
fromTarget: 'postBucketPosts',
toTarget: 'PostBucket',
}, inverse:{as: 'posts'}, foreignKey: 'postId', otherKey: 'bucketId'})
declare buckets?:NonAttribute<Bucket[]>;
declare getUser: BelongsToGetAssociationMixin<User>;
declare static associations: {
@@ -62,16 +68,25 @@ export class Post extends Model<InferAttributes<Post>, InferCreationAttributes<P
postBuckets: Association<Bucket, Post>
postTags: Association<Tag, Post>;
};
declare getUser: BelongsToGetAssociationMixin<User>;
declare getBuckets: BelongsToManyGetAssociationsMixin<Bucket>;
declare setBuckets: BelongsToManySetAssociationsMixin<Bucket,Bucket['id']>;
declare addBucket: BelongsToManyAddAssociationMixin<Bucket,Bucket['id']>;
declare addBuckets: BelongsToManyAddAssociationMixin<Bucket,Bucket['id']>;
declare removeBucket: BelongsToManyRemoveAssociationMixin<Bucket,Bucket['id']>;
declare removeBuckets: BelongsToManyRemoveAssociationMixin<Bucket,Bucket['id']>;
declare createBucket: BelongsToManyCreateAssociationMixin<Bucket>;
}
export class PostBucket extends Model<InferAttributes<PostBucket>, InferCreationAttributes<PostBucket>>{
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, PostBucket]
})
declare post?:NonAttribute<Post>
declare bucket?:NonAttribute<Bucket>
PostBucket.sync();
}