1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `View Configurations`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you'll learn about view configurations in the Settings Module.
10
11 <Note title="Experimental" type="warning">
12
13 This feature is a work in progress, so it may change in future releases.
14
15 </Note>
16
17 <Prerequisites
18 items={[
19 {
20 text: "Medusa v2.18.0+",
21 href: "https://github.com/medusajs/medusa/releases/tag/v2.18.0",
22 }
23 ]}
24 />
25
26 ## Enable the Feature Flag
27
28 View configurations are disabled by default. To enable them, set the `view_configurations` feature flag in your `medusa-config.ts` file:
29
30 ```ts title="medusa-config.ts"
31 module.exports = defineConfig({
32 // ...
33 featureFlags: {
34 view_configurations: true,
35 },
36 })
37 ```
38
39 Alternatively, set the `MEDUSA_FF_VIEW_CONFIGURATIONS` environment variable:
40
41 ```bash
42 MEDUSA_FF_VIEW_CONFIGURATIONS=true
43 ```
44
45 ---
46
47 ## What is a View Configuration?
48
49 A view configuration is a saved data-table view for an entity, such as products or orders. It lets admin users customize how a table shows its data, then save that customization as a reusable view.
50
51 The [ViewConfiguration](/references/settings/models/ViewConfiguration) data model stores a view's customizations in its `configuration` property, which includes:
52
53 - `visible_columns`: The columns shown in the table.
54 - `column_order`: The order of the columns.
55 - `column_widths`: The width of each column.
56 - `filters`: The applied filters.
57 - `sorting`: The column and direction to sort by.
58 - `search`: The search query.
59
60 ---
61
62 ## View Configuration Types
63
64 There are three types of views for an entity:
65
66 1. Code default: The default view built into the dashboard. It has no stored view configuration.
67 2. System default: A stored view configuration that has its `is_system_default` property enabled and no `user_id`. It applies to all users that don't have a personal view. Only one system default can exist per entity.
68 3. Personal view: A stored view configuration that has a `user_id`. It's private to the user that created it.
69
70 ---
71
72 ## Active View
73
74 Each user has an active view for an entity, which is the view they're currently using. The Settings Module resolves a user's active view in the following order:
75
76 1. The view set as active in the user's preferences.
77 2. The user's first personal view, if any.
78 3. The system default, if any.
79
80 The `getActiveViewConfiguration` method of the module's service resolves the active view, and the `setActiveViewConfiguration` method sets it.
81
82 ---
83
84 ## Manage View Configurations
85
86 The Settings Module's service provides methods to create, retrieve, update, and delete view configurations programmatically. Resolve the service from the Medusa container using `Modules.SETTINGS`.
87
88 ### Create a View Configuration
89
90 Use the `createViewConfigurations` method to create a new view configuration:
91
92 ```ts
93 const viewConfiguration =
94 await settingsModuleService.createViewConfigurations({
95 entity: "product",
96 name: "My Products View",
97 configuration: {
98 visible_columns: ["title", "status"],
99 column_order: ["title", "status"],
100 },
101 })
102 ```
103
104 ### Retrieve a View Configuration
105
106 Use the `retrieveViewConfiguration` method to retrieve a view configuration by its ID:
107
108 ```ts
109 const viewConfiguration =
110 await settingsModuleService.retrieveViewConfiguration("viewcfg_123")
111 ```
112
113 ### List View Configurations
114
115 Use the `listViewConfigurations` method to list view configurations with optional filters:
116
117 ```ts
118 const viewConfigurations =
119 await settingsModuleService.listViewConfigurations({
120 entity: "product",
121 })
122 ```
123
124 To retrieve a paginated list with the total count, use `listAndCountViewConfigurations`:
125
126 ```ts
127 const [viewConfigurations, count] =
128 await settingsModuleService.listAndCountViewConfigurations(
129 { entity: "product" },
130 { take: 20, skip: 0 }
131 )
132 ```
133
134 ### Update a View Configuration
135
136 Use the `updateViewConfigurations` method to update a view configuration by its ID:
137
138 ```ts
139 const viewConfiguration =
140 await settingsModuleService.updateViewConfigurations("viewcfg_123", {
141 name: "Updated View",
142 })
143 ```
144
145 ### Delete a View Configuration
146
147 Use the `deleteViewConfigurations` method to delete a view configuration by its ID:
148
149 ```ts
150 await settingsModuleService.deleteViewConfigurations("viewcfg_123")
151 ```
152
153 ### Manage the Active View
154
155 Use `getActiveViewConfiguration` to retrieve a user's active view for an entity, and `setActiveViewConfiguration` to change it:
156
157 ```ts
158 const activeView =
159 await settingsModuleService.getActiveViewConfiguration(
160 "user_123",
161 "product"
162 )
163
164 await settingsModuleService.setActiveViewConfiguration(
165 "user_123",
166 "product",
167 "viewcfg_123"
168 )
169 ```
170
171 ---
172
173 ## Configure View Configurations
174
175 You can customize how columns are generated for an entity, add computed columns, and change how a column's value renders. You can also show a configurable data table in your admin customizations.
176
177 Learn how in the [Configure View Configurations](../configure-view-configurations/page.mdx) guide.