models.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='amazon2home') => {
  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. executedOrders: "executor"
  19. }
  20. }
  21. }
  22. SlicedSavable.addClass(User)
  23. class OwnerSlicedSavable extends SlicedSavable {
  24. get owner(){
  25. if (!this.___owner) return this.___owner
  26. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  27. }
  28. }
  29. class Image extends OwnerSlicedSavable {
  30. constructor(...params){
  31. super(...params)
  32. }
  33. static async fromFileData(fileData){
  34. let image = new Image({})
  35. image.fileData = fileData
  36. image.url = `images/${fileData.filename}`
  37. image.originalFileName = fileData.originalname
  38. await image.save()
  39. return image;
  40. }
  41. async save(...params){
  42. if (this.userAvatar){
  43. if (this.userAvatar._id.toString() !== id){
  44. throw new ReferenceError(`You can't set ava for other user`)
  45. }
  46. }
  47. return await super.save(...params)
  48. }
  49. static get relations(){
  50. return {
  51. userAvatar: "avatar", //if it is ava
  52. order: ["images"]
  53. }
  54. }
  55. }
  56. SlicedSavable.addClass(Image)
  57. class Order extends OwnerSlicedSavable {
  58. constructor(...params){
  59. super(...params)
  60. }
  61. static get relations(){
  62. return {
  63. executor: ["executedOrders"],
  64. messages: "order",
  65. comments: "order",
  66. images: "order"
  67. }
  68. }
  69. }
  70. SlicedSavable.addClass(Order)
  71. class Message extends OwnerSlicedSavable {
  72. constructor(...params){
  73. super(...params)
  74. }
  75. static get relations(){
  76. return {
  77. order: ["messages"]
  78. }
  79. }
  80. }
  81. SlicedSavable.addClass(Message)
  82. class Comment extends OwnerSlicedSavable {
  83. constructor(...params){
  84. super(...params)
  85. }
  86. static get relations(){
  87. return {
  88. order: ["comments"]
  89. }
  90. }
  91. }
  92. SlicedSavable.addClass(Comment)
  93. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  94. return {models: {
  95. SlicedSavable, ...SlicedSavable.classes
  96. },
  97. thisUser}
  98. }
  99. return {
  100. Savable,
  101. slice,
  102. getModels
  103. }
  104. }