models.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  52. }
  53. }
  54. SlicedSavable.addClass(Image)
  55. class Good extends SlicedSavable {
  56. constructor(...params){
  57. super(...params)
  58. }
  59. static get relations(){
  60. return {
  61. categories: ["goods"],
  62. orderGoods: "good",
  63. }
  64. }
  65. }
  66. SlicedSavable.addClass(Good)
  67. class Category extends SlicedSavable {
  68. constructor(...params){
  69. super(...params)
  70. }
  71. static get relations(){
  72. return {
  73. goods: ["categories"],
  74. }
  75. }
  76. }
  77. SlicedSavable.addClass(Category)
  78. class Order extends SlicedSavable {
  79. constructor(...params){
  80. super(...params)
  81. }
  82. get total(){
  83. return (async() => (await Promise.all(this.orderGoods)).reduce((a,b) => (a.total || a) + b.total, 0))()
  84. }
  85. static get relations(){
  86. return {
  87. user: ["orders"],
  88. orderGoods: "order"
  89. }
  90. }
  91. }
  92. SlicedSavable.addClass(Order)
  93. class OrderGood extends SlicedSavable {
  94. constructor(...params){
  95. super(...params)
  96. }
  97. get total(){
  98. return this.price*this.count
  99. }
  100. static get relations(){
  101. return {
  102. good: ["orderGoods"],
  103. order: ["orderGoods"]
  104. }
  105. }
  106. }
  107. SlicedSavable.addClass(OrderGood)
  108. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  109. return {models: {
  110. SlicedSavable, ...SlicedSavable.classes
  111. },
  112. thisUser}
  113. }
  114. return {
  115. Savable,
  116. slice,
  117. getModels
  118. }
  119. }