|
1
|
export const metadata = { |
|
2
|
title: `User Preferences`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this guide, you'll learn about user preferences in the Settings Module. |
|
8
|
|
|
9
|
## What is a User Preference? |
|
10
|
|
|
11
|
A user preference stores an arbitrary setting for a user as a key-value pair. For example, you can store a user's preferred theme or language. |
|
12
|
|
|
13
|
The [UserPreference](/references/settings/models/UserPreference) data model represents a user preference. It denotes the user it belongs to through the `user_id` property, and it stores the setting's key and value in its `key` and `value` properties. A user can have only one preference per key. |
|
14
|
|
|
15
|
--- |
|
16
|
|
|
17
|
## Manage User Preferences |
|
18
|
|
|
19
|
The Settings Module's service provides methods to create, retrieve, update, and delete user preferences. Resolve the service from the Medusa container using `Modules.SETTINGS`. |
|
20
|
|
|
21
|
### Set a User Preference |
|
22
|
|
|
23
|
Use the `setUserPreference` method to create or update a user preference by its key: |
|
24
|
|
|
25
|
```ts |
|
26
|
const preference = await settingsModuleService.setUserPreference( |
|
27
|
"user_123", |
|
28
|
"theme", |
|
29
|
{ mode: "dark" } |
|
30
|
) |
|
31
|
``` |
|
32
|
|
|
33
|
### Get a User Preference |
|
34
|
|
|
35
|
Use the `getUserPreference` method to retrieve a user's preference by key: |
|
36
|
|
|
37
|
```ts |
|
38
|
const preference = await settingsModuleService.getUserPreference( |
|
39
|
"user_123", |
|
40
|
"theme" |
|
41
|
) |
|
42
|
``` |
|
43
|
|
|
44
|
This returns `null` if no preference exists for the specified user and key. |
|
45
|
|
|
46
|
### Create User Preferences |
|
47
|
|
|
48
|
Use the `createUserPreferences` method to create one or more preferences at once: |
|
49
|
|
|
50
|
```ts |
|
51
|
const preferences = |
|
52
|
await settingsModuleService.createUserPreferences([ |
|
53
|
{ |
|
54
|
user_id: "user_123", |
|
55
|
key: "theme", |
|
56
|
value: "dark", |
|
57
|
}, |
|
58
|
]) |
|
59
|
``` |
|
60
|
|
|
61
|
### Retrieve a User Preference by ID |
|
62
|
|
|
63
|
Use the `retrieveUserPreference` method to retrieve a preference by its ID: |
|
64
|
|
|
65
|
```ts |
|
66
|
const preference = |
|
67
|
await settingsModuleService.retrieveUserPreference("uspref_123") |
|
68
|
``` |
|
69
|
|
|
70
|
### List User Preferences |
|
71
|
|
|
72
|
Use the `listUserPreferences` method to list preferences with optional filters: |
|
73
|
|
|
74
|
```ts |
|
75
|
const preferences = |
|
76
|
await settingsModuleService.listUserPreferences({ |
|
77
|
user_id: "user_123", |
|
78
|
}) |
|
79
|
``` |
|
80
|
|
|
81
|
To retrieve a paginated list with the total count, use `listAndCountUserPreferences`: |
|
82
|
|
|
83
|
```ts |
|
84
|
const [preferences, count] = |
|
85
|
await settingsModuleService.listAndCountUserPreferences( |
|
86
|
{ user_id: "user_123" }, |
|
87
|
{ take: 20, skip: 0 } |
|
88
|
) |
|
89
|
``` |
|
90
|
|
|
91
|
### Update User Preferences |
|
92
|
|
|
93
|
Use the `updateUserPreferences` method to update existing preferences by their IDs: |
|
94
|
|
|
95
|
```ts |
|
96
|
const preferences = |
|
97
|
await settingsModuleService.updateUserPreferences([ |
|
98
|
{ |
|
99
|
id: "uspref_123", |
|
100
|
value: "light", |
|
101
|
}, |
|
102
|
]) |
|
103
|
``` |
|
104
|
|
|
105
|
### Delete User Preferences |
|
106
|
|
|
107
|
Use the `deleteUserPreferences` method to delete preferences by their IDs: |
|
108
|
|
|
109
|
```ts |
|
110
|
await settingsModuleService.deleteUserPreferences("uspref_123") |
|
111
|
``` |
|
112
|
|
|
113
|
--- |
|
114
|
|
|
115
|
## Internal Usage |
|
116
|
|
|
117
|
The Settings Module uses user preferences internally to track a user's active view for an entity and active layout scope for a zone. |
|
118
|
|
|
119
|
Learn more about these concepts in the [View Configurations](../view-configurations/page.mdx) and [Layout Configurations](../layout-configurations/page.mdx) documents. |