models.js 4.5 KB

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