I have an API that updates a database. I want to query the database and delete a row if it no longer exists in the API.
THE GOAL HERE IS TO COMPARE THE DATABASE TO THE API INFO AND DELETE THE ROW THAT IS NO LONGER IN THE API.
FETCH IS ABOVE THIS.
.then( (data) => {
let bulk = data['listings'].listing;
return wixData.query('Inventory')
.find()
.then((results) => {
let all = results.items;
NEED HELP HERE TO COMPARE THE DATABASE TO THE API
})
.catch((err) => {
let errorMsg = err;
});
})
.catch( (err) => {
console.log(err);
});
}
LISTING in data[‘listings’].listing is an array that is inserted to the database from the API.
This is the array:
The Inventory array is located in let all = results.items;. It returns an array that is “27” items long. One of them is no longer in the API and I want to delete it from the database.
How do I compare the two arrays and delete the entry that is no longer in the API?
Since this is my first Velo project, you’ll probably laugh at how I put this together. But I have a function bulkinsert() the listings and it’s set on a job to update every hour.
export async function getInventory() {
const url = 'APIURL';
const key = await getSecret('apiKey');
let fullUrl = url + key;
return fetch(fullUrl, {method: 'get'})
.then( (httpResponse) => {
let url = httpResponse.url;
let statusCode = httpResponse.status;
let statusText = httpResponse.statusText;
let headers = httpResponse.headers;
let bodyUsed = httpResponse.bodyUsed;
if (httpResponse.ok) {
return httpResponse.json();
}
else {
return Promise.reject("Fetch did not succeed");
}
} )
.then( (data) => {
let options = {
"suppressAuth": true,
};
wixData.bulkInsert("Inventory", data['listings'].listing, options)
.then((record) => {
console.log(`record inserted: ${record}`);
}).catch((err) => {
console.log(err, "see error if there is here")
});
})
.catch( (err) => {
console.log(err);
});
}
I was creating this as a second function and I was going to call it in jobs.config after bulkInsert().
Do you think it would be more efficient to truncate the database and refill it every hour? I have a beforeInsert() hook that avoids database duplicates and it updates great. Just need it to remove any items from inventory that were sold.
.then((data) => {
let options = {"suppressAuth": true, };
wixData.bulkInsert("Inventory", data['listings'].listing, options)
.then((record) => {
console.log(`record inserted: ${record}`);
}).catch((err) => {
console.log(err, "see error if there is here")
});
})
I am right when i say you want to refresh DATABASE (let it be hourly getting the data somewhere from the net ?).
scenario-(A)
If your FETCHED-DATA → is a complete DATA-BLOCK
you can first delete your current data inside your Wix-DATABASE (TRUNCATE) and replace it with the new fetched (update/refreshed) DATA-BLOCK.
scenario-(B)
If your FETCHED-DATA → is a PART-DATA-BLOCK which has to be inserted/implemented/updated in your FULL-DATA-BLOCK inside your Wix-DATABASE, than you won’t be able to truncate first and refill, because you would loose DATA.
So if it is scenario-(A) → then use TRUNCATE first and just refill DATABASE with new found (fetched) DATA.
And if it is scenario-(B), you will have to go the way of your own idea.
Comparing your FULL-DATA-BLOCK with the FETCHED-PART-DATA-BLOCK, searching for the changed data-row inside.
No, they’re populating two databases and updating them without removing. (If I read through them correctly… I could be illiterate). I created this new function to check the API again, and queried the Inventory, and I’m stuck at the crossroad where I have both of the arrays… But aren’t sure how to compare and remove the entry.
I actually got the API to compare the Inventory… But I need to do the opposite. I need to compare the Inventory to the API.
All of the API comparisons came back “True”, and if I could do the opposite I would get one “False” which I believe I could remove that _id.