models.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 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 OwnerSlicedSavable extends SlicedSavable {
  23. get owner(){
  24. if (!this.___owner) return this.___owner
  25. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  26. }
  27. }
  28. class Image extends OwnerSlicedSavable {
  29. constructor(...params){
  30. super(...params)
  31. }
  32. static async fromFileData(fileData){
  33. let image = new Image({})
  34. image.fileData = fileData
  35. image.url = `images/${fileData.filename}`
  36. image.originalFileName = fileData.originalname
  37. await image.save()
  38. return image;
  39. }
  40. static get relations(){
  41. return {
  42. userAvatar: "avatar", //if it is ava
  43. }
  44. }
  45. }
  46. SlicedSavable.addClass(Image)
  47. class Snippet extends OwnerSlicedSavable {
  48. constructor(...params){
  49. super(...params)
  50. }
  51. static get relations(){
  52. return {
  53. comments: "snippet",
  54. files: "snippet",
  55. }
  56. }
  57. static get guestRelations(){
  58. return ["comments"]
  59. }
  60. }
  61. SlicedSavable.addClass(Snippet)
  62. class Comment extends OwnerSlicedSavable {
  63. constructor(...params){
  64. super(...params)
  65. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  66. //cached like count, which incremented and decremented
  67. }
  68. static get relations(){
  69. return {
  70. snippet: ["comments"],
  71. answers: "answerTo",
  72. answerTo: ["answers"],
  73. }
  74. }
  75. static get guestRelations(){
  76. return ["answers"]
  77. }
  78. }
  79. SlicedSavable.addClass(Comment)
  80. class File extends OwnerSlicedSavable {
  81. constructor(...params){
  82. super(...params)
  83. }
  84. static get relations(){
  85. return {
  86. snippet: ["files"],
  87. }
  88. }
  89. }
  90. SlicedSavable.addClass(File)
  91. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  92. return {models: {
  93. SlicedSavable, ...SlicedSavable.classes
  94. },
  95. thisUser}
  96. }
  97. return {
  98. Savable,
  99. slice,
  100. getModels
  101. }
  102. }