models.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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', 'admin'])
  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. static get relations(){
  42. return {
  43. images: "posts",
  44. comments: "post",
  45. directs: "post",
  46. collections: "posts"
  47. }
  48. }
  49. }
  50. SlicedSavable.addClass(Post)
  51. class Image extends SlicedSavable {
  52. constructor(...params){
  53. super(...params)
  54. //TODO: multer, file data and so
  55. }
  56. static get relations(){
  57. return {
  58. userAvatar: "avatar", //if it is ava
  59. posts: "images", //if in post: m2m with posts
  60. directs: "image", //if in direct: m2o with directs
  61. }
  62. }
  63. }
  64. SlicedSavable.addClass(Image)
  65. class Comment extends SlicedSavable {
  66. constructor(...params){
  67. super(...params)
  68. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  69. //cached like count, which incremented and decremented
  70. }
  71. static get relations(){
  72. return {
  73. post: "comments",
  74. answers: "answerTo",
  75. answerTo: "answers",
  76. }
  77. }
  78. }
  79. SlicedSavable.addClass(Comment)
  80. class Direct extends SlicedSavable {
  81. constructor(...params){
  82. super(...params)
  83. }
  84. static get relations(){
  85. return {
  86. to: "incomings",
  87. attach: "directs",
  88. likes: "entity",
  89. }
  90. }
  91. }
  92. SlicedSavable.addClass(Direct)
  93. class Collection extends SlicedSavable {
  94. constructor(...params){
  95. super(...params)
  96. }
  97. static get relations(){
  98. return {
  99. posts: "collections"
  100. }
  101. }
  102. }
  103. SlicedSavable.addClass(Collection)
  104. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  105. return {models: {
  106. SlicedSavable, ...SlicedSavable.classes
  107. },
  108. thisUser}
  109. }
  110. return {
  111. Savable,
  112. slice,
  113. getModels
  114. }
  115. }