From 33822a10ce33909bfbdef5c4d29350396cd4a4b4 Mon Sep 17 00:00:00 2001 From: oceanlvr <657531018@qq.com> Date: Wed, 4 Aug 2021 16:11:36 +0800 Subject: [PATCH] refactor(entity): refactor model types --- src/components/MessageList.vue | 16 ++++++++------ src/components/MsgPublish.vue | 3 +-- src/database/models/CollectionEntity.ts | 6 ++--- src/database/models/ConnectionEntity.ts | 12 +++++----- .../models/HistoryMessageHeaderEntity.ts | 8 +++---- .../models/HistoryMessagePayloadEntity.ts | 4 ++-- src/database/models/MessageEntity.ts | 8 +++---- src/database/models/ScriptEntity.ts | 4 ++-- src/database/models/SubscriptionEntity.ts | 8 +++---- src/database/models/WillEntity.ts | 8 +++---- src/database/services/ConnectionService.ts | 12 +++++----- src/database/services/MessageService.ts | 12 ++++++++++ src/types/global.d.ts | 16 +++++++------- src/views/connections/ConnectionsDetail.vue | 22 +++++++++---------- 14 files changed, 76 insertions(+), 63 deletions(-) create mode 100644 src/database/services/MessageService.ts diff --git a/src/components/MessageList.vue b/src/components/MessageList.vue index 50bb96bf7..7158989ae 100644 --- a/src/components/MessageList.vue +++ b/src/components/MessageList.vue @@ -5,7 +5,7 @@ :style="{ height: `${height}px`, marginTop: `${marginTop}px` }" > -
+
{ if (addMessages.length > 0) { - const id = addMessages[addMessages.length - 1].mid - const idBox = document.getElementById(id.toString() as string) - if (idBox) { - idBox.scrollIntoView(true) + const id = addMessages[addMessages.length - 1].id + if (id) { + const idBox = document.getElementById(id.toString() as string) + if (idBox) { + idBox.scrollIntoView(true) + } + this.showLoadingIcon = false } - this.showLoadingIcon = false } }) window.clearTimeout(timer) @@ -93,7 +95,7 @@ export default class MessageList extends Vue { } private getNewMessages(newMessageList: MessageModel[], oldMessageList: MessageModel[]) { - const newMessages = newMessageList.filter((item) => oldMessageList.every((one) => one.mid !== item.mid)) + const newMessages = newMessageList.filter((item) => oldMessageList.every((one) => one.id !== item.id)) return newMessages } diff --git a/src/components/MsgPublish.vue b/src/components/MsgPublish.vue index 6dfac1dfc..034ee2a41 100644 --- a/src/components/MsgPublish.vue +++ b/src/components/MsgPublish.vue @@ -119,7 +119,6 @@ export default class MsgPublish extends Vue { private payloadsHistory: HistoryMessagePayloadModel[] | [] = [] private historyIndex: number = -1 private defaultMsgRecord: MessageModel = { - mid: 0, createAt: '', out: true, qos: 0, @@ -216,7 +215,7 @@ export default class MsgPublish extends Vue { } private async send() { - this.msgRecord.mid = uuidv4() + this.msgRecord.id = uuidv4() this.$emit('handleSend', this.msgRecord, this.payloadType, this.loadHistoryData) } diff --git a/src/database/models/CollectionEntity.ts b/src/database/models/CollectionEntity.ts index 44ed7e53a..57f6b3bb8 100644 --- a/src/database/models/CollectionEntity.ts +++ b/src/database/models/CollectionEntity.ts @@ -3,8 +3,8 @@ import ConnectionEntity from './ConnectionEntity' @Entity('CollectionEntity') export default class CollectionEntity { - @PrimaryGeneratedColumn({ type: 'integer' }) - id?: number + @PrimaryGeneratedColumn('uuid') + id!: string @Column({ type: 'varchar' }) name!: string @@ -17,7 +17,7 @@ export default class CollectionEntity { // current collection parent @ManyToOne(() => CollectionEntity, (collection) => collection.collection, { onDelete: 'CASCADE' }) - collection!: CollectionEntity[] + collection!: CollectionEntity // collections children @OneToMany(() => CollectionEntity, (collection) => collection.collection) diff --git a/src/database/models/ConnectionEntity.ts b/src/database/models/ConnectionEntity.ts index a1cea1d3d..e108c5c0f 100644 --- a/src/database/models/ConnectionEntity.ts +++ b/src/database/models/ConnectionEntity.ts @@ -6,8 +6,8 @@ import CollectionEntity from './CollectionEntity' import WillEntity from './WillEntity' @Entity('ConnectionEntity') export default class ConnectionEntity { - @PrimaryGeneratedColumn({ type: 'integer' }) - id?: number + @PrimaryGeneratedColumn('uuid') + id!: string @Column({ type: 'varchar' }) clientId!: string @@ -18,8 +18,8 @@ export default class ConnectionEntity { @Column({ type: 'boolean', default: true }) clean!: boolean - @Column({ type: 'varchar' }) - protocol!: string + @Column({ type: 'simple-enum', enum: ['ws', 'wss', 'mqtt', 'mqtts'], default: 'mqtt' }) + protocol!: Protocol @Column({ type: 'varchar' }) host!: string @@ -45,8 +45,8 @@ export default class ConnectionEntity { @Column({ type: 'varchar', nullable: true }) path!: string - @Column({ type: 'varchar', nullable: true }) - certType!: string + @Column({ type: 'varchar', enum: ['', 'server', 'self'], default: '', nullable: true }) + certType!: CertType @Column({ type: 'boolean' }) ssl!: boolean diff --git a/src/database/models/HistoryMessageHeaderEntity.ts b/src/database/models/HistoryMessageHeaderEntity.ts index fd5fe78aa..4e6f64949 100644 --- a/src/database/models/HistoryMessageHeaderEntity.ts +++ b/src/database/models/HistoryMessageHeaderEntity.ts @@ -2,8 +2,8 @@ import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm' @Entity('historyMessageHeaderEntity') export default class historyMessageHeaderEntity { - @PrimaryGeneratedColumn({ type: 'integer' }) - id?: number + @PrimaryGeneratedColumn('uuid') + id!: string @Column({ type: 'boolean' }) retain!: boolean @@ -11,6 +11,6 @@ export default class historyMessageHeaderEntity { @Column({ type: 'varchar' }) topic!: string - @Column({ type: 'integer' }) - qos!: number + @Column({ type: 'simple-enum', enum: [0, 1, 2], default: 0 }) + qos!: QoS } diff --git a/src/database/models/HistoryMessagePayloadEntity.ts b/src/database/models/HistoryMessagePayloadEntity.ts index ce6941153..bcf668e11 100644 --- a/src/database/models/HistoryMessagePayloadEntity.ts +++ b/src/database/models/HistoryMessagePayloadEntity.ts @@ -2,8 +2,8 @@ import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm' @Entity('historyMessagePayloadEntity') export default class historyMessagePayloadEntity { - @PrimaryGeneratedColumn({ type: 'integer' }) - id?: number + @PrimaryGeneratedColumn('uuid') + id!: string @Column({ type: 'varchar' }) payload!: string diff --git a/src/database/models/MessageEntity.ts b/src/database/models/MessageEntity.ts index cbe029943..6e100c1ed 100644 --- a/src/database/models/MessageEntity.ts +++ b/src/database/models/MessageEntity.ts @@ -3,8 +3,8 @@ import ConnectionEntity from './ConnectionEntity' @Entity('MessageEntity') export default class MessageEntity { - @PrimaryGeneratedColumn({ type: 'integer' }) - mid!: number + @PrimaryGeneratedColumn('uuid') + id!: string @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' }) createAt!: string @@ -15,8 +15,8 @@ export default class MessageEntity { @Column({ type: 'varchar' }) payload!: string - @Column({ type: 'integer' }) - qos!: number + @Column({ type: 'simple-enum', enum: [0, 1, 2], default: 0 }) + qos!: QoS @Column({ type: 'boolean' }) retain!: boolean diff --git a/src/database/models/ScriptEntity.ts b/src/database/models/ScriptEntity.ts index 55762f926..40881d2c9 100644 --- a/src/database/models/ScriptEntity.ts +++ b/src/database/models/ScriptEntity.ts @@ -2,8 +2,8 @@ import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm' @Entity('ScriptEntity') export default class ScriptEntity { - @PrimaryGeneratedColumn({ type: 'integer' }) - id?: number + @PrimaryGeneratedColumn('uuid') + id!: string @Column({ type: 'varchar' }) name!: string diff --git a/src/database/models/SubscriptionEntity.ts b/src/database/models/SubscriptionEntity.ts index fdfc25518..a93e88385 100644 --- a/src/database/models/SubscriptionEntity.ts +++ b/src/database/models/SubscriptionEntity.ts @@ -3,14 +3,14 @@ import ConnectionEntity from './ConnectionEntity' @Entity('SubscriptionEntity') export default class SubscriptionEntity { - @PrimaryGeneratedColumn({ type: 'integer' }) - id?: number + @PrimaryGeneratedColumn('uuid') + id!: string @Column({ type: 'varchar' }) topic!: string - @Column({ type: 'integer' }) - qos!: number + @Column({ type: 'simple-enum', enum: [0, 1, 2], default: 0 }) + qos!: QoS @Column({ type: 'varchar' }) alias!: string diff --git a/src/database/models/WillEntity.ts b/src/database/models/WillEntity.ts index d141a97d9..7f9eef158 100644 --- a/src/database/models/WillEntity.ts +++ b/src/database/models/WillEntity.ts @@ -3,8 +3,8 @@ import ConnectionEntity from './ConnectionEntity' @Entity('WillEntity') export default class WillEntity { - @PrimaryGeneratedColumn({ type: 'integer' }) - id?: number + @PrimaryGeneratedColumn('uuid') + id!: string @Column({ type: 'varchar' }) lastWillTopic!: string @@ -12,8 +12,8 @@ export default class WillEntity { @Column({ type: 'varchar' }) lastWillPayload!: string - @Column({ type: 'integer' }) - lastWillQos!: number + @Column({ type: 'simple-enum', enum: [0, 1, 2], default: 0 }) + lastWillQos!: QoS @Column({ type: 'boolean' }) lastWillRetain!: boolean diff --git a/src/database/services/ConnectionService.ts b/src/database/services/ConnectionService.ts index cf0c7ab71..b8676d7cf 100644 --- a/src/database/services/ConnectionService.ts +++ b/src/database/services/ConnectionService.ts @@ -13,7 +13,7 @@ export default class ConnectionService { public async updateConnectionCollectionId( id: number, - updatedCollectionId: number, + updatedCollectionId: string, ): Promise { const query: ConnectionEntity | undefined = await this.connectionRepository.findOne({ where: { @@ -36,7 +36,7 @@ export default class ConnectionService { } deepMerge(res, data) const query: ConnectionEntity | undefined = await this.connectionRepository.save(res) - return query + return query as ConnectionModel } public async importConnections(data: ConnectionModel[]): Promise { @@ -102,8 +102,8 @@ export default class ConnectionService { return removed as ConnectionEntity } - public async loadAllConnectionsIds(): Promise { - const res: number[] = [] + public async loadAllConnectionsIds(): Promise { + const res: string[] = [] const query: ConnectionEntity[] | undefined = await this.connectionRepository.find({ select: ['id'], }) @@ -111,8 +111,8 @@ export default class ConnectionService { return } query.forEach((entity) => { - if (entity && entity.id) res?.push(entity.id) + if (entity && entity.id) res.push(entity.id) }) - return res as number[] + return res as string[] } } diff --git a/src/database/services/MessageService.ts b/src/database/services/MessageService.ts new file mode 100644 index 000000000..6b030a862 --- /dev/null +++ b/src/database/services/MessageService.ts @@ -0,0 +1,12 @@ +import { Service } from 'typedi' +import { InjectRepository } from 'typeorm-typedi-extensions' +import MessageEntity from '../models/MessageEntity' +import { Repository } from 'typeorm' + +@Service() +export default class MessageService { + constructor( + @InjectRepository(MessageEntity) + private messageRepository: Repository, + ) {} +} diff --git a/src/types/global.d.ts b/src/types/global.d.ts index 26c2b4bb2..e3b2a001a 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -147,18 +147,18 @@ declare global { interface SubscriptionModel { topic: string - qos: number + qos: QoS alias?: string retain?: boolean color?: string } interface MessageModel { - mid: number + id?: string createAt: string out: boolean payload: string - qos: number + qos: QoS retain: boolean topic: string } @@ -168,7 +168,7 @@ declare global { id?: string retain: boolean topic: string - qos: number + qos: QoS } interface HistoryMessagePayloadModel { @@ -195,17 +195,17 @@ declare global { interface WillModel { lastWillTopic: string lastWillPayload: string - lastWillQos: number + lastWillQos: QoS lastWillRetain: boolean properties?: WillPropertiesModel } interface ConnectionModel extends SSLPath { - readonly id?: number + readonly id?: string clientId: string name: string clean: boolean - protocol?: string + protocol?: Protocol host: string port: number keepalive: number @@ -214,7 +214,7 @@ declare global { username: string password: string path: string - certType?: string + certType?: CertType ssl: boolean mqttVersion: string unreadMessageCount: number diff --git a/src/views/connections/ConnectionsDetail.vue b/src/views/connections/ConnectionsDetail.vue index c079b0539..101684132 100644 --- a/src/views/connections/ConnectionsDetail.vue +++ b/src/views/connections/ConnectionsDetail.vue @@ -635,12 +635,12 @@ export default class ConnectionsDetail extends Vue { // Delete message private async handleDeleteMessage() { const connectID = this.record.id - let mid = '' - if (this.selectedMessage) { - mid = this.selectedMessage.mid.toString() + let id = '' + if (this.selectedMessage && this.selectedMessage.id) { + id = this.selectedMessage.id.toString() } if (connectID) { - const res: ConnectionModel | null = await deleteMessage(connectID.toString() as string, mid as string) + const res: ConnectionModel | null = await deleteMessage(connectID.toString() as string, id as string) if (res) { this.showContextmenu = false this.$message.success(this.$t('common.deleteSuccess') as string) @@ -1026,7 +1026,7 @@ export default class ConnectionsDetail extends Vue { const convertPayload = this.convertPayloadByType(payload, this.receivedMsgType, 'receive') as string const receviedPayload = this.convertPayloadByScript(convertPayload, 'publish') const receivedMessage: MessageModel = { - mid: uuidv4(), + id: uuidv4(), out: false, createAt: time.getNowDate(), topic, @@ -1076,7 +1076,7 @@ export default class ConnectionsDetail extends Vue { this.messagesAddedNewItem = true this.$log.info( `Message arrived: message added #${JSON.stringify( - receivedMessage.mid, + receivedMessage.id, )} added to topic ${topic}, MQTT.js onMessageArrived trigger`, ) } @@ -1113,9 +1113,9 @@ export default class ConnectionsDetail extends Vue { this.stopTimedSend() this.sendTimeId = window.setInterval(() => { const { ...oneMessage } = message - let { mid } = oneMessage - mid = uuidv4() - this.sendOneMessage(Object.assign(oneMessage, { mid }), type) + let { id } = oneMessage + id = uuidv4() + this.sendOneMessage(Object.assign(oneMessage, { id }), type) }, time * 1000) } @@ -1144,7 +1144,7 @@ export default class ConnectionsDetail extends Vue { this.stopTimedSend() return false } - const { mid, topic, qos, payload, retain } = message + const { id, topic, qos, payload, retain } = message if (!topic) { this.$message.warning(this.$t('connections.topicReuired') as string) this.stopTimedSend() @@ -1168,7 +1168,7 @@ export default class ConnectionsDetail extends Vue { return false } const publishMessage: MessageModel = { - mid, + id, out: true, createAt: time.getNowDate(), topic,