Used to perform data cleaning through merging duplicates to a single record. This is done from the crud page where the currently checked records can be merged.
xxxxxxxxxx
public async merge():Promise<void>{
Collect the data to merge:-
De-structure the subject to reveal the database and reference table.
xxxxxxxxxx
const [ename, dbname] = this.subject;
Collect the primary keys of checked records.
xxxxxxxxxx
const nodelist = this.document.querySelectorAll(
"input[name='multi_select']:checked"
);
Convert the node-list keys to an array of key values.
xxxxxxxxxx
const keys = (Array.from(nodelist)).map(
element=>(<HTMLInputElement>element).value
);
For the merging, there must be at least two selected keys.
xxxxxxxxxx
if (keys.length<2){
alert(`There must be at least 2 records to merge. Found ${keys.length}`);
return;
}
To retrieve the members to merge, formulate an SQL query from the checked records. And convert the names to backtick enclosed strings.
xxxxxxxxxx
const dbname_str = "`" + dbname + "`";
const ename_str = "`" + ename + "`";
//
const members =
`select
${dbname_str}.${ename_str}.${ename_str} as member
from
${dbname_str}.${ename_str}
where
${dbname_str}.${ename_str}.${ename_str} in (${keys.join(', ')})`;
Compile the imerger structure.
xxxxxxxxxx
const imerger = {ename, dbname, members};
Create a new object keeping in mind the merger class extends a baby.
xxxxxxxxxx
const Merger = new merger(imerger, this)
Wait for the merging process to finish. The result is the primary key of the principal(main) that represents the merged members.
xxxxxxxxxx
const principal:number|undefined = await Merger.administer();
Use the principal to update the page and highlight the record if need be, wait for the page to refresh , then get the table-row that matches the principal and select it.
xxxxxxxxxx
if (principal!==undefined){
await this.review();
}
}