> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: Data Collections Extension Files and Code

## Article: Data Collections Extension Files and Code

## Article Link: https://dev.wix.com/docs/backend-extensions/data-collections/data-collections-extension-files-and-code.md

## Article Content:

# Data Collections Extension Files and Code

A [data collections](https://dev.wix.com/docs/overview/extensions-framework/backend-extensions/data-collections.md) extension is available in Wix CLI apps and Wix-managed headless projects. 

> **Note:** A Studio Two site doesn't use this extension. A site creates collections directly instead. See [Create a Collection](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/introduction/create-a-collection.md) in Studio Two.

When you add the extension, the generated files land under `src/extensions/backend/data-collections/`. One builder file registers every collection, and each collection has its own definition file:

```text
src/
└── extensions/
    └── backend/
        └── data-collections/
            ├── data-collections.extension.ts
            └── <collection-name>.ts
```

| File | What it's for |
|------|---------------|
| `data-collections.extension.ts` | The [builder file](#builder-file) that registers the project's collections. A project has one, shared by all its collections. |
| `<collection-name>.ts` | The [definition](#collection-definition-file) of one collection, including its fields, permissions, indexes, and initial data. |

`<collection-name>` comes from the name you give the collection. It must be 1 to 36 characters, using only letters, numbers, underscores, and hyphens, and it must be unique in the folder.

The extension is also registered in the project's `src/extensions.ts`, with an import and a `.use()` call. When you add more collections, their definition files go in the same folder, and the builder file imports them.

## Builder file

The `data-collections.extension.ts` file calls `extensions.dataCollections()` with the collections to register. Import `extensions` from `@wix/astro/builders`, in both a Wix CLI app and a headless project:

```ts
import { extensions } from '@wix/astro/builders';
```

This example registers two collections:

```ts
import { extensions } from '@wix/astro/builders';

import customerFeedbackCollection from './customer-feedback';
import productReviewsCollection from './product-reviews';

export default extensions.dataCollections({
  id: 'c7bdcb61-9c98-45d5-8a1e-76ce89b407eb',
  name: 'Data Collections',
  collections: [customerFeedbackCollection, productReviewsCollection],
});
```

### Builder fields

The configuration object takes these fields:

| Field | Type | Description |
|--|--|--|
| `id` | string | Required. The extension ID as a GUID. It's generated with the file, and it must be unique across extensions in the project. |
| `name` | string | Required. A name for the extension. Defaults to `'Data Collections'`. |
| `collections` | array | Required. The default export of each collection definition file to register. |

## Collection definition file

Each `<collection-name>.ts` file defines one collection. Its default export is a collection definition. A generated file starts with two fields and default permissions:

```ts
export const collectionIdSuffix = 'reviews';

export default {
  idSuffix: collectionIdSuffix,
  displayName: 'reviews',
  fields: [
    { type: 'TEXT', displayName: 'Title', key: 'title' },
    { type: 'IMAGE', displayName: 'Image', key: 'image' },
  ],
  displayField: 'title',
  dataPermissions: {
    itemInsert: 'CMS_EDITOR',
    itemRead: 'CMS_EDITOR',
    itemRemove: 'CMS_EDITOR',
    itemUpdate: 'CMS_EDITOR',
  },
  indexes: [],
  initialData: [],
};
```

The file also exports `collectionIdSuffix`, the same string as `idSuffix`, so other code in the project can refer to the collection without repeating the string.

### Collection fields

A collection definition takes these top-level fields:

| Field | Type | Description |
|--|--|--|
| `idSuffix` | string | Required. The last part of the collection ID. The full ID is the app namespace followed by `/` and `idSuffix`. 1 to 36 characters, using only letters, numbers, underscores, and hyphens. |
| `displayName` | string | The collection's name in the CMS. |
| `fields` | array | Required. The collection's fields. Each field has a `key`, a `type`, and an optional `displayName`. |
| `displayField` | string | The `key` of the field that identifies an item when another collection references it. |
| `dataPermissions` | object | Required. The role that each item action needs. The actions are `itemInsert`, `itemRead`, `itemRemove`, and `itemUpdate`. |
| `indexes` | array | Indexes that speed up queries on the collection. |
| `initialData` | array | Items Wix adds when it creates the collection on a site. Each item must match `fields`. |

For every field type, field option, and index option, and for the initial data format, see the [Data Collections Extension JSON Reference](https://dev.wix.com/docs/api-reference/business-solutions/cms/collection-management/data-collections-extension/introduction.md).

### Permissions

Each `dataPermissions` action takes one of these roles, from the most open to the most restricted:

- `ANYONE`
- `SITE_MEMBER`
- `SITE_MEMBER_AUTHOR`
- `CMS_EDITOR`
- `PRIVILEGED`

For who each role includes, see [About the Data Permissions API](https://dev.wix.com/docs/api-reference/business-solutions/cms/collection-management/data-permissions/introduction.md).

## See also

- [About Data Collections](https://dev.wix.com/docs/backend-extensions/data-collections/about-data-collections.md)
- [Data Collections](https://dev.wix.com/docs/overview/extensions-framework/backend-extensions/data-collections.md)
- [Data Collections Extension JSON Reference](https://dev.wix.com/docs/api-reference/business-solutions/cms/collection-management/data-collections-extension/introduction.md)