models.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='marketplace') => {
  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. incomings: "to",
  19. }
  20. }
  21. static get guestRelations(){
  22. return ["incomings"]
  23. }
  24. }
  25. SlicedSavable.addClass(User)
  26. class OwnerSlicedSavable extends SlicedSavable {
  27. get owner(){
  28. if (!this.___owner) return this.___owner
  29. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  30. }
  31. }
  32. class Image extends OwnerSlicedSavable {
  33. constructor(...params){
  34. super(...params)
  35. }
  36. static async fromFileData(fileData){
  37. let image = new Image({})
  38. image.fileData = fileData
  39. image.url = `images/${fileData.filename}`
  40. image.originalFileName = fileData.originalname
  41. await image.save()
  42. return image;
  43. }
  44. static get relations(){
  45. return {
  46. userAvatar: "avatar", //if it is ava
  47. ad: ["images"],
  48. message: "image",
  49. }
  50. }
  51. }
  52. SlicedSavable.addClass(Image)
  53. class Ad extends OwnerSlicedSavable {
  54. constructor(...params){
  55. super(...params)
  56. }
  57. static get relations(){
  58. return {
  59. images: "ad", //if it is ava
  60. comments: "ad",
  61. }
  62. }
  63. static get guestRelations(){
  64. return ["comments"]
  65. }
  66. }
  67. SlicedSavable.addClass(Ad)
  68. class Comment extends OwnerSlicedSavable {
  69. constructor(...params){
  70. super(...params)
  71. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  72. //cached like count, which incremented and decremented
  73. }
  74. static get relations(){
  75. return {
  76. ad: ["comments"],
  77. answers: "answerTo",
  78. answerTo: ["answers"],
  79. }
  80. }
  81. static get guestRelations(){
  82. return ["answers"]
  83. }
  84. }
  85. SlicedSavable.addClass(Comment)
  86. class Message extends OwnerSlicedSavable {
  87. constructor(...params){
  88. super(...params)
  89. }
  90. static get relations(){
  91. return {
  92. to: ["incomings"],
  93. image: "message",
  94. }
  95. }
  96. //static get guestRelations(){
  97. //return ["to"]
  98. //}
  99. async save(...params){
  100. if (this.to) this.___permissions.read.push(this.to._id.toString())
  101. return await super.save(...params)
  102. }
  103. static get defaultPermissions(){
  104. return {
  105. //savable refs, objectid's, words like 'tags' or 'roles'
  106. read: ['owner'],
  107. write: ['owner'],
  108. create: ['user'],
  109. /*permission
  110. * TODO: permissions for read and write permissions
  111. *
  112. */
  113. }
  114. }
  115. }
  116. SlicedSavable.addClass(Message)
  117. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  118. return {models: {
  119. SlicedSavable, ...SlicedSavable.classes
  120. },
  121. thisUser}
  122. }
  123. return {
  124. Savable,
  125. slice,
  126. getModels
  127. }
  128. }