I have this below eventing function. I am trying to bulk insert into hist and it is giving me timeout and having a doubt its adding more rows in insert because of messed for loop.
This is for one time execution only because we are doing only for existing old data.
// Eventing function to insert history with missing split refs
function OnUpdate(doc, meta) {
try {
var data =
SELECT d.id, h AS historyid FROM pure-ems-cms
d UNNEST d.history AS h
WHERE d.type=“page” AND ARRAY_LENGTH(d.history) > 0
AND IFMISSING(d.isStack, FALSE) != TRUE
AND IFMISSING(d.isUnschedul , FALSE) != TRUE
AND IFMISSING(d.sup, 0 ) = 0
AND d.id NOT IN (SELECT DISTINCT RAW k.iRef FROM pure-ems-history
k
WHERE k.type = “history” AND IFMISSING(K.details.type, FALSE) != TRUE
AND k.iRef IS NOT NULL);
// loop through the data resultset
for (var val of data) {
if (!val.historyid && !val.id) {
log(‘no records to insert into history’);
break;
}
var historyID = val.historyid;
var vID = val.id;
if (val.historyid = historyID) {
try {
// get data from history and insert a new document with key=uuid() and replace iRef from above query
// getting timeouts here and also have a feeling this is adding more records than intended
INSERT INTO `pure-ems-history` (KEY UUID(), VALUE OBJECT_PUT(docx, "iRef", $vID)) SELECT d AS docx FROM `pure-ems-history` AS d WHERE d.id = $historyID;
log('insert successful', vID);
} catch(e) { log(e, vID); }
}
} data.close();
}catch(e) { log(e);
}
}