A working Data Store element/component again!!! Saving!

This commit is contained in:
Scott Idem
2026-01-29 11:51:12 -05:00
parent 6a132af2ae
commit ab3aa52e07
3 changed files with 161 additions and 122 deletions

View File

@@ -4,12 +4,12 @@ import { api } from '$lib/api/api';
import type { ae_DataStore } from '$lib/types/ae_types';
/**
* Fetches a data_store object by its unique code.
* Fetches a data_store object by its unique code (V3).
*
* @param api_cfg - The API configuration object.
* @param code - The code of the data store to fetch.
* @param data_type - The expected data type ('text', 'json', 'html', 'md', 'sql').
* @param save_idb - Whether to save the fetched data to localStorage.
* @param save_idb - Whether to save the fetched data to IndexedDB (Dexie).
* @param timeout - The request timeout in milliseconds.
* @param log_lvl - The logging level.
* @returns The data from the data store (e.g., text content or JSON object).
@@ -34,60 +34,52 @@ export async function load_ae_obj_by_code__data_store({
}
if (!code) {
console.log(`*ae_func* No code provided!`);
return null;
}
if (!api_cfg.account_id) {
console.log(`*ae_func* No account_id found in API config!`);
if (log_lvl) console.log(`*ae_func* No code provided!`);
return null;
}
try {
const get_ds_result = await api.get_data_store_obj_w_code({
api_cfg: api_cfg,
data_store_code: code,
data_type: data_type,
timeout: timeout,
log_lvl: log_lvl
const get_ds_result = await api.get_data_store_v3({
api_cfg,
code,
log_lvl
});
if (!get_ds_result) {
console.log('*ae_func* No results returned.');
if (log_lvl) console.log('*ae_func* No results returned.');
return null;
}
if (log_lvl) {
console.log(`*ae_func* Got a result for code ${code}`);
}
const ds_id = get_ds_result.data_store_id_random || get_ds_result.id_random;
if (!get_ds_result.data_store_id_random) {
console.log('*ae_func* Something went wrong? No data store ID found.');
if (!ds_id) {
if (log_lvl) console.log('*ae_func* Something went wrong? No data store ID found.');
return null;
}
let return_this: any = null;
// Simplified data extraction
if (data_type === 'html') {
return_this = get_ds_result.html;
} else if (data_type === 'json') {
return_this = get_ds_result.json;
} else {
return_this = get_ds_result.text;
}
// Map content fields for convenience
const text_val = get_ds_result.text || '';
const json_val = get_ds_result.json || (get_ds_result.json_str ? JSON.parse(get_ds_result.json_str) : null);
const mapped_ds: ae_DataStore = {
...get_ds_result,
id: ds_id,
text: text_val,
html: text_val, // Map text to html by default
json: json_val
};
if (save_idb && browser) {
const key_prefix = 'ae_ds__';
if (log_lvl) {
console.log(`*ae_func* localStorage key: ${code}, value:`, get_ds_result);
}
localStorage.setItem(`${key_prefix}${code}`, JSON.stringify(get_ds_result));
const { db_core } = await import('./db_core');
await db_core.data_store.put(mapped_ds);
}
return return_this;
if (data_type === 'html') return mapped_ds.html;
if (data_type === 'json') return mapped_ds.json;
return mapped_ds.text;
} catch (error) {
console.log('*ae_func* No results returned or failed.', error);
if (log_lvl) console.error('*ae_func* Fetch failed.', error);
return null;
}
}