Skip to content

Commit

Permalink
Merge pull request #40 from joyja/indexes
Browse files Browse the repository at this point in the history
Indexes
  • Loading branch information
joyja committed Apr 7, 2020
2 parents b0535ea + be8aad5 commit ca03b0c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ sw.*

# production database
/spread-edge*.db
/tentacle-edge*.db
11 changes: 9 additions & 2 deletions src/database/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
require('make-promises-safe')
const { start } = require('./server')
start('spread-edge.db')
start('tentacle-edge.db')
9 changes: 8 additions & 1 deletion src/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

// ==============================
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions src/service/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca03b0c

Please sign in to comment.