import type { key_val } from '$lib/ae_stores'; import { api } from '$lib/api'; import { db_save_ae_obj_li__ae_obj } from "$lib/ae_core/core__idb_dexie"; import { db_archives } from "$lib/ae_archives/db_archives"; let ae_promises: key_val = {}; // TESTING NOTE: I changed these to all use async and await. Not sure if this helps or hurts things. Mainly this is related to the Dexie DB changes. 2024-11-08 // Updated 2024-09-25 export async function load_ae_obj_id__archive_content( { api_cfg, archive_content_id, try_cache = true, log_lvl = 0 }: { api_cfg: any, archive_content_id: string, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** load_ae_obj_id__archive_content() *** archive_content_id=${archive_content_id}`); } let params = {}; ae_promises.load__archive_content_obj = await api.get_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'archive_content', obj_id: archive_content_id, // NOTE: This is the FQDN, not normally the ID. use_alt_table: false, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config. use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value params: params, log_lvl: log_lvl }) .then(async function (archive_content_obj_get_result) { if (archive_content_obj_get_result) { if (try_cache) { // Process the results first let processed_obj_li = await process_ae_obj__archive_content_props({ obj_li: [archive_content_obj_get_result], log_lvl: log_lvl, }); if (log_lvl) { console.log('Processed object list:', processed_obj_li); } // Save the updated results list to the database if (log_lvl) { console.log('Saving to DB...'); } await db_save_ae_obj_li__ae_obj({ db_instance: db_archives, table_name: 'content', obj_li: processed_obj_li, properties_to_save: properties_to_save, log_lvl: log_lvl, }); if (log_lvl) { console.log('DB save completed.'); } // // This is expecting a list // await db_save_ae_obj_li__archive_content({ // obj_type: 'archive_content', // obj_li: [archive_content_obj_get_result] // }); } return archive_content_obj_get_result; } else { console.log('No results returned.'); return null; } }) .catch(function (error) { console.log('No results returned or failed.', error); }); return ae_promises.load__archive_content_obj; } // Updated 2024-11-20 export async function load_ae_obj_li__archive_content( { api_cfg, for_obj_type = 'archive', for_obj_id, enabled = 'enabled', hidden = 'not_hidden', limit = 99, offset = 0, order_by_li = {'priority': 'DESC', 'sort': 'DESC', 'original_datetime': 'ASC', 'name': 'ASC', 'updated_on': 'DESC', 'created_on': 'DESC'}, params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, for_obj_type: string, for_obj_id: string, enabled?: string, hidden?: string, limit?: number, offset?: number, order_by_li?: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** load_ae_obj_li__archive_content() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`); } // let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled // let hidden: string = (params.qry__hidden ?? 'not_hidden'); // all, hidden, not_hidden // let limit: number = (params.qry__limit ?? 99); // 99 // let offset: number = (params.qry__offset ?? 0); // 0 let params_json: key_val = {}; // console('params_json:', params_json); ae_promises.load__archive_content_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({ api_cfg: api_cfg, obj_type: 'archive_content', for_obj_type: for_obj_type, for_obj_id: for_obj_id, use_alt_tbl: false, use_alt_mdl: false, use_alt_exp: false, enabled: enabled, hidden: hidden, order_by_li: order_by_li, limit: limit, offset: offset, params_json: params_json, params: params, log_lvl: log_lvl }) .then(async function (archive_content_obj_li_get_result) { if (archive_content_obj_li_get_result) { if (try_cache) { // Process the results first let processed_obj_li = await process_ae_obj__archive_content_props({ obj_li: archive_content_obj_li_get_result, log_lvl: log_lvl, }); if (log_lvl) { console.log('Processed object list:', processed_obj_li); } // Save the updated results list to the database if (log_lvl) { console.log('Saving to DB...'); } await db_save_ae_obj_li__ae_obj({ db_instance: db_archives, table_name: 'content', obj_li: processed_obj_li, properties_to_save: properties_to_save, log_lvl: log_lvl, }); if (log_lvl) { console.log('DB save completed.'); } // await db_save_ae_obj_li__archive_content({ // obj_type: 'archive_content', obj_li: archive_content_obj_li_get_result // }); } return archive_content_obj_li_get_result; } else { return []; } }) .catch(function (error) { console.log('No results returned or failed.', error); }); if (log_lvl) { console.log('ae_promises.load__archive_content_obj_li:', ae_promises.load__archive_content_obj_li); } return ae_promises.load__archive_content_obj_li; } // Updated 2024-09-25 export async function create_ae_obj__archive_content( { api_cfg, archive_id, data_kv, params={}, try_cache = true, log_lvl = 0 }: { api_cfg: any, archive_id: string, data_kv: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** create_ae_obj__archive_content() *** archive_id=${archive_id}`); } ae_promises.create__archive_content = await api.create_ae_obj_crud({ api_cfg: api_cfg, obj_type: 'archive_content', fields: { archive_id_random: archive_id, ...data_kv }, key: api_cfg.api_crud_super_key, params: params, return_obj: true, log_lvl: log_lvl }) .then(async function (archive_content_obj_create_result) { if (archive_content_obj_create_result) { if (try_cache) { await db_save_ae_obj_li__archive_content( { obj_type: 'archive_content', obj_li: [archive_content_obj_create_result] }); } return archive_content_obj_create_result; } else { return null; } }) .catch(function (error) { console.log('No results returned or failed.', error); }); if (log_lvl) { console.log('ae_promises.create__archive_content:', ae_promises.create__archive_content); } return ae_promises.create__archive_content; } // Updated 2024-11-08 export async function delete_ae_obj_id__archive_content( { api_cfg, archive_content_id, method = 'delete', // 'delete', 'disable', 'hide' params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, archive_content_id: string, method?: string, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** delete_ae_obj_id__archive_content() *** archive_content_id=${archive_content_id}`); } ae_promises.delete__archive_content_obj = await api.delete_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'archive_content', obj_id: archive_content_id, key: api_cfg.api_crud_super_key, params: params, method: method, log_lvl: log_lvl }) .catch(function (error) { console.log('No results returned or failed.', error); }) .finally(function () { if (try_cache) { if (log_lvl) { console.log(`Attempting to remove IDB entry for archive_content_id=${archive_content_id}`); } db_archives.content.delete(archive_content_id); // Delete from the DB no matter what. } }); if (log_lvl) { console.log('ae_promises.delete__archive_content_obj:', ae_promises.delete__archive_content_obj); } return ae_promises.delete__archive_content_obj; } // Updated 2024-09-25 export async function update_ae_obj__archive_content( { api_cfg, archive_content_id, data_kv, params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, archive_content_id: string, data_kv: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** update_ae_obj__archive_content() *** archive_content_id=${archive_content_id}`, data_kv); } ae_promises.update__archive_content_obj = await api.update_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'archive_content', obj_id: archive_content_id, fields: data_kv, key: api_cfg.api_crud_super_key, params: params, return_obj: true, log_lvl: log_lvl }) .then(async function (archive_content_obj_update_result) { if (archive_content_obj_update_result) { if (try_cache) { await db_save_ae_obj_li__archive_content({ obj_type: 'archive_content', obj_li: [archive_content_obj_update_result] }); } return archive_content_obj_update_result; } else { return null; } }) .catch(function (error) { console.log('No results returned or failed.', error); }) .finally(function () { }); if (log_lvl) { console.log('ae_promises.update__archive_content_obj:', ae_promises.update__archive_content_obj); } return ae_promises.update__archive_content_obj; } // This function will loop through the archive_content_obj_li and save each one to the DB. // Updated 2024-09-25 export async function db_save_ae_obj_li__archive_content( { obj_type, obj_li, log_lvl = 0 }: { obj_type: string, obj_li: any, log_lvl?: number } ) { if (log_lvl) { console.log(`*** db_save_ae_obj_li__archive_content() ***`); } if (obj_li && obj_li.length) { obj_li.forEach(async function (obj: any) { if (log_lvl) { console.log(`ae_obj ${obj_type}:`, obj); } try { const id_random = await db_archives.content.put({ id: obj.archive_content_id_random, archive_content_id: obj.archive_content_id_random, archive_id: obj.archive_id_random, archive_content_type: obj.archive_content_type, name: obj.name, description: obj.description, content_html: obj.content_html, content_json: obj.content_json, url: obj.url, url_text: obj.url_text, hosted_file_id: obj.hosted_file_id_random, file_path: obj.file_path, filename: obj.filename, file_extension: obj.file_extension, original_datetime: obj.original_datetime, original_timezone: obj.original_timezone, original_location: obj.original_location, original_url: obj.original_url, original_url_text: obj.original_url_text, enable_for_public: obj.enable_for_public, cfg_json: obj.cfg_json, enable: obj.enable, hide: obj.hide, priority: obj.priority, sort: obj.sort, group: obj.group, notes: obj.notes, created_on: obj.created_on, updated_on: obj.updated_on, // Generated fields for sorting locally only tmp_sort_1: `${obj.original_datetime}_${obj.group}_${obj.priority}_${obj.sort}`, tmp_sort_2: `${obj.group}_${obj.original_datetime}_${obj.priority}_${obj.sort}`, // From SQL view archive_code: obj.archive_code, archive_name: obj.archive_name, hash_sha256: obj.hosted_file_hash_sha256 }); // console.log(`Put obj with ID: ${obj.archive_content_id_random} or ${id_random}`); } catch (error) { let status = `Failed to put ${obj.archive_content_id_random}: ${error}`; console.log(status); } // const id_random = await db_archives.content.put(obj); // console.log(`Put obj with ID: ${obj.archive_content_id_random}`); }); return true; } } // Updated 2025-06-04 export const properties_to_save = [ 'id', 'archive_content_id', // 'archive_content_id_random', 'archive_id', // 'archive_id_random', 'archive_content_type', 'name', 'description', 'content_html', 'content_json', 'url', 'url_text', 'hosted_file_id', 'hosted_file_id_random', 'file_path', 'filename', 'file_extension', 'original_datetime', 'original_timezone', 'original_location', 'original_url', 'original_url_text', 'enable_for_public', 'cfg_json', 'enable', 'hide', 'priority', 'sort', 'group', 'notes', 'created_on', 'updated_on', // Generated fields for sorting locally only 'tmp_sort_1', 'tmp_sort_2', // 'tmp_sort_a', // 'tmp_sort_b', // From SQL view 'archive_code', 'archive_name', 'hash_sha256' ]; // Updated 2025-06-04 export async function process_ae_obj__archive_content_props({ obj_li, log_lvl = 0, }: { obj_li: any[]; log_lvl?: number; }) { if (log_lvl) { console.log(`*** process_ae_obj__archive_content_props() ***`, obj_li); } if (!obj_li || obj_li.length === 0) { if (log_lvl) console.log('No objects to process.'); return []; } const processed_obj_li = []; for (const obj of obj_li) { if (log_lvl) console.log(`Processing ae_obj archive_content:`, obj); let processed_obj = { id: obj.archive_content_id_random, archive_content_id: obj.archive_content_id_random, // archive_content_id_random: obj.archive_content_id_random, archive_id: obj.archive_id_random, // archive_id_random: obj.archive_id_random, archive_content_type: obj.archive_content_type, name: obj.name, description: obj.description, content_html: obj.content_html, content_json: obj.content_json, url: obj.url, url_text: obj.url_text, hosted_file_id: obj.hosted_file_id_random, hosted_file_id_random: obj.hosted_file_id_random, file_path: obj.file_path, filename: obj.filename, file_extension: obj.file_extension, original_datetime: obj.original_datetime, original_timezone: obj.original_timezone, original_location: obj.original_location, original_url: obj.original_url, original_url_text: obj.original_url_text, enable_for_public: obj.enable_for_public, cfg_json: obj.cfg_json, enable: obj.enable, hide: obj.hide, priority: obj.priority, sort: obj.sort, group: obj.group, notes: obj.notes, created_on: obj.created_on, updated_on: obj.updated_on, // Generated fields for sorting locally only tmp_sort_1: `${obj.original_datetime}_${obj.group}_${obj.priority}_${obj.sort}`, tmp_sort_2: `${obj.group}_${obj.original_datetime}_${obj.priority}_${obj.sort}`, // tmp_sort_a: `${obj.group}_${obj.priority}_${obj.sort}_${obj.updated_on ?? obj.created_on}`, // tmp_sort_b: `${obj.group}_${obj.priority}_${obj.sort}_${obj.updated_on}_${obj.created_on}`, // From SQL view archive_code: obj.archive_code, archive_name: obj.archive_name, hash_sha256: obj.hosted_file_hash_sha256 }; processed_obj_li.push(processed_obj); } return processed_obj_li; }