Got a lot of data that you want to add to a collection ? Try using the bulkInsert function. It’s much faster than individual inserts for large amounts of data.
As an example let's load historical stock price data. You can get free historical quotes from a number of sites on the net. To get some data, we'll use a backend fetch function. See the end of this post for the fetch implementation. The output of our fetch contains a prices array which looks like this:
{
"prices": [
{
"date": 1562074200,
"open": 143.6999969482422,
"high": 145.63999938964844,
"low": 142.55999755859375,
"close": 144.6199951171875,
"volume": 279700,
"adjclose": 144.6199951171875
},...
]
}
To use bulkInsert, your data will need to be in an array with attributes matching the name of the attributes in your collection.
We have a collection called stock_quotes with the same attributes as the prices array above, plus a few more. Attributes in the collection that are not in the array are left as null.
export function loadData(inputData) {
let options = {
"suppressAuth": true,
"suppressHooks": true
};
return wixData.bulkInsert("stock_quotes", inputData, options)
.then((results) => {
let response = {
"inserted": results.inserted,
"skipped": results.skipped,
"errors": results.errors
};
return response;
})
.catch((err) => {
return err
});
}
Note that bulkInsert has a limit of 1000 rows so if you want to load more, you need to iterate through your input data 1000 items at a time.
Tip 1: Don't iterate through your input data on the backend. If you have a lot of data, you can easily hit the timeout on calls to the backend.
Tip 2: Use the options to turn off hooks and authorization to make it faster.
Tip3: If update-or-insert is what you need, check out bulkSave( ) There are also bulkRemove and bulkUpdate functions.
From the example, the fetch looks like this:
import { fetch } from 'wix-fetch';
export function myFetch() {
return fetch("https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-historical-data?frequency=1d&filter=history&period1=1383748200&period2=1562086800&symbol=wix", {
"method": "get",
"headers": {
"Content-Type": "application/json",
"x-rapidapi-host": "apidojo-yahoo-finance-v1.p.rapidapi.com",
"x-rapidapi-key": "000-get-a-free-api-key-000"
}
})
.then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject("Fetch did not succeed");
}
})
.catch((err) => {
console.log("error in fetch=", err);
return err
})
.then((dataToInsert) => {
return dataToInsert.prices;
})
}
Thank you for your great advice.