models.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='snippet') => {
  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. async getACL(){
  16. return [this._id.toString(), "user"]
  17. }
  18. static get relations(){ //don't needed due to ___owner in most cases
  19. return {
  20. avatar : "userAvatar",
  21. }
  22. }
  23. }
  24. SlicedSavable.addClass(User)
  25. class OwnerSlicedSavable extends SlicedSavable {
  26. get owner(){
  27. if (!this.___owner) return this.___owner
  28. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  29. }
  30. }
  31. class Image extends OwnerSlicedSavable {
  32. constructor(...params){
  33. super(...params)
  34. }
  35. static async fromFileData(fileData){
  36. try {
  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. catch(e){
  45. console.log('wrong file')
  46. }
  47. }
  48. static get relations(){
  49. return {
  50. userAvatar: "avatar", //if it is ava
  51. }
  52. }
  53. }
  54. SlicedSavable.addClass(Image)
  55. class Snippet extends OwnerSlicedSavable {
  56. constructor(...params){
  57. super(...params)
  58. }
  59. static get relations(){
  60. return {
  61. comments: "snippet",
  62. files: "snippet",
  63. }
  64. }
  65. static get guestRelations(){
  66. return ["comments"]
  67. }
  68. }
  69. SlicedSavable.addClass(Snippet)
  70. class Comment extends OwnerSlicedSavable {
  71. constructor(...params){
  72. super(...params)
  73. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  74. //cached like count, which incremented and decremented
  75. }
  76. static get relations(){
  77. return {
  78. snippet: ["comments"],
  79. answers: "answerTo",
  80. answerTo: ["answers"],
  81. }
  82. }
  83. static get guestRelations(){
  84. return ["answers"]
  85. }
  86. }
  87. SlicedSavable.addClass(Comment)
  88. class File extends OwnerSlicedSavable {
  89. constructor(...params){
  90. super(...params)
  91. }
  92. static get relations(){
  93. return {
  94. snippet: ["files"],
  95. }
  96. }
  97. }
  98. SlicedSavable.addClass(File)
  99. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  100. return {models: {
  101. SlicedSavable, ...SlicedSavable.classes
  102. },
  103. thisUser}
  104. }
  105. return {
  106. Savable,
  107. slice,
  108. getModels
  109. }
  110. }