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
-6
View File
@@ -34,9 +34,3 @@ export class APIKey extends Model<InferAttributes<APIKey>, InferCreationAttribut
declare updatedAt: CreationOptional<Date>;
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [APIKey]
});
+1 -6
View File
@@ -26,10 +26,5 @@ export class Attachment extends Model<InferAttributes<Attachment>, InferCreation
declare static associations: {
bucket: Association<Bucket, Attachment>;
};
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [Attachment]
})
-6
View File
@@ -46,9 +46,3 @@ export class Auth extends Model<InferAttributes<Auth>, InferCreationAttributes<A
};
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [Auth]
});
+4 -9
View File
@@ -1,4 +1,4 @@
import { Association, BelongsToGetAssociationMixin, BelongsToManyGetAssociationsMixin, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize, sql } from "@sequelize/core";
import { Association, BelongsToGetAssociationMixin, BelongsToManyAddAssociationMixin, BelongsToManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManyRemoveAssociationMixin, BelongsToManySetAssociationsMixin, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize, sql } from "@sequelize/core";
import { Post } from "./Post";
import { SqliteDialect } from '@sequelize/sqlite3';
@@ -14,19 +14,14 @@ export class Bucket extends Model<InferAttributes<Bucket>, InferCreationAttribut
declare id: UUID
/** Defined by {@link Post.buckets} */
declare bucketPosts?:NonAttribute<Post>[];
declare posts?:NonAttribute<Post>[];
/** Defined by {@link Attachment.bucket} */
declare attachments?:NonAttribute<Attachment>[];
declare static associations: {
bucketPosts: Association<Post, Bucket>;
posts: Association<Post, Bucket>;
attachments: Association<Attachment, Bucket>;
};
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [Bucket]
})
-6
View File
@@ -32,9 +32,3 @@ export class DBState extends Model<InferAttributes<DBState>, InferCreationAttrib
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [DBState]
});
+25 -11
View File
@@ -1,15 +1,29 @@
import { User } from './User';
import { Auth } from './Auth';
import { UserPerms } from './UserPerms';
import { Post } from './Post';
import { Tag } from './Tag';
import { Attachment } from './Attachment';
import { Bucket } from './Bucket';
import { DBState } from './DBState';
import { Post, PostBucket } from './Post';
import { PostTag } from './PostTag';
import { Project } from './Project';
import { Tag } from './Tag';
import { User, addUserScopes } from './User';
import { UserPerms, addUserPermsScopes } from './UserPerms';
import { SqliteDialect } from '@sequelize/sqlite3';
import Sequelize from '@sequelize/core';
User.sync();
Auth.sync();
UserPerms.sync();
PostTag.sync();
Tag.sync();
Post.sync();
export { User, Auth, UserPerms, Post, Tag, PostTag }
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [Auth, Attachment, Bucket, DBState, Post, PostBucket, PostTag, Project, Tag, User, UserPerms],
});
const dbSync = (async ()=> await sequelize.sync())().then(()=>{
addUserScopes();
addUserPermsScopes();
});
export { sequelize, dbSync, Auth, Attachment, Bucket, DBState, Post, PostBucket, PostTag, Project, Tag, User, UserPerms }
+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();
}
-5
View File
@@ -11,10 +11,5 @@ class PostTag extends Model<InferAttributes<PostTag>,InferCreationAttributes<Pos
declare tagId:number;
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [PostTag]
})
export { PostTag }
-6
View File
@@ -17,9 +17,3 @@ export class Project extends Model<InferAttributes<Project>, InferCreationAttrib
};
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [Project]
})
-6
View File
@@ -18,9 +18,3 @@ export class Tag extends Model<InferAttributes<Tag>, InferCreationAttributes<Tag
posts: Association<Post, Tag>;
};
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [Tag]
})
+13 -23
View File
@@ -22,9 +22,7 @@ type UserCreationAttributes = {
password:string;
perms?:Partial<InferAttributes<UserPerms>>
}
export class User extends Model<InferAttributes<User>, InferCreationAttributes<User>>{
@Attribute(DataTypes.INTEGER)
@PrimaryKey
@AutoIncrement
@@ -63,24 +61,16 @@ export class User extends Model<InferAttributes<User>, InferCreationAttributes<U
};
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [User]
});
User.addScope('defaultScope',{
attributes: {
exclude: ['password', 'createdAt', 'updatedAt'],
}
});
User.addScope('withPerms',{
include: [{association: 'perms' }]
})
User.addScope('withAuthtokens',{
include: [{association: 'authtokens'}]
})
export function addUserScopes(){
User.addScope('defaultScope',{
attributes: {
exclude: ['password', 'createdAt', 'updatedAt'],
}
});
User.addScope('withPerms',{
include: [{association: 'perms' }]
})
User.addScope('withAuthtokens',{
include: [{association: 'authtokens'}]
})
}
+4 -8
View File
@@ -26,13 +26,7 @@ export class UserPerms extends Model<InferAttributes<UserPerms>, InferCreationAt
}
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: 'db.sqlite',
models: [UserPerms],
});
export function addUserPermsScopes(){
UserPerms.addScope('defaultScope',{
attributes: {
@@ -43,4 +37,6 @@ UserPerms.addScope('withTimestamps',{
attributes: {
include: ['createdAt', 'updatedAt'],
}
});
});
}