Lots of work trying to use the new core db_save_ae_obj_li__ae_obj() function.
This commit is contained in:
@@ -59,6 +59,11 @@ import {
|
||||
db_update_ae_obj_id__hosted_file
|
||||
} from "$lib/ae_core/core__hosted_files";
|
||||
|
||||
// This has more generic general purpose functions that can eventually replace the custom ones per object type.
|
||||
import {
|
||||
db_save_ae_obj_li__ae_obj,
|
||||
} from "$lib/ae_core/core__idb_dexie";
|
||||
|
||||
import {
|
||||
add_url_params,
|
||||
clean_headers,
|
||||
@@ -431,6 +436,7 @@ let export_obj = {
|
||||
delete_ae_obj_id__hosted_file: delete_ae_obj_id__hosted_file,
|
||||
db_save_ae_obj_li__hosted_file: db_save_ae_obj_li__hosted_file,
|
||||
db_update_ae_obj_id__hosted_file: db_update_ae_obj_id__hosted_file,
|
||||
db_save_ae_obj_li__ae_obj: db_save_ae_obj_li__ae_obj,
|
||||
add_url_params: add_url_params,
|
||||
clean_headers: clean_headers,
|
||||
handle_load_ae_obj_id__site_domain: handle_load_ae_obj_id__site_domain,
|
||||
|
||||
91
src/lib/ae_core/core__idb_dexie.ts
Normal file
91
src/lib/ae_core/core__idb_dexie.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
|
||||
// This function will save an array of objects to a Dexie database table.
|
||||
// It will first attempt to update existing records using bulkUpdate.
|
||||
// If that fails, it will fall back to bulkPut.
|
||||
// The function takes a database instance, table name, array of objects, and properties to save.
|
||||
// It also accepts a log level for debugging purposes.
|
||||
// Updated 2025-05-09
|
||||
export async function db_save_ae_obj_li__ae_obj({
|
||||
db_instance,
|
||||
table_name,
|
||||
obj_li,
|
||||
properties_to_save,
|
||||
log_lvl = 0,
|
||||
}: {
|
||||
db_instance: any; // The Dexie database instance
|
||||
table_name: string; // The name of the table in the database
|
||||
obj_li: any[];
|
||||
properties_to_save: string[];
|
||||
log_lvl?: number;
|
||||
}) {
|
||||
log_lvl = 1;
|
||||
if (log_lvl) {
|
||||
console.log(`*** db_save_ae_obj_li__ae_obj() *** table_name=${table_name}`, obj_li);
|
||||
}
|
||||
|
||||
if (!obj_li || obj_li.length === 0) {
|
||||
if (log_lvl) {
|
||||
console.log('No objects to save.');
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
const db_table = db_instance[table_name];
|
||||
if (!db_table) {
|
||||
console.error(`Table not found: ${table_name}`);
|
||||
return [];
|
||||
}
|
||||
|
||||
const bulkUpdateData = [];
|
||||
const bulkPutData = [];
|
||||
|
||||
for (const obj of obj_li) {
|
||||
const obj_record: Record<string, any> = {};
|
||||
|
||||
// Extract only the specified properties to save
|
||||
for (const prop of properties_to_save) {
|
||||
obj_record[prop] = obj[prop];
|
||||
}
|
||||
|
||||
// Ensure the `id` field is included
|
||||
obj_record.id = obj_record.id || obj.id || obj[`${table_name}_id`] || obj[`${table_name}_id_random`];
|
||||
|
||||
if (!obj_record.id) {
|
||||
console.error(`Object is missing an ID:`, obj);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Prepare data for bulkUpdate or bulkPut
|
||||
bulkUpdateData.push({
|
||||
key: obj_record.id,
|
||||
changes: obj_record,
|
||||
});
|
||||
bulkPutData.push(obj_record);
|
||||
}
|
||||
|
||||
// Attempt bulkUpdate first
|
||||
try {
|
||||
const updatedKeys = await db_table.bulkUpdate(bulkUpdateData);
|
||||
if (log_lvl) {
|
||||
console.log(`Bulk update completed. Updated keys:`, updatedKeys);
|
||||
}
|
||||
} catch (error) {
|
||||
// This is fairly common and normal if the object is new
|
||||
if (log_lvl) {
|
||||
console.error(`Bulk update failed. Falling back to bulkPut.`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Use bulkPut for any records that couldn't be updated
|
||||
try {
|
||||
const putKeys = await db_table.bulkPut(bulkPutData);
|
||||
if (log_lvl) {
|
||||
console.log(`Bulk put completed. Put keys:`, putKeys);
|
||||
}
|
||||
return putKeys;
|
||||
} catch (error) {
|
||||
// This should not happen if the object is new
|
||||
console.error(`Bulk put failed.`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user