models.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. let image = new Image({})
  37. image.fileData = fileData
  38. image.url = `images/${fileData.filename}`
  39. image.originalFileName = fileData.originalname
  40. await image.save()
  41. return image;
  42. }
  43. static get relations(){
  44. return {
  45. userAvatar: "avatar", //if it is ava
  46. }
  47. }
  48. }
  49. SlicedSavable.addClass(Image)
  50. class Snippet extends OwnerSlicedSavable {
  51. constructor(...params){
  52. super(...params)
  53. }
  54. static get relations(){
  55. return {
  56. comments: "snippet",
  57. files: "snippet",
  58. }
  59. }
  60. static get guestRelations(){
  61. return ["comments"]
  62. }
  63. }
  64. SlicedSavable.addClass(Snippet)
  65. class Comment extends OwnerSlicedSavable {
  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. snippet: ["comments"],
  74. answers: "answerTo",
  75. answerTo: ["answers"],
  76. }
  77. }
  78. static get guestRelations(){
  79. return ["answers"]
  80. }
  81. }
  82. SlicedSavable.addClass(Comment)
  83. class File extends OwnerSlicedSavable {
  84. constructor(...params){
  85. super(...params)
  86. }
  87. static get relations(){
  88. return {
  89. snippet: ["files"],
  90. }
  91. }
  92. }
  93. SlicedSavable.addClass(File)
  94. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  95. return {models: {
  96. SlicedSavable, ...SlicedSavable.classes
  97. },
  98. thisUser}
  99. }
  100. return {
  101. Savable,
  102. slice,
  103. getModels
  104. }
  105. }