models.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='hipstagram') => {
  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 Like extends SlicedSavable {
  23. constructor(...params){
  24. super(...params)
  25. //TODO:
  26. //entity
  27. //entityLikesCount?
  28. }
  29. static get relations(){
  30. return {
  31. }
  32. }
  33. }
  34. SlicedSavable.addClass(Like)
  35. class Post extends SlicedSavable {
  36. constructor(...params){
  37. super(...params)
  38. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  39. //cached like count, which incremented and decremented
  40. //
  41. //
  42. this.comments = this.comments instanceof Array ?
  43. this.comments : this.comments ? [] : [this.comments]
  44. }
  45. static get relations(){
  46. return {
  47. images: "posts",
  48. comments: "post",
  49. directs: "post",
  50. collections: "posts"
  51. }
  52. }
  53. }
  54. SlicedSavable.addClass(Post)
  55. class Image extends SlicedSavable {
  56. constructor(...params){
  57. super(...params)
  58. }
  59. static async fromFileData(fileData){
  60. let image = new Image({})
  61. image.fileData = fileData
  62. image.url = `images/${fileData.filename}`
  63. image.originalFileName = fileData.originalname
  64. await image.save()
  65. return image;
  66. }
  67. async save(...params){
  68. if (this.userAvatar){
  69. if (this.userAvatar._id.toString() !== id){
  70. throw new ReferenceError(`You can't set ava for other user`)
  71. }
  72. }
  73. return await super.save(...params)
  74. }
  75. static get relations(){
  76. return {
  77. userAvatar: "avatar", //if it is ava
  78. posts: "images", //if in post: m2m with posts
  79. directs: "image", //if in direct: m2o with directs
  80. }
  81. }
  82. }
  83. SlicedSavable.addClass(Image)
  84. class Comment extends SlicedSavable {
  85. constructor(...params){
  86. super(...params)
  87. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  88. //cached like count, which incremented and decremented
  89. }
  90. static get relations(){
  91. return {
  92. post: "comments",
  93. answers: "answerTo",
  94. answerTo: "answers",
  95. }
  96. }
  97. }
  98. SlicedSavable.addClass(Comment)
  99. class Direct extends SlicedSavable {
  100. constructor(...params){
  101. super(...params)
  102. }
  103. static get relations(){
  104. return {
  105. to: "incomings",
  106. attach: "directs",
  107. likes: "entity",
  108. }
  109. }
  110. }
  111. SlicedSavable.addClass(Direct)
  112. class Collection extends SlicedSavable {
  113. constructor(...params){
  114. super(...params)
  115. }
  116. static get relations(){
  117. return {
  118. posts: "collections"
  119. }
  120. }
  121. }
  122. SlicedSavable.addClass(Collection)
  123. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  124. return {models: {
  125. SlicedSavable, ...SlicedSavable.classes
  126. },
  127. thisUser}
  128. }
  129. return {
  130. Savable,
  131. slice,
  132. getModels
  133. }
  134. }