Creates a new data collection.
The request body must include an ID, details for at least 1 field, and a permissions object. If any of these are missing, the collection isn't created.
This function requires elevated permissions and runs only on the backend and on dashboard pages.
function createDataCollection(
collection: DataCollection,
): Promise<DataCollection>;
Collection details.
import { collections } from "wix-data.v2";
/* Sample collection object:
*
* const newCollection = {
* _id: "myMusicCollection",
* permissions: {
* read: "ANYONE",
* insert: "SITE_MEMBER_AUTHOR",
* update: "SITE_MEMBER_AUTHOR",
* remove: "SITE_MEMBER_AUTHOR"
* },
* fields: [
* {
* key: "artist",
* displayName: "Artist Name",
* description: "The artist's name",
* type: "TEXT"
* },
* {
* key: "songTitle",
* displayName: "Song Title",
* description: "The song's full title",
* type: "TEXT"
* },
* {
* key: "uploadedBy",
* displayName: "Uploaded By",
* description: "The person who uploaded the song",
* type: "TEXT",
* plugins: [
* {
* type: "CMS",
* cmsOptions: {
* internal: true
* }
* }
* ]
* }
* ]
* };
*/
export async function myCreateDataCollectionFunction(newCollection) {
try {
const createDataCollectionResponse =
await collections.createDataCollection(newCollection);
return createDataCollectionResponse;
} catch (error) {
console.error(error);
// Handle the error
}
}
/* Returns a promise that resolves to the created collection:
*
* {
* "_createdDate": "2024-01-04T08:57:43.872Z",
* "_id": "myThirdMusicCollection",
* "_updatedDate": "2024-01-04T08:57:43.872Z",
* "capabilities": {
* "collectionOperations": [
* "REMOVE",
* "UPDATE"
* ],
* "dataOperations": [
* "AGGREGATE",
* "BULK_INSERT",
* "BULK_REMOVE",
* "BULK_SAVE",
* "BULK_UPDATE",
* "COUNT",
* "DISTINCT",
* "FIND",
* "GET",
* "INSERT",
* "INSERT_REFERENCE",
* "IS_REFERENCED",
* "QUERY_REFERENCED",
* "REPLACE_REFERENCES",
* "REMOVE",
* "REMOVE_REFERENCE",
* "SAVE",
* "UPDATE"
* ],
* "indexLimits": {
* "regular": 3,
* "total": 4,
* "unique": 1
* }
* },
* "collectionType": "NATIVE",
* "displayField": "artist",
* "displayName": "myMusicCollection",
* "fields": [
* {
* "capabilities": {
* "queryOperators": [
* "CONTAINS",
* "ENDS_WITH",
* "EQ",
* "EXISTS",
* "GTE",
* "GT",
* "HAS_ALL",
* "HAS_SOME",
* "LT",
* "LTE",
* "NE",
* "STARTS_WITH",
* "URLIZED"
* ],
* "sortable": true
* },
* "description": "The artist's name",
* "displayName": "Artist Name",
* "encrypted": false,
* "key": "artist",
* "plugins": [],
* "systemField": false,
* "type": "TEXT"
* },
* {
* "capabilities": {
* "queryOperators": [
* "CONTAINS",
* "ENDS_WITH",
* "EQ",
* "EXISTS",
* "GTE",
* "GT",
* "HAS_ALL",
* "HAS_SOME",
* "LT",
* "LTE",
* "NE",
* "STARTS_WITH",
* "URLIZED"
* ],
* "sortable": true
* },
* "description": "The song's full title",
* "displayName": "Song Title",
* "encrypted": false,
* "key": "songTitle",
* "plugins": [],
* "systemField": false,
* "type": "TEXT"
* },
* {
* "capabilities": {
* "queryOperators": [
* "CONTAINS",
* "ENDS_WITH",
* "EQ",
* "EXISTS",
* "GTE",
* "GT",
* "HAS_ALL",
* "HAS_SOME",
* "LT",
* "LTE",
* "NE",
* "STARTS_WITH",
* "URLIZED"
* ],
* "sortable": true
* },
* "description": "The person who uploaded the song",
* "displayName": "Uploaded By",
* "encrypted": false,
* "key": "uploadedBy",
* "plugins": [
* {
* "cmsOptions": {
* "internal": true
* },
* "type": "CMS"
* }
* ],
* "systemField": false,
* "type": "TEXT"
* },
* {
* "capabilities": {
* "queryOperators": [
* "CONTAINS",
* "ENDS_WITH",
* "EQ",
* "EXISTS",
* "GTE",
* "GT",
* "HAS_ALL",
* "HAS_SOME",
* "LT",
* "LTE",
* "NE",
* "STARTS_WITH",
* "URLIZED"
* ],
* "sortable": true
* },
* "displayName": "ID",
* "encrypted": false,
* "key": "_id",
* "plugins": [],
* "systemField": true,
* "type": "TEXT"
* },
* {
* "capabilities": {
* "queryOperators": [
* "CONTAINS",
* "ENDS_WITH",
* "EQ",
* "EXISTS",
* "GTE",
* "GT",
* "HAS_ALL",
* "HAS_SOME",
* "LT",
* "LTE",
* "NE",
* "STARTS_WITH",
* "URLIZED"
* ],
* "sortable": true
* },
* "displayName": "Created Date",
* "encrypted": false,
* "key": "_createdDate",
* "plugins": [],
* "systemField": true,
* "type": "DATETIME"
* },
* {
* "capabilities": {
* "queryOperators": [
* "CONTAINS",
* "ENDS_WITH",
* "EQ",
* "EXISTS",
* "GTE",
* "GT",
* "HAS_ALL",
* "HAS_SOME",
* "LT",
* "LTE",
* "NE",
* "STARTS_WITH",
* "URLIZED"
* ],
* "sortable": true
* },
* "displayName": "Updated Date",
* "encrypted": false,
* "key": "_updatedDate",
* "plugins": [],
* "systemField": true,
* "type": "DATETIME"
* },
* {
* "capabilities": {
* "queryOperators": [
* "CONTAINS",
* "ENDS_WITH",
* "EQ",
* "EXISTS",
* "GTE",
* "GT",
* "HAS_ALL",
* "HAS_SOME",
* "LT",
* "LTE",
* "NE",
* "STARTS_WITH",
* "URLIZED"
* ],
* "sortable": true
* },
* "displayName": "Owner",
* "encrypted": false,
* "key": "_owner",
* "plugins": [],
* "systemField": true,
* "type": "TEXT"
* }
* ],
* "pagingModes": [
* "OFFSET"
* ],
* "permissions": {
* "insert": "SITE_MEMBER_AUTHOR",
* "read": "ANYONE",
* "remove": "SITE_MEMBER_AUTHOR",
* "update": "SITE_MEMBER_AUTHOR"
* },
* "plugins": [],
* "revision": "1"
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.