Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3rd party broker #5015

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions forge/db/migrations/20250109-01-EE-3rd-party-broker-creds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Add 3rd party broker credentials table
*/
const { DataTypes } = require('sequelize')

module.exports = {
/**
* upgrade database
* @param {QueryInterface} context Sequelize.QueryInterface
*/
up: async (context) => {
await context.createTable('BrokerCredentials', {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: DataTypes.STRING, allowNull: false },
host: { type: DataTypes.STRING, allowNull: false },
port: { type: DataTypes.INTEGER, allowNull: false, default: 1883 },
protocol: { type: DataTypes.STRING, allowNull: false, default: 'mqtt:' },
protocolVersion: { type: DataTypes.INTEGER, allowNull: false, default: 4 },
ssl: { type: DataTypes.BOOLEAN, allowNull: false, default: false },
verifySSL: { type: DataTypes.BOOLEAN, allowNull: false, default: false },
clientId: { type: DataTypes.STRING, allowNull: false },
credentials: { type: DataTypes.TEXT, allowNull: false },
createdAt: { type: DataTypes.DATE },
updatedAt: { type: DataTypes.DATE },
TeamId: {
type: DataTypes.INTEGER,
references: { model: 'Teams', key: 'id' },
onDelete: 'cascade',
onUpdate: 'cascade'
}
})
await context.addIndex('BrokerCredentials', { name: 'broker_name_team_unique', fields: ['name', 'TeamId'], unique: true })
},
down: async (context) => {
}
}
50 changes: 50 additions & 0 deletions forge/db/models/BrokerCredentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
*

Check failure on line 2 in forge/db/models/BrokerCredentials.js

View workflow job for this annotation

GitHub Actions / Postgres tests (18.x)

Trailing spaces not allowed
*/
const { DataTypes } = require('sequelize')

module.exports = {
name: 'BrokerCredentials',
schema: {
name: { type: DataTypes.STRING, allowNull: false },
host: { type: DataTypes.STRING, allowNull: false },
port: { type: DataTypes.INTEGER, allowNull: false, default: 1883 },
protocol: { type: DataTypes.STRING, allowNull: false, default: 'mqtt:' },
protocolVersion: { type: DataTypes.INTEGER, allowNull: false, default: 4 },
ssl: { type: DataTypes.BOOLEAN, allowNull: false, default: false },
verifySSL: { type: DataTypes.BOOLEAN, allowNull: false, default: false },
clientId: { type: DataTypes.STRING, allowNull: false },
credentials: { type: DataTypes.TEXT, allowNull: false }
},
indexes: [
{ name: 'broker_name_team_unique', fields: ['name', 'TeamId'], unique: true }
],
associations: function (M) {
this.belongsTo(M.Team)
},
finders: function (M) {
return {
static: {
byId: async function (idOrHash) {
let id = idOrHash
if (typeof id === 'string') {
id = M.BrokerCredentials.decodeHashid(idOrHash)
}
return this.findOne({
where: { id }
})
},
byTeam: async function (teamId) {
if (typeof teamId === 'string') {
teamId = M.Team.decodeHashid(teamId)
}
return this.findAll({
where: {
TeamId: teamId
}
})
}
}
}
}
}
3 changes: 2 additions & 1 deletion forge/db/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const modelTypes = [
'BrokerClient',
'OAuthSession',
'Notification',
'TeamBrokerClient'
'TeamBrokerClient',
'BrokerCredentials'
]

// A local map of the known models.
Expand Down
23 changes: 23 additions & 0 deletions forge/db/views/BrokerCredentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
credentials: function (app, credentials) {
const filtered = []
credentials.forEach(c => {
filtered.push(this.clean(app, c))
})
return filtered
},
clean: function (app, cred) {
const result = cred.toJSON()
const cleaned = {
id: result.hashid,
name: result.name,
host: result.host,
port: result.port,
protocol: result.protocol,
ssl: result.ssl,
verifySSL: result.verifySSL,
clientId: result.clientId
}
return cleaned
}
}
3 changes: 2 additions & 1 deletion forge/db/views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const modelTypes = [
'Team',
'TeamType',
'User',
'TeamBrokerClient'
'TeamBrokerClient',
'BrokerCredentials'
]

async function register (app, viewType, viewModule) {
Expand Down
Loading
Loading