A custom data type groups related fields under a predefined schema: each key is known, and each value has a specified type. It reorganizes fields without making their structure arbitrary. Use it when several resource fields share a meaningful theme; use a dynamic map when the data has no expected structure. section 5.7
Grouping chat-room security settings
Instead of placing password, requirePassword, allowAnonymousUsers, and allowChatBots directly on ChatRoom, define a securityConfig field of type SecurityConfig. This makes the resource easier to organize while preserving a fixed schema. section 5.7
Apply these design rules:
- Group fields only when they share a common theme and belong naturally inside the resource. section 5.7
- Prefer a singleton sub-resource when grouped fields are fundamentally distinct and meaningful outside the parent resource. section 5.7
- Keep arbitrary, dynamic data in a map, and bound map keys and values when maps are used. section 5.7 section 5.7.1
Listing 5.8 Storing data directly on a resource or in a separate structure
interface ChatRoomFlat {
id: string;
name: string;
password: string; #1
requirePassword: boolean; #1
allowAnonymousUsers: boolean; #1
allowChatBots: boolean; #1
}
interface ChatRoomReGrouped {
id: string;
name: string;
securityConfig: SecurityConfig; #2
}
interface SecurityConfig { #2
password: string;
requirePassword: boolean;
allowAnonymousUsers: boolean;
allowChatBots: boolean;
}
Compare flat and regrouped ChatRoom schemas. section 5.7
