Successfully writing to sqlite database

This commit is contained in:
watsonb8 2019-09-22 18:40:03 -04:00
parent acf8889874
commit 38c27e9316
5 changed files with 19 additions and 9 deletions

View File

@ -0,0 +1,4 @@
export interface ISequence {
name: string;
seq: number;
}

View File

@ -7,9 +7,10 @@ import { Database } from "sqlite3";
import { mkdirSync } from 'fs'; import { mkdirSync } from 'fs';
import { join } from 'path'; import { join } from 'path';
import Knex from 'knex'; import Knex from 'knex';
import { IEvent } from "../Models/db/event"; import { IEvent } from "../Models/db/iEvent";
import { IEventServiceInstance } from "../Models/db/eventServiceInstance"; import { IEventServiceInstance } from "../Models/db/iEventServiceInstance";
import { IEventInstanceCharacteristics } from "../Models/db/eventServiceInstanceCharacteristics"; import { IEventInstanceCharacteristics } from "../Models/db/iEventServiceInstanceCharacteristics";
import { ISequence } from "../Models/db/iSequence";
@singleton() @singleton()
@ -122,20 +123,25 @@ export default class Monitor {
} }
private onServiceUpdated = async (updatedServices: Array<IServiceType>): Promise<void> => { private onServiceUpdated = async (updatedServices: Array<IServiceType>): Promise<void> => {
const eventId = this.knex && this.knex().into('events').insert<IEvent>({ this.knex && await this.knex('events').insert<IEvent>({
timestamp: this.knex && this.knex.fn.now(), timestamp: this.knex && this.knex.fn.now(),
}).returning('id'); });
const seq = this.knex && await this.knex<ISequence>('sqlite_sequence').where('name', 'events').first();
const eventId = seq && seq.seq;
await Promise.all(updatedServices.map(async (service: IServiceType): Promise<void> => { await Promise.all(updatedServices.map(async (service: IServiceType): Promise<void> => {
const instanceId = this.knex && this.knex().into('event_service_instances').insert<IEventServiceInstance>({ this.knex && await this.knex('event_service_instances').insert<IEventServiceInstance>({
eventId: eventId, eventId: eventId,
serviceUniqueId: service.uniqueId serviceUniqueId: service.uniqueId
}).returning('id'); });
const instanceSequence = this.knex && await this.knex<ISequence>('sqlite_sequence').where('name', 'event_service_instances').first();
const instanceId = instanceSequence && instanceSequence.seq;
await Promise.all(service.serviceCharacteristics.map(async (characteristic: ICharacteristicType): Promise<void> => { await Promise.all(service.serviceCharacteristics.map(async (characteristic: ICharacteristicType): Promise<void> => {
this.knex && await this.knex().into('event_instance_characteristics').insert<IEventInstanceCharacteristics>({ this.knex && await this.knex('event_instance_characteristics').insert<IEventInstanceCharacteristics>({
instanceId: instanceId, instanceId: instanceId,
eventId: eventId,
key: characteristic.serviceName, key: characteristic.serviceName,
type: characteristic.type, type: characteristic.type,
value: characteristic.value value: characteristic.value