Successfully writing to sqlite database
This commit is contained in:
		
							
								
								
									
										4
									
								
								src/Models/db/iSequence.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/Models/db/iSequence.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
export interface ISequence {
 | 
			
		||||
    name: string;
 | 
			
		||||
    seq: number;
 | 
			
		||||
}
 | 
			
		||||
@@ -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<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(),
 | 
			
		||||
        }).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> => {
 | 
			
		||||
            const instanceId = this.knex && this.knex().into('event_service_instances').insert<IEventServiceInstance>({
 | 
			
		||||
            this.knex && await this.knex('event_service_instances').insert<IEventServiceInstance>({
 | 
			
		||||
                eventId: eventId,
 | 
			
		||||
                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> => {
 | 
			
		||||
                this.knex && await this.knex().into('event_instance_characteristics').insert<IEventInstanceCharacteristics>({
 | 
			
		||||
                this.knex && await this.knex('event_instance_characteristics').insert<IEventInstanceCharacteristics>({
 | 
			
		||||
                    instanceId: instanceId,
 | 
			
		||||
                    eventId: eventId,
 | 
			
		||||
                    key: characteristic.serviceName,
 | 
			
		||||
                    type: characteristic.type,
 | 
			
		||||
                    value: characteristic.value
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user