design-documents.coffee 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. # Design документ для универсальной CMS
  2. design_documents =
  3. # Design документ для блог постов
  4. blog_posts:
  5. version: "1.2"
  6. views:
  7. # Все опубликованные блог посты
  8. published:
  9. map: ((doc) ->
  10. if doc.type is 'blog_post' and doc.status is 'published'
  11. emit(doc.created_at, doc)).toString()
  12. # Блог посты по домену и языку
  13. by_domain_language:
  14. map: ((doc) ->
  15. if doc.type is 'blog_post' and doc.status is 'published' and doc.domain and doc.language
  16. for domain in doc.domain
  17. for lang in doc.language
  18. emit([domain, lang, doc.created_at], doc)).toString()
  19. # Избранные блог посты
  20. featured:
  21. map: ((doc) ->
  22. if doc.type is 'blog_post' and doc.status is 'published' and doc.featured
  23. emit(doc.created_at, doc)).toString()
  24. # Блог посты по категории
  25. by_category:
  26. map: ((doc) ->
  27. if doc.type is 'blog_post' and doc.status is 'published' and doc.category_id
  28. emit([doc.category_id, doc.created_at], doc)).toString()
  29. # Блог посты по тегам
  30. by_tags:
  31. map: ((doc) ->
  32. if doc.type is 'blog_post' and doc.status is 'published' and doc.tags
  33. for tag_array, lang_index in doc.tags
  34. for tag in tag_array
  35. emit([tag, doc.created_at], doc)).toString()
  36. # Design документ для страниц (page)
  37. pages:
  38. version: "1.1"
  39. views:
  40. # Все опубликованные страницы
  41. published:
  42. map: ((doc) ->
  43. if doc.type is 'page' and doc.status is 'published'
  44. emit(doc.order || 0, doc)).toString()
  45. # Страницы по домену и языку
  46. by_domain_language:
  47. map: ((doc) ->
  48. if doc.type is 'page' and doc.status is 'published' and doc.domain and doc.language
  49. for domain in doc.domain
  50. for lang in doc.language
  51. emit([domain, lang, doc.order || 0], doc)).toString()
  52. # Страницы по пути (для меню и навигации)
  53. by_path:
  54. map: ((doc) ->
  55. if doc.type is 'page' and doc.status is 'published' and doc.category_path
  56. for path_segment in doc.category_path
  57. emit([path_segment, doc.order || 0], doc)).toString()
  58. # Страницы по родительскому ID (для иерархии)
  59. by_parent:
  60. map: ((doc) ->
  61. if doc.type is 'page' and doc.status is 'published'
  62. parent_id = doc.parent_id || 'root'
  63. emit([parent_id, doc.order || 0], doc)).toString()
  64. # Защищенные страницы (требующие авторизации)
  65. protected:
  66. map: ((doc) ->
  67. if doc.type is 'page' and doc.status is 'published' and doc.protected
  68. emit(doc.order || 0, doc)).toString()
  69. # Страницы для карты сайта
  70. sitemap:
  71. map: ((doc) ->
  72. if doc.type is 'page' and doc.status is 'published' and doc.show_in_sitemap != false
  73. emit(doc.order || 0, doc)).toString()
  74. # Design документ для событий (events)
  75. events:
  76. version: "1.2"
  77. views:
  78. # Все опубликованные события
  79. published:
  80. map: ((doc) ->
  81. if doc.type is 'event' and doc.status is 'published'
  82. emit(doc.event_data?.event_date || doc.created_at, doc)).toString()
  83. # События по домену и языку
  84. by_domain_language:
  85. map: ((doc) ->
  86. if doc.type is 'event' and doc.status is 'published' and doc.domain and doc.language
  87. for domain in doc.domain
  88. for lang in doc.language
  89. emit([domain, lang, doc.event_data?.event_date || doc.created_at], doc)).toString()
  90. # Предстоящие события
  91. upcoming:
  92. map: ((doc) ->
  93. if doc.type is 'event' and doc.status is 'published' and doc.event_data
  94. event_date = doc.event_data.event_date
  95. if event_date and new Date(event_date) > new Date()
  96. emit(event_date, doc)).toString()
  97. # Прошедшие события
  98. past:
  99. map: ((doc) ->
  100. if doc.type is 'event' and doc.status is 'published' and doc.event_data
  101. event_date = doc.event_data.event_date
  102. if event_date and new Date(event_date) <= new Date()
  103. emit(event_date, doc)).toString()
  104. # События по статусу
  105. by_status:
  106. map: ((doc) ->
  107. if doc.type is 'event' and doc.status is 'published' and doc.event_data
  108. status = doc.event_data.status || 'upcoming'
  109. emit([status, doc.event_data?.event_date || doc.created_at], doc)).toString()
  110. # Избранные события
  111. featured:
  112. map: ((doc) ->
  113. if doc.type is 'event' and doc.status is 'published' and doc.featured
  114. emit(doc.event_data?.event_date || doc.created_at, doc)).toString()
  115. # События по категории
  116. by_category:
  117. map: ((doc) ->
  118. if doc.type is 'event' and doc.status is 'published' and doc.category_id
  119. emit([doc.category_id, doc.event_data?.event_date || doc.created_at], doc)).toString()
  120. # События по местоположению
  121. by_location:
  122. map: ((doc) ->
  123. if doc.type is 'event' and doc.status is 'published' and doc.event_data?.location
  124. for location_array, lang_index in doc.event_data.location
  125. for location in location_array
  126. emit([location, doc.event_data?.event_date || doc.created_at], doc)).toString()
  127. # Design документ для товаров (products)
  128. products:
  129. version: "1.1"
  130. views:
  131. # Все опубликованные товары
  132. published:
  133. map: ((doc) ->
  134. if doc.type is 'product' and doc.status is 'published'
  135. emit(doc.created_at, doc)).toString()
  136. # Товары по домену и языку
  137. by_domain_language:
  138. map: ((doc) ->
  139. if doc.type is 'product' and doc.status is 'published' and doc.domain and doc.language
  140. for domain in doc.domain
  141. for lang in doc.language
  142. emit([domain, lang, doc.created_at], doc)).toString()
  143. # Товары по статусу наличия
  144. by_availability:
  145. map: ((doc) ->
  146. if doc.type is 'product' and doc.status is 'published' and doc.product_data
  147. status = doc.product_data.status || 'available'
  148. emit([status, doc.created_at], doc)).toString()
  149. # Избранные товары
  150. featured:
  151. map: ((doc) ->
  152. if doc.type is 'product' and doc.status is 'published' and doc.featured
  153. emit(doc.created_at, doc)).toString()
  154. # Товары по категории
  155. by_category:
  156. map: ((doc) ->
  157. if doc.type is 'product' and doc.status is 'published' and doc.category_id
  158. emit([doc.category_id, doc.created_at], doc)).toString()
  159. # Товары по цене
  160. by_price:
  161. map: ((doc) ->
  162. if doc.type is 'product' and doc.status is 'published' and doc.product_data?.price
  163. price = doc.product_data.price[0] || 0
  164. emit(price, doc)).toString()
  165. # Design документ для слайдеров (slides)
  166. slides:
  167. version: "1.1"
  168. views:
  169. # Все активные слайды
  170. active:
  171. map: ((doc) ->
  172. if doc.type is 'slide' and doc.status is 'published' and doc.slide_data?.active != false
  173. current_date = new Date().toISOString()
  174. start_date = doc.slide_data.start_date || '2000-01-01'
  175. end_date = doc.slide_data.end_date || '2100-01-01'
  176. if start_date <= current_date <= end_date
  177. emit(doc.slide_data?.order || 0, doc)).toString()
  178. # Слайды по домену и языку
  179. by_domain_language:
  180. map: ((doc) ->
  181. if doc.type is 'slide' and doc.status is 'published' and doc.slide_data?.active != false and doc.domain and doc.language
  182. current_date = new Date().toISOString()
  183. start_date = doc.slide_data.start_date || '2000-01-01'
  184. end_date = doc.slide_data.end_date || '2100-01-01'
  185. if start_date <= current_date <= end_date
  186. for domain in doc.domain
  187. for lang in doc.language
  188. emit([domain, lang, doc.slide_data?.order || 0], doc)).toString()
  189. # Слайды по порядку отображения
  190. by_order:
  191. map: ((doc) ->
  192. if doc.type is 'slide' and doc.status is 'published' and doc.slide_data?.active != false
  193. current_date = new Date().toISOString()
  194. start_date = doc.slide_data.start_date || '2000-01-01'
  195. end_date = doc.slide_data.end_date || '2100-01-01'
  196. if start_date <= current_date <= end_date
  197. emit(doc.slide_data.order || 0, doc)).toString()
  198. # Design документ для категорий
  199. categories:
  200. version: "1.1"
  201. views:
  202. # Все активные категории
  203. active:
  204. map: ((doc) ->
  205. if doc.type is 'category' and doc.active != false
  206. emit(doc.level || 0, doc)).toString()
  207. # Категории по домену и языку
  208. by_domain_language:
  209. map: ((doc) ->
  210. if doc.type is 'category' and doc.active != false and doc.domain and doc.language
  211. for domain in doc.domain
  212. for lang in doc.language
  213. emit([domain, lang, doc.level || 0], doc)).toString()
  214. # Категории по родительскому ID
  215. by_parent:
  216. map: ((doc) ->
  217. if doc.type is 'category' and doc.active != false
  218. parent_id = doc.parent_id || 'root'
  219. emit([parent_id, doc.order || 0], doc)).toString()
  220. # Категории для меню
  221. menu:
  222. map: ((doc) ->
  223. if doc.type is 'category' and doc.active != false and doc.show_in_menu != false
  224. emit([doc.level || 0, doc.menu_order || 0], doc)).toString()
  225. # Избранные категории
  226. featured:
  227. map: ((doc) ->
  228. if doc.type is 'category' and doc.active != false and doc.featured
  229. emit(doc.level || 0, doc)).toString()
  230. # Design документ для настроек доменов
  231. domain_settings:
  232. version: "1.1"
  233. views:
  234. # Все активные настройки доменов
  235. active:
  236. map: ((doc) ->
  237. if doc.type is 'domain_settings' and doc.active != false
  238. emit(doc.domain, doc)).toString()
  239. # Настройки по приоритету
  240. by_priority:
  241. map: ((doc) ->
  242. if doc.type is 'domain_settings' and doc.active != false
  243. emit(doc.priority || 0, doc)).toString()
  244. # Design документ для пользователей
  245. users:
  246. version: "1.1"
  247. views:
  248. # Все активные пользователи
  249. active:
  250. map: ((doc) ->
  251. if doc.type is 'user' and doc.active != false
  252. emit(doc.email, doc)).toString()
  253. # Пользователи по роли
  254. by_role:
  255. map: ((doc) ->
  256. if doc.type is 'user' and doc.active != false
  257. emit(doc.role, doc)).toString()
  258. # Пользователи по домену доступа
  259. by_domain_access:
  260. map: ((doc) ->
  261. if doc.type is 'user' and doc.active != false and doc.domains_access
  262. for domain in doc.domains_access
  263. emit([domain, doc.email], doc)).toString()
  264. # Design документ для заказов
  265. orders:
  266. version: "1.1"
  267. views:
  268. # Все заказы
  269. all:
  270. map: ((doc) ->
  271. if doc.type is 'order'
  272. emit(doc.created_at, doc)).toString()
  273. # Заказы по статусу
  274. by_status:
  275. map: ((doc) ->
  276. if doc.type is 'order'
  277. emit([doc.status, doc.created_at], doc)).toString()
  278. # Заказы по пользователю
  279. by_user:
  280. map: ((doc) ->
  281. if doc.type is 'order' and doc.user_id
  282. emit([doc.user_id, doc.created_at], doc)).toString()
  283. # Заказы по домену
  284. by_domain:
  285. map: ((doc) ->
  286. if doc.type is 'order' and doc.domain
  287. emit([doc.domain, doc.created_at], doc)).toString()
  288. # Design документ для аудита
  289. audit_logs:
  290. version: "1.1"
  291. views:
  292. # Все записи аудита
  293. all:
  294. map: ((doc) ->
  295. if doc.type is 'audit_log'
  296. emit(doc.created_at, doc)).toString()
  297. # Записи аудита по пользователю
  298. by_user:
  299. map: ((doc) ->
  300. if doc.type is 'audit_log' and doc.user_id
  301. emit([doc.user_id, doc.created_at], doc)).toString()
  302. # Записи аудита по типу ресурса
  303. by_resource_type:
  304. map: ((doc) ->
  305. if doc.type is 'audit_log' and doc.resource_type
  306. emit([doc.resource_type, doc.created_at], doc)).toString()
  307. # Записи аудита по действию
  308. by_action:
  309. map: ((doc) ->
  310. if doc.type is 'audit_log' and doc.action
  311. emit([doc.action, doc.created_at], doc)).toString()
  312. # Универсальные представления для всех типов документов
  313. universal:
  314. version: "1.2"
  315. views:
  316. # Документы по типу и домену
  317. by_type_domain:
  318. map: ((doc) ->
  319. if doc.type and doc.domain
  320. if Array.isArray(doc.domain)
  321. for domain in doc.domain
  322. emit([doc.type, domain, doc.created_at], doc)
  323. else
  324. emit([doc.type, doc.domain, doc.created_at], doc)).toString()
  325. # Документы по типу и статусу
  326. by_type_status:
  327. map: ((doc) ->
  328. if doc.type and doc.status
  329. emit([doc.type, doc.status, doc.created_at], doc)).toString()
  330. # Поиск по текстовым полям (для полнотекстового поиска)
  331. text_search:
  332. map: ((doc) ->
  333. if doc.title and Array.isArray(doc.title)
  334. for title in doc.title
  335. words = title.toLowerCase().split(/\s+/)
  336. for word in words when word.length > 2
  337. emit(word, {_id: doc._id, type: doc.type, title: doc.title})
  338. if doc.content and Array.isArray(doc.content)
  339. for content in doc.content
  340. words = content.toLowerCase().split(/\s+/)
  341. for word in words when word.length > 2
  342. emit(word, {_id: doc._id, type: doc.type, content: content})
  343. if doc.tags and Array.isArray(doc.tags)
  344. for tag_array in doc.tags
  345. for tag in tag_array
  346. emit(tag.toLowerCase(), {_id: doc._id, type: doc.type, tags: doc.tags})).toString()
  347. # Документы с поддержкой мультиязычности
  348. multilingual:
  349. map: ((doc) ->
  350. if doc.language and Array.isArray(doc.language) and doc.domain
  351. domain_array = if Array.isArray(doc.domain) then doc.domain else [doc.domain]
  352. for domain in domain_array
  353. for lang in doc.language
  354. emit([domain, lang, doc.type, doc.created_at], doc)).toString()
  355. # Экспорт design документов
  356. module.exports = design_documents