bulkSave( )
Inserts or updates a number of items in a collection.
Description
The bulkSave()
function returns a Promise that resolves after the items
have been added or updated in the specified collection. The Promise is rejected if the
current user does not have the necessary permissions for the collection.
The bulkSave()
function inserts or updates the specified items, depending on
whether they already exist in the collection. It compares the _id
property
value of the specified items with the _id
property values of the items in
the specified collection.
- If an item in the collection has the specified
_id
value,bulkSave()
usesupdate()
to update the item in the collection, triggering thebeforeUpdate()
andafterUpdate()
hooks for that item if they have been defined. - If none of the items in the collection contain that
_id
value, the specified item does not have an_id
property, or if the specified item's_id
property isundefined
,bulkSave()
usesinsert()
to add the specified item into the collection. This triggers thebeforeInsert()
andafterInsert()
hooks for that item if they have been defined.
Use the options
parameter to run bulkSave()
from backend code without
checking for permissions or without its registered hooks.
When saving items to a collection that has a reference field, set
the values of the reference fields to the referenced item's _id
value or
the entire referenced item object.
The maximum size of an item that you can save to a collection is 500kb.
Notes:
- If an item's
_id
property value is set tonull
or an empty string, an error is thrown.- Bulk operations are limited to 1000 items per function call.
- The
bulkSave()
function is not supported for Single Item Collections.
Syntax
function bulkSave(collectionId: string, items: Array<Object>, [options: WixDataOptions]): Promise<WixDataBulkResult>
bulkSave Parameters
NAME
TYPE
DESCRIPTION
The ID of the collection to save the item to.
To find your collectionId
, select the Databases tab in the Velo Sidebar.
Hover over your collection, click the three dots, and select Edit Settings.
The items to insert or update.
The array must contain at least one item. Passing an empty array returns an error.
An object containing options to use when processing this operation.
Returns
Fulfilled - The results of the bulk save. Rejected - The error that caused the rejection.
Return Type:
NAME
TYPE
DESCRIPTION
The number of inserted items.
The number of updated items.
The number of skipped items.
List of IDs of inserted items.
List of IDs of updated items.
List of errors.
Was this helpful?
1import wixData from 'wix-data';23// ...45let toSave1 = {6 "title": "Mr.",7 "first_name": "John",8 "last_name": "Doe"9};1011let toSave2 = {12 "title": "Ms.",13 "first_name": "Jane",14 "last_name": "Doe"15};1617wixData.bulkSave("myCollection", [toSave1, toSave2])18 .then((results) => {19 let inserted = results.inserted; // 220 let insertedIds = results.insertedItemIds; // see below21 let updated = results.updated; // 022 let skipped = results.skipped; // 023 let errors = results.errors; // []24 })25 .catch((err) => {26 let errorMsg = err;27 });2829/* insertedIds is:30 *31 * [32 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",33 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"34 * ]35 */
1import wixData from 'wix-data';23// ...45let toSave1 = {6 "_id": "00001",7 "title": "Mr.",8 "first_name": "John",9 "last_name": "Schmoe"10};1112let toSave2 = {13 "_id": "00003",14 "title": "Prof.",15 "first_name": "Jan",16 "last_name": "Doey"17};1819wixData.bulkSave("myCollection", [toSave1, toSave2])20 .then((results) => {21 let inserted = results.inserted; // 122 let insertedIds = results.insertedItemIds; // ["00003"]23 let updated = results.updated; // 124 let skipped = results.skipped; // 025 let errors = results.errors; // []26 })27 .catch((err) => {28 let errorMsg = err;29 });
1import wixData from 'wix-data';23// ...45let toSave1 = {6 "_id": "00001",7 "title": "Mr.",8 "first_name": "John",9 "last_name": "Schmoe"10};1112let toSave2 = {13 "_id": "00003",14 "title": "Prof.",15 "first_name": "Jan",16 "last_name": "Doey"17};1819let options = {20 "suppressAuth": true,21 "suppressHooks": true22};2324wixData.bulkSave("myCollection", [toSave1, toSave2], options)25 .then((results) => {26 let inserted = results.inserted; // 127 let insertedIds = results.insertedItemIds; // ["00003"]28 let updated = results.updated; // 129 let skipped = results.skipped; // 030 let errors = results.errors; // []31 })32 .catch((err) => {33 let errorMsg = err;34 });
1import wixData from 'wix-data';23// ...45let countryId = "id-of-usa-item";67let toSave1 = {8 "title": "Mr.",9 "first_name": "John",10 "last_name": "Doe",11 "country": countryId12};1314let toSave2 = {15 "title": "Ms.",16 "first_name": "Jane",17 "last_name": "Doe",18 "country": countryId19};2021wixData.bulkSave("myCollection", [toSave1, toSave2])22 .then((results) => {23 let inserted = results.inserted; // 224 let insertedIds = results.insertedItemIds; // see below25 let updated = results.updated; // 026 let skipped = results.skipped; // 027 let errors = results.errors; // []28 })29 .catch((err) => {30 let errorMsg = err;31 });3233/* insertedIds is:34 *35 * [36 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",37 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"38 * ]39 */
1import wixData from 'wix-data';23// ...45let countryItem = // get country item from somewhere67let toSave1 = {8 "title": "Mr.",9 "first_name": "John",10 "last_name": "Doe",11 "country": countryItem12};1314let toSave2 = {15 "title": "Ms.",16 "first_name": "Jane",17 "last_name": "Doe",18 "country": countryItem19};2021wixData.bulkSave("myCollection", [toSave1, toSave2])22 .then((results) => {23 let inserted = results.inserted; // 224 let insertedIds = results.insertedItemIds; // see below25 let updated = results.updated; // 026 let skipped = results.skipped; // 027 let errors = results.errors; // []28 })29 .catch((err) => {30 let errorMsg = err;31 });3233/* insertedIds is:34 *35 * [36 * "d9ea65a6-726a-4c3e-b97d-1128c0a06b5f",37 * "472c1da9-e5e4-4620-8ee3-962e9d1a7df5"38 * ]39 */