Updates a data collection.
A collection ID, revision number, permissions, and at least 1 field must be provided within options.collection
.
If a collection with that ID exists, and if its current revision
number matches the one provided, it is updated.
Otherwise, the request fails.
When a collection is updated, its _updatedDate
property is changed to the current date and its revision
property is incremented.
Note:
After a collection is updated, it only contains the properties included in the updateDataCollection()
call. If the existing collection has properties with values and those properties
aren't included in the updated collection details, their values are lost.
This function requires elevated permissions and runs only on the backend and on dashboard pages.
function updateDataCollection(
collection: DataCollection,
): Promise<DataCollection>;
Updated collection details. The existing collection is replaced with this version.
import { collections } from "wix-data.v2";
/* Example collection parameter:
* Elevate read permissions to SITE_MEMBER, and add a field
*
* const updatedDataCollection = {
* _id: "myMusicCollection",
* fields:
* [
* {
* key: "artist",
* displayName: "Artist Name",
* description: "The artist's name",
* },
* {
* key: "song",
* displayName: "Song Title",
* description: "The song's title",
* "required": true
* },
* ]
* revision: "1",
* permissions: {
* read: "SITE_MEMBER",
* insert: "SITE_MEMBER_AUTHOR",
* update: "SITE_MEMBER_AUTHOR",
* remove: "SITE_MEMBER_AUTHOR"
* },
* };
*/
export async function myUpdateDataCollectionFunction(updatedDataCollection) {
try {
const updateDataCollectionResponse = await collections.updateDataCollection(
updatedDataCollection,
);
return updateDataCollectionResponse;
} catch (error) {
console.error(error);
// Handle the error
}
}
/* Returns a promise that resolves to the updated collection:
* {
* "_id": "myMusicCollection",
* "collectionType": "NATIVE",
* "displayName": "My Music Collection",
* "displayField": "myMusicCollection",
* "capabilities": {
* "dataOperations": [
* "IS_REFERENCED",
* "INSERT",
* "SAVE",
* "BULK_INSERT",
* "BULK_UPDATE",
* "UPDATE",
* "TRUNCATE",
* "REMOVE",
* "REMOVE_REFERENCE",
* "COUNT",
* "FIND",
* "REPLACE_REFERENCES",
* "BULK_REMOVE",
* "INSERT_REFERENCE",
* "GET",
* "BULK_SAVE",
* "QUERY_REFERENCED",
* "DISTINCT",
* "AGGREGATE"
* ],
* "collectionOperations": [
* "UPDATE",
* "REMOVE"
* ],
* "indexLimits": {
* "regular": 3,
* "unique": 1,
* "total": 4
* }
* },
* "fields": [
* {
* "key": "artist",
* "displayName": "Artist Name",
* "type": "TEXT",
* "systemField": false,
* "capabilities": {
* "sortable": true,
* "queryOperators": [
* "EQ",
* "LT",
* "GT",
* "NE",
* "LTE",
* "GTE",
* "STARTS_WITH",
* "ENDS_WITH",
* "CONTAINS",
* "HAS_SOME",
* "HAS_ALL",
* "EXISTS",
* "URLIZED"
* ]
* },
* "encrypted": false,
* "description": "The artist's name",
* "required": true
* },
* {
* "key": "song",
* "displayName": "Song Title",
* "type": "TEXT",
* "systemField": false,
* "capabilities": {
* "sortable": true,
* "queryOperators": [
* "EQ",
* "LT",
* "GT",
* "NE",
* "LTE",
* "GTE",
* "STARTS_WITH",
* "ENDS_WITH",
* "CONTAINS",
* "HAS_SOME",
* "HAS_ALL",
* "EXISTS",
* "URLIZED"
* ]
* },
* "encrypted": false,
* "description": "The song's title",
* "required": true
* },
* {
* "key": "_id",
* "displayName": "ID",
* "type": "TEXT",
* "systemField": true,
* "capabilities": {
* "sortable": true,
* "queryOperators": [
* "EQ",
* "LT",
* "GT",
* "NE",
* "LTE",
* "GTE",
* "STARTS_WITH",
* "ENDS_WITH",
* "CONTAINS",
* "HAS_SOME",
* "HAS_ALL",
* "EXISTS",
* "URLIZED"
* ]
* },
* "encrypted": false
* },
* {
* "key": "_createdDate",
* "displayName": "Created Date",
* "type": "DATETIME",
* "systemField": true,
* "capabilities": {
* "sortable": true,
* "queryOperators": [
* "EQ",
* "LT",
* "GT",
* "NE",
* "LTE",
* "GTE",
* "STARTS_WITH",
* "ENDS_WITH",
* "CONTAINS",
* "HAS_SOME",
* "HAS_ALL",
* "EXISTS",
* "URLIZED"
* ]
* },
* "encrypted": false
* },
* {
* "key": "_updatedDate",
* "displayName": "Updated Date",
* "type": "DATETIME",
* "systemField": true,
* "capabilities": {
* "sortable": true,
* "queryOperators": [
* "EQ",
* "LT",
* "GT",
* "NE",
* "LTE",
* "GTE",
* "STARTS_WITH",
* "ENDS_WITH",
* "CONTAINS",
* "HAS_SOME",
* "HAS_ALL",
* "EXISTS",
* "URLIZED"
* ]
* },
* "encrypted": false
* },
* {
* "key": "_owner",
* "displayName": "Owner",
* "type": "TEXT",
* "systemField": true,
* "capabilities": {
* "sortable": true,
* "queryOperators": [
* "EQ",
* "LT",
* "GT",
* "NE",
* "LTE",
* "GTE",
* "STARTS_WITH",
* "ENDS_WITH",
* "CONTAINS",
* "HAS_SOME",
* "HAS_ALL",
* "EXISTS",
* "URLIZED"
* ]
* },
* "encrypted": false
* }
* ],
* "permissions": {
* "insert": "SITE_MEMBER",
* "update": "SITE_MEMBER_AUTHOR",
* "remove": "SITE_MEMBER_AUTHOR",
* "read": "SITE_MEMBER_AUTHOR"
* },
* "revision": "2",
* "plugins": [],
* "pagingModes": [
* "OFFSET"
* ],
* "_createdDate": "2023-07-19T12:40:02.372Z",
* "_updatedDate": "2023-07-19T14:00:28.836Z"
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.