models.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='shop') => {
  4. const {Savable, slice} = await connect(dbName)
  5. async function getModels({id}){
  6. const SlicedSavable = slice([id, 'user'])
  7. class User extends SlicedSavable {
  8. constructor(...params){
  9. super(...params)
  10. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  11. //cached like count, which incremented and decremented
  12. //
  13. //following and followers array
  14. }
  15. static get relations(){ //don't needed due to ___owner in most cases
  16. return {
  17. avatar : "userAvatar",
  18. }
  19. }
  20. }
  21. SlicedSavable.addClass(User)
  22. class OwnerSlicedSavable extends SlicedSavable {
  23. get owner(){
  24. if (!this.___owner) return this.___owner
  25. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  26. }
  27. }
  28. class Image extends OwnerSlicedSavable {
  29. constructor(...params){
  30. super(...params)
  31. }
  32. static async fromFileData(fileData){
  33. let image = new Image({})
  34. image.fileData = fileData
  35. image.url = `images/${fileData.filename}`
  36. image.originalFileName = fileData.originalname
  37. await image.save()
  38. return image;
  39. }
  40. async save(...params){
  41. if (this.userAvatar){
  42. if (this.userAvatar._id.toString() !== id){
  43. throw new ReferenceError(`You can't set ava for other user`)
  44. }
  45. }
  46. return await super.save(...params)
  47. }
  48. static get relations(){
  49. return {
  50. userAvatar: "avatar", //if it is ava
  51. good: ["images"], //if it is ava
  52. category: "image", //if it is ava
  53. }
  54. }
  55. }
  56. SlicedSavable.addClass(Image)
  57. class Good extends SlicedSavable {
  58. constructor(...params){
  59. super(...params)
  60. }
  61. static get relations(){
  62. return {
  63. categories: ["goods"],
  64. orderGoods: "good",
  65. images: "good"
  66. }
  67. }
  68. static get guestRelations(){
  69. return ["categories", "orderGoods"]
  70. }
  71. }
  72. SlicedSavable.addClass(Good)
  73. class Category extends SlicedSavable {
  74. constructor(...params){
  75. super(...params)
  76. }
  77. static get relations(){
  78. return {
  79. goods: ["categories"],
  80. image: "category",
  81. }
  82. }
  83. static get defaultPermissions(){
  84. return {
  85. //savable refs, objectid's, words like 'tags' or 'roles'
  86. read: ['owner', 'user'],
  87. write: ['owner', 'admin'],
  88. create: ['user'],
  89. delete: ['admin', 'owner'],
  90. /*permission
  91. * TODO: permissions for read and write permissions
  92. *
  93. */
  94. }
  95. }
  96. }
  97. SlicedSavable.addClass(Category)
  98. class Order extends OwnerSlicedSavable {
  99. constructor(...params){
  100. super(...params)
  101. }
  102. get total(){
  103. return (async() => (await Promise.all(this.orderGoods)).reduce((a,b) => (a.total || a) + b.total, 0))()
  104. }
  105. static get relations(){
  106. return {
  107. orderGoods: "order"
  108. }
  109. }
  110. static get defaultPermissions(){
  111. return {
  112. //savable refs, objectid's, words like 'tags' or 'roles'
  113. read: ['owner', 'admin'],
  114. write: ['owner', 'admin'],
  115. create: ['user'],
  116. delete: ['admin'],
  117. /*permission
  118. * TODO: permissions for read and write permissions
  119. *
  120. */
  121. }
  122. }
  123. }
  124. SlicedSavable.addClass(Order)
  125. class OrderGood extends OwnerSlicedSavable {
  126. constructor(...params){
  127. super(...params)
  128. }
  129. get total(){
  130. return this.price*this.count
  131. }
  132. async save(...params){
  133. if (!this.price && this.good && this.good.price){
  134. this.price = this.good.price
  135. }
  136. return await super.save(...params)
  137. }
  138. static get relations(){
  139. return {
  140. good: ["orderGoods"],
  141. order: ["orderGoods"]
  142. }
  143. }
  144. }
  145. SlicedSavable.addClass(OrderGood)
  146. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  147. return {models: {
  148. SlicedSavable, ...SlicedSavable.classes
  149. },
  150. thisUser}
  151. }
  152. return {
  153. Savable,
  154. slice,
  155. getModels
  156. }
  157. }