More updates related to replacing the forEach loop with the for loop. Async... await...

This commit is contained in:
Scott Idem
2025-05-09 14:32:45 -04:00
parent dc7da8c930
commit 5ef6d7dc0c
4 changed files with 242 additions and 180 deletions

View File

@@ -519,21 +519,19 @@ export async function db_save_ae_obj_li__event(
} }
) { ) {
if (log_lvl) { if (log_lvl) {
console.log(`*** db_save_ae_obj_li__event() ***`); console.log(`*** db_save_ae_obj_li__event() *** obj_type=${obj_type}`, obj_li);
} }
if (obj_li && obj_li.length) { if (obj_li && obj_li.length) {
// let obj_li_id: string[] = []; let obj_li_id: string[] = [];
for (const obj of obj_li) { for (const obj of obj_li) {
// obj_li.forEach(async function (obj: any) {
if (log_lvl) { if (log_lvl) {
console.log(`ae_obj ${obj_type}:`, obj); console.log(`Processing ae_obj ${obj_type}:`, obj);
} }
try { let obj_record = {
const id_random = await db_events.events.put({
id: obj.event_id_random, id: obj.event_id_random,
// id_random: obj.event_id_random,
event_id: obj.event_id_random, event_id: obj.event_id_random,
event_id_random: obj.event_id_random, event_id_random: obj.event_id_random,
@@ -613,20 +611,41 @@ export async function db_save_ae_obj_li__event(
file_count_all: obj.file_count_all, file_count_all: obj.file_count_all,
internal_use_count: obj.internal_use_count, internal_use_count: obj.internal_use_count,
event_file_id_li_json: obj.event_file_id_li_json, event_file_id_li_json: obj.event_file_id_li_json,
}); };
// console.log(`Put obj with ID: ${obj.event_id_random} or ${id_random}`);
// obj_li_id.push(obj_record.id); let id_random = null;
try {
id_random = await db_events.events.update(obj_record.id, obj_record);
} catch (error) { } catch (error) {
let status = `Failed to put ${obj.event_id_random}: ${error}`; console.error(`Error: Failed to update ${obj_record.id}: ${error}`);
console.log(status);
} }
// const id_random = await db_events.events.put(obj); if (!id_random) {
// console.log(`Put obj with ID: ${obj.event_id_random}`); if (log_lvl) {
console.log(`Failed to update record with ID: ${obj_record.id}. Trying put...`);
}
try {
id_random = await db_events.events.put(obj_record);
} catch (error) {
console.error(`Error: Failed to put ${obj.event_id_random}: ${error}`);
}
} else {
if (log_lvl) {
console.log(`Updated record with ID: ${obj_record.id}`);
}
} }
return true; if (!id_random) {
console.error(`Failed to save record with ID: ${obj_record.id}`);
} else {
if (log_lvl) {
console.log(`Saved record with ID: ${obj_record.id}`);
}
obj_li_id.push(obj_record.id);
}
}
return obj_li_id;
} }
} }

View File

@@ -778,7 +778,8 @@ export async function search__event_session(
// This function will loop through the event_session_obj_li and save each one to the DB. // This function will loop through the event_session_obj_li and save each one to the DB.
export function db_save_ae_obj_li__event_session( // Updated 2025-05-09
export async function db_save_ae_obj_li__event_session(
{ {
obj_type, obj_type,
obj_li, obj_li,
@@ -790,17 +791,18 @@ export function db_save_ae_obj_li__event_session(
} }
) { ) {
if (log_lvl) { if (log_lvl) {
console.log(`*** db_save_ae_obj_li__event_session() ***`); console.log(`*** db_save_ae_obj_li__event_session() *** obj_type=${obj_type}`, obj_li);
} }
if (obj_li && obj_li.length) { if (obj_li && obj_li.length) {
obj_li.forEach(async function (obj: any) { let obj_li_id: string[] = [];
for (const obj of obj_li) {
if (log_lvl) { if (log_lvl) {
console.log(`ae_obj ${obj_type}:`, obj); console.log(`Processing ae_obj ${obj_type}:`, obj);
} }
try { let obj_record = {
const id_random = await db_events.sessions.put({
id: obj.event_session_id_random, id: obj.event_session_id_random,
event_session_id: obj.event_session_id_random, event_session_id: obj.event_session_id_random,
event_session_id_random: obj.event_session_id_random, event_session_id_random: obj.event_session_id_random,
@@ -870,18 +872,47 @@ export function db_save_ae_obj_li__event_session(
// A key value list of the presentations // A key value list of the presentations
event_presentation_kv: obj.event_presentation_kv, event_presentation_kv: obj.event_presentation_kv,
event_presentation_li: obj.event_presentation_li, event_presentation_li: obj.event_presentation_li,
}); };
// console.log(`Put obj with ID: ${obj.event_session_id_random} or ${id_random}`);
let id_random = null;
try {
id_random = await db_events.event_sessions.update(obj_record.id, obj_record);
} catch (error) { } catch (error) {
let status = `Failed to put ${obj.event_session_id_random}: ${error}`; console.error(`Error: Failed to update ${obj_record.id}: ${error}`);
console.log(status);
} }
// const id_random = await db_events.sessions.put(obj); if (!id_random) {
// console.log(`Put obj with ID: ${obj.event_session_id_random}`); if (log_lvl) {
}); console.log(`Failed to update record with ID: ${obj_record.id}. Trying put...`);
}
try {
id_random = await db_events.event_sessions.put(obj_record);
} catch (error) {
console.error(`Error: Failed to put ${obj.event_session_id_random}: ${error}`);
}
} else {
if (log_lvl) {
console.log(`Updated record with ID: ${obj_record.id}`);
}
}
return true; if (!id_random) {
console.error(`Failed to save record with ID: ${obj_record.id}`);
} else {
if (log_lvl) {
console.log(`Saved record with ID: ${obj_record.id}`);
}
obj_li_id.push(obj_record.id);
}
}
return obj_li_id;
} else {
if (log_lvl) {
console.log('No objects to save.');
}
return [];
} }
} }

View File

@@ -544,10 +544,11 @@ export async function db_save_ae_obj_li__journal(
if (obj_li && obj_li.length) { if (obj_li && obj_li.length) {
let obj_li_id: string[] = []; let obj_li_id: string[] = [];
for (const obj of obj_li) { for (const obj of obj_li) {
// obj_li.forEach(async function (obj: any) { // obj_li.forEach(async function (obj: any) {
if (log_lvl) { if (log_lvl) {
console.log(`ae_obj ${obj_type}:`, obj); console.log(`Processing ae_obj ${obj_type}:`, obj);
} }
let description = obj.description ?? ''; let description = obj.description ?? '';
@@ -662,5 +663,10 @@ export async function db_save_ae_obj_li__journal(
} }
return obj_li_id; return obj_li_id;
} else {
if (log_lvl) {
console.log('No objects to save.');
}
return [];
} }
} }

View File

@@ -397,10 +397,11 @@ export async function db_save_ae_obj_li__journal_entry(
if (obj_li && obj_li.length) { if (obj_li && obj_li.length) {
// let obj_li_id = obj_li.map((obj: any) => obj.journal_entry_id_random); // let obj_li_id = obj_li.map((obj: any) => obj.journal_entry_id_random);
let obj_li_id: string[] = []; let obj_li_id: string[] = [];
for (const obj of obj_li) { for (const obj of obj_li) {
// obj_li.forEach(async function (obj: any) { // obj_li.forEach(async function (obj: any) {
if (log_lvl) { if (log_lvl) {
console.log(`ae_obj ${obj_type}:`, obj); console.log(`Processing ae_obj ${obj_type}:`, obj);
} }
let content = obj.content ?? ''; let content = obj.content ?? '';
@@ -577,5 +578,10 @@ export async function db_save_ae_obj_li__journal_entry(
} }
return obj_li_id; return obj_li_id;
} else {
if (log_lvl) {
console.log('No objects to save.');
}
return [];
} }
} }