Prechádzať zdrojové kódy

refactor: Updates relation definitions

Appends Model(s) to relation name to avoid conflicts with some foreign keys
Owen Diffey 4 mesiacov pred
rodič
commit
78eea1e329

+ 1 - 0
backend/src/main.ts

@@ -75,6 +75,7 @@ ModuleManager.startup().then(async () => {
 
 	console.log(
 		await News.findAll({
+			include: ["createdByModel"],
 			where: {
 				status: NewsStatus.PUBLISHED
 			}

+ 31 - 3
backend/src/modules/DataModule/models/News.ts

@@ -3,7 +3,13 @@ import {
 	Model,
 	InferAttributes,
 	InferCreationAttributes,
-	CreationOptional
+	CreationOptional,
+	ForeignKey,
+	NonAttribute,
+	BelongsToCreateAssociationMixin,
+	BelongsToGetAssociationMixin,
+	BelongsToSetAssociationMixin,
+	Association
 } from "sequelize";
 import { NewsStatus } from "@models/News/NewsStatus";
 import EventsModule from "@/modules/EventsModule";
@@ -24,9 +30,23 @@ export class News extends Model<
 
 	declare showToNewUsers: CreationOptional<boolean>;
 
+	declare createdBy: ForeignKey<User["_id"]>;
+
 	declare createdAt: CreationOptional<Date>;
 
 	declare updatedAt: CreationOptional<Date>;
+
+	declare getCreatedByModel: BelongsToGetAssociationMixin<User>;
+
+	declare setCreatedByModel: BelongsToSetAssociationMixin<User, number>;
+
+	declare createCreatedByModel: BelongsToCreateAssociationMixin<User>;
+
+	declare createdByModel?: NonAttribute<User>;
+
+	declare static associations: {
+		createdByModel: Association<News, User>;
+	};
 }
 
 export const schema = {
@@ -67,8 +87,16 @@ export const schema = {
 export const options = {};
 
 export const setup = async () => {
-	News.belongsTo(User, { foreignKey: "createdBy" });
-	User.hasMany(News, { foreignKey: "createdBy" });
+	News.belongsTo(User, {
+		as: "createdByModel",
+		foreignKey: {
+			name: "createdBy",
+			type: DataTypes.OBJECTID,
+			allowNull: false
+		},
+		onDelete: "RESTRICT",
+		onUpdate: "RESTRICT"
+	});
 
 	News.afterSave(async record => {
 		const oldDoc = record.previous();

+ 32 - 2
backend/src/modules/DataModule/models/Session.ts

@@ -3,9 +3,16 @@ import {
 	Model,
 	InferAttributes,
 	InferCreationAttributes,
-	CreationOptional
+	CreationOptional,
+	ForeignKey,
+	NonAttribute,
+	BelongsToCreateAssociationMixin,
+	BelongsToGetAssociationMixin,
+	BelongsToSetAssociationMixin,
+	Association
 } from "sequelize";
 import { ObjectIdType } from "@/modules/DataModule";
+import User from "./User";
 
 export class Session extends Model<
 	InferAttributes<Session>,
@@ -13,11 +20,23 @@ export class Session extends Model<
 > {
 	declare sessionId: ObjectIdType;
 
-	declare userId: number;
+	declare userId: ForeignKey<User["_id"]>;
 
 	declare createdAt: CreationOptional<Date>;
 
 	declare updatedAt: CreationOptional<Date>;
+
+	declare getUserModel: BelongsToGetAssociationMixin<User>;
+
+	declare setUserModel: BelongsToSetAssociationMixin<User, number>;
+
+	declare createUserModel: BelongsToCreateAssociationMixin<User>;
+
+	declare userModel?: NonAttribute<User>;
+
+	declare static associations: {
+		userModel: Association<Session, User>;
+	};
 }
 
 export const schema = {
@@ -43,6 +62,17 @@ export const schema = {
 export const options = {};
 
 export const setup = async () => {
+	Session.belongsTo(User, {
+		as: "userModel",
+		foreignKey: {
+			name: "userId",
+			type: DataTypes.OBJECTID,
+			allowNull: false
+		},
+		onDelete: "RESTRICT",
+		onUpdate: "RESTRICT"
+	});
+
 	// Session.afterSave(async record => {
 	// });
 	// Session.afterDestroy(async record => {

+ 98 - 1
backend/src/modules/DataModule/models/User.ts

@@ -3,10 +3,24 @@ import {
 	Model,
 	InferAttributes,
 	InferCreationAttributes,
-	CreationOptional
+	CreationOptional,
+	HasManyAddAssociationMixin,
+	HasManyCountAssociationsMixin,
+	HasManyCreateAssociationMixin,
+	HasManyGetAssociationsMixin,
+	HasManyHasAssociationMixin,
+	HasManySetAssociationsMixin,
+	HasManyAddAssociationsMixin,
+	HasManyHasAssociationsMixin,
+	HasManyRemoveAssociationMixin,
+	HasManyRemoveAssociationsMixin,
+	NonAttribute,
+	Association
 } from "sequelize";
 import { UserRole } from "./User/UserRole";
 import { ObjectIdType } from "@/modules/DataModule";
+import Session from "./Session";
+import News from "./News";
 
 export class User extends Model<
 	InferAttributes<User>,
@@ -23,6 +37,67 @@ export class User extends Model<
 	declare createdAt: CreationOptional<Date>;
 
 	declare updatedAt: CreationOptional<Date>;
+
+	declare getSessionModels: HasManyGetAssociationsMixin<Session>;
+
+	declare addSessionModel: HasManyAddAssociationMixin<Session, number>;
+
+	declare addSessionModels: HasManyAddAssociationsMixin<Session, number>;
+
+	declare setSessionModels: HasManySetAssociationsMixin<Session, number>;
+
+	declare removeSessionModel: HasManyRemoveAssociationMixin<Session, number>;
+
+	declare removeSessionModels: HasManyRemoveAssociationsMixin<
+		Session,
+		number
+	>;
+
+	declare hasSessionModel: HasManyHasAssociationMixin<Session, number>;
+
+	declare hasSessionModels: HasManyHasAssociationsMixin<Session, number>;
+
+	declare countSessionModels: HasManyCountAssociationsMixin;
+
+	declare createSessionModel: HasManyCreateAssociationMixin<
+		Session,
+		"userId"
+	>;
+
+	declare sessionModels?: NonAttribute<Session[]>;
+
+	declare getCreatedNewsModels: HasManyGetAssociationsMixin<News>;
+
+	declare addCreatedNewsModel: HasManyAddAssociationMixin<News, number>;
+
+	declare addCreatedNewsModels: HasManyAddAssociationsMixin<News, number>;
+
+	declare setCreatedNewsModels: HasManySetAssociationsMixin<News, number>;
+
+	declare removeCreatedNewsModel: HasManyRemoveAssociationMixin<News, number>;
+
+	declare removeCreatedNewsModels: HasManyRemoveAssociationsMixin<
+		News,
+		number
+	>;
+
+	declare hasCreatedNewsModel: HasManyHasAssociationMixin<News, number>;
+
+	declare hasCreatedNewsModels: HasManyHasAssociationsMixin<News, number>;
+
+	declare countCreatedNewsModels: HasManyCountAssociationsMixin;
+
+	declare createCreatedNewsModel: HasManyCreateAssociationMixin<
+		News,
+		"createdBy"
+	>;
+
+	declare createdNewsModels?: NonAttribute<News[]>;
+
+	declare static associations: {
+		sessionModels: Association<User, Session>;
+		createdNewsModels: Association<User, News>;
+	};
 }
 
 export const schema = {
@@ -61,6 +136,28 @@ export const schema = {
 export const options = {};
 
 export const setup = async () => {
+	User.hasMany(Session, {
+		as: "sessionModels",
+		foreignKey: {
+			name: "userId",
+			type: DataTypes.OBJECTID,
+			allowNull: false
+		},
+		onDelete: "RESTRICT",
+		onUpdate: "RESTRICT"
+	});
+
+	User.hasMany(News, {
+		as: "createdNewsModels",
+		foreignKey: {
+			name: "createdBy",
+			type: DataTypes.OBJECTID,
+			allowNull: false
+		},
+		onDelete: "RESTRICT",
+		onUpdate: "RESTRICT"
+	});
+
 	// User.afterSave(async record => {});
 
 	// User.afterDestroy(async record => {});