Search.../

createDataCollection( )

Developer Preview

Creates a new data collection.

Description

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.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function createDataCollection(collection: DataCollection): Promise<DataCollection>

createDataCollection Parameters

NAME
TYPE
DESCRIPTION
collection
DataCollection

Collection details.

Returns

Details of collection created.

Return Type:

Promise<
DataCollection
>
NAME
TYPE
DESCRIPTION
_createdDate
Date

Date the collection was created.

_id
string

Collection ID. For example, my-first-collection. May include a namespace.

_updatedDate
Date

Date the collection was last updated.

capabilities
CollectionCapabilities

Capabilities the collection supports.

collectionType
string

Collection type. Indicates how the collection was created and is stored.

  • NATIVE: User-created collection.
  • WIX_APP: Collection created by a Wix app, including starter collections created when a Wix app is installed.
  • BLOCKS_APP: Collection created by a Wix Blocks app.
  • EXTERNAL: Collection located in externally connected storage.
defaultDisplayOrder
Sort

Indicates how the collection's items are sorted by default when a query doesn't specify an order.

displayField
string

The field whose value the CMS displays to represent the collection item when referenced in a different collection.

displayName
string

Collection's display name as shown in the CMS. For example, My First Collection.

displayNamespace
string

UI-friendly namespace of the Wix app with which the data collection is associated, such as Stores or Bookings. Empty for all data collections not owned by internal Wix apps.

fields
Array<
Field
>

Collection's field structure.

maxPageSize
number

Maximum number of items returned in a single query, based on the underlying storage. Native collections have a maximum page size of 1000 for offset-based queries or 100 for cursor-based queries. External collections' maximum page size defaults to 50, but an external provider can set any maximum value up to 1000.

ownerAppId
string

ID of the app that defined this collection. For user-defined collections, this value is null.

pagingModes
Array<
string
>

All paging modes the collection supports. In native collections, offset-based paging is supported by default.

permissions
Permissions

Levels of permission for accessing and modifying data, defined by lowest role needed to perform each action.

plugins
Array<
Plugin
>

All plugins the collection uses. Plugins apply additional capabilities to the collection or extend its functionality.

revision
string

Collection's current revision number, which increments each time the collection is updated. For an update operation to succeed, you must pass the latest revision number.

Was this helpful?

Create a data collection that only authorized users can edit

Copy Code
1import { Permissions, webMethod } from "wix-web-module";
2import { collections } from "wix-data.v2";
3
4/* Sample collection object:
5 *
6 * const newCollection = {
7 * _id: "myMusicCollection",
8 * permissions: {
9 * read: "ANYONE",
10 * insert: "SITE_MEMBER_AUTHOR",
11 * update: "SITE_MEMBER_AUTHOR",
12 * remove: "SITE_MEMBER_AUTHOR"
13 * },
14 * fields: [
15 * {
16 * key: "artist",
17 * displayName: "Artist Name",
18 * description: "The artist's name",
19 * type: "TEXT"
20 * },
21 * {
22 * key: "songTitle",
23 * displayName: "Song Title",
24 * description: "The song's full title",
25 * type: "TEXT"
26 * },
27 * {
28 * key: "uploadedBy",
29 * displayName: "Uploaded By",
30 * description: "The person who uploaded the song",
31 * type: "TEXT",
32 * plugins: [
33 * {
34 * type: "CMS",
35 * cmsOptions: {
36 * internal: true
37 * }
38 * }
39 * ]
40 * }
41 * ]
42 * };
43 */
44
45export const myCreateDataCollectionFunction = webMethod(Permissions.Anyone, async (newCollection) => {
46 try {
47 const createDataCollectionResponse = await collections.createDataCollection(newCollection);
48 return createDataCollectionResponse;
49 } catch (error) {
50 console.error(error);
51 // Handle the error
52 }
53});
54
55
56/* Returns a promise that resolves to the created collection:
57 *
58 * {
59 * "_createdDate": "2024-01-04T08:57:43.872Z",
60 * "_id": "myThirdMusicCollection",
61 * "_updatedDate": "2024-01-04T08:57:43.872Z",
62 * "capabilities": {
63 * "collectionOperations": [
64 * "REMOVE",
65 * "UPDATE"
66 * ],
67 * "dataOperations": [
68 * "AGGREGATE",
69 * "BULK_INSERT",
70 * "BULK_REMOVE",
71 * "BULK_SAVE",
72 * "BULK_UPDATE",
73 * "COUNT",
74 * "DISTINCT",
75 * "FIND",
76 * "GET",
77 * "INSERT",
78 * "INSERT_REFERENCE",
79 * "IS_REFERENCED",
80 * "QUERY_REFERENCED",
81 * "REPLACE_REFERENCES",
82 * "REMOVE",
83 * "REMOVE_REFERENCE",
84 * "SAVE",
85 * "UPDATE"
86 * ],
87 * "indexLimits": {
88 * "regular": 3,
89 * "total": 4,
90 * "unique": 1
91 * }
92 * },
93 * "collectionType": "NATIVE",
94 * "displayField": "artist",
95 * "displayName": "myMusicCollection",
96 * "fields": [
97 * {
98 * "capabilities": {
99 * "queryOperators": [
100 * "CONTAINS",
101 * "ENDS_WITH",
102 * "EQ",
103 * "EXISTS",
104 * "GTE",
105 * "GT",
106 * "HAS_ALL",
107 * "HAS_SOME",
108 * "LT",
109 * "LTE",
110 * "NE",
111 * "STARTS_WITH",
112 * "URLIZED"
113 * ],
114 * "sortable": true
115 * },
116 * "description": "The artist's name",
117 * "displayName": "Artist Name",
118 * "encrypted": false,
119 * "key": "artist",
120 * "legacyDisplayName": "Artist Name",
121 * "plugins": [],
122 * "systemField": false,
123 * "type": "TEXT"
124 * },
125 * {
126 * "capabilities": {
127 * "queryOperators": [
128 * "CONTAINS",
129 * "ENDS_WITH",
130 * "EQ",
131 * "EXISTS",
132 * "GTE",
133 * "GT",
134 * "HAS_ALL",
135 * "HAS_SOME",
136 * "LT",
137 * "LTE",
138 * "NE",
139 * "STARTS_WITH",
140 * "URLIZED"
141 * ],
142 * "sortable": true
143 * },
144 * "description": "The song's full title",
145 * "displayName": "Song Title",
146 * "encrypted": false,
147 * "key": "songTitle",
148 * "legacyDisplayName": "Song Title",
149 * "plugins": [],
150 * "systemField": false,
151 * "type": "TEXT"
152 * },
153 * {
154 * "capabilities": {
155 * "queryOperators": [
156 * "CONTAINS",
157 * "ENDS_WITH",
158 * "EQ",
159 * "EXISTS",
160 * "GTE",
161 * "GT",
162 * "HAS_ALL",
163 * "HAS_SOME",
164 * "LT",
165 * "LTE",
166 * "NE",
167 * "STARTS_WITH",
168 * "URLIZED"
169 * ],
170 * "sortable": true
171 * },
172 * "description": "The person who uploaded the song",
173 * "displayName": "Uploaded By",
174 * "encrypted": false,
175 * "key": "uploadedBy",
176 * "legacyDisplayName": "Uploaded By",
177 * "plugins": [
178 * {
179 * "cmsOptions": {
180 * "internal": true
181 * },
182 * "type": "CMS"
183 * }
184 * ],
185 * "systemField": false,
186 * "type": "TEXT"
187 * },
188 * {
189 * "capabilities": {
190 * "queryOperators": [
191 * "CONTAINS",
192 * "ENDS_WITH",
193 * "EQ",
194 * "EXISTS",
195 * "GTE",
196 * "GT",
197 * "HAS_ALL",
198 * "HAS_SOME",
199 * "LT",
200 * "LTE",
201 * "NE",
202 * "STARTS_WITH",
203 * "URLIZED"
204 * ],
205 * "sortable": true
206 * },
207 * "displayName": "ID",
208 * "encrypted": false,
209 * "key": "_id",
210 * "legacyDisplayName": "ID",
211 * "plugins": [],
212 * "systemField": true,
213 * "type": "TEXT"
214 * },
215 * {
216 * "capabilities": {
217 * "queryOperators": [
218 * "CONTAINS",
219 * "ENDS_WITH",
220 * "EQ",
221 * "EXISTS",
222 * "GTE",
223 * "GT",
224 * "HAS_ALL",
225 * "HAS_SOME",
226 * "LT",
227 * "LTE",
228 * "NE",
229 * "STARTS_WITH",
230 * "URLIZED"
231 * ],
232 * "sortable": true
233 * },
234 * "displayName": "Created Date",
235 * "encrypted": false,
236 * "key": "_createdDate",
237 * "legacyDisplayName": "Created Date",
238 * "plugins": [],
239 * "systemField": true,
240 * "type": "DATETIME"
241 * },
242 * {
243 * "capabilities": {
244 * "queryOperators": [
245 * "CONTAINS",
246 * "ENDS_WITH",
247 * "EQ",
248 * "EXISTS",
249 * "GTE",
250 * "GT",
251 * "HAS_ALL",
252 * "HAS_SOME",
253 * "LT",
254 * "LTE",
255 * "NE",
256 * "STARTS_WITH",
257 * "URLIZED"
258 * ],
259 * "sortable": true
260 * },
261 * "displayName": "Updated Date",
262 * "encrypted": false,
263 * "key": "_updatedDate",
264 * "legacyDisplayName": "Updated Date",
265 * "plugins": [],
266 * "systemField": true,
267 * "type": "DATETIME"
268 * },
269 * {
270 * "capabilities": {
271 * "queryOperators": [
272 * "CONTAINS",
273 * "ENDS_WITH",
274 * "EQ",
275 * "EXISTS",
276 * "GTE",
277 * "GT",
278 * "HAS_ALL",
279 * "HAS_SOME",
280 * "LT",
281 * "LTE",
282 * "NE",
283 * "STARTS_WITH",
284 * "URLIZED"
285 * ],
286 * "sortable": true
287 * },
288 * "displayName": "Owner",
289 * "encrypted": false,
290 * "key": "_owner",
291 * "legacyDisplayName": "Owner",
292 * "plugins": [],
293 * "systemField": true,
294 * "type": "TEXT"
295 * }
296 * ],
297 * "legacyDisplayName": "myThirdMusicCollection",
298 * "pagingModes": [
299 * "OFFSET"
300 * ],
301 * "permissions": {
302 * "insert": "SITE_MEMBER_AUTHOR",
303 * "read": "ANYONE",
304 * "remove": "SITE_MEMBER_AUTHOR",
305 * "update": "SITE_MEMBER_AUTHOR"
306 * },
307 * "plugins": [
308 * {
309 * "gridapplessOptions": {
310 * "migrated": true
311 * },
312 * "type": "GRIDAPPLESS"
313 * }
314 * ],
315 * "revision": "1"
316 * }
317 */
318
Create a permissions-protected collection with dynamic URLs

This function creates a collection whose items anyone can view but only site members with author permissions can edit. A dynamic URL is generated for each item in the collection.

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { collections } from 'wix-data.v2';
3
4/* Sample collection object:
5 * Includes a plugin that generates dynamic pages for each item in the collection.
6 *
7 * const newCollection = {
8 * _id: "shirts",
9 * displayName: "Shirts",
10 * displayField: "shirts",
11 * permissions: {
12 * read: "ANYONE",
13 * insert: "SITE_MEMBER_AUTHOR",
14 * update: "SITE_MEMBER_AUTHOR",
15 * remove: "SITE_MEMBER_AUTHOR"
16 * },
17 * fields:
18 * [
19 * {
20 * key: "name",
21 * displayName: "Item Name",
22 * description: "Name of item, required",
23 * required: true
24 * },
25 * {
26 * key: "price",
27 * displayName: "Item Price",
28 * description: "Price of item, required",
29 * required: true
30 * },
31 * {
32 * key: "description",
33 * displayName: "Item Description",
34 * description: "Item description, not required",
35 * required: false
36 * },
37 * ],
38 * plugins:
39 * [
40 * {
41 * type: "URLIZED",
42 * urlizedOptions: {
43 * format: "ORIGINAL"
44 * }
45 * }
46 * ]
47 * };
48 */
49
50export const myCreateDataCollectionFunctionAdvanced = webMethod(Permissions.Anyone, async (newCollection) => {
51 try {
52 const createDataCollectionResponse = await collections.createDataCollection(newCollection);
53 return createDataCollectionResponse;
54 } catch (error) {
55 console.error(error);
56 // Handle the error
57 }
58});
59
60/* Returns a promise that resolves to the created collection:
61 * {
62 * "_id": "shirts",
63 * "collectionType": "NATIVE",
64 * "displayName": "Shirts",
65 * "displayField": "shirts",
66 * "capabilities": {
67 * "dataOperations": [
68 * "IS_REFERENCED",
69 * "INSERT",
70 * "SAVE",
71 * "BULK_INSERT",
72 * "BULK_UPDATE",
73 * "UPDATE",
74 * "TRUNCATE",
75 * "REMOVE",
76 * "REMOVE_REFERENCE",
77 * "COUNT",
78 * "FIND",
79 * "REPLACE_REFERENCES",
80 * "BULK_REMOVE",
81 * "INSERT_REFERENCE",
82 * "GET",
83 * "BULK_SAVE",
84 * "QUERY_REFERENCED",
85 * "DISTINCT",
86 * "AGGREGATE"
87 * ],
88 * "collectionOperations": [
89 * "UPDATE",
90 * "REMOVE"
91 * ],
92 * "indexLimits": {
93 * "regular": 3,
94 * "unique": 1,
95 * "total": 4
96 * }
97 * },
98 * "fields": [
99 * {
100 * "key": "name",
101 * "displayName": "Item Name",
102 * "type": "TEXT",
103 * "systemField": false,
104 * "capabilities": {
105 * "sortable": true,
106 * "queryOperators": [
107 * "EQ",
108 * "LT",
109 * "GT",
110 * "NE",
111 * "LTE",
112 * "GTE",
113 * "STARTS_WITH",
114 * "ENDS_WITH",
115 * "CONTAINS",
116 * "HAS_SOME",
117 * "HAS_ALL",
118 * "EXISTS",
119 * "URLIZED"
120 * ]
121 * },
122 * "encrypted": false,
123 * "description": "Name of item, required",
124 * "required": true
125 * },
126 * // Additional system fields ...
127 * ],
128 * "permissions": {
129 * "insert": "SITE_MEMBER_AUTHOR",
130 * "update": "SITE_MEMBER_AUTHOR",
131 * "remove": "SITE_MEMBER_AUTHOR",
132 * "read": "ANYONE"
133 * },
134 * "revision": "1",
135 * "plugins":
136 * [
137 * {
138 * "type": "URLIZED",
139 * "urlizedOptions": {
140 * "format": "ORIGINAL"
141 * }
142 * },
143 * {
144 * "type": "GRIDAPPLESS",
145 * "gridapplessOptions": {
146 * "migrated": true
147 * }
148 * }
149 * ],
150 * "pagingModes":
151 * [
152 * "OFFSET"
153 * ],
154 * "_createdDate": "2023-07-23T11:38:41.518Z",
155 * "_updatedDate": "2023-07-23T11:38:41.518Z"
156 * };
157 */
158