1

I have two object/model, log and logTemp that I manipulate on the code side and on the database side in order to store http request logs in a temporary way and then keep only the logs that interest me (error log for example).

So I get the records from database as an array of objects. These objects are logTemp. I want to modify each logTemp to remove some properties of it, add a new one and create new object from it all which is log

How can I use a logTemp array, modify each LogTemp in it and create new logs to save to the database?

First how can I achieve what I want in TypeScript? with logTemp : ILogTemp and **log :ILog **?

Second: What is the best way for performance?

Thanks and regards

Here are the typescript interfaces of these two objects to be manipulated.

interface ILogTemp {
    id: string;
    parentId?: string;
    logTempId?: string;
    service?: string;
    event?: string;
    type?: string;
    url?: string;
    query?: string;
    urlParams?: string;
    method?: string;
    db?: string;
    headers?: Object;
    body?: Object;
    status?: string;
    code?: number;
    internalCode?: number;
    response?: Object;
    tryCount?: number;
    partnerPrefix?: string;
    serviceConfigId?: number;
    createdDate?: Date;
    updatedDate?: Date;
    debugInfo?: Object;
    serviceRequestId?: number;
    tracking?: string;
}

interface ILog extends Omit<ILogTemp, 'tracking' | 'id'> {
    id: string;
    logTempId: string | undefined;
}

Here's how I do it without types

static async processTemporaryLogs() {
        try {
            // SELECT LAST LOGS
            const tracking = 'to_process';
            const temporaryLogs = await Log.LogTemp.getLogsByTracking(tracking);

            for (let i = 0; i <= temporaryLogs; temporaryLogs.length) {
                const logTemp = temporaryLogs[i];
                const id = logTemp.id;
                delete logTemp.tracking;
                delete logTemp.id;
                
                let log: ILog = structuredClone(logTemp);

                // // COPY LOG TO LOG TABLE
                await Log.Log.addLog(log);
                // UPDATE LOGTEMP TRACKING STATUS
                await Log.LogTemp.updateTrackingStatus('processed', log.id);
            }
        } catch (err: any) {
            console.error(err);
        }
    }

I know I can do a foreach, a map, a for... of or any other loop but one thing is I have to use asynchronous, and the other thing is I don't know which is the better way to do it for performance

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • I think a map in Promise.all will be more efficient here - https://stackoverflow.com/a/53799939/904956 bu depends on your needs. – Wesley LeMahieu Feb 14 '23 at 03:45

0 Answers0