Skip to content

Commit

Permalink
refactor(entity): refactor model types
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanlvr authored and ysfscream committed Aug 4, 2021
1 parent f7860a8 commit 6905657
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 63 deletions.
16 changes: 9 additions & 7 deletions src/components/MessageList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:style="{ height: `${height}px`, marginTop: `${marginTop}px` }"
>
<span v-show="showLoadingIcon" class="loading-icon"><i class="el-icon-loading"></i></span>
<div v-for="message in showMessages" :key="message.mid" :id="message.mid">
<div v-for="message in showMessages" :key="message.id" :id="message.id">
<MsgLeftItem
v-if="!message.out"
:subsList="subscriptions"
Expand Down Expand Up @@ -78,12 +78,14 @@ export default class MessageList extends Vue {
this.showMessages = addMessages.concat(this.showMessages)
this.$nextTick(() => {
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)
Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/MsgPublish.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/database/models/CollectionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/database/models/ConnectionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/database/models/HistoryMessageHeaderEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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

@Column({ type: 'varchar' })
topic!: string

@Column({ type: 'integer' })
qos!: number
@Column({ type: 'simple-enum', enum: [0, 1, 2], default: 0 })
qos!: QoS
}
4 changes: 2 additions & 2 deletions src/database/models/HistoryMessagePayloadEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/database/models/MessageEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/database/models/ScriptEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/database/models/SubscriptionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/database/models/WillEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import ConnectionEntity from './ConnectionEntity'

@Entity('WillEntity')
export default class WillEntity {
@PrimaryGeneratedColumn({ type: 'integer' })
id?: number
@PrimaryGeneratedColumn('uuid')
id!: string

@Column({ type: 'varchar' })
lastWillTopic!: string

@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
Expand Down
12 changes: 6 additions & 6 deletions src/database/services/ConnectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class ConnectionService {

public async updateConnectionCollectionId(
id: number,
updatedCollectionId: number,
updatedCollectionId: string,
): Promise<ConnectionModel | undefined> {
const query: ConnectionEntity | undefined = await this.connectionRepository.findOne({
where: {
Expand All @@ -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<string> {
Expand Down Expand Up @@ -102,17 +102,17 @@ export default class ConnectionService {
return removed as ConnectionEntity
}

public async loadAllConnectionsIds(): Promise<number[] | undefined> {
const res: number[] = []
public async loadAllConnectionsIds(): Promise<string[] | undefined> {
const res: string[] = []
const query: ConnectionEntity[] | undefined = await this.connectionRepository.find({
select: ['id'],
})
if (!query) {
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[]
}
}
12 changes: 12 additions & 0 deletions src/database/services/MessageService.ts
Original file line number Diff line number Diff line change
@@ -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<MessageEntity>,
) {}
}
16 changes: 8 additions & 8 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -168,7 +168,7 @@ declare global {
id?: string
retain: boolean
topic: string
qos: number
qos: QoS
}

interface HistoryMessagePayloadModel {
Expand All @@ -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
Expand All @@ -214,7 +214,7 @@ declare global {
username: string
password: string
path: string
certType?: string
certType?: CertType
ssl: boolean
mqttVersion: string
unreadMessageCount: number
Expand Down
22 changes: 11 additions & 11 deletions src/views/connections/ConnectionsDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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`,
)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand All @@ -1168,7 +1168,7 @@ export default class ConnectionsDetail extends Vue {
return false
}
const publishMessage: MessageModel = {
mid,
id,
out: true,
createAt: time.getNowDate(),
topic,
Expand Down

0 comments on commit 6905657

Please sign in to comment.