From 9311e2a55acf53df5d72a8ebce56e1fbbb086a8e Mon Sep 17 00:00:00 2001 From: joyja Date: Thu, 2 Apr 2020 18:46:01 +0000 Subject: [PATCH 1/2] fixed a bug where deleting tag would crash (trying to publish with deviceSource that has no existing tag) added getAll on deviceSources after tag delete --- src/database/model.js | 11 +++++++++-- src/relations.js | 9 ++++++++- src/service/service.js | 3 --- src/tag.js | 3 +++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/database/model.js b/src/database/model.js index f1b33ec..cb94a41 100644 --- a/src/database/model.js +++ b/src/database/model.js @@ -50,7 +50,7 @@ class Model { logger.error(error, { message: `sql: ${sql}` }) }) } - static createTable() { + static async createTable() { // fields should be formatted { colName, colType } for typical columns // fields should be formatted { colName, colRef, onDelete } for foreign key this.checkInitialized() @@ -69,7 +69,14 @@ class Model { } }) sql = `${sql});` - return this.executeUpdate(sql) + const result = await this.executeUpdate(sql) + for (const field of this.fields) { + if (field.colRef) { + sql = `CREATE INDEX IF NOT EXISTS idx_${this.table}_${field.colName} ON ${this.table} (${field.colName});` + await this.executeUpdate(sql) + } + } + return result } static async initialize(db, pubsub) { this.initialized = true diff --git a/src/relations.js b/src/relations.js index b57a39b..bf74a85 100644 --- a/src/relations.js +++ b/src/relations.js @@ -10,7 +10,7 @@ const { Service, Mqtt, MqttSource, MqttHistory } = require('./service') const { User } = require('./auth') const getTime = require('date-fns/getTime') -// This file creates any properties that form relationships with other models. +// This file creates properties and defines methods requiring relationships with other models. // It is defined here to prevent circular dependencies. // ============================== @@ -45,6 +45,13 @@ Object.defineProperties(ScanClass.prototype, { } }) +Tag.delete = async function(selector) { + const deleted = await this._deleteModel(selector) + await ModbusSource.getAll() + await EthernetIPSource.getAll() + return deleted +} + Tag.prototype.setScanClass = async function(id) { const scanClass = ScanClass.findById(id) if (scanClass) { diff --git a/src/service/service.js b/src/service/service.js index c2afccb..cf32297 100644 --- a/src/service/service.js +++ b/src/service/service.js @@ -40,9 +40,6 @@ class Service extends Model { this._createdBy = result.createdBy this._createdOn = result.createdOn } - delete() { - return super.delete(Service) - } get name() { this.checkInit() return this._name diff --git a/src/tag.js b/src/tag.js index 4fe6c09..597564a 100644 --- a/src/tag.js +++ b/src/tag.js @@ -46,6 +46,9 @@ class Tag extends Model { } return super.create(fields) } + static _deleteModel(selector) { + return super.delete(selector) + } async init(async) { const result = await super.init(async) this._name = result.name From be8aad52ed8b7ad3480705bd47d539a1e91e840d Mon Sep 17 00:00:00 2001 From: joyja Date: Thu, 2 Apr 2020 19:23:03 +0000 Subject: [PATCH 2/2] changed db name to tentacle-edge.db and added to git-ignore --- .gitignore | 1 + src/index.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1ee23ad..3de33c7 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,4 @@ sw.* # production database /spread-edge*.db +/tentacle-edge*.db diff --git a/src/index.js b/src/index.js index eb7b74d..3ac8130 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ #!/usr/bin/env node require('make-promises-safe') const { start } = require('./server') -start('spread-edge.db') +start('tentacle-edge.db')