models.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. }
  84. SlicedSavable.addClass(Category)
  85. class Order extends SlicedSavable {
  86. constructor(...params){
  87. super(...params)
  88. }
  89. get total(){
  90. return (async() => (await Promise.all(this.orderGoods)).reduce((a,b) => (a.total || a) + b.total, 0))()
  91. }
  92. static get relations(){
  93. return {
  94. user: ["orders"],
  95. orderGoods: "order"
  96. }
  97. }
  98. }
  99. SlicedSavable.addClass(Order)
  100. class OrderGood extends SlicedSavable {
  101. constructor(...params){
  102. super(...params)
  103. }
  104. get total(){
  105. return this.price*this.count
  106. }
  107. static get relations(){
  108. return {
  109. good: ["orderGoods"],
  110. order: ["orderGoods"]
  111. }
  112. }
  113. }
  114. SlicedSavable.addClass(OrderGood)
  115. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  116. return {models: {
  117. SlicedSavable, ...SlicedSavable.classes
  118. },
  119. thisUser}
  120. }
  121. return {
  122. Savable,
  123. slice,
  124. getModels
  125. }
  126. }