createIndex( )
Creates an index for a data collection.
Description
The index can't be used immediately, as the process of generating the index takes time.
You can check whether your index is ready using listIndexes()
.
Note that when an index fails to create, the failed index still occupies a slot.
To remove the failed index and free up the slot for a new index, use dropIndex()
.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function createIndex(dataCollectionId: string, index: Index): Promise<Index>
createIndex Parameters
NAME
TYPE
DESCRIPTION
ID of the data collection for which to generate the index.
Details of the index to be created.
Returns
Details of the index being generated.
Return Type:
NAME
TYPE
DESCRIPTION
Whether the index ignores case.
Default: false
Contains details about the reasons for failure when status
is FAILED
.
Fields for which the index is defined.
Max: 3 fields (for a unique index: 1 field)
Name of the index.
Current status of the index.
BUILDING
: Index creation is in progress.ACTIVE
: Index has been successfully created and can be used in queries.DROPPING
: Index is in the process of being dropped.DROPPED
: Index has been dropped successfully.FAILED
: Index creation has failed.INVALID
: Index contains incorrectly indexed data.
Whether the index enforces uniqueness of values in the field for which it is defined.
If true
, the index can have only one field.
Default: false
Was this helpful?
1import { indexes } from 'wix-data.v2';23/*4 * Sample dataCollectionId value = 'Jackets'5 *6 * Sample index value:7 * {8 * fields: [9 * {10 * path: 'price'11 * }12 * ],13 * name: 'byPrice'14 * }15 */161718export async function myCreateIndexFunction(dataCollectionId, index) {19 try {20 const createIndexResponse = await indexes.createIndex(dataCollectionId, index);21 console.log(`Successfully created an index named ${createIndexResponse.name}. Full response: `, createIndexResponse);2223 return createIndexResponse;24 } catch (error) {25 console.error(error);26 // Handle the error27 }28}2930/* Promise resolves to:31 * {32 * "caseInsensitive": false,33 * "fields": [34 * {35 * "order": "ASC",36 * "path": "price"37 * }38 * ],39 * "name": "byPrice",40 * "status": "BUILDING",41 * "unique": false,42 * }43 */44
1import { Permissions, webMethod } from 'wix-web-module';2import { indexes } from 'wix-data.v2';3import { elevate } from 'wix-auth';45/*6 * Sample dataCollectionId value = 'Jackets'7 *8 * Sample index value:9 * {10 * fields: [11 * {12 * path: 'price'13 * }14 * ],15 * name: 'byPrice'16 * }17 */1819export const myCreateIndexFunction = webMethod(Permissions.Anyone, async (dataCollectionId, index) => {20 try {21 const elevatedCreateIndex = elevate(indexes.createIndex);22 const createIndexResponse = await elevatedCreateIndex(dataCollectionId, index);23 console.log(`Successfully created an index named ${createIndexResponse.name}. Full response: `, createIndexResponse);2425 return createIndexResponse;26 } catch (error) {27 console.error(error);28 // Handle the error29 }30});3132/* Promise resolves to:33 * {34 * "caseInsensitive": false,35 * "fields": [36 * {37 * "order": "ASC",38 * "path": "price"39 * }40 * ],41 * "name": "byPrice",42 * "status": "BUILDING",43 * "unique": false,44 * }45 */46
Create a case-insensitive index based on the item name and price in ascending order, and item size in descending order
1import { Permissions, webMethod } from 'wix-web-module';2import { indexes } from 'wix-data.v2';3import { elevate } from 'wix-auth';45/*6 * Sample dataCollectionId value = 'Jackets'7 *8 * Sample index value:9 * {10 * caseInsensitive: true,11 * fields: [12 * {13 * path: 'itemName'14 * },15 * {16 * path: 'price'17 * },18 * {19 * order: 'DESC',20 * path: 'size'21 * }22 * ],23 * name: 'byItemNameAndPriceAndSize'24 * }25 */2627export const myCreateIndexFunction = webMethod(Permissions.Anyone, async (dataCollectionId, index) => {28 try {29 const elevatedCreateIndex = elevate(indexes.createIndex);30 const createIndexResponse = await elevatedCreateIndex(dataCollectionId, index);31 console.log(`Successfully created an index named ${createIndexResponse.name}. Full response: `, createIndexResponse);3233 return createIndexResponse;34 } catch (error) {35 console.error(error);36 // Handle the error37 }38});3940/* Promise resolves to:41 * {42 * "caseInsensitive": true,43 * "fields": [44 * {45 * "order": "ASC",46 * "path": "itemName"47 * },48 * {49 * "order": "ASC",50 * "path": "price"51 * },52 * {53 * "order": "DESC",54 * "path": "size"55 * }56 * ],57 * "name": "byItemNameAndPriceAndSize",58 * "status": "BUILDING",59 * "unique": false,60 * }61 */62
1import { Permissions, webMethod } from 'wix-web-module';2import { indexes } from 'wix-data.v2';3import { elevate } from 'wix-auth';45/*6 * Sample dataCollectionId value = 'shoes';7 *8 * Sample index value =9 * {10 * name: 'bySerialNum',11 * unique: true,12 * fields: [13 * {14 * path: 'serial'15 * }16 * ]17 * };18*/1920export const myCreateIndexFunction = webMethod(Permissions.Anyone, async (dataCollectionId, index) => {21 try {22 const elevatedCreateIndex = elevate(indexes.createIndex);23 const createUniqueIndexResponse = await elevatedCreateIndex(dataCollectionId, index);24 console.log(`Successfully created an index named ${createUniqueIndexResponse.name}. Full response: `, createUniqueIndexResponse);2526 return createUniqueIndexResponse;27 } catch (error) {28 console.error(error);29 // Handle the error30 }31});3233/* Returns a promise that resolves to the index being created:34 * {35 * "caseInsensitive": false,36 * "fields": [37 * {38 * "order": "ASC",39 * "path": "serial"40 * }41 * ],42 * "name": "bySerialNum",43 * "status": "BUILDING",44 * "unique": true,45 * }46 */47