###### tags: `ELK-ROOM` # Setting Schema ## Prisma https://prismaliser.app/ ![](https://i.imgur.com/9rGZWWW.png) ```typescript= model User { ... userSettings UserSetting[] } model Setting { id Int @id @default(autoincrement()) name String @unique type SettingType category SettingCategory constrained Boolean @default(false) minValue Int? maxValue Int? description String? settingValues SettingValue[] userSettings UserSetting[] } enum SettingType { INTERGER STRING BOOLEAN } enum SettingCategory { NORMAL NOTIFICATION PRIVACY } model SettingValue { id Int @id @default(autoincrement()) settingId Int setting Setting @relation(fields: [settingId], references: [id], onDelete: Cascade) value String description String? userSettings UserSetting[] } model UserSetting { id Int @id @default(autoincrement()) userId String user User @relation(fields: [userId], references: [id], onDelete: Cascade) settingId Int setting Setting @relation(fields: [settingId], references: [id], onDelete: Cascade) valueId Int? constrainedValue SettingValue? @relation(fields: [valueId], references: [id], onDelete: Cascade) unconstrainedValue Int? } ``` ## DB Table `description` 非必填,主要給開發者看的 ### Setting Table ![](https://i.imgur.com/yAImgjg.png) ### SettingValue Table ![](https://i.imgur.com/eAPzWcd.png =40%x) ### UserSetting Table ![](https://i.imgur.com/iwVbIul.png =85%x) ## Query API ### query settings by uid ```typescript= const settings = await prisma.userSetting.findMany({ where: { user: { id: 'R72kvgB8HfUh1mdp8F0t4FHpnC63' }, }, select: { setting: { select: { name: true, type: true, category: true, constrained: true, description: true, }, }, constrainedValue: true, unconstrainedValue: true, }, }) const result: { [key: string]: { name: string, type: string, category: string, constrained: boolean, description?: string, value: number | boolean | string[] } } = {} settings.forEach((setting) => { if (result[`${setting.setting.name}`]) { result[`${setting.setting.name}`].value.push(setting.constrainedValue?.value) } else { let value if (setting.setting.constrained) { value = setting.constrainedValue ? [setting.constrainedValue.value] : [] } else { value = setting.setting.type === 'BOOLEAN' ? !!setting.unconstrainedValue : setting.unconstrainedValue } result[`${setting.setting.name}`] = { ...setting.setting, value, } } }) console.log('result', Object.values(result)) ``` ### print result ```json= result [ { name: 'SPEED_BALL', type: 'BOOLEAN', category: 'NORMAL', constrained: false, description: '浮動加速球', value: true }, { name: 'NEWS', type: 'BOOLEAN', category: 'NOTIFICATION', constrained: false, description: '新聞通知', value: false }, { name: 'FORUM_LANG', type: 'STRING', category: 'NORMAL', constrained: true, description: '討論區語言', value: [ 'zho', 'eng', 'jpn' ] }, { name: 'RECEIVE_PLAYING', type: 'BOOLEAN', category: 'NOTIFICATION', constrained: false, description: '是否要收到您關注玩家的遊玩通知', value: false }, { name: 'SEND_PLAYING', type: 'BOOLEAN', category: 'NOTIFICATION', constrained: false, description: '玩遊戲時是否通知關注您的玩家', value: true }, { name: 'SLEEP_START_TIME', type: 'INTERGER', category: 'NOTIFICATION', constrained: false, description: '睡眠勿擾起始時間', value: 23 }, { name: 'SLEEP_END_TIME', type: 'INTERGER', category: 'NOTIFICATION', constrained: false, description: '睡眠勿擾結束時間', value: 7 } ] ``` -------------- 或是比較簡單的做法 Setting Table 的 FORUM_LANG 改成 JSON ![](https://i.imgur.com/QWOhbQX.png) 不用創建 SettingValue Table UserSetting Table 的 value 改成 String ![](https://i.imgur.com/ISt9e2s.png =85%x)