From 38c27e93161f328d18c4c6a010bd1cfad25fb1ff Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Sun, 22 Sep 2019 18:40:03 -0400 Subject: [PATCH] Successfully writing to sqlite database --- src/Models/db/{event.ts => iEvent.ts} | 0 ...ceInstance.ts => iEventServiceInstance.ts} | 0 ...> iEventServiceInstanceCharacteristics.ts} | 0 src/Models/db/iSequence.ts | 4 ++++ src/services/monitor.ts | 24 ++++++++++++------- 5 files changed, 19 insertions(+), 9 deletions(-) rename src/Models/db/{event.ts => iEvent.ts} (100%) rename src/Models/db/{eventServiceInstance.ts => iEventServiceInstance.ts} (100%) rename src/Models/db/{eventServiceInstanceCharacteristics.ts => iEventServiceInstanceCharacteristics.ts} (100%) create mode 100644 src/Models/db/iSequence.ts diff --git a/src/Models/db/event.ts b/src/Models/db/iEvent.ts similarity index 100% rename from src/Models/db/event.ts rename to src/Models/db/iEvent.ts diff --git a/src/Models/db/eventServiceInstance.ts b/src/Models/db/iEventServiceInstance.ts similarity index 100% rename from src/Models/db/eventServiceInstance.ts rename to src/Models/db/iEventServiceInstance.ts diff --git a/src/Models/db/eventServiceInstanceCharacteristics.ts b/src/Models/db/iEventServiceInstanceCharacteristics.ts similarity index 100% rename from src/Models/db/eventServiceInstanceCharacteristics.ts rename to src/Models/db/iEventServiceInstanceCharacteristics.ts diff --git a/src/Models/db/iSequence.ts b/src/Models/db/iSequence.ts new file mode 100644 index 0000000..64e21d5 --- /dev/null +++ b/src/Models/db/iSequence.ts @@ -0,0 +1,4 @@ +export interface ISequence { + name: string; + seq: number; +} \ No newline at end of file diff --git a/src/services/monitor.ts b/src/services/monitor.ts index c0221db..306d11c 100644 --- a/src/services/monitor.ts +++ b/src/services/monitor.ts @@ -7,9 +7,10 @@ import { Database } from "sqlite3"; import { mkdirSync } from 'fs'; import { join } from 'path'; import Knex from 'knex'; -import { IEvent } from "../Models/db/event"; -import { IEventServiceInstance } from "../Models/db/eventServiceInstance"; -import { IEventInstanceCharacteristics } from "../Models/db/eventServiceInstanceCharacteristics"; +import { IEvent } from "../Models/db/iEvent"; +import { IEventServiceInstance } from "../Models/db/iEventServiceInstance"; +import { IEventInstanceCharacteristics } from "../Models/db/iEventServiceInstanceCharacteristics"; +import { ISequence } from "../Models/db/iSequence"; @singleton() @@ -122,20 +123,25 @@ export default class Monitor { } private onServiceUpdated = async (updatedServices: Array): Promise => { - const eventId = this.knex && this.knex().into('events').insert({ + this.knex && await this.knex('events').insert({ timestamp: this.knex && this.knex.fn.now(), - }).returning('id'); + }); + const seq = this.knex && await this.knex('sqlite_sequence').where('name', 'events').first(); + const eventId = seq && seq.seq; + await Promise.all(updatedServices.map(async (service: IServiceType): Promise => { - const instanceId = this.knex && this.knex().into('event_service_instances').insert({ + this.knex && await this.knex('event_service_instances').insert({ eventId: eventId, serviceUniqueId: service.uniqueId - }).returning('id'); + }); + + const instanceSequence = this.knex && await this.knex('sqlite_sequence').where('name', 'event_service_instances').first(); + const instanceId = instanceSequence && instanceSequence.seq; await Promise.all(service.serviceCharacteristics.map(async (characteristic: ICharacteristicType): Promise => { - this.knex && await this.knex().into('event_instance_characteristics').insert({ + this.knex && await this.knex('event_instance_characteristics').insert({ instanceId: instanceId, - eventId: eventId, key: characteristic.serviceName, type: characteristic.type, value: characteristic.value