Skip to content

Commit

Permalink
refactor(connection): migration connection api to service
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanlvr authored and ysfscream committed Aug 4, 2021
1 parent ba711a2 commit f7860a8
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 36 deletions.
24 changes: 15 additions & 9 deletions src/database/models/CollectionEntity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne } from 'typeorm'
import ConnectionEntity from './ConnectionEntity'

@Entity('CollectionEntity')
Expand All @@ -7,17 +7,23 @@ export default class CollectionEntity {
id?: number

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

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

@Column({ type: 'integer', comment: 'order in the collection' })
@Column({ type: 'integer', nullable: true, comment: 'order in the collection' })
orderId!: number

@OneToMany(() => ConnectionEntity, (connection) => connection.collection)
connections!: ConnectionEntity[]
@Column({ type: 'boolean', default: true })
isCollection!: true

@OneToMany(() => CollectionEntity, (collection) => collection.id)
// current collection parent
@ManyToOne(() => CollectionEntity, (collection) => collection.collection, { onDelete: 'CASCADE' })
collection!: CollectionEntity[]

// collections children
@OneToMany(() => CollectionEntity, (collection) => collection.collection)
collections!: CollectionEntity[]

// connections children
@OneToMany(() => ConnectionEntity, (connection) => connection.collection)
connections!: ConnectionEntity[]
}
17 changes: 10 additions & 7 deletions src/database/models/ConnectionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ export default class ConnectionEntity {
@Column({ type: 'boolean' })
reconnect!: boolean

@Column({ type: 'varchar' })
@Column({ type: 'varchar', nullable: true })
username!: string

@Column({ type: 'varchar' })
@Column({ type: 'varchar', nullable: true })
password!: string

@Column({ type: 'varchar' })
@Column({ type: 'varchar', nullable: true })
path!: string

@Column({ type: 'varchar' })
@Column({ type: 'varchar', nullable: true })
certType!: string

@Column({ type: 'boolean' })
Expand All @@ -63,7 +63,7 @@ export default class ConnectionEntity {
@ManyToOne(() => CollectionEntity, (collection) => collection.collections, { onDelete: 'CASCADE' })
collection!: ConnectionEntity

@Column({ type: 'integer', comment: 'order in the collection' })
@Column({ type: 'integer', nullable: true, comment: 'order in the collection' })
orderId!: number

@Column({ type: 'boolean' })
Expand All @@ -78,6 +78,9 @@ export default class ConnectionEntity {
@Column({ type: 'varchar' })
key!: string

@Column({ type: 'boolean', default: false })
isCollection!: false

@OneToOne(() => WillEntity, (will) => will.connection, { cascade: true, onDelete: 'CASCADE' })
@JoinColumn()
will!: WillEntity
Expand All @@ -88,9 +91,9 @@ export default class ConnectionEntity {
@OneToMany(() => SubscriptionEntity, (subscription) => subscription.connection)
subscriptions!: SubscriptionEntity[]

@Column({ type: 'datetime' })
@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
createAt!: string

@Column({ type: 'datetime' })
@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
updateAt!: string
}
2 changes: 1 addition & 1 deletion src/database/models/MessageEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class MessageEntity {
@PrimaryGeneratedColumn({ type: 'integer' })
mid!: number

@Column({ type: 'datetime' })
@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
createAt!: string

@Column({ type: 'boolean' })
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 @@ -18,16 +18,16 @@ export default class WillEntity {
@Column({ type: 'boolean' })
lastWillRetain!: boolean

@Column({ type: 'integer' })
@Column({ type: 'integer', nullable: true })
willDelayInterval!: number

@Column({ type: 'boolean' })
@Column({ type: 'boolean', nullable: true })
payloadFormatIndicator!: boolean

@Column({ type: 'integer' })
@Column({ type: 'integer', nullable: true })
messageExpiryInterval!: number

@Column({ type: 'varchar' })
@Column({ type: 'varchar', default: '' })
contentType!: string

@OneToOne(() => ConnectionEntity, (connection) => connection.will, { onDelete: 'CASCADE' })
Expand Down
112 changes: 97 additions & 15 deletions src/database/services/ConnectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,117 @@ import { Service } from 'typedi'
import { InjectRepository } from 'typeorm-typedi-extensions'
import ConnectionEntity from '../models/ConnectionEntity'
import { Repository } from 'typeorm'

interface ConnectionQueryModel {
id?: number
createDesc?: boolean
}
import deepMerge from '@/utils/deepMerge'

@Service()
export default class ConnectionService {
constructor(
@InjectRepository(ConnectionEntity)
private collectionRepository: Repository<ConnectionEntity>,
private connectionRepository: Repository<ConnectionEntity>,
) {}

public async loadConnection(
connectionQuery: Partial<ConnectionQueryModel>,
): Promise<Partial<ConnectionModel> | undefined> {
let res: ConnectionModel | undefined = undefined
const query: ConnectionEntity | undefined = await this.collectionRepository.findOne({
public async updateConnectionCollectionId(
id: number,
updatedCollectionId: number,
): Promise<ConnectionModel | undefined> {
const query: ConnectionEntity | undefined = await this.connectionRepository.findOne({
where: {
id,
},
})
if (!query) {
return
}
query.collection.id = updatedCollectionId
await this.connectionRepository.save(query)
const res: ConnectionModel = query
return res
}

public async updateConnection(id: number, data: Partial<ConnectionModel>): Promise<ConnectionModel | undefined> {
let res: ConnectionEntity | undefined = await this.connectionRepository.findOne(id)
if (!res) {
return
}
deepMerge(res, data)
const query: ConnectionEntity | undefined = await this.connectionRepository.save(res)
return query
}

public async importConnections(data: ConnectionModel[]): Promise<string> {
try {
await this.connectionRepository.save(data)
} catch (err) {
return err.toString()
}
return 'ok'
}

public async updateConnectionSequenceId(id: number, updatedOrder: number): Promise<ConnectionModel | undefined> {
const query: ConnectionEntity | undefined = await this.connectionRepository.findOne({
where: {
id: connectionQuery.id,
id,
},
})
if (!query) {
return
}
query.orderId = updatedOrder
await this.connectionRepository.save(query)
return query as ConnectionModel
}

public async loadOneConnectionById(id: number): Promise<ConnectionModel | undefined> {
const query: ConnectionEntity | undefined = await this.connectionRepository.findOne({
where: {
id,
},
})
if (query === undefined) {
return undefined
}
return query as ConnectionModel
}

public async loadAllConnections(): Promise<ConnectionModel[] | undefined> {
const query: ConnectionEntity[] | undefined = await this.connectionRepository.find()
if (!query) {
return
}
return query as ConnectionModel[]
}

public async createConnection(connectionInsertParam: ConnectionModel): Promise<ConnectionModel | undefined> {
let res: ConnectionModel | undefined = connectionInsertParam
res = {
...query,
isCollection: false,
...res,
// FIXME: current DB don't need id generator
id: undefined,
}
return res
this.connectionRepository.save(res)
return res as ConnectionModel
}

public async deleteConnectionById(id: number): Promise<ConnectionModel | undefined> {
const query: ConnectionEntity | undefined = await this.connectionRepository.findOne(id)
if (!query) {
return
}
const removed: ConnectionEntity = await this.connectionRepository.remove(query)
return removed as ConnectionEntity
}

public async loadAllConnectionsIds(): Promise<number[] | undefined> {
const res: number[] = []
const query: ConnectionEntity[] | undefined = await this.connectionRepository.find({
select: ['id'],
})
if (!query) {
return
}
query.forEach((entity) => {
if (entity && entity.id) res?.push(entity.id)
})
return res as number[]
}
}

0 comments on commit f7860a8

Please sign in to comment.