Search.../

createDataCollection( )

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 (dashboard page code)

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

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